定义两个model,一个给get的,一个给post的
var Sequelize = require('sequelize');
const DeviceNos = sequelize.define('DeviceDetail', { DeviceNo: { type: Sequelize.INTEGER } }, { tableName: 'DeviceDetail', timestamps: false, freezeTableName: true }) const Device = sequelize.define('DeviceDetail', { DeviceNo: { type: Sequelize.INTEGER }, Tem: { type: Sequelize.FLOAT, get() { return this.getDataValue("Tem").toFixed(2); } }, Hum: { type: Sequelize.FLOAT, get() { return this.getDataValue("Hum").toFixed(2); } }, Lng: { type: Sequelize.FLOAT, get() { return this.getDataValue("Lng").toFixed(2); } }, Lat: { type: Sequelize.FLOAT, get() { return this.getDataValue("Lat").toFixed(2); } }, ServiceTime: { type: Sequelize.DATE, get() { return moment(this.getDataValue('ServiceTime')).format('YYYY-MM-DD HH:mm:ss'); } } }, { tableName: 'DeviceDetail', timestamps: false, freezeTableName: true });
定义运算符
const Op = Sequelize.Op;
定义get/Post方法
router.post('/searchDeviceRecord', async function (ctx, next) {
let deviceNo = ctx.request.body.deviceNo;
let st = ctx.request.body.st;
let et = ctx.request.body.et;
console.log(st);
try {
var data = await Device.findAll({
attributes: ['DeviceNo', 'Tem', 'Hum', 'Lng', 'Lat', 'ServiceTime'],
where: {
deviceNo: deviceNo,
serviceTime: {
[Op.lte]: et,
[Op.gte]: st
}
},
order: [['ServiceTime', 'ASC']]
})
ctx.body = JSON.stringify(data);
} catch (e) {
console.log(e);
}
});
router.get('/getDeviceList', async function (ctx, next) {
try {
var data = await DeviceNos.findAll({
attributes: [[sequelize.literal('distinct DeviceNo'), 'DeviceNo']], order: [['DeviceNo', 'ASC']]
})
ctx.body = JSON.stringify(data);
} catch (e) {
console.log(e);
}
});

浙公网安备 33010602011771号