bookshelf-modelbase
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -1,8 +0,7 @@ | ||
var _ = require('lodash'); | ||
_.str = require('underscore.string'); | ||
var Joi = require('joi'); | ||
var extend = require('xtend') | ||
var Joi = require('joi') | ||
module.exports = function modelBase(bookshelf, params) { | ||
module.exports = function modelBase (bookshelf, params) { | ||
if (!bookshelf) { | ||
throw new Error('Must pass an initialized bookshelf instance'); | ||
throw new Error('Must pass an initialized bookshelf instance') | ||
} | ||
@@ -20,6 +19,6 @@ | ||
} else { | ||
this.validate = Joi.any(); | ||
this.validate = Joi.any() | ||
} | ||
this.on('saving', this.validateSave); | ||
this.on('saving', this.validateSave) | ||
}, | ||
@@ -30,7 +29,7 @@ | ||
validateSave: function () { | ||
var validation = Joi.validate(this.attributes, this.validate); | ||
var validation = Joi.validate(this.attributes, this.validate) | ||
if (validation.error) { | ||
throw new Error(validation.error); | ||
throw new Error(validation.error) | ||
} else { | ||
return validation.value; | ||
return validation.value | ||
} | ||
@@ -50,3 +49,3 @@ } | ||
findAll: function (filter, options) { | ||
return this.forge(filter).fetchAll(options); | ||
return this.forge(filter).fetchAll(options) | ||
}, | ||
@@ -61,3 +60,4 @@ | ||
findOne: function (data, options) { | ||
return this.forge(data).fetch(options); | ||
options = extend({ require: true }, options || {}) | ||
return this.forge(data).fetch(options) | ||
}, | ||
@@ -73,3 +73,3 @@ | ||
return this.forge(data) | ||
.save(null, options); | ||
.save(null, options) | ||
}, | ||
@@ -84,9 +84,7 @@ | ||
update: function (data, options) { | ||
_.defaults(options, { | ||
patch: true | ||
}); | ||
options = extend({ patch: true, require: true }, options || {}) | ||
return this.forge({ id: options.id }).fetch(options) | ||
.then(function (model) { | ||
if (model) { | ||
return model.save(data, options); | ||
return model.save(data, options) | ||
} | ||
@@ -103,3 +101,3 @@ }) | ||
return this.forge({ id: options.id }) | ||
.destroy(options); | ||
.destroy(options) | ||
}, | ||
@@ -114,13 +112,13 @@ | ||
findOrCreate: function (data, options) { | ||
var self = this; | ||
return self.findOne(data, options) | ||
options = extend({ require: false }, options || {}) | ||
return this.findOne(data, options) | ||
.bind(this) | ||
.then(function (model) { | ||
return model ? model : self.create(data, options); | ||
return model ? model : this.create(data, options) | ||
}) | ||
} | ||
}); | ||
}) | ||
return model; | ||
}; | ||
return model | ||
} |
{ | ||
"name": "bookshelf-modelbase", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Extensible ModelBase for bookshelf-based model layers", | ||
"main": "./lib", | ||
"scripts": { | ||
"test": "istanbul cover _mocha" | ||
"test": "standard && istanbul cover _mocha" | ||
}, | ||
@@ -23,4 +23,3 @@ "repository": { | ||
"joi": "^5.0.2", | ||
"lodash": "^2.4.1", | ||
"underscore.string": "^2.4.0" | ||
"xtend": "^4.0.0" | ||
}, | ||
@@ -33,4 +32,5 @@ "devDependencies": { | ||
"sinon": "^1.12.1", | ||
"sqlite3": "^3.0.4" | ||
"sqlite3": "^3.0.4", | ||
"standard": "^5.2.1" | ||
} | ||
} |
@@ -39,3 +39,3 @@ # bookshelf-modelbase | ||
tableName: 'users' | ||
}, { | ||
// validation is passed to Joi.object(), so use a raw object | ||
@@ -71,12 +71,31 @@ validate: { | ||
```js | ||
/** | ||
* Naive add - create and save a model based on data | ||
* @param {Object} data | ||
* @param {Object} options (optional) | ||
* @return {Promise(bookshelf.Model)} single Model | ||
*/ | ||
create: function (data, options) { | ||
return this.forge(data) | ||
.save(null, options); | ||
} | ||
``` | ||
#### model.destroy | ||
```js | ||
/** | ||
* Naive destroy | ||
* @param {Object} options | ||
* @return {Promise(bookshelf.Model)} empty Model | ||
*/ | ||
destroy: function (options) { | ||
return this.forge({ id: options.id }) | ||
.destroy(options); | ||
} | ||
``` | ||
#### model.findAll | ||
#### model.findOne | ||
#### model.findOrCreate | ||
#### model.update | ||
```javascript | ||
@@ -90,4 +109,8 @@ /** | ||
return bookshelf.Collection.forge([], { model: this }).fetch(options); | ||
}, | ||
} | ||
``` | ||
#### model.findOne | ||
```js | ||
/** | ||
@@ -101,15 +124,26 @@ * Naive findOne - fetch data for `this` matching data | ||
return this.forge(data).fetch(options); | ||
}, | ||
} | ||
``` | ||
#### model.findOrCreate | ||
```js | ||
/** | ||
* Naive add - create and save a model based on data | ||
* Find or create - try and find the model, create one if not found | ||
* @param {Object} data | ||
* @param {Object} options (optional) | ||
* @param {Object} options | ||
* @return {Promise(bookshelf.Model)} single Model | ||
*/ | ||
create: function (data, options) { | ||
return this.forge(data) | ||
.save(null, options); | ||
}, | ||
findOrCreate: function (data, options) { | ||
var self = this; | ||
return self.findOne(data, options) | ||
.then(function (model) { | ||
return model ? model : self.create(data, options); | ||
}) | ||
} | ||
``` | ||
#### model.update | ||
```js | ||
/** | ||
@@ -131,13 +165,3 @@ * Naive update - update a model based on data | ||
}) | ||
}, | ||
/** | ||
* Naive destroy | ||
* @param {Object} options | ||
* @return {Promise(bookshelf.Model)} empty Model | ||
*/ | ||
destroy: function (options) { | ||
return this.forge({ id: options.id }) | ||
.destroy(options); | ||
} | ||
``` |
@@ -1,4 +0,4 @@ | ||
var knexFile = require(__dirname + '/knexfile'); | ||
var knex = require('knex')(knexFile['development']); | ||
var knexFile = require(__dirname + '/knexfile') | ||
var knex = require('knex')(knexFile['development']) | ||
module.exports = knex; | ||
module.exports = knex |
@@ -1,21 +0,20 @@ | ||
var Joi = require('joi'); | ||
var sinon = require('sinon'); | ||
var mocha = require('mocha'); | ||
var chai = require('chai'); | ||
var expect = chai.expect; | ||
var sinon = require('sinon'); | ||
var db = require('./db'); | ||
var bookshelf = require('bookshelf')(db); | ||
var ModelBase = require('../lib/index')(bookshelf); | ||
/* global describe, before, beforeEach, it */ | ||
var Joi = require('joi') | ||
var chai = require('chai') | ||
var expect = chai.expect | ||
var db = require('./db') | ||
var bookshelf = require('bookshelf')(db) | ||
var ModelBase = require('../lib/index')(bookshelf) | ||
describe('modelBase', function () { | ||
var specimen; | ||
var specimenClass; | ||
var specimen | ||
var SpecimenClass | ||
before(function () { | ||
return db.migrate.latest(); | ||
}); | ||
return db.migrate.latest() | ||
}) | ||
beforeEach(function () { | ||
specimenClass = ModelBase.extend({ | ||
SpecimenClass = ModelBase.extend({ | ||
tableName: 'test_table', | ||
@@ -25,10 +24,10 @@ validate: { | ||
} | ||
}); | ||
}) | ||
specimen = new specimenClass({ | ||
specimen = new SpecimenClass({ | ||
name: 'hello' | ||
}); | ||
}) | ||
return specimen.save(); | ||
}); | ||
return specimen.save() | ||
}) | ||
@@ -38,11 +37,11 @@ describe('initialize', function () { | ||
expect(function () { | ||
require('../lib/index')(); | ||
}).to.throw(/Must pass an initialized bookshelf instance/); | ||
}); | ||
require('../lib/index')() | ||
}).to.throw(/Must pass an initialized bookshelf instance/) | ||
}) | ||
it('should default to any validation', function () { | ||
specimen = new ModelBase(); | ||
expect(specimen.validate.isJoi).to.eql(true); | ||
expect(specimen.validate._type).to.eql('any'); | ||
}); | ||
specimen = new ModelBase() | ||
expect(specimen.validate.isJoi).to.eql(true) | ||
expect(specimen.validate._type).to.eql('any') | ||
}) | ||
}) | ||
@@ -54,12 +53,12 @@ | ||
name: 'hello' | ||
}); | ||
}); | ||
}) | ||
}) | ||
it('should error on invalid attributes', function () { | ||
specimen.set('name', 1); | ||
specimen.set('name', 1) | ||
expect(function () { | ||
specimen.validateSave(); | ||
}).to.throw(/ValidationError/); | ||
}); | ||
}); | ||
specimen.validateSave() | ||
}).to.throw(/ValidationError/) | ||
}) | ||
}) | ||
@@ -69,38 +68,38 @@ describe('constructor', function () { | ||
return expect(ModelBase.extend({ tableName: 'test' })) | ||
.to.itself.respondTo('extend'); | ||
}); | ||
}); | ||
.to.itself.respondTo('extend') | ||
}) | ||
}) | ||
describe('findAll', function () { | ||
it('should return a collection', function () { | ||
return specimenClass.findAll() | ||
return SpecimenClass.findAll() | ||
.then(function (collection) { | ||
return expect(collection).to.be.instanceof(bookshelf.Collection); | ||
return expect(collection).to.be.instanceof(bookshelf.Collection) | ||
}) | ||
}); | ||
}); | ||
}) | ||
}) | ||
describe('findOne', function () { | ||
it('should return a model', function () { | ||
return specimenClass.findOne() | ||
return SpecimenClass.findOne() | ||
.then(function (model) { | ||
expect(model).to.be.instanceof(specimenClass); | ||
}); | ||
}); | ||
}); | ||
expect(model).to.be.instanceof(SpecimenClass) | ||
}) | ||
}) | ||
}) | ||
describe('create', function () { | ||
it('should return a model', function () { | ||
return specimenClass.create({ | ||
return SpecimenClass.create({ | ||
name: 'hello' | ||
}) | ||
.then(function (model) { | ||
return expect(model.id).to.not.eql(specimen.id); | ||
}); | ||
}); | ||
}); | ||
return expect(model.id).to.not.eql(specimen.id) | ||
}) | ||
}) | ||
}) | ||
describe('update', function () { | ||
it('should return a model', function () { | ||
return specimenClass.forge({ | ||
return SpecimenClass.forge({ | ||
name: 'goodbye' | ||
@@ -111,51 +110,51 @@ }, { id: specimen.id }) | ||
.then(function (model) { | ||
this.modelId = model.id; | ||
return specimenClass.update({ | ||
this.modelId = model.id | ||
return SpecimenClass.update({ | ||
name: 'hello' | ||
}, { id: this.modelId }); | ||
}, { id: this.modelId }) | ||
}) | ||
.then(function () { | ||
return specimenClass.findOne({ id: this.modelId }); | ||
return SpecimenClass.findOne({ id: this.modelId }) | ||
}) | ||
.then(function (model) { | ||
return expect(model.get('name')).to.eql('hello'); | ||
}); | ||
}); | ||
}); | ||
return expect(model.get('name')).to.eql('hello') | ||
}) | ||
}) | ||
}) | ||
describe('destroy', function () { | ||
it('should destroy the model', function () { | ||
return specimenClass.forge({ name: 'hello' }) | ||
return SpecimenClass.forge({ name: 'hello' }) | ||
.bind({}) | ||
.save() | ||
.then(function (model) { | ||
this.modelId = model.id; | ||
return specimenClass.destroy({ id: this.modelId }) | ||
this.modelId = model.id | ||
return SpecimenClass.destroy({ id: this.modelId }) | ||
}) | ||
.then(function (model) { | ||
return specimenClass.findOne({ id: this.modelId }); | ||
return SpecimenClass.findOne({ id: this.modelId }) | ||
}) | ||
.then(function (model) { | ||
return expect(model).to.eql(null); | ||
}); | ||
}); | ||
}); | ||
.catch(function (err) { | ||
expect(err.message).to.eql('EmptyResponse') | ||
}) | ||
}) | ||
}) | ||
describe('findOrCreate', function () { | ||
it('should find an existing model', function () { | ||
return specimenClass.findOrCreate() | ||
return SpecimenClass.findOrCreate() | ||
.then(function (model) { | ||
expect(model).to.be.instanceof(specimenClass); | ||
}); | ||
}); | ||
expect(model).to.be.instanceof(SpecimenClass) | ||
}) | ||
}) | ||
it('should create when model not found', function () { | ||
return specimenClass.findOrCreate({ | ||
return SpecimenClass.findOrCreate({ | ||
name: 'goodbye' | ||
}) | ||
.then(function (model) { | ||
return expect(model.id).to.not.eql(specimen.id); | ||
}); | ||
}); | ||
}); | ||
}); | ||
return expect(model.id).to.not.eql(specimen.id) | ||
}) | ||
}) | ||
}) | ||
}) |
// Update with your config settings. | ||
module.exports = { | ||
development: { | ||
@@ -14,3 +13,2 @@ client: 'sqlite3', | ||
} | ||
}; | ||
} |
@@ -1,13 +0,13 @@ | ||
'use strict'; | ||
'use strict' | ||
exports.up = function(knex, Promise) { | ||
exports.up = function (knex, Promise) { | ||
return knex.schema.createTable('test_table', function (table) { | ||
table.increments('id'); | ||
table.string('name'); | ||
table.timestamps(); | ||
}); | ||
}; | ||
table.increments('id') | ||
table.string('name') | ||
table.timestamps() | ||
}) | ||
} | ||
exports.down = function(knex, Promise) { | ||
return knex.schema.dropTable('test_table'); | ||
}; | ||
exports.down = function (knex, Promise) { | ||
return knex.schema.dropTable('test_table') | ||
} |
@@ -1,5 +0,5 @@ | ||
var Promise = require('bluebird'); | ||
var Promise = require('bluebird') | ||
Promise.onPossiblyUnhandledRejection(function (err) { | ||
throw err; | ||
}); | ||
throw err | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15567
4
163
7
268
+ Addedxtend@^4.0.0
+ Addedxtend@4.0.2(transitive)
- Removedlodash@^2.4.1
- Removedunderscore.string@^2.4.0
- Removedunderscore.string@2.4.0(transitive)