Sequelize 是 Node.js 的基于 Promise 的 ORM。 它可與 PostgreSQL婚温,MySQL召噩,SQLite 和 MSSQL 方言配合使用,并具有可靠的事務(wù)支持眼溶,關(guān)系,讀取復(fù)制等功能晓勇。(對(duì)象關(guān)系映射(ORM)是一種從面向?qū)ο蟮恼Z(yǔ)言訪問關(guān)系數(shù)據(jù)庫(kù)的技術(shù)堂飞。)
下面以MySQL為例。
首先安裝sequelize.js:
npm i sequelize
創(chuàng)建連接
這里把數(shù)據(jù)庫(kù)的基本信息做了一個(gè)封裝
基本信息配置文件 dbinfo.js
const config = {
database: "db", // 數(shù)據(jù)庫(kù)名稱
username: "root", // 用戶名
password: "123456", // 密碼
host: "localhost", // 主機(jī)地址
port: "3306", // 端口號(hào)
dialect: "mysql", //數(shù)據(jù)庫(kù)類型绑咱,支持: 'mysql', 'sqlite', 'postgres', 'mssql'
// logging: true, // 是否啟用日志
}
module.exports = config
創(chuàng)建連接配置文件 connect.js
// 在connect.js文件中,使用sequelize連接數(shù)據(jù)庫(kù)
const config = require('./dbinfo') // 引入數(shù)據(jù)庫(kù)配置信息
const { Sequelize, DataTypes ,Op} = require("sequelize") // 引入sequelize依賴
const sequelize = new Sequelize(
config.database,
config.username,
config.password,
{
dialect: config.dialect,
dialectOptions: {
dateStrings: true,
typeCast: true
},
host: config.host,
port: config.port,
logging: config.logging,
pool: { // 連接池配置
min: 0, // 最小連接數(shù)
max: 5, // 最大鏈接數(shù)
idle: 30000,
acquire: 60000,
},
define: {
// 字段以下劃線(_)來分割(默認(rèn)是駝峰命名風(fēng)格)
underscored: true
},
timezone: '+08:00'
}
)
module.exports = {
sequelize,// 將sequelize暴露出接口方便Model調(diào)用
DataTypes,
Op
}
創(chuàng)建模型
舉個(gè)例子绰筛,創(chuàng)建一個(gè)新聞表模型
./models/news.js
// 引入連接
const {
sequelize,
DataTypes
} = require('../config/connect')
const News = sequelize.define(
'xx_news',
{
news_id: {
type: DataTypes.INTEGER(11),
allowNull: false, // 是否允許為空
autoIncrement: true,
primaryKey: true, // 是否主鍵
},
cover_img: {
type: DataTypes.STRING,
// allowNull: false,
comment: '新聞列表項(xiàng)封面'
},
news_title: {
type: DataTypes.STRING,
allowNull: false,
comment: '新聞標(biāo)題'
},
news_desc: {
type: DataTypes.STRING,
// allowNull: false,
allowNull: false,
comment: '新聞列表項(xiàng)簡(jiǎn)要描述'
},
news_content: {
type: DataTypes.TEXT,
// allowNull: false,
comment: '新聞內(nèi)容'
},
is_hot: {
type: DataTypes.BOOLEAN,
// allowNull: false,
allowNull: false,
defaultValue: false,
comment: '新聞是否熱門'
},
news_path: {
type: DataTypes.STRING,
allowNull: true,
comment: '新聞訪問路徑'
},
state: {
type: DataTypes.BOOLEAN, // 字段類型
allowNull: false, // 是否允許為空
defaultValue: false,
comment: '狀態(tài):true/已發(fā)布,false/草稿'
}
}, {
timestamps: true,
// paranoid 模型, 軟刪除
paranoid: true,
// 如果要為 deletedAt 列指定自定義名稱
deletedAt: 'destroy_time',
// 不想要 createdAt
createdAt: 'publish_time',
// 想要 updatedAt 但是希望名稱叫做 updateTime
updatedAt: 'update_time'
}
)
// News.sync({force:true}) // 是否自動(dòng)創(chuàng)建表
module.exports = News
初始化
./models/index.js
const {
sequelize,
Op
} = require('../config/connect')
const News = require('./news')
sequelize.sync({ alter: true })
module.exports = {
News
}
/*
// 標(biāo)準(zhǔn)同步
// 只有當(dāng)數(shù)據(jù)庫(kù)中不存在與模型同名的數(shù)據(jù)表時(shí)描融,才會(huì)同步
sequelize.sync()
// 動(dòng)態(tài)同步
// 修改同名數(shù)據(jù)表結(jié)構(gòu)铝噩,以適用模型。
sequelize.sync({alter: true})
// 強(qiáng)制同步
// 刪除同名數(shù)據(jù)表后同步窿克,謹(jǐn)慎使用骏庸,會(huì)導(dǎo)致數(shù)據(jù)丟失
sequelize.sync({force: true})
// 另外毛甲,當(dāng)你指定表與表之間的關(guān)聯(lián)后,修改被關(guān)聯(lián)的表結(jié)構(gòu)時(shí)會(huì)拋出異常具被。
// 需要先注釋掉關(guān)聯(lián)代碼玻募,然后更新同步模型后,再取消掉注釋即可一姿。
// 再另外七咧,當(dāng)你有新的關(guān)聯(lián)時(shí)必須使用動(dòng)態(tài)同步才會(huì)生效。
*/
先到這里叮叹,下回繼續(xù)艾栋。