npm install sequelize --save
npm install mysql2 --save
var config = {
dbname: 'testdb',
uname: 'root',
upwd: 'root',
host: 'localhost',
port: 3306,
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
};
module.exports = config;
// 引入模块
const Sequelize = require('sequelize');
// 读取配置
const mysqlConfig = require('../configs/mysql-config');
// 根据配置实例化seq
var seq = new Sequelize(mysqlConfig.dbname, mysqlConfig.uname, mysqlConfig.upwd, {
host: mysqlConfig.host,
dialect: mysqlConfig.dialect,
pool: mysqlConfig.pool
});
/**
* 定义数据模型
*
* @param {any} name 模型名称【数据库表名】
* @param {any} attributes 数据字段集合
* @returns 数据模型对象
*/
function defineModel (name, attributes) {
var attrs = {};
for (let key in attributes) {
let value = attributes[key];
if (typeof value === 'object' && value['type']) {
value.allowNull = value.allowNull || false;
attrs[key] = value;
} else {
attrs[key] = {
type: value,
allowNull: false
};
}
}
// 附加公共字段
// attrs.id = {
// type: ID_TYPE,
// primaryKey: true
// };
attrs.createAt = {
type: Sequelize.BIGINT,
allowNull: false
};
attrs.updateAt = {
type: Sequelize.BIGINT,
allowNull: false
};
attrs.version = {
type: Sequelize.BIGINT,
allowNull: false
};
// 状态:0表示有效,1表示无效,2表示已删除,默认为0.
attrs.status = {
type: Sequelize.INTEGER,
allowNull: false
};
// 调用seq的方法定义模型并返回
return seq.define(name, attrs, {
tableName: name,
timestamps: false,
hooks: {
beforeValidate: function (obj) {
let now = Date.now();
if (obj.isNewRecord) {
obj.createAt = now;
obj.updateAt = now;
obj.version = 0;
} else {
obj.updateAt = now;
++obj.version;
}
}
}
});
}
exports.defineModel = defineModel;
var db = require('../db');
var seq = require('sequelize');
var Model = db.defineModel('Notices', {
content: seq.TEXT,
title: seq.STRING(30),
startDate: seq.BIGINT,
expireDate: seq.BIGINT,
gmId: seq.INTEGER(10),
});
// 导出模型对象
module.exports = Model;
Model.sync();
Model.sync({force: true});
var Notices = require('../data/model/Notices');
Notices.create({
content: '我是公告内容。',
title: '系统公告的标题',
gmId: '10086',
status: 0,
expireDate: 1527396599123,
startDate: Date.now()
}).then((data) => {
res.json({ code: 0, msg: '公告发布成功', result: data });
});
(async () => {
var data = await Notices.create({
content: '我是公告内容。',
title: '系统公告的标题',
gmId: '10086',
status: 0,
expireDate: 1527396599123,
startDate: Date.now()
});
res.json({ code: 0, msg: '公告发布成功', result: data });
})();
Notices.update({
status: 2
}, {
where: {id: 100}
});
Notices.findAll();
Notices.findAll({order: [['createAt', 'DESC']], limit: 10, where: {'status': 0}});
var seq = require('sequelize');
var Op = seq.Op;
// 其它代码...
Model.update({
status: 2,
gmId: 10086
}, {
where: {
id: {
[Op.in]: [1, 4, 2, 8, 13, 20]
}
}
});
var date = Date.now();
Model.findAll({
where: {
status: 0,
sendDate: {[Op.lte]: date},
expireDate: {[Op.gte]: date},
[Op.or]: [
{to: 1000017},
{to: 0}
]
}
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有