查找器
findAll
在上⼀教程中已经知道 findAll ⽅法. 它⽣成⼀个标准的 SELECT 查询,该查询将从表中检索所有条⽬(除⾮受到 where ⼦句的限制).
findByPk
findByPk ⽅法使⽤提供的主键从表中仅获得⼀个条⽬.
app.get('/searchById', (req, res) => {
TCar.findByPk(req.query.id).then(data => {
res.send({
code: 0,
msg: "操作成功",
data: data
})
}).catch((error) => {
// 失败
res.send({
code: -1,
msg: "操作出错",
detail: error
})
})
})
findOne
findOne ⽅法获得它找到的第⼀个条⽬(它可以满⾜提供的可选查询参数).
app.get('/searchByPrice', (req, res) => {
TCar.findOne({
where: {
guide_price: {
[Op.gt]: req.query.price
}
}
}).then(data => {
res.send({
code: 0,
msg: "操作成功",
data: data
})
}).catch((error) => {
// 失败
res.send({
code: -1,
msg: "操作出错",
detail: error
})
})
})
.findOrCreate
除⾮找到⼀个满⾜查询参数的结果,否则⽅法 findOrCreate 将在表中创建⼀个条⽬. 在这两种情况下,它将返回⼀个实例(找到的实例或创建的实例)和⼀个布尔值,指示该实例是已创建还是已经存在.使⽤ where 参数来查找条⽬,⽽使⽤ defaults 参数来定义必须创建的内容. 如果 defaults 不包含每⼀列的值,则 Sequelize 将采⽤ where 的值(如果存在).假设我们有⼀个空的数据库,该数据库具有⼀个 User 模型,该模型具有⼀个 username 和⼀个 job.
app.post('/findOrCreate', (req, res) => {
TCar.findOrCreate({
where: {
car_num: req.body.car_num // 查询条件
},
defaults: req.body // 如果找不到则创建的默认值
}).then(data => {
res.send({
code: 0,
msg: "操作成功",
data: data
})
}).catch((error) => {
// 失败
res.send({
code: -1,
msg: "操作出错",
detail: error
})
})
})
findAndCountAll
findAndCountAll ⽅法是结合了 findAll 和 count 的便捷⽅法. 在处理与分⻚有关的查询时⾮常有
⽤,在分⻚中,你想检索带有 limit 和 offset 的数据,但⼜需要知道与查询匹配的记录总数.
当没有提供 group 时, findAndCountAll ⽅法返回⼀个具有两个属性的对象:
-
count - ⼀个整数 - 与查询匹配的记录总数
-
rows - ⼀个数组对象 - 获得的记录
当提供了 group 时, findAndCountAll ⽅法返回⼀个具有两个属性的对象:
-
count - ⼀个数组对象 - 包含每组中的合计和预设属性
-
rows - ⼀个数组对象 - 获得的记录
const { count, rows } = await Project.findAndCountAll({
where: {
title: {
[Op.like]: 'foo%'
}
},
offset: 10,
limit: 2
});
console.log(count);
console.log(rows);
获取器
获取器是为模型定义中的⼀列定义的get()函数:
const { sequelize, DataTypes, } = require('../config/database');
const TCar = sequelize.define('t_car', {
car_num: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
comment: '编号',
validate: {
isNumeric:true,
max: 99999
}
},
guide_price: {
type: DataTypes.DECIMAL(10, 2),
allowNull: true,
defaultValue: null,
comment: '⼚商指导价',
get() {
return "¥"+this.getDataValue('guide_price')
}
},
}, {
tableName: 't_car',
timestamps: false,
});
module.exports = TCar
设置器
设置器是为模型定义中的⼀列定义的 set() 函数. 它接收要设置的值:
const { sequelize, DataTypes, } = require('../config/database');
const TCar = sequelize.define('t_car', {
brand: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
comment: '品牌',
set(value) {
this.setDataValue('brand', value.trim()+"牌")
}
}
}, {
tableName: 't_car',
timestamps: false,
});
module.exports = TCar
如果我们想将模型实例中的另⼀个字段包含在计算中,那也是可以的,⽽且⾮常容易!
const User = sequelize.define('user', {
username: DataTypes.STRING,
password: {
type: DataTypes.STRING,
set(value) {
// 在数据库中以明⽂形式存储密码是很糟糕的.
// 使⽤适当的哈希函数来加密哈希值更好.
// 使⽤⽤户名作为盐更好.
this.setDataValue('password', hash(this.username + value));
}
}
});
虚拟字段
虚拟字段是 Sequelize 在后台填充的字段,但实际上它们不存在于数据库中.
例如,假设我们有⼀个 User 的 firstName 和 lastName 属性.
const { sequelize, DataTypes, } = require('../config/database');
const TCar = sequelize.define('t_car', {
id: {
type: DataTypes.BIGINT,
allowNull: false,
autoIncrement: true,
primaryKey: true,
comment: '唯⼀标识'
},
car_num: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
comment: '编号',
validate: {
isNumeric: true,
max: 99999
}
},
brand: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
comment: '品牌',
set(value) {
this.setDataValue('brand', value.trim() + "牌")
}
},
guide_price: {
type: DataTypes.DECIMAL(10, 2),
allowNull: true,
defaultValue: null,
comment: '⼚商指导价',
get() {
return "¥" + this.getDataValue('guide_price')
}
},
product_time: {
type: DataTypes.CHAR(10),
allowNull: true,
defaultValue: null,
comment: '⽣产⽇期'
},
car_type: {
type: DataTypes.STRING(255),
allowNull: true,
defaultValue: null,
comment: '汽⻋类型'
},
introduce: {
type: DataTypes.VIRTUAL,
get() {
return `${this.product_time}年⽣产的${this.brand}${this.car_typ
e}`;
},
set(value) {
throw new Error('不要尝试设置 `fullName` 的值!');
}
}
}, {
tableName: 't_car',
timestamps: false,
});
module.exports = TCar