sequelize-automate
Version:
Automatically generate bare sequelize models from your database.
28 lines (27 loc) • 5.5 kB
JSON
[
{
"file": "user.ts",
"code": "import { IApplicationContext, providerWrapper } from 'midway';\nimport { DataTypes } from 'sequelize';\nimport { IDB } from './db';\nexport default async function setupModel(context: IApplicationContext) {\n const db: IDB = await context.getAsync('DB');\n const attributes = {\n id: {\n type: DataTypes.INTEGER(11).UNSIGNED,\n allowNull: false,\n defaultValue: null,\n primaryKey: true,\n autoIncrement: true,\n comment: \"primary ket\",\n field: \"id\"\n },\n name: {\n type: DataTypes.STRING(100),\n allowNull: false,\n defaultValue: null,\n primaryKey: false,\n autoIncrement: false,\n comment: \"user name\",\n field: \"name\",\n unique: \"uk_name\"\n },\n email: {\n type: DataTypes.STRING(255),\n allowNull: false,\n defaultValue: null,\n primaryKey: false,\n autoIncrement: false,\n comment: \"user email\",\n field: \"email\"\n },\n createdAt: {\n type: DataTypes.DATE,\n allowNull: false,\n defaultValue: null,\n primaryKey: false,\n autoIncrement: false,\n comment: \"created datetime\",\n field: \"created_at\"\n },\n updatedAt: {\n type: DataTypes.DATE,\n allowNull: false,\n defaultValue: null,\n primaryKey: false,\n autoIncrement: false,\n comment: \"updated datetime\",\n field: \"updated_at\"\n }\n };\n const options = {\n tableName: \"user\",\n comment: \"\",\n indexes: []\n };\n return db.sequelize.define(\"userModel\", attributes, options);\n}\nproviderWrapper([{\n id: \"UserModel\",\n provider: setupModel\n}]);",
"fileType": "model"
},
{
"file": "user_post.ts",
"code": "import { IApplicationContext, providerWrapper } from 'midway';\nimport { DataTypes } from 'sequelize';\nimport { IDB } from './db';\nexport default async function setupModel(context: IApplicationContext) {\n const db: IDB = await context.getAsync('DB');\n const attributes = {\n id: {\n type: DataTypes.INTEGER(11).UNSIGNED,\n allowNull: false,\n defaultValue: null,\n primaryKey: true,\n autoIncrement: true,\n comment: \"primary key\",\n field: \"id\"\n },\n userId: {\n type: DataTypes.INTEGER(11).UNSIGNED,\n allowNull: false,\n defaultValue: null,\n primaryKey: false,\n autoIncrement: false,\n comment: \"user id\",\n field: \"user_id\",\n references: {\n key: \"id\",\n model: \"userModel\"\n }\n },\n title: {\n type: DataTypes.STRING(255),\n allowNull: false,\n defaultValue: null,\n primaryKey: false,\n autoIncrement: false,\n comment: \"post title\",\n field: \"title\"\n },\n content: {\n type: DataTypes.TEXT,\n allowNull: true,\n defaultValue: null,\n primaryKey: false,\n autoIncrement: false,\n comment: \"post content\",\n field: \"content\"\n },\n createdAt: {\n type: DataTypes.DATE,\n allowNull: false,\n defaultValue: null,\n primaryKey: false,\n autoIncrement: false,\n comment: \"created datetime\",\n field: \"created_at\"\n },\n updatedAt: {\n type: DataTypes.DATE,\n allowNull: false,\n defaultValue: null,\n primaryKey: false,\n autoIncrement: false,\n comment: \"updated datetime\",\n field: \"updated_at\"\n }\n };\n const options = {\n tableName: \"user_post\",\n comment: \"\",\n indexes: [{\n name: \"fk_user_id\",\n unique: false,\n type: \"BTREE\",\n fields: [\"user_id\"]\n }]\n };\n return db.sequelize.define(\"userPostModel\", attributes, options);\n}\nproviderWrapper([{\n id: \"UserPostModel\",\n provider: setupModel\n}]);",
"fileType": "model"
},
{
"file": "user.d.ts",
"code": "import { Model, BuildOptions } from 'sequelize';\nexport interface IUserAttributes {\n id: number,\n name: string,\n email: string,\n createdAt: Date,\n updatedAt: Date,\n}\nexport interface IUserModel extends IUserAttributes, Model {}\nexport type IUserModelStatic = typeof Model & {\n new (values?: object, options?: BuildOptions): IUserModel;\n};",
"fileType": "definition"
},
{
"file": "user_post.d.ts",
"code": "import { Model, BuildOptions } from 'sequelize';\nexport interface IUserPostAttributes {\n id: number,\n userId: number,\n title: string,\n content?: string,\n createdAt: Date,\n updatedAt: Date,\n}\nexport interface IUserPostModel extends IUserPostAttributes, Model {}\nexport type IUserPostModelStatic = typeof Model & {\n new (values?: object, options?: BuildOptions): IUserPostModel;\n};",
"fileType": "definition"
},
{
"file": "db.ts",
"code": "import { async, config, init, provide, scope, ScopeEnum } from 'midway';\nimport { Sequelize } from 'sequelize';\n\nexport interface IDB {\n sequelize: Sequelize;\n}\n\n@scope(ScopeEnum.Singleton)\n@async()\n@provide('DB')\nexport default class DB implements IDB {\n public sequelize: Sequelize;\n\n @config('mysql')\n config;\n\n @init()\n public connect() {\n const { database, username, password, options } = this.config;\n // https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor\n this.sequelize = new Sequelize(database, username, password, options);\n }\n}\n",
"fileType": "model"
}
]