Skip to content

Commit

Permalink
resolves #2
Browse files Browse the repository at this point in the history
  • Loading branch information
jilvin committed May 29, 2018
1 parent d8d7ee4 commit f841def
Show file tree
Hide file tree
Showing 4 changed files with 757 additions and 690 deletions.
18 changes: 12 additions & 6 deletions src/bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ app.set('port', port);

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/
server.on('error', onError);
server.on('listening', onListening);
server.listen(port);
modelCallback = function() {
models.sequelize.sync().then(function() {
/**
* Listen on provided port, on all network interfaces.
*/
server.on('error', onError);
server.on('listening', onListening);
server.listen(port);
});
}

models.callback = modelCallback

/**
* Normalize a port into a number, string, or false.
Expand Down
3 changes: 2 additions & 1 deletion src/data/methods/Menu/obtainMenu/menuDataFind.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var menuDataFind = function (entitySlug, menuType) {
where: { entity_slug: entitySlug }
}
],
where: { menu_type: menuType }
where: { menu_type: menuType },
rejectOnEmpty: true
}).then(function (result) {
resolve(result)
}).catch(function (err) {
Expand Down
148 changes: 91 additions & 57 deletions src/data/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,87 @@ var env = process.env.NODE_ENV || 'development'
var config = require(path.join(__dirname, '/../../config/config.json'))[env]
var db = {}

var sequelize

// initialize for walker
var walk = require('walk')
var walker
var walkerOptions

walkerOptions = {
followLinks: false
}

var Promise = require('bluebird')

var promisesArray = []

var _ = require('lodash')

// function to create an object following the breadTrail
var addBreadTrail = function (object, breadTrail, value) {
return new Promise(function (resolve, reject) {
// create an object following the breadTrail
var breadTrailObject = {}

while (breadTrail.length !== 0) {
var breadCrumb = breadTrail.pop()
let breadCrumb = breadTrail.pop()
breadTrailObject[breadCrumb] = value
value = breadTrailObject
breadTrailObject = {}
}
breadTrailObject = value

// merging with object
_.merge(object, breadTrailObject)
resolve(object)
})
}

// function to process a directory
function processDirectory (dirname) {
function processDirectory (dirname, sequelize, modelsObject) {
return new Promise(function (resolve, reject) {
walker = walk.walk(dirname, walkerOptions)
// uses the npm package walk to recursively traverse the directory and to
// import models

// walkerOptions
var walkerOptions = {
followLinks: false
}

// initialize walker
var walker = walk.walk(dirname, walkerOptions)

// listen for file event on walker
walker.on('file', function (root, fileStats, next) {
if ((fileStats.name.indexOf('.') !== 0) && (fileStats.name !== basename) && (fileStats.name.slice(-3) === '.js')) {
// filtering of files
// 1) - hidden files
// 2) - samefile(index.js)
// 3) - all files with extension other than .js
if ((fileStats.name.indexOf('.') !== 0) &&
(fileStats.name !== basename) && (fileStats.name.slice(-3) === '.js')) {
// case of allowed file

// import model using sequelize['import']
// console.log({dirname: dirname, root: root, name: fileStats.name})
// console.log({pathJoin: path.join(root, fileStats.name)})
var model = sequelize['import'](path.join(root, fileStats.name))

// console.log(model)

var relativeSubtractPath = path.relative(__dirname, root)
// console.log(relativeSubtractPath)

var breadTrail = relativeSubtractPath.split('/')
if (breadTrail.length === 1) {
if (breadTrail[0] === '') {
breadTrail.pop()
}
}

setImmediate(function () {
// if (breadTrail.length === 1) {
// if (breadTrail[0] === '') {
// breadTrail.pop()
// }
// }

process.nextTick(function () {
breadTrail.push(model.name)

addBreadTrail(db, breadTrail, model).then(function (object) {
db = object
console.log(breadTrail)
addBreadTrail(modelsObject, breadTrail, model).then(function (object) {
modelsObject = object
next()
}).catch(function (err) {
console.log('Error in addBreadTrail' + err)
})
})
} else {
// case of rejected file
next()
}
})
Expand All @@ -78,68 +97,83 @@ function processDirectory (dirname) {
})

walker.on('end', function () {
// all elements in the current directory processed
resolve()
// all elements in the directory processed
resolve(modelsObject)
})
})
}

function associate (object) {
function associate (modelsObject, object) {
return new Promise(function (resolve, reject) {
if (object instanceof Function) {
if (object.associate) {
object.associate(db)
object.associate(modelsObject)
}
setImmediate(function () {
process.nextTick(function () {
resolve()
})
} else {
console.log('gets here first')
_.forEach(object, function (key) {
associate(key).then(function () {
associate(modelsObject, key).then(function () {
console.log('gets here too')
resolve()
})
})
}
})
}

function dbComplete () {
function finish (sequelize, modelsObject) {
return new Promise(function (resolve, reject) {
db.sequelize = sequelize
db.Sequelize = Sequelize
modelsObject.sequelize = sequelize
modelsObject.Sequelize = Sequelize

resolve()
resolve(modelsObject)
})
}

var init = function () {
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config)
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config)
}

processDirectory(__dirname).then(function () {
if (!_.isEmpty(db)) {
promisesArray.push(associate(db))

Promise.all(promisesArray).then(function () {
dbComplete().then(function () {
if (config.use_env_variable) {
db.sequelize.sync()
} else {
db.sequelize.sync()
}
})
})
// function to initialize loading of models
var init = function (modelsObject) {
return new Promise(function (resolve, reject) {
// initialize Sequelize()
var sequelize
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config)
} else {
console.log('Hello! No models found to load using sequelize. Just informing. :)')
sequelize = new Sequelize(config.database, config.username, config.password, config)
}
}).catch(function (err) {
console.log(err)

processDirectory(__dirname, sequelize, modelsObject).then(function (modelsObject) {
console.log({modelsObject: modelsObject})
if (!_.isEmpty(modelsObject)) {
associate(modelsObject, modelsObject).then(function () {
finish(sequelize, modelsObject).then(function (finalObject) {
console.log('gets here anyway')
resolve(finalObject)
})
})
} else {
console.log('Hello there! No models found to load using sequelize. Just informing. :)')
}
}).catch(function (err) {
console.log(err)
})
})
}

init()
// initialize loading of models
console.log('initializing models')
console.log({dbInital: db})
init(db).then(function (finalObject) {
console.log('gets here')
db = finalObject
console.log({dbFinal: db})
// db.sequelize.sync()
if (typeof db.callback === 'function') {
db.callback()
}
})

db.callback = null
module.exports = db
Loading

0 comments on commit f841def

Please sign in to comment.