Last active
January 21, 2020 15:16
-
-
Save darkcolonist/0ac3f004f0696cd436cbe8d18dd25b06 to your computer and use it in GitHub Desktop.
sequelize centralized model facade pattern
This file contains hidden or 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
{ | |
"conversations": [ | |
{ | |
"id": 1, | |
"title": "messenger dev", | |
"status": "active", | |
"created_by": 4, | |
"date_created": "2017-04-17T14:45:28.000Z", | |
"tbl_thread_users": [ | |
{ | |
"id": 2, | |
"thread_id": 1, | |
"user_id": 3 | |
}, | |
{ | |
"id": 3, | |
"thread_id": 1, | |
"user_id": 4 | |
} | |
] | |
}, | |
{ | |
"id": 2, | |
"title": "lulz room", | |
"status": "active", | |
"created_by": 4, | |
"date_created": "2017-04-18T16:10:44.000Z", | |
"tbl_thread_users": [] | |
} | |
] | |
} |
This file contains hidden or 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 Sequelize = require('sequelize'); | |
var RandomString = require('randomstring'); | |
var util = require('./util.helper'); | |
var factory = { | |
appdb: null, | |
// all models will be in here | |
models: {}, | |
init: function(databaseCfg){ | |
var appdb = new Sequelize(databaseCfg.name, databaseCfg.user, databaseCfg.password, databaseCfg); | |
// circular referencing for the lulz! | |
appdb.factory = factory; | |
factory.appdb = appdb; | |
// initialize all models | |
factory.initModels(); | |
// initialize all relationships | |
factory.initRelationships(); | |
// set models into appdb | |
appdb.models = factory.models; | |
return appdb; | |
}, | |
initModels: function(){ | |
let m = factory.models; | |
m.TblUsers = factory.appdb.define('TblUsers', { | |
// id : { type: Sequelize.INTEGER(11), primaryKey: true}, | |
hr_hash : Sequelize.STRING(32), | |
first_name : Sequelize.STRING(50), | |
last_name : Sequelize.STRING(50), | |
nickname : Sequelize.STRING(50), | |
email : Sequelize.STRING(100), | |
department_id : Sequelize.INTEGER(11), | |
position_id : Sequelize.INTEGER(11), | |
is_hr : Sequelize.BOOLEAN(), | |
is_finance : Sequelize.BOOLEAN(), | |
is_active : Sequelize.BOOLEAN(), | |
date_activated : { | |
type: Sequelize.DATE(), | |
defaultValue: util.sqldate() | |
}, | |
date_deactivated : { | |
type: Sequelize.DATE(), | |
defaultValue: util.sqldate() | |
}, | |
}, { | |
tableName: 'tbl_users', | |
timestamps: false, | |
getterMethods: { | |
fullName: function(){ | |
return this.first_name + " " + this.last_name; | |
} | |
} | |
}); | |
m.TblThreadUser = factory.appdb.define('TblThreadUser', { | |
// id : { type: Sequelize.INTEGER(11), primaryKey: true}, | |
// thread_id : Sequelize.INTEGER(11), | |
// user_id : Sequelize.INTEGER(11), | |
}, { | |
tableName: 'tbl_thread_user', | |
timestamps: false | |
}); | |
m.TblThreads = factory.appdb.define('TblThreads', { | |
// id : { type: Sequelize.INTEGER(11), primaryKey: true}, | |
title : Sequelize.STRING(100), | |
status : { | |
type: Sequelize.ENUM('active', 'archived'), | |
defaultValue: 'active' | |
}, | |
created_by : Sequelize.INTEGER(11), | |
date_created : { | |
type: Sequelize.DATE(), | |
defaultValue: util.sqldate() | |
}, | |
date_updated : { | |
type: Sequelize.DATE(), | |
defaultValue: util.sqldate() | |
}, | |
hash : { | |
type: Sequelize.STRING(32), | |
defaultValue: function() { | |
return RandomString.generate(32); | |
} | |
}, | |
version : { | |
type: Sequelize.INTEGER(11), | |
defaultValue: 1 | |
} | |
}, { | |
tableName: 'tbl_threads', | |
timestamps: false, | |
setterMethods: { | |
touch: function(){ | |
return this.update({ | |
date_updated: util.moment(), | |
version: this.version + 1 | |
}); | |
} | |
} | |
}); | |
m.TblMessages = factory.appdb.define('TblMessages', { | |
// id : { type: Sequelize.BIGINT(20), primaryKey: true}, | |
// thread_id : Sequelize.INTEGER(11), | |
// sender_id : Sequelize.INTEGER(11), | |
body : Sequelize.TEXT(), | |
date_created : { | |
type: Sequelize.DATE(), | |
defaultValue: util.sqldate() | |
}, | |
}, { | |
tableName: 'tbl_messages', | |
timestamps: false, | |
getterMethods: { | |
date_created_f: function(){ | |
return util.moment(this.date_created).format("LT"); | |
}, | |
date_created_f_full: function(){ | |
return util.moment(this.date_created).format("LLLL"); | |
}, | |
} | |
}); | |
}, | |
initRelationships: function(){ | |
let m = factory.models; | |
m.TblThreads.hasMany(m.TblThreadUser, {foreignKey: 'thread_id'}); | |
m.TblThreadUser.belongsTo(m.TblThreads, {foreignKey: 'thread_id'}); | |
m.TblUsers.hasMany(m.TblThreadUser, {foreignKey: 'user_id'}); | |
m.TblThreadUser.belongsTo(m.TblUsers, {foreignKey: 'user_id'}); | |
m.TblThreads.hasMany(m.TblMessages, {foreignKey: 'thread_id'}); | |
m.TblUsers.hasMany(m.TblMessages, {foreignKey: 'sender_id'}); | |
m.TblMessages.belongsTo(m.TblUsers, {foreignKey: 'sender_id'}); | |
} | |
}; | |
module.exports = factory; |
This file contains hidden or 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
var sql = require('./sequelize.factory.js'); | |
var appdb = sql.init(config.database); | |
function test(req, res) { | |
appdb.models.TblThreads.findAll({ | |
include: [{ | |
model: appdb.models.TblThreadUser, | |
// attributes: ['id','user_id', 'thread_id'] | |
}] | |
// raw: true | |
}).then(function(threads){ | |
res.send({ | |
"conversations" : threads | |
}); | |
}); | |
} |
This file contains hidden or 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 moment = require('moment'); | |
var moment = require('moment-timezone'); | |
var util = { | |
/** | |
* get a moment instance pre-configured | |
* @param string datetime nullable | |
* @param Object customParams | |
* { | |
* // null if you prefer application timezone | |
* timezone: "America/Los_Angeles", | |
* | |
* // null if you prefer the default format of momentJS | |
* format: "MMMM Do YYYY, h:mm:ss a", | |
* } | |
* @return {[type]} [description] | |
*/ | |
moment: function(datetime, customParams){ | |
if(datetime === null) | |
datetime = undefined; | |
var myMoment = moment(datetime); | |
if(customParams === undefined) | |
customParams = {}; | |
if(customParams.timezone !== undefined){ | |
myMoment.tz(customParams.timezone) | |
} | |
if(customParams.format !== undefined){ | |
return myMoment.format(customParams.format); | |
}else{ | |
return myMoment; | |
} | |
}, | |
sqldate: function(){ | |
var myMoment = moment().tz("GMT0"); | |
// var toreturn = myMoment.format("YYYY-MM-DD HH:mm:ss"); | |
return myMoment.format(); | |
}, | |
log: function(message){ | |
console.log(util.moment(null, | |
{ format: "MMMM Do YYYY, h:mm:ss a" }) | |
+ ": " + message); | |
}, | |
rand: function(min, max){ | |
min = Math.ceil(min); | |
max = Math.floor(max); | |
return Math.floor(Math.random() * (max - min)) + min; | |
} | |
}; | |
module.exports = util; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment