import { IApplicationContext, providerWrapper } from 'midway'; import { DataTypes } from 'sequelize'; import { IDB } from './db'; export default async function setupModel(context: IApplicationContext) { const db: IDB = await context.getAsync('DB'); const { sequelize } = db; const attributes = { id: { type: DataTypes.BIGINT, allowNull: false, defaultValue: null, primaryKey: false, autoIncrement: false, comment: null, field: 'id', unique: 'uk_id', }, name: { type: DataTypes.STRING(32), allowNull: false, defaultValue: null, primaryKey: false, autoIncrement: false, comment: 'user name', field: 'name', }, email: { type: DataTypes.STRING(32), allowNull: false, defaultValue: null, primaryKey: false, autoIncrement: false, comment: 'user email', field: 'name', }, createdAt: { type: DataTypes.DATE, allowNull: false, defaultValue: null, primaryKey: false, autoIncrement: false, comment: 'created time', field: 'created_at', }, updatedAt: { type: DataTypes.DATE, allowNull: false, defaultValue: null, primaryKey: false, autoIncrement: false, comment: 'update time', field: 'updated_at', }, }; const options = { tableName: 'user', comment: 'user table', indexes: [{ name: 'uk_name_email', unique: true, fields: [ 'name', 'email', ], }] }; return sequelize.define('userModel', attributes, options); } providerWrapper([ { id: 'UserModel', provider: setupModel, }, ]);