tonymx227
tonymx227

Reputation: 5451

Inheritance in ExpressJS with Sequelize

I try to use Sequelize for my Express website. And it works, but on my project I have two tables, "Creations" and "Posts" and I have two routes, "admin.js" and "index.js".

In each route I have this code below:

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();

So, I generate the tables two times. In "index.js" and "admin.js" and I would like to know where can I put the code above for using the script one time. And I would like to get the variable "Post" and "Creation" in "index.js" and "admin.js".

Maybe that's the best way, I need to create a "mysql" route and import it route in "index" and "admin"?

Anthony

Upvotes: 0

Views: 1302

Answers (2)

sdepold
sdepold

Reputation: 6231

you could take a look at the article about getting started on heroku, in which I've also explained express application usage: http://sequelizejs.com/heroku#heroku-sequelize

Upvotes: 2

Dan Kohn
Dan Kohn

Reputation: 34327

Try putting your models in separate files and requiring then once at the top of a single server.js file that include all of your routes. More info here: Nodejs with Sequelizejs using separate files per model

Upvotes: 1

Related Questions