<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] > [github](https://github.com/sequelize/sequelize) ## 安裝 ``` $ npm install --save sequelize # 添加需要的 引擎 $ npm install --save pg pg-hstore $ npm install --save mysql2 $ npm install --save mariadb $ npm install --save sqlite3 $ npm install --save tedious # MSSQL ``` ## 基礎使用 ``` const Sequelize = require('sequelize'); const sequelize = new Sequelize('database', 'username', 'password', { host: 'localhost', dialect: 'mysql'|'sqlite'|'postgres'|'mssql', pool: { max: 5, min: 0, acquire: 30000, idle: 10000 }, ## 應用全局的模型參數 define: { freezeTableName: false, //取消自動表加s charset: 'utf8', dialectOptions: { collate: 'utf8_general_ci' }, timestamps: false, //不添加時間戳屬性 (updatedAt, createdAt) paranoid: true,//偽刪除 ,deletedAt必須設置, 前提開啟timestamps }, }); //創建模型 const User = sequelize.define('user', { firstName: { type: Sequelize.STRING }, lastName: { type: Sequelize.STRING } }); // force: true 如果表已經存在,將會丟棄表 User.sync({force: true}).then(() => { // 表已創建 return User.create({ firstName: 'John', lastName: 'Hancock' }); }); ``` ``` const sequelize = new Sequelize('connectionUri', { define: { timestamps: false // 默認為 true } }); ``` ## 模型定義 ``` { primaryKey: true, // type: Sequelize.STRING, //DATE BOOLEAN INTEGER allowNull: false, someUnique: {type: Sequelize.STRING, unique: true}, autoIncrement: true } //自動插入時間戳 myDate: { type: Sequelize.DATE, defaultValue: Sequelize.NOW } ``` ### Getters & setters ``` /* 一個用于 'title' 屬性的 getter */ get() { return this.getDataValue('title') } set(title) { this.setDataValue('title', title.toString().toLowerCase()); } ```` ### 驗證 `create` , `update` 和 `save` 上。 你也可以調用 validate() 手動驗證一個實例 ``` const ValidateMe = sequelize.define('foo', { foo: { type: Sequelize.STRING, validate: { is: ["^[a-z]+$",'i'], // 只允許字母 is: /^[a-z]+$/i, // 與上一個示例相同,使用了真正的正則表達式 not: ["[a-z]",'i'], // 不允許字母 isEmail: true, // 檢查郵件格式 (foo@bar.com) isEven(value) { if (parseInt(value) % 2 != 0) { throw new Error('Only even values are allowed!') // 我們也在模型的上下文中,所以如果它存在的話, // this.otherField會得到otherField的值。 } } }}); ``` ## 增刪改查 ### 查 查詢特定字段 ``` Model.findAll({ attributes: ['foo', 'bar'] }); ``` 重命名 ``` Model.findAll({ attributes: ['foo', ['bar', 'baz']] }); ``` 聚合 ``` Model.findAll({ attributes: [[sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']] }); ``` where ``` Post.findAll({ where: { authorId: 2 } }); Post.findAll({ where: { [Op.or]: [{authorId: 12}, {authorId: 13}] } }); // SELECT * FROM post WHERE authorId = 12 OR authorId = 13; Post.findAll({ where: { authorId: { [Op.or]: [12, 13] } } }); ``` ## 更新 ``` Post.update({ updatedAt: null, }, { where: { deletedAt: { [Op.ne]: null } } }); ``` 操作符 ``` const Op = Sequelize.Op [Op.and]: {a: 5} // 且 (a = 5) [Op.or]: [{a: 5}, {a: 6}] // (a = 5 或 a = 6) [Op.gt]: 6, // id > 6 [Op.gte]: 6, // id >= 6 [Op.lt]: 10, // id < 10 [Op.lte]: 10, // id <= 10 [Op.ne]: 20, // id != 20 [Op.eq]: 3, // = 3 [Op.not]: true, // 不是 TRUE [Op.between]: [6, 10], // 在 6 和 10 之間 [Op.notBetween]: [11, 15], // 不在 11 和 15 之間 [Op.in]: [1, 2], // 在 [1, 2] 之中 [Op.notIn]: [1, 2], // 不在 [1, 2] 之中 [Op.like]: '%hat', // 包含 '%hat' [Op.notLike]: '%hat' // 不包含 '%hat' [Op.iLike]: '%hat' // 包含 '%hat' (不區分大小寫) (僅限 PG) [Op.notILike]: '%hat' // 不包含 '%hat' (僅限 PG) [Op.regexp]: '^[h|a|t]' // 匹配正則表達式/~ '^[h|a|t]' (僅限 MySQL/PG) [Op.notRegexp]: '^[h|a|t]' // 不匹配正則表達式/!~ '^[h|a|t]' (僅限 MySQL/PG) [Op.iRegexp]: '^[h|a|t]' // ~* '^[h|a|t]' (僅限 PG) [Op.notIRegexp]: '^[h|a|t]' // !~* '^[h|a|t]' (僅限 PG) [Op.like]: { [Op.any]: ['cat', 'hat']} // 包含任何數組['cat', 'hat'] - 同樣適用于 iLike 和 notLike [Op.overlap]: [1, 2] // && [1, 2] (PG數組重疊運算符) [Op.contains]: [1, 2] // @> [1, 2] (PG數組包含運算符) [Op.contained]: [1, 2] // <@ [1, 2] (PG數組包含于運算符) [Op.any]: [2,3] // 任何數組[2, 3]::INTEGER (僅限PG) ``` ## 分頁 / 限制 ``` // 獲取10個實例/行 Project.findAll({ limit: 10 }) // 跳過8個實例/行 Project.findAll({ offset: 8 }) // 跳過5個實例,然后取5個 Project.findAll({ offset: 5, limit: 5 }) ``` ## 一對一關聯 ``` const Player = this.sequelize.define('player', {/* attributes */}); const Team = this.sequelize.define('team', {/* attributes */}); Player.belongsTo(Team); // 將向 Player 添加一個 teamId 屬性以保存 Team 的主鍵值 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看