Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@adminjs/sql

Package Overview
Dependencies
Maintainers
0
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@adminjs/sql

An official AdminJS adapter for SQL databases.

  • 2.2.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
757
decreased by-9.34%
Maintainers
0
Weekly downloads
 
Created
Source

@adminjs/sql

This is an official AdminJS adapter for SQL databases. It does not require you to use any ORMs, instead you just provide database connection and you model the data sources using AdminJS configuration.

Supported databases:

  • PostgreSQL
  • MySQL
  • more coming soon

This adapter is heavily inspired by wirekang's adminjs-sql which is an unofficial adapter for a MySQL database.

Installation

$ yarn add @adminjs/sql

Usage with Express

The example below shows usage with @adminjs/express. The usage of Adapter class is required to parse your database's schema.

import AdminJS from 'adminjs'
import express from 'express'
import Plugin from '@adminjs/express'
import Adapter, { Database, Resource } from '@adminjs/sql'

AdminJS.registerAdapter({
  Database,
  Resource,
})

const start = async () => {
  const app = express()

  const db = await new Adapter('postgresql', {
    connectionString: '<your database connection string>', //  postgresql://[user]:[password]@[netloc]:[port]/[dbname]
    database: '<your database name>',
  }).init();

  const admin = new AdminJS({
    resources: [
      {
        resource: db.table('users'),
        options: { /* any resource options, rbac, etc */ },
      },
    ],
    // databases: [db] <- you can also provide the DB connection to register all tables at once
  });

  admin.watch()

  const router = Plugin.buildRouter(admin)

  app.use(admin.options.rootPath, router)

  app.listen(8080, () => {
    console.log('app started')
  })
}

start()

Database Relations

Currently only many-to-one relation works out of the box if you specify foreign key constraints in your database. Other relations will require you to make UI/backend customizations. Please see our documentation to learn more.

Enums

As of version 1.0.0 database enums aren't automatically detected and loaded. You can assign them manually in your resource options:

// ...
  const admin = new AdminJS({
    resources: [{
      resource: db.table('users'),
      options: {
        properties: {
          role: {
            availableValues: [
              { label: 'Admin', value: 'ADMIN' },
              { label: 'Client', value: 'CLIENT' },
            ],
          },
        },
      },
    }],
  })
// ...

Timestamps

If your database tables have automatically default-set timestamps (created_at, updated_at, etc) they will be visible in create/edit forms by default. You can hide them in resource options:

// ...
  const admin = new AdminJS({
    resources: [{
      resource: db.table('users'),
      options: {
        properties: {
          created_at: { isVisible: false },
        },
      },
    }],
  })
// ...

License

AdminJS is copyrighted © 2023 rst.software. It is a free software, and may be redistributed under the terms specified in the LICENSE file.

About rst.software

We’re an open, friendly team that helps clients from all over the world to transform their businesses and create astonishing products.

  • We are available for hire.
  • If you want to work for us - check out the career page.

Keywords

FAQs

Package last updated on 10 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc