不要怂,就是干,撸起袖子干!

Commit 556a6639 by Martin Aspeli

Topologically sorted iterator for DAOs taking dependencies into account

1 parent 10ec7ff6
Showing with 33 additions and 1 deletions
var Toposort = require('toposort-class')
module.exports = (function() {
var DAOFactoryManager = function(sequelize) {
this.daos = []
......@@ -31,5 +33,34 @@ module.exports = (function() {
return this.daos
})
/**
* Iterate over DAOs in an order suitable for e.g. creating tables. Will
* take foreign key constraints into account so that dependencies are visited
* before dependents.
*/
DAOFactoryManager.prototype.forEachDAO = function(iterator) {
var daos = {}
, sorter = new Toposort()
this.daos.forEach(function(dao) {
daos[dao.tableName] = dao
var deps = []
for(var attrName in dao.rawAttributes) {
if(dao.rawAttributes.hasOwnProperty(attrName)) {
if(dao.rawAttributes[attrName].references) {
deps.push(dao.rawAttributes[attrName].references)
}
}
}
sorter.add(dao.tableName, deps)
})
sorter.sort().reverse().forEach(function(name) {
iterator(daos[name])
})
}
return DAOFactoryManager
})()
......@@ -36,7 +36,8 @@
"moment": "~1.7.0",
"commander": "~0.6.0",
"generic-pool": "1.0.9",
"dottie": "0.0.6-1"
"dottie": "0.0.6-1",
"toposort-class": "0.1.4"
},
"devDependencies": {
"jasmine-node": "1.5.0",
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!