Skip to content

Commit

Permalink
feat(seeds): initiate support for seeds and factories
Browse files Browse the repository at this point in the history
implment seeds and factories api
  • Loading branch information
thetutlage committed Mar 11, 2016
1 parent afde4d4 commit 22e8eb2
Show file tree
Hide file tree
Showing 14 changed files with 459 additions and 4 deletions.
24 changes: 24 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const i = require('inflect')
const _ = require('lodash')
const autoLoader = require('auto-loader')
const prettyHrtime = require('pretty-hrtime')
const util = exports = module.exports = {}
const isolatedLodash = _.runInContext()
Expand Down Expand Up @@ -269,3 +270,26 @@ util.timeDiff = function (start) {
let end = process.hrtime(start)
return prettyHrtime(end)
}

/**
* loads all .js files from a given directory and
* then back as object.
*
* @method loadJsFiles
*
* @param {String} fromPath
* @return {Object}
*
* @public
*/
util.loadJsFiles = function (fromPath) {
return _(autoLoader.load(fromPath))
.map(function (file, name) {
if (name.endsWith('.js')) {
return [name.replace('.js', ''), file]
}
})
.compact()
.fromPairs()
.value()
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
}
},
"dependencies": {
"auto-loader": "git+https://github.com/thetutlage/node-auto-loader.git",
"cat-log": "^1.0.0",
"co": "^4.6.0",
"co-functional": "^0.2.1",
Expand Down
113 changes: 113 additions & 0 deletions src/Factory/ModelFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
'use strict'

/**
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const cf = require('co-functional')
const _ = require('lodash')
const Ioc = require('adonis-fold').Ioc
const faker = require('faker')

class ModelFactory {

constructor (binding, callback) {
this.binding = Ioc.use(binding)
this.callback = callback
this.instances = []
}

/**
* makes instance of a given model
*
* @param {Object} values
* @return {Object}
*
* @private
*/
_makeInstance (values) {
const Model = this.binding
return new Model(values)
}

/**
* calls blueprint and passed faker library
* to it.
*
* @return {Object}
*
* @private
*/
_callBlueprint () {
return this.callback(faker)
}

/**
* returns a model instace by calling the blueprint
* and setting values on model instance
*
* @return {Object}
*
* @public
*/
make () {
return this._makeInstance(this._callBlueprint())
}

/**
* creates rows inside the database by calling create
* method on the given model
*
* @method create
*
* @param {Number} rows
* @return {Object} reference to this
*
* @public
*/
* create (rows) {
const self = this
const range = _.range(rows)
this.instances = yield cf.mapSerial(function * () {
return yield self.binding.create(self._callBlueprint())
}, range)
return this
}

/**
* loops through all the created instances and
* executes a callback with support for
* calling generators
*
* @method each
*
* @param {Function} callback
*
* @public
*/
each (callback) {
return cf.forEach(function * (instance) {
yield callback(instance)
}, this.instances)
}

/**
* will reset the given model by calling
* truncate method on it.
*
* @return {Number}
*
* @public
*/
reset () {
return this.binding.query().truncate()
}

}

module.exports = ModelFactory
73 changes: 73 additions & 0 deletions src/Factory/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'

/**
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const Factory = exports = module.exports = {}
const ModelFactory = require('./ModelFactory')
const NE = require('node-exceptions')
let blueprints = {}

/**
* defines a new factory blueprint mapped on a given
* key. Later callback is called and passed the
* faker object.
*
* @method define
*
* @param {String} key
* @param {Function} callback
*
* @public
*/
Factory.blueprint = function (key, callback) {
if (typeof (callback) !== 'function') {
throw new NE.InvalidArgumentException('callback should be a function while define a factory blueprint')
}
blueprints[key] = callback
}

/**
* returns all registered blueprints inside a factory
* @method blueprints
*
* @return {Object}
*
* @public
*/
Factory.blueprints = function () {
return blueprints
}

/**
* clears all registered blueprints
*
* @method clear
*
* @public
*/
Factory.clear = function () {
blueprints = {}
}

/**
* returns instance of model factory and pass it
* the blueprint defination.
*
* @method model
*
* @param {String} key
* @return {Object}
*
* @public
*/
Factory.model = function (key) {
const callback = blueprints[key]
return new ModelFactory(key, callback)
}
11 changes: 9 additions & 2 deletions src/Schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@
* file that was distributed with this source code.
*/

const proxy = require('./proxy')
const proxyHandler = require('./proxyHandler')
require('harmony-reflect')

class Schema {
constructor () {
this.store = {}
return new Proxy(this, proxy)
return new Proxy(this, proxyHandler)
}

/**
* connection to be used while creating schema
*
* @return {String}
*
* @public
*/
static get connection () {
return 'default'
}
Expand Down
4 changes: 2 additions & 2 deletions src/Schema/proxy.js → src/Schema/proxyHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const aliases = {
dropIfExists: 'dropTableIfExists'
}

let proxy = exports = module.exports = {}
let proxyHandler = exports = module.exports = {}

/**
* proxies target get calls and returns custom
Expand All @@ -42,7 +42,7 @@ let proxy = exports = module.exports = {}
*
* @public
*/
proxy.get = function (target, name) {
proxyHandler.get = function (target, name) {
if (target[name] !== undefined || mustImplement.indexOf(name) > -1) {
return target[name]
}
Expand Down
31 changes: 31 additions & 0 deletions src/Seeder/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

/**
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const cf = require('co-functional')
const Ioc = require('adonis-fold').Ioc
const Seeder = exports = module.exports = {}

/**
* executes given seeds in a sequence by calling
* run method on them.
*
* @method exec
*
* @param {Array} seeds
*
* @public
*/
Seeder.exec = function (seeds) {
cf.forEach(function * (Seed) {
const seedInstance = typeof (Seed) === 'string' ? Ioc.make(Seed) : new Seed()
yield seedInstance.run()
}, seeds)
}
8 changes: 8 additions & 0 deletions test/unit/app/Model/Hooks/Users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

const UsersHooks = exports = module.exports = {}

UsersHooks.validate = function * (next) {
this.username = 'viahook'
yield next
}
Empty file added test/unit/autoload/foo.js
Empty file.
Empty file added test/unit/autoload/paths.js
Empty file.
Empty file added test/unit/autoload/readme.md
Empty file.

0 comments on commit 22e8eb2

Please sign in to comment.