bookshelf-modelbase
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -51,2 +51,64 @@ var _ = require('lodash'); | ||
}, | ||
}, { | ||
/* Model CRUD */ | ||
/** | ||
* Naive findAll - fetches all data for `this` | ||
* @param {Object} options (optional) | ||
* @return {Promise(bookshelf.Collection)} Bookshelf Collection of all Models | ||
*/ | ||
findAll: function (options) { | ||
return bookshelf.Collection.forge([], { model: this }).fetch(options); | ||
}, | ||
/** | ||
* Naive findOne - fetch data for `this` matching data | ||
* @param {Object} data | ||
* @param {Object} options (optional) | ||
* @return {Promise(bookshelf.Model)} single Model | ||
*/ | ||
findOne: function (data, options) { | ||
return this.forge(data).fetch(options); | ||
}, | ||
/** | ||
* 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); | ||
}, | ||
/** | ||
* Naive update - update a model based on data | ||
* @param {Object} data | ||
* @param {Object} options | ||
* @return {Promise(bookshelf.Model)} edited Model | ||
*/ | ||
update: function (data, options) { | ||
_.defaults(options, { | ||
patch: true | ||
}); | ||
return this.forge({ id: options.id }).fetch(options) | ||
.then(function (model) { | ||
if (model) { | ||
return model.save(data, options); | ||
} | ||
}) | ||
}, | ||
/** | ||
* Naive destroy | ||
* @param {Object} options | ||
* @return {Promise(bookshelf.Model)} empty Model | ||
*/ | ||
destroy: function (options) { | ||
return this.forge({ id: options.id }) | ||
.destroy(options); | ||
} | ||
}); | ||
@@ -53,0 +115,0 @@ |
{ | ||
"name": "bookshelf-modelbase", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Extensible ModelBase for bookshelf-based model layers", | ||
@@ -5,0 +5,0 @@ "main": "./lib", |
@@ -31,2 +31,3 @@ ModelBase [![Build Status](https://travis-ci.org/bsiddiqui/bookshelf-modelbase.svg?branch=master)](https://travis-ci.org/bsiddiqui/bookshelf-modelbase) [![Code Climate](https://codeclimate.com/github/bsiddiqui/bookshelf-modelbase/badges/gpa.svg)](https://codeclimate.com/github/bsiddiqui/bookshelf-modelbase) [![Test Coverage](https://codeclimate.com/github/bsiddiqui/bookshelf-modelbase/badges/coverage.svg)](https://codeclimate.com/github/bsiddiqui/bookshelf-modelbase) [![npm version](https://badge.fury.io/js/bookshelf-modelbase.svg)](http://badge.fury.io/js/bookshelf-modelbase) | ||
* Naive CRUD methods - `findAll`, `findOne`, `create`, `update`, and `destroy` | ||
@@ -33,0 +34,0 @@ ##Usage |
@@ -1,4 +0,4 @@ | ||
var knexFile = require('./knexfile'); | ||
var knexFile = require(__dirname + '/knexfile'); | ||
var knex = require('knex')(knexFile['development']); | ||
module.exports = knex; |
@@ -6,2 +6,3 @@ var Joi = require('joi'); | ||
var expect = chai.expect; | ||
var sinon = require('sinon'); | ||
var db = require('./db'); | ||
@@ -13,6 +14,12 @@ var bookshelf = require('bookshelf')(db); | ||
var specimen; | ||
var specimenClass; | ||
before(function () { | ||
return db.migrate.latest(); | ||
}); | ||
beforeEach(function () { | ||
specimenClass = ModelBase.extend({ | ||
validate: { name: Joi.string().valid('hello') } | ||
tableName: 'test_table', | ||
validate: { name: Joi.string().valid('hello', 'goodbye') } | ||
}); | ||
@@ -74,2 +81,71 @@ | ||
}); | ||
describe('findAll', function () { | ||
it('should return a collection', function () { | ||
return specimenClass.findAll() | ||
.then(function (collection) { | ||
return expect(collection).to.be.instanceof(bookshelf.Collection); | ||
}) | ||
}); | ||
}); | ||
describe('findOne', function () { | ||
it('should return a model', function () { | ||
return specimenClass.findOne() | ||
.then(function (model) { | ||
expect(model).to.be.instanceof(specimenClass); | ||
}); | ||
}); | ||
}); | ||
describe('create', function () { | ||
it('should return a model', function () { | ||
return specimenClass.create({ | ||
name: 'hello' | ||
}) | ||
.then(function (model) { | ||
return expect(model.id).to.not.eql(specimen.id); | ||
}); | ||
}); | ||
}); | ||
describe('update', function () { | ||
it('should return a model', function () { | ||
return specimenClass.forge({ | ||
name: 'goodbye' | ||
}, { id: specimen.id }) | ||
.save() | ||
.bind({}) | ||
.then(function (model) { | ||
this.modelId = model.id; | ||
return specimenClass.update({ | ||
name: 'hello' | ||
}, { id: this.modelId }); | ||
}) | ||
.then(function () { | ||
return specimenClass.findOne({ id: this.modelId }); | ||
}) | ||
.then(function (model) { | ||
return expect(model.get('name')).to.eql('hello'); | ||
}); | ||
}); | ||
}); | ||
describe('destroy', function () { | ||
it('should destroy the model', function () { | ||
return specimenClass.forge({ name: 'hello' }) | ||
.bind({}) | ||
.save() | ||
.then(function (model) { | ||
this.modelId = model.id; | ||
return specimenClass.destroy({ id: this.modelId }) | ||
}) | ||
.then(function (model) { | ||
return specimenClass.findOne({ id: this.modelId }); | ||
}) | ||
.then(function (model) { | ||
return expect(model).to.eql(null); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -8,3 +8,6 @@ // Update with your config settings. | ||
connection: { | ||
filename: './dev.sqlite3' | ||
filename: __dirname + '/dev.sqlite3' | ||
}, | ||
migrations: { | ||
directory: __dirname + '/migrations' | ||
} | ||
@@ -11,0 +14,0 @@ } |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
19111
13
263
48
1