0

我尝试将 Sequelize 用于我的 Express 网站。它可以工作,但在我的项目中,我有两个表,“Creations”和“Posts”,我有两个路由,“admin.js”和“index.js”。

在每条路线中,我都有以下代码:

var Sequelize = require("sequelize");

var db = new Sequelize('express', 'root', 'root', {
    host: '127.0.0.1',
    port: '3306'
});

var Post = db.define('Posts', {
    slug: Sequelize.STRING,
    title: Sequelize.STRING,
    thumbnail: Sequelize.STRING,
    content: Sequelize.TEXT,
    click: Sequelize.INTEGER,
    tags: Sequelize.TEXT,
    state: Sequelize.INTEGER
});

var Creation = db.define('Creations', {
    slug: Sequelize.STRING,
    screenshot: Sequelize.STRING,
    title: Sequelize.STRING,
    subtitle: Sequelize.STRING,
    url: {type: Sequelize.STRING, validate: {isUrl: true}},
    content: Sequelize.TEXT,
    role: Sequelize.TEXT,
    webdesigner: {type: Sequelize.STRING, allowNull: true},
    developper: {type: Sequelize.STRING, allowNull: true},
    integrator: {type: Sequelize.STRING, allowNull: true},
    designer: {type: Sequelize.STRING, allowNull: true},
    state: Sequelize.INTEGER
});

Post.sync();
Creation.sync();

所以,我生成了两次表格。在“index.js”和“admin.js”中,我想知道我可以把上面的代码放在哪里,以便一次性使用脚本。我想在“index.js”和“admin.js”中获取变量“Post”和“Creation”。

也许这是最好的方法,我需要创建一个“mysql”路由并将其导入“index”和“admin”中的路由?

安东尼

4

2 回答 2

2

您可以查看有关 Heroku 入门的文章,其中我还解释了快速应用程序的用法:http ://sequelizejs.com/heroku#heroku-sequelize

于 2013-04-15T05:39:18.927 回答
1

尝试将您的模型放在单独的文件中,然后在包含所有路由的单个 server.js 文件的顶部放置一次。更多信息:Nodejs 和 Sequelizejs,每个模型使用单独的文件

于 2013-04-14T12:04:15.167 回答