Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Sequelize = require('sequelize')
- // models is optional based on app
- const UserModel = require('./models/user')
- const BlogModel = require('./models/blog')
- const TagModel = require('./models/tag')
- const dbname = 'DB_NAME';
- const dbuser = 'DB_USER';
- const dbpass = 'DB_PASS';
- const sequelize = new Sequelize(dbname, dbuser, dbpass, {
- host: 'localhost',
- dialect: 'mysql',
- pool: {
- max: 10,
- min: 0,
- acquire: 30000, // in ms
- idle: 10000 // in ms
- }
- })
- /* -- MODEL DEFINITION --
- *
- */
- const User = UserModel(sequelize, Sequelize)
- // BlogTag will be our way of tracking relationship between Blog and Tag models
- // each Blog can have multiple tags and each Tag can have multiple blogs
- const BlogTag = sequelize.define('blog_tag', {})
- const Blog = BlogModel(sequelize, Sequelize)
- const Tag = TagModel(sequelize, Sequelize)
- /* -- MODEL RELATIONSHIP -- */
- Blog.belongsToMany(Tag, { through: BlogTag, unique: false })
- Tag.belongsToMany(Blog, { through: BlogTag, unique: false })
- Blog.belongsTo(User);
- /* SYNC DATABASE ORM */
- sequelize.sync({ force: true })
- .then(() => {
- console.log(`Database & tables created!`)
- })
- module.exports = {
- User,
- Blog,
- Tag
- }
Add Comment
Please, Sign In to add comment