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);
      }
    
    });
  • 相关阅读:
    7-1 N个数求和
    3662. 最大上升子序列和
    树状数组
    堆优化Dijkstra java模板
    皮亚诺曲线距离
    最长公共子序列(计数问题)
    最小路径覆盖
    极角排序
    2619. 询问
    Hessian矩阵与局部极小值
  • 原文地址:https://www.cnblogs.com/smartsensor/p/8058027.html
Copyright © 2011-2022 走看看