用Sequelize继承ExpressJS

时间:2013-04-13 20:30:47

标签: inheritance express sequelize.js

我尝试将Sequelize用于我的Express网站。它有效,但在我的项目中,我有两个表,“创作”和“帖子”,我有两个路线,“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”中导入它的路由?

安东尼

2 个答案:

答案 0 :(得分:2)

你可以看看关于开始使用heroku的文章,其中我还解释了明确的应用程序用法:http://sequelizejs.com/heroku#heroku-sequelize

答案 1 :(得分:1)

尝试将模型放在单独的文件中,然后在包含所有路径的单个server.js文件的顶部进行一次。更多信息:Nodejs with Sequelizejs using separate files per model