欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          nodejs版的orm庫–sequelize

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

          nodejs版的orm庫--sequelize

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

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

          具體文檔

          • 官網(wǎng)
          • github

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

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