Skip to content

Commit

Permalink
feat(factory): add database factory support
Browse files Browse the repository at this point in the history
database factory will help in seeding database without lucid model
  • Loading branch information
thetutlage committed Mar 20, 2016
1 parent 10f07f0 commit dca2fcd
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/Factory/DatabaseFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
'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 DatabaseFactory {

constructor (binding, callback) {
this.dbTable = binding
this.callback = callback
this.binding = Ioc.use('Adonis/Src/Database')
this.returningField = 'id'
}

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

/**
* sets table name to be used by the query
* builder
*
* @param {String} tableName
* @return {Object} reference to this
*
* @public
*/
table (tableName) {
this.dbTable = tableName
return this
}

/**
* defines the returning field to be used
* when doing insert statement
*
* @param {String} field
* @return {Object} reference to this
*
* @public
*/
returning (field) {
this.returningField = field
return this
}

/**
* creates rows inside the database by calling insert
* method on database query builder
*
* @method create
*
* @param {Number} rows
* @return {Arrays} Array of inserted ids
*
* @public
*/
* create (rows) {
const self = this
this.binding = this.binding.table(this.dbTable)
rows = rows || 1
const range = _.range(rows)
const ids = yield cf.mapSerial(function * () {
return yield self.binding.insert(self._callBlueprint()).returning(self.returningField)
}, range)
return _.flatten(ids)
}

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

module.exports = DatabaseFactory
1 change: 1 addition & 0 deletions src/Factory/ModelFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class ModelFactory {
* @public
*/
* create (rows) {
rows = rows || 1
const self = this
const range = _.range(rows)
this.instances = yield cf.mapSerial(function * () {
Expand Down
17 changes: 17 additions & 0 deletions src/Factory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

Expand Down Expand Up @@ -71,3 +72,19 @@ Factory.model = function (key) {
const callback = blueprints[key]
return new ModelFactory(key, callback)
}

/**
* returns instance of database factory and pass it
* the blueprint defination.
*
* @method get
*
* @param {String} key
* @return {Object}
*
* @public
*/
Factory.get = function (key) {
const callback = blueprints[key]
return new DatabaseFactory(key, callback)
}
39 changes: 39 additions & 0 deletions test/unit/factory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ describe('Factory', function () {
Database._setConfigProvider(config)
yield filesFixtures.createDir()
yield modelFixtures.up(Database)
Ioc.bind('Adonis/Src/Database', function () {
return Database
})
Factory.clear()
})

Expand Down Expand Up @@ -178,4 +181,40 @@ describe('Factory', function () {
const afterReset = yield User.all()
expect(afterReset.size()).to.equal(0)
})

it('should be able to create records using the database factory', function * () {
Factory.blueprint('users', function (faker) {
return {
username: faker.internet.userName(),
firstname: faker.name.firstName()
}
})
const ids = yield Factory.get('users').create(10)
expect(ids).to.be.an('array')
expect(ids.length).to.equal(10)
})

it('should be able to define different table name when using database factory', function * () {
Factory.blueprint('forUsers', function (faker) {
return {
username: faker.internet.userName(),
firstname: faker.name.firstName()
}
})
const ids = yield Factory.get('forUsers').table('users').create(10)
expect(ids).to.be.an('array')
expect(ids.length).to.equal(10)
})

it('should be able to define different returning field when using database factory', function * () {
Factory.blueprint('forUsers', function (faker) {
return {
username: faker.internet.userName(),
firstname: faker.name.firstName()
}
})
const dbFactory = Factory.get('forUsers').table('users').returning('username')
yield dbFactory.create(10)
expect(dbFactory.returningField).to.equal('username')
})
})

0 comments on commit dca2fcd

Please sign in to comment.