本文只做简单总结,更多操作请查阅官方文档:https://itbilu.com/nodejs/npm/VkYIaRPz-.html
Sequelize 简介
Sequelize是一个nodejs下通过ORM(Object Relational Mapping)操作数据库的工具,与原生的mysql工具相比,可以减少大量的编写数据库表上的工作
Sequelize的大致实现原理是:因为数据库表是二维的,因此可以被抽象为一个json对象,例如:
id | name | gender |
---|---|---|
1 | jojo | male |
2 | lisalisa | female |
3 | dio | male |
上表可以被抽象为:
[
{
id:1,
name:"jojo",
gender:"male"
},
{
id:2,
name:"lisalisa",
gender:"female"
},
{
id:3,
name:"dio",
gender:"male"
},
]
通过这种方式,我们就可以将面向对象的思想延伸到数据库操作的领域
Sequelize 入门
1.安装Sequelize
npm install sequelize -s
根据使用的数据库种类不同,还需要安装不同的驱动,这里以MySQL为例:
npm install mysql2 -s
具体Sequelize支持的数据库以及驱动安装方式请查看官方文档:https://itbilu.com/nodejs/npm/VkYIaRPz-.html#induction-install
2.连接数据库
const Sequelize = require("sequelize");
// 引入Sequelize
const sequelize = new Sequelize(
"数据库名",
"数据库账号",
"数据库密码",
{
'dialect': "mysql", // 数据库种类
'host': "", // 数据库地址
"port": "3306", // 端口
"define": {
"underscored": true,
},
"charset": 'utf8mb4', // 字符集
"collate": 'utf8_general_ci', // 排序顺序
"logging": false, // 是否需要打印SQL语句
}
)
通过如上的方式,我们便创建了一个数据库连接
3.定义模型
在Sequelize中,一个模型就是一张表,通过定义模型,就可以在数据库中获取一张表格,比如我们定义一个用户模型
var User = sequelize.define(
"users", // 表名
{
email: {
"type": Sequelize.STRING,
"allowNull": false,
},
passwd: {
"type": Sequelize.STRING,
"allowNull": false,
},
nickname: {
"type": Sequelize.STRING,
"defaultValue": "用户12138",
}
}
)
然后将模型同步到数据库
User.sync({force:false})
4.新建数据
const userinfo = {
email:"123@grimoire.cn",
passwd:"mrsen",
nickname:"北城",
}
// User.create(userinfo) // 返回一个 Primose 对象
User.create(userinfo)
.then(value => console.log(value.get({plain:true})))
.catch(error => console.error(error))
5.新建查询
// 单个查询
User.findOne({where:{email:"123@grimoire.cn"}})
.then(value => console.log(value.dataValues))
.catch(error => console.log(error))
// 查询全部
User.findAll({where:{gender:"male"}})
.then(value => console.log(value.dataValues))
.catch(error => console.log(error))
// 分页查询
// 查询 6-10 条数据
User.findAll({
where:{gender:"male"},
offset:5, // 偏移量为5
limit:5, // 限制返回数为5
}).then(value => console.log(value.dataValues))
.catch(error => console.log(error))
Sequelize 进阶
众所周知,MySQL是关系型数据库,因此我们可以在Sequelize里面关联数据表格进行关联查询
1.一对多
我们假设有两张表:用户表和产品表,我们规定每个用户可以拥有多个产品,
那么用户表和产品表之间就是一对多的关系
我们先将这两张表(模型)定义出来:
// 用户表
var User = sequelize.define(
"users", // 表名
{
email: {
"type": Sequelize.STRING,
"allowNull": false,
},
passwd: {
"type": Sequelize.STRING,
"allowNull": false,
},
nickname: {
"type": Sequelize.STRING,
"defaultValue": "用户12138",
}
}
)
// 产品表
var Products = sequelize.define(
"products",
{
name: {
type:Sequelize.STRING,
allowNull:false,
},
user_id:{
type:Sequelize.INTEGER,
allowNull:false,
}
}
)
为了让两张表产生关系,我们将两张表关联:
User.hasMany(Products, {foreignKey:"user_id", sourceKey:"id"});
Products.belongsTo(User, {foreignKey:"user_id", targetKey:"id"});
解释:使用Sequelize创建的表中,默认会使用一个自增的id字段来作为主键,因此我们只需要将Product 中的 user_id 字段与User中的id相绑定即可
查询语句:
Products.findAll({
where:{"$email$":"123@163.com"},
attributes:[Sequelize.col("email"), "name"],
include:[{
model:User,
attributes:[]
}],
raw:true,
}).then(value => console.log(value))
.catch(err => {
console.error(err)
})
// 查询 email 为 124@163.com 的用户拥有的所有产品
作者:NorthCity1984
出处:https://grimoire.cn/js/sequelize.html
版权:本文《Sequelize操作数据库》版权归作者所有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
出处:https://grimoire.cn/js/sequelize.html
版权:本文《Sequelize操作数据库》版权归作者所有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任