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
- findOrCreate 除非找到一个满足查询参数的结果,否则方法
findOrCreate
将在表中创建一个条目(有查询到结果就查询否则就是往表里面插入)- 它将返回一个实例(找到的实例或创建的实例)和一个布尔值,指示该实例是已创建还是已经存在
- 使用
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)等常用接口