Last active
August 29, 2015 14:26
-
-
Save totuworld/07394fdfef998fb0e36c to your computer and use it in GitHub Desktop.
express + sequelize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var fs = require("fs"); | |
var path = require("path"); | |
var Sequelize = require("sequelize"); | |
var env = process.env.NODE_ENV || "development"; | |
var config = require(__dirname + '/../config/config.json')[env]; | |
var sequelize = new Sequelize(config.database, config.username, config.password, config); | |
var db = {}; | |
fs | |
.readdirSync(__dirname) | |
.filter(function(file) { | |
return (file.indexOf(".") !== 0) && (file !== "index.js"); | |
}) | |
.forEach(function(file) { | |
var model = sequelize.import(path.join(__dirname, file)); | |
db[model.name] = model; | |
}); | |
Object.keys(db).forEach(function(modelName) { | |
if ("associate" in db[modelName]) { | |
db[modelName].associate(db); | |
} | |
}); | |
db.sequelize = sequelize; | |
db.Sequelize = Sequelize; | |
module.exports = db; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(sequelize, DataTypes) { | |
var usercore = sequelize.define('usercore', { | |
no : { type : DataTypes.INTEGER.UNSIGNED, primaryKey: true, autoIncrement: true} | |
, id : { type : DataTypes.STRING(14) } | |
, gems : { type : DataTypes.INTEGER(5).UNSIGNED, defaultValue: 0} | |
, coins : { type : DataTypes.INTEGER.UNSIGNED, defaultValue: 0} | |
, hearts : { type : DataTypes.INTEGER(3).UNSIGNED, defaultValue: 0} | |
, highScore : { type : DataTypes.INTEGER.UNSIGNED, defaultValue: 0} | |
, loginTime : { type : DataTypes.DATE, defaultValue: '2002-06-05 00:00:00' | |
, get:function(){var convertTime=new Date(this.getDataValue('loginTime')); return convertTime.getTime()/1000;}} | |
}, { | |
timestamps: false, | |
tableName: 'usercore' | |
}); | |
return usercore; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* Module dependencies. | |
*/ | |
//---(중략)--- | |
var models = require("../models"); //추가한 부분. | |
//---(중략)--- | |
/** | |
* Listen on provided port, on all network interfaces. | |
*/ | |
// server.listen(port); //주석처리 | |
server.on('error', onError); | |
server.on('listening', onListening); | |
//추가한 부분. | |
//sequelize의 싱크 작업을 시작하고 완료되면 설정된 포트를 통해서 통신 가능하도록 한다. | |
models.sequelize.sync().then(function () { | |
server.listen(app.get('port'), function() { | |
debug('Express server listening on port ' + server.address().port); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment