-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(seeds): initiate support for seeds and factories
implment seeds and factories api
- Loading branch information
1 parent
afde4d4
commit 22e8eb2
Showing
14 changed files
with
459 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
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 |
This file contains 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
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) | ||
} |
This file contains 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
This file contains 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
This file contains 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
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) | ||
} |
This file contains 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
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.
Empty file.
Empty file.
Oops, something went wrong.