什么網(wǎng)站可以做美食寧波網(wǎng)絡(luò)營銷推廣咨詢報(bào)價(jià)
sequelize egg-sequelize-auto整個過程還是有一些坑 包括兼容性問題? 依賴安裝問題? 需要注意? 缺少一個條件 包跑不起來 或使用體驗(yàn)很差
1. 全局安裝插件?
pnpm install -g sequelize-cli ?sequelize mysql2 egg-sequelize-auto
2. 執(zhí)行命令創(chuàng)建?migrate遷移文件 以及 model文件
npx sequelize model:generate --name Activation --attributes activationCode:string,machineCode:string,activationVersion:string,email:string
2.1執(zhí)行這個命令的前提是在根目錄下創(chuàng)建.sequelizerc文件,告訴sequelize 各個命令執(zhí)行以及生成的存放目錄
'use strict';const path = require('path');module.exports = {config: path.join(__dirname, 'config/config.json'),'migrations-path': path.join(__dirname, 'database/migrations'),'seeders-path': path.join(__dirname, 'database/seeders'),'models-path': path.join(__dirname, 'app/model'),
};
注意 : 生成的model 很可能用不了? 因?yàn)榧嫒莸膯栴}?
3.給自動生成的migrate文件添加注釋?
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {async up(queryInterface, Sequelize) {await queryInterface.createTable('Activations', {id: {allowNull: false,autoIncrement: true,primaryKey: true,type: Sequelize.INTEGER},activationCode: {type: Sequelize.STRING,comment: '激活碼'},machineCode: {type: Sequelize.STRING,comment: '機(jī)器碼'},activationVersion: {type: Sequelize.STRING,comment: '激活版本'},email: {type: Sequelize.STRING,comment: '郵箱'},createdAt: {allowNull: false,type: Sequelize.DATE},updatedAt: {allowNull: false,type: Sequelize.DATE}});},async down(queryInterface, Sequelize) {await queryInterface.dropTable('Activations');}
};
4. 運(yùn)行 Sequelize 數(shù)據(jù)庫遷移的命令
這個命令會根據(jù)migrate遷移文件 , 在mysql中生成對應(yīng)的表
"db": "npx sequelize db:migrate"
這個時(shí)候運(yùn)行eggjs項(xiàng)目 ,跑不起來可能是因?yàn)榈诙募嫒輪栴} ,可以刪除model下的文件 執(zhí)行第五步
5.?從現(xiàn)有的數(shù)據(jù)庫表結(jié)構(gòu)生成對應(yīng)的 Sequelize model模型文件
"model": "egg-sequelize-auto -o ./app/model -d sqlapplestven -h mysql.sqlpub.com -u applestven -p 3306 -x 7At1P1IH9eV",