第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

nodejs使用Sequelize框架操作數(shù)據(jù)庫(kù)

標(biāo)簽:
Node.js

sequelize.define

使用该方法可以定义model,例子如下:

const Sequelize = require('sequelize');

var sequelize = new Sequelize(config.database, config.username, config.password, {
    host: config.host,
    dialect: 'mysql',
    pool: {
        max: 5,
        min: 0,
        idle: 30000
    }
});

var Website = sequelize.define('website', {
    id: {
        type: Sequelize.BIGINT,
        primaryKey: true,
        autoIncrement: true
    },
    url: Sequelize.STRING(255),
    title: Sequelize.STRING(255),
    status: Sequelize.INTEGER,
    delete_mark: Sequelize.BOOLEAN
}, {
    timestamps: false
});

该方法传入的第一个参数是数据表的单数形式,怎么理解呢?例如这里传入的是website其实是模型名,数据表默认是websites这样的复数形式,这种约定我在Laravel中也碰见过,
也就是常说的,约定大于定义,也就是说,如果我们都按照约定的规范去开发,那么效率其实比重新定义,要高很多。
那么,定义好了模型,该怎么进行使用呢?

(async () => {
    let demo = await Website.create({
       url:'http://www.xxxx.com/',
       title:'demo'
    });
    console.log(demo);
})();

继承Model

const {Sequelize, DataTypes, Model} = require('sequelize');
const config = require('../config');

const sequelize = new Sequelize(config.database, config.username, config.password, {
    host: config.host,
    dialect: 'mysql',
    pool: {
        max: 5,
        min: 0,
        idle: 30000
    }
});

/**
 * @author chaojilaji
 * 数据表websites的关系对象映射
 */
class WebSite extends Model {

}

WebSite.init({
    id: {
        type: Sequelize.BIGINT,
        primaryKey: true,
        autoIncrement: true
    },
    url: Sequelize.STRING(255),
    title: Sequelize.STRING(255),
    status: Sequelize.INTEGER,
    delete_mark: Sequelize.BOOLEAN
}, {
    sequelize,
    modelName: 'Website',
    timestamps:false
});

(async () => {
    await sequelize.sync();
    let x = await WebSite.create({
        url: 'http://www.xxxxxxxx.com/',
        title: 'demo2'
    });
    console.log(x);
})();

module.exports = WebSite;

我比较推荐使用继承Model这种方式,通过创建一个class,这样可以使用model.exports=模块名的方式,将该模型封装起来。供别的地方使用,只需要require进去即可。

具体如何对数据表进行操作,就比较简单了,只需要参考API即可。sequelize文档地址
>炒鸡辣鸡原创文章,转载请注明来源

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消