1 Star 0 Fork 7

黄欣欣/Koa使用Sequelize的Demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
db.js 1.56 KB
一键复制 编辑 原始数据 按行查看 历史
"use strict";
const { Sequelize, DataTypes } = require("sequelize");
const sequelize = new Sequelize("officer", "postgres", "qq_112358", {
host: "8.129.171.243",
dialect:
"postgres" /* 选择 'mysql' | 'mariadb' | 'postgres' | 'mssql' 其一 */,
});
function defineModel(seqInstance, name, attrs) {
let objAttrs = {};
objAttrs.id = {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
};
for (let p in attrs) {
let value = attrs[p];
if (typeof value === "object" && value["type"]) {
value.allowNull = value.allowNull === undefined ? false : value.allowNull;
objAttrs[p] = value;
} else {
objAttrs[p] = {
type: value,
allowNull: false,
};
}
}
objAttrs.createdAt = {
type: DataTypes.BIGINT,
allowNull: false,
};
objAttrs.updatedAt = {
type: DataTypes.BIGINT,
allowNull: false,
};
objAttrs.version = {
type: DataTypes.BIGINT,
allowNull: false,
};
objAttrs.remarks = {
type: DataTypes.STRING(800),
allowNull: true,
};
let User = seqInstance.define(name, objAttrs, {
tableName: name,
timestamps: false,
hooks: {
beforeValidate: function (obj) {
let now = Date.now();
if (obj.isNewRecord) {
obj.createdAt = now;
obj.updatedAt = now;
obj.version = 0;
} else {
obj.updatedAt = now;
obj.version += 1;
}
},
},
});
return User;
}
let obj = {
sequelize: sequelize,
defineModel: defineModel,
DataTypes:DataTypes
};
module.exports = obj;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bear-h-xx/my_koa_sequelize_app.git
[email protected]:bear-h-xx/my_koa_sequelize_app.git
bear-h-xx
my_koa_sequelize_app
Koa使用Sequelize的Demo
master

搜索帮助