NodeJs 笔记

Sequelize基本用法

Sequelize中用group by进行分组聚合查询

await ctx.model.Log.sum('log_agent_earn', { group: 'log_sho_id', plain:false, having:[{log_sho_id: body.id}]})

生成的SQL如下:

SELECT sum(`log_agent_earn`) AS `sum` FROM `t_log` AS `t_log` GROUP BY `log_sho_id` HAVING (`log_sho_id` = '1');

  

let data = await ctx.model.Log.findAll({
            attributes: ['log_date',
                [this.app.Sequelize.fn('SUM', this.app.Sequelize.col('log_order_num')), 'log_order_num'],
                [this.app.Sequelize.fn('SUM', this.app.Sequelize.col('log_pay_money')), 'log_pay_money'],
                [this.app.Sequelize.fn('SUM', this.app.Sequelize.col('log_shop_earn')), 'log_shop_earn'],
                [this.app.Sequelize.fn('SUM', this.app.Sequelize.col('log_pc_earn')), 'log_pc_earn'],
                [this.app.Sequelize.fn('SUM', this.app.Sequelize.col('log_agent_earn')), 'log_agent_earn'],
                [this.app.Sequelize.fn('SUM', this.app.Sequelize.col('log_user_earn')), 'log_user_earn'],
                [this.app.Sequelize.fn('SUM', this.app.Sequelize.col('log_other_earn')), 'log_other_earn'],
            ],
            group: 'log_date', raw: true,
            having:[{log_date: {
                $gte: body.startTime, // >=
                $lte: body.endTime    // <=
            }}]
        })

生成的SQL如下:

SELECT `log_date`,  SUM(`log_order_num`) AS `log_order_num`, 
            SUM(`log_pay_money`) AS `log_pay_money`,             SUM(`log_shop_earn`) AS `log_shop_earn`,
            SUM(`log_pc_earn`) AS `log_pc_earn`,
            SUM(`log_agent_earn`) AS `log_agent_earn`,
            SUM(`log_user_earn`) AS `log_user_earn`,
            SUM(`log_other_earn`) AS `log_other_earn`
FROM `t_log` AS `t_log` GROUP BY `log_date`
HAVING ((`log_date` >= '2019-01-06 00:00:00' AND `log_date` <= '2019-01-08 17:11:28'));

汇总

await ctx.model.Log.sum('log_agent_earn', { where: { log_age_id: body.id } })

统计 count()

await ctx.model.Shop.count({ where: { sho_age_id: body.id } })

最大或最小

最大或最小
Task.max('id').on('success', function(max){
    console.log(max);
}).on('failure', function(err){
    console.log(err);
});

按ID查询

const oneUser = await UserModel.findById(id)

查询所有

const allUser = await UserModel.findAll()

按条件查询

  const someUser = await UserModel.findAll({
    where: {
      // 模糊查询
      name: {
        $like: '%小%',
      },

      // 精确查询
      password: 'root',
    }
  })

分页查询

  const size = 10 // 每页10条数据
  const page = 1 // 页数
  const pageUser = await UserModel.findAndCountAll({
    where: {
      name: {
        $like: '%小%',
      },
    },
    limit: size,
    offset: size * (page - 1),
  })
});

排序

await ctx.model.Log.findAndCountAll({
  where: { log_sho_id: body.id, log_date: body.date },
  'order': [
    ['log_create_time', 'DESC']  
  ],
  limit: parseInt(body.pageSize),
  offset: parseInt((body.page - 1) * (body.pageSize))
})
 

连表查询

    this.ctx.model.Bill.belongsTo(this.ctx.model.User, { foreignKey: 'bil_use_id' })  // 建立临时外键
    this.ctx.model.Bill.belongsTo(this.ctx.model.Shop, { foreignKey: 'bil_sho_id' })

    let list = await ctx.model.Bill.findAndCountAll({
      include: [{
        model: this.ctx.model.User,
        attributes: ['use_name', 'use_phone']   // 只显示那些字段
      },
      {
        model: this.ctx.model.Shop,
        attributes: ['sho_name']
      }
      ],
      where: {
        bil_sho_id: body.id,
        bil_order_state: 1,   // 已付款的订单
        bil_create_time: {
          $gte: body.startTime, // >=
          $lte: body.endTime    // <=
        }
      },
      limit: parseInt(body.pageSize),
      offset: parseInt(body.page - 1) * parseInt(body.pageSize)
    })

egg 笔记

 

读文件

        let ret = path.join(this.config.baseDir, 'app/controller', 'sandbox_private.pem');

        let data = fs.readFileSync( ret, 'utf-8');

momentJS时间加减处理

    const moment = require('moment');

console.log(moment().format(
"YYYY-MM-DD HH:mm:ss")); //当前时间 console.log(moment().subtract(10, "days").format("YYYY-MM-DD")); //当前时间的前10天时间 console.log(moment().subtract(1, "years").format("YYYY-MM-DD")); //当前时间的前1年时间 console.log(moment().subtract(3, "months").format("YYYY-MM-DD")); //当前时间的前3个月时间 console.log(moment().subtract(1, "weeks").format("YYYY-MM-DD")); //当前时间的前一个星期时间 console.log(moment().subtract(1, "hours").format("YYYY-MM-DD HH:mm:ss")); // 当前时间的前1小时 console.log(moment().subtract(5, "minutes").format("YYYY-MM-DD HH:mm:ss")); // 当前时间的前5分钟 console.log(moment().add(1, "hours").format("YYYY-MM-DD HH:mm:ss")); // 当前时间的后1小时

 

原文地址:https://www.cnblogs.com/zyulike/p/10155685.html