【发布时间】:2015-12-15 05:39:07
【问题描述】:
我刚刚开始在我的应用程序中使用 Node.js ORM 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 文件,但很难找到如何在我的代码中使用这些模型。 谁能帮我举一个非常简单的例子(没有关系),包括在单独文件中定义模型及其在其他代码文件中的用法。
【问题讨论】:
标签: javascript node.js orm sequelize.js