Code coverage report for upstream/lib/dao-factory-manager.js

Statements: 96.88% (31 / 32)      Branches: 80% (8 / 10)      Functions: 90.91% (10 / 11)      Lines: 96.88% (31 / 32)     

All files » upstream/lib/ » dao-factory-manager.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 691     1 1 7 7     1 435   435     1 14 29       1 14 14   14 29     14     1                 1 59     59 155   155   155 774 774 4         155     59 155       1    
var Toposort   = require('toposort-class')
  , DaoFactory = require('./dao-factory')
 
module.exports = (function() {
  var DAOFactoryManager = function(sequelize) {
    this.daos = []
    this.sequelize = sequelize
  }
 
  DAOFactoryManager.prototype.addDAO = function(dao) {
    this.daos.push(dao)
 
    return dao
  }
 
  DAOFactoryManager.prototype.removeDAO = function(dao) {
    this.daos = this.daos.filter(function(_dao) {
      return _dao.name != dao.name
    })
  }
 
  DAOFactoryManager.prototype.getDAO = function(daoName, options) {
    options = options || {}
    options.attribute = options.attribute || 'name'
 
    var dao = this.daos.filter(function(dao) {
      return dao[options.attribute] === daoName
    })
 
    return !!dao ? dao[0] : null
  }
 
  DAOFactoryManager.prototype.__defineGetter__('all', 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) {
      var deps = []
 
      daos[dao.tableName] = dao
 
      for (var attrName in dao.rawAttributes) {
        Eif (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
})()