久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      nodejs版的orm庫(kù)–sequelize

      本篇文章帶大家了解一下nodejs數(shù)據(jù)庫(kù)orm擴(kuò)展-sequelize。有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)大家有所幫助。

      nodejs版的orm庫(kù)--sequelize

      sequelize是nodejs版的orm庫(kù),用過laravelORM的能很快能上手

      【視頻教程推薦:node js教程 】

      具體文檔

      • 官網(wǎng)
      • github

      簡(jiǎn)單代碼demo

      const { Sequelize, DataTypes, Model, QueryTypes, Op } = require("sequelize"); const sequelize = new Sequelize("sqlite://sql.db", { logging: false });  class User extends Model {} class Address extends Model {}  User.init(   {     // 在這里定義模型屬性     id: {       type: DataTypes.INTEGER,       primaryKey: true,       autoIncrement: true,     },     name: {       type: DataTypes.STRING,       unique: true,       // allowNull 默認(rèn)為 true       validate: {         async isUnique(name) {           const res = await User.findOne({where: {name}})           if (res) throw new Error('用戶名已存在')         },         // len: [1,2]       }     },   },   {     // 這是其他模型參數(shù)     sequelize, // 我們需要傳遞連接實(shí)例     // modelName: "User", // 我們需要選擇模型名稱     tableName:'users' // 表名,默認(rèn)為模型名的復(fù)數(shù)單詞   } );  Address.init(   {     id: {       type: DataTypes.INTEGER,       primaryKey: true,       autoIncrement: true,     },     name: {       type: DataTypes.STRING,       unique: true,       // allowNull 默認(rèn)為 true     },   },   {     sequelize,     modelName: "Address",   } );  // 模型關(guān)系 多對(duì)多 User.belongsToMany(Address, { through: "userAddress", as:'addres' }); // through 代表中間表的名字,as是查詢別名 Address.belongsToMany(User, { through: "userAddress" });  (async () => {   try {     // await sequelize.sync({ alter: true });  // 同步模型到數(shù)據(jù)庫(kù)-創(chuàng)建表     // const user = await User.findOne({ where: { name: {[Op.like]:'%小%'} } }); // 基本查詢     const [user] = await User.findOrCreate({where:{name:'小小'},include:'addres'}); // 順帶查詢到關(guān)聯(lián)模型的數(shù)據(jù)          const [address] = await Address.findOrCreate({where:{name:'小小de地址'}});     await user.addAddress(address); // 關(guān)聯(lián)增加      console.log(user.toJSON());   } catch (e) {     console.log(e);   } })();

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)