bookshelf-modelbase
Advanced tools
Comparing version 1.4.0 to 1.4.1
{ | ||
"name": "bookshelf-modelbase", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"description": "Extensible ModelBase for bookshelf-based model layers", | ||
@@ -5,0 +5,0 @@ "main": "./lib", |
@@ -41,2 +41,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) | ||
var User = ModelBase.extend({ | ||
tableName: 'users' | ||
}, { | ||
@@ -47,3 +48,83 @@ // validation is passed to Joi.object(), so use a raw object | ||
} | ||
}); | ||
User.create({ firstName: 'Grayson' }) | ||
.then(function () { | ||
return User.findOne({ firstName: 'Grayson' }, { require: true }); | ||
}) | ||
.then(function (grayson) { | ||
// passes patch: true to .save() by default | ||
return User.update({ firstName: 'Basil' }, { id: grayson.id }); | ||
}) | ||
.then(function (basil) { | ||
return User.destroy({ id: basil.id }); | ||
}) | ||
.then(function () { | ||
return User.findAll(); | ||
}) | ||
.then(function (collection) { | ||
console.log(collection.models.length); // => 0 | ||
}) | ||
``` | ||
### CRUD | ||
```javascript | ||
/** | ||
* 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); | ||
} | ||
``` |
@@ -28,2 +28,4 @@ var Joi = require('joi'); | ||
}); | ||
return specimen.save(); | ||
}); | ||
@@ -30,0 +32,0 @@ |
Sorry, the diff of this file is not supported yet
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
264
129
14929
11