Sequelize
@adminjs/sequelize
This guide will assume you have set up Sequelize using it's documentation or Nest.js documentation.
There are small differences in how you connect Sequelize to Nest.js vs other plugins, so the guide will be split into two sections accordingly.
Example model:
import { DataTypes, Model, Optional } from 'sequelize'
import sequelize from './index.js'
interface ICategory {
id: number;
name: string;
createdAt: Date;
updatedAt: Date;
}
export type CategoryCreationAttributes = Optional<ICategory, 'id'>
export class Category extends Model<ICategory, CategoryCreationAttributes> {
declare id: number;
declare name: string;
declare createdAt: Date;
declare updatedAt: Date;
}
Category.init(
{
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
name: {
type: new DataTypes.STRING(128),
allowNull: false,
},
createdAt: {
type: DataTypes.DATE,
},
updatedAt: {
type: DataTypes.DATE,
},
},
{
sequelize,
tableName: 'categories',
modelName: 'category',
}
)Sequelize connection:
Standard
Make sure you have followed the tutorial for the framework you are using in the Plugins section.
The configuration for non-Nest.js plugins is basically the same for each one of them:
You must import
AdminJSSequelizeadapter and register itYou must import the entities you want to use and pass them to AdminJS
resourcesoptions
Nest.js
Make sure you have set up your app.module.ts according to Nest.js documentation and you have followed Nest.js plugin tutorial as well.
Your app.module.ts should have imports option which contains:
SequelizeModule.forRoot({ uri: '...', dialect: '...' })to set up SequelizeAdminModule.createAdminAsync({ ... }
In your app.module.ts add these imports at the top of the file:
Following this, register AdminJSSequelize adapter somewhere after your imports:
This will allow you to pass Sequelize models for AdminJS to load. If we use the Category entity that we used as en example earlier, you should import it into app.module.ts and pass it into resources in your adminJsOptions:
Last updated
