我剛開始在我的應用程序中使用Node.js ORM Sequelize。到目前爲止,我已經在同一個文件中定義了數據庫模型,並在我的控制器文件中使用它們來完成基本操作 這裏是我定義的模型:在單獨文件中使用Sequelize創建模型並在項目中使用它們
var sqlize = require("sequelize");
var sq = new sqlize('test', 'root', 'root', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
function services(){
var ser = sq.define('services',{
idservices: {
type: sqlize.INTEGER,
autoIncrement: true,
primaryKey: true
},
title: sqlize.STRING,
des: sqlize.TEXT,
vendor: sqlize.STRING,
rating: sqlize.STRING,
pricing_hr: sqlize.STRING,
pricing_mn: sqlize.STRING,
size: sqlize.STRING,
cpu: sqlize.STRING,
ram: sqlize.STRING,
os: sqlize.STRING,
img_path: sqlize.STRING
});
sq.sync();
return ser;
}
function category(){
var category = sq.define('category',{
id: {
type: sqlize.INTEGER,
autoIncrement: true,
primaryKey: true
},
category: sqlize.STRING,
sid: sqlize.INTEGER
},{
freezeTableName: true,
timestamps: false
});
sq.sync();
return category;
}
function cat(){
var cat = sq.define('cat',{
idcat: {
type: sqlize.INTEGER,
autoIncrement: true,
primaryKey: true
},
cat: sqlize.STRING
},{
freezeTableName: true,
timestamps: false
});
sq.sync();
return cat;
}
exports.services=services;
exports.category=category;
exports.cat=cat;
但有你定義在單獨的文件模型和創建,你加載你的模型索引文件的另一種方式。我發現Sequelize的官方文檔不足以讓我忽略這個用法。我知道如何創建模型和index.js文件,但如何在我的代碼中使用這些模型很難找到。 任何人都可以請一個非常簡單的例子(沒有關係)幫助我,其中包括模型在單獨文件中的定義以及它們在其他代碼文件中的用法。