Sequelize模型的【查找】以及【批量新增】【批量编辑】等常用接口

1.查询

  • findAll

findAll 它会生成一个标准的 select 查询,该查询将在表里面检索所有的条目,除非收到 where 的限制

具体的使用如下:

// service => test.ts
import { Service } from 'egg';
export default class TestService extends Service {
    public async list() {
        return await ctx.model.Test.findAll({
            order: [['id': 'DESC']],
        })
    }
}
  • findByPK

findByPK 使用表中的主键,从表中获取一条数据

具体的使用如下:

// service => test.ts
import { Service } from 'egg';
export default class TestService extends Service {
    public async listByPK() {
        return await ctx.model.Test.findByPK(1)
    }
}
  • findOne

findOne 获得从表中找到的第一条的条目

具体的使用如下:

// service => test.ts
import { Service } from 'egg';
export default class TestService extends Service {
    const { ctx } = this;
    const { id } = ctx.query;
    public async listOne() {
        return await ctx.model.Test.findOne({
            where: {
                id
            }
        })
    }
}
  • findAndCountAll

findAndCountAll 方法是结合了 findAll 和 count 的便捷方法. 在处理与分页有关的查询时非常有用,在分页中,你想检索带有 limit 和 offset 的数据,但又需要知道与查询匹配的记录总数。

findAndCountAll 方法返回一个具有两个属性的对象:

  • count - 一个整数 - 符合查询条件的记录总数
  • rows - 一个数组对象 - 获得的记录

具体的使用如下:

// service => test.ts
import { Service } from 'egg';
export default class TestService extends Service {
    const { ctx } = this;
    const { id } = ctx.query;
    public async listPage() {
        return await ctx.model.Test.findAndCountAll({
            order: [['id', 'DESC']],
            offset: 15,
            limit: 1,
            where: {
                id
            }
        })
    }
}
  • findOrCreate
  1. findOrCreate 除非找到一个满足查询参数的结果,否则方法 findOrCreate 将在表中创建一个条目(有查询到结果就查询否则就是往表里面插入)
  2. 它将返回一个实例(找到的实例或创建的实例)和一个布尔值,指示该实例是已创建还是已经存在
  3. 使用 where 参数来查找条目,而使用 defaults 参数来定义必须创建的内容. 如果 defaults 不包含每一列的值,则 Sequelize 将采用 where 的值(如果存在).

具体的使用如下:

// service => test.ts
import { Service } from 'egg';
export default class TestService extends Service {
    const { ctx } = this;
    const { id } = ctx.query;
    public async listCreate() {
        return await ctx.model.Test.findOrCreate({
            where: {
                id: 100
            },
            defaults: { job: 'javaScript' }
        })
    }
}

2.新增单条数据

  • create

create 只要把要要增加的数据传入即可

具体的使用如下:

// service => test.ts
import { Service } from 'egg';
export default class TestService extends Service {
    const { ctx } = this;
    const { id } = ctx.query;
    public async listCreate() {
        return await ctx.model.Test.create({
            where: {
                name: "张三",
                age: 24
            }
        })
    }
}

3.修改单挑数据

  • update

update 根据 id 修改数据,传入要修改的数据即可

具体的使用如下:

// service => test.ts
import { Service } from 'egg';
export default class TestService extends Service {
    const { ctx } = this;
    const { id } = ctx.query;
    public async listUpdate() {
        return await ctx.model.Test.update({
            name: "张三",
            age: 24
            },{
            where: {
                id: 1
            } })
    }
}

4.批量新增或者批量修改

  • bulkCreate 的 updateOnDuplicate

如果id存在,则update,否则insert

具体的使用如下:

// service => test.ts
import { Service } from 'egg';
export default class TestService extends Service {
    const { ctx } = this;
    const { status} = ctx.request.body; // 结构status: [{id: 1, status: 2}]
    public async listBatch() {
        try {
            return await ctx.model.Test.bulkCreate(status, {
                updateOnDuplicate: ['status']
            })
        } catch (e) {
            ctx.body = {
                code: 99,
                message: e && e.message || e
            };
        }
    }
}

参考文章:Sequelize Model Querying - Finders - 模型查询(查找器)及批量新增或批量更新(bulkCreate的updateOnDuplicate)等常用接口​​​​​​​ 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值