我尝试将 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”中的路由?
安东尼