zoukankan      html  css  js  c++  java
  • sequelize的get/post方法例子

    定义两个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);
      }
    
    });
  • 相关阅读:
    luoguP3822 [NOI2017]整数
    luoguP2150 [NOI2015]寿司晚宴
    luoguP3868 [TJOI2009]猜数字
    luoguP4777 【模板】扩展中国剩余定理(EXCRT)
    luoguP2048 超级钢琴
    题解 P1004 【方格取数】
    戊戌年西安游记
    题解 P4388 【付公主的矩形】
    题解 P4277 【河城荷取的烟花】
    001 dynamic Linq
  • 原文地址:https://www.cnblogs.com/smartsensor/p/8058027.html
Copyright © 2011-2022 走看看