SQL
@adminjs/sql
Before reading this article, make sure you have set up an AdminJS instance using one of the supported Plugins.
Additionally, you should have installed @adminjs/sql
as described in Getting started section.
@adminjs/sql
is an official adapter for SQL databases which does not require you to use an ORM and allows you to simply connect your admin panel directly to the database.
Currently supported databases
PostgreSQL
more coming soon
Usage
Most of the setup is very similar to other AdminJS adapters. The main difference is that you must feed your database credentials to the Adapter instead of an ORM so that it can parse your database schema and run queries against your database.
First of all, import Adapter
, Resource
and Database
from @adminjs/sql
.
Afterwards, register the adapter and initialize it to let it read your database schema:
When creating an Adapter
instance, the first parameter is your database's dialect (currently only postgresql
can be used) while the second parameter is an object with your database connection options. Please refer to Knex documentation to find out how the configuration object should look like as @adminjs/sql
is based on Knex.
Finally, create an AdminJS
instance and either include db
in databases
option to load your entire database (not recommended) or define resources
separately:
The rest of the configuration is identical to other plugins and adapters. If you are confused, you can use the example below as a reference:
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:
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:
Last updated