Skip to content

Instantly share code, notes, and snippets.

@mjason
Created November 28, 2013 15:40
Show Gist options
  • Save mjason/7693863 to your computer and use it in GitHub Desktop.
Save mjason/7693863 to your computer and use it in GitHub Desktop.
sequelize for express 配置
var config = require('./config.json')
var environment = process.env.NODE_ENV || 'development'
var databaseConfig = config[environment]
var Sequelize = require('sequelize')
var sequelize = new Sequelize(databaseConfig.database,
databaseConfig.username, databaseConfig.password, {
host: databaseConfig.host,
dialect: databaseConfig.dialect,
logging: false
})
global.models = {
Sequelize: Sequelize,
sequelize: sequelize,
}
require("fs").readdirSync("./models").forEach(function(file) {
if (file != 'associations.js') {
file = file.split('.')[0]
models[file] = sequelize.import(__dirname + '/../models/' + file)
}
})
require('../models/associations')
if (environment == 'development') {
sequelize.sync().complete(function(err) {
if (err) {
throw err
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment