Comparing version 0.10.0 to 0.10.1
## Change Log | ||
**0.10.1** - <small>_Jun 29, 2016_</small> — [Diff](https://github.com/tgriesser/bookshelf/compare/0.10.0...0.10.1) | ||
* Allows using knex 0.12 as a peerDependency | ||
* knex instance used by bookshelf may be swapped out | ||
## Change Log | ||
**0.10.0** — <small>_Jun 29, 2016_</small> — [Diff](https://github.com/tgriesser/bookshelf/compare/0.9.5...0.10.0) | ||
@@ -4,0 +11,0 @@ |
@@ -157,6 +157,7 @@ 'use strict'; | ||
* @param {bool} [options.omitPivot=false] Exclude pivot values. | ||
* @param {bool} [options.omitNew=false] Exclude models that return true for isNew. | ||
* @returns {Object} Serialized model as a plain object. | ||
*/ | ||
CollectionBase.prototype.serialize = function (options) { | ||
return (0, _lodash.invokeMap)(this.models, 'toJSON', options); | ||
return (0, _lodash.invokeMap)(this.models, 'toJSON', options).filter((0, _lodash.negate)(_lodash.isNull)); | ||
}; | ||
@@ -247,7 +248,7 @@ | ||
} else if (add) { | ||
if (!(model = this._prepareModel(attrs, options))) continue; | ||
toAdd.push(model); | ||
this._byId[model.cid] = model; | ||
if (model.id != null) this._byId[model.id] = model; | ||
} | ||
if (!(model = this._prepareModel(attrs, options))) continue; | ||
toAdd.push(model); | ||
this._byId[model.cid] = model; | ||
if (model.id != null) this._byId[model.id] = model; | ||
} | ||
if (order) order.push(existing || model); | ||
@@ -254,0 +255,0 @@ } |
@@ -58,3 +58,3 @@ 'use strict'; | ||
(0, _classCallCheck3.default)(this, Events); | ||
return (0, _possibleConstructorReturn3.default)(this, Object.getPrototypeOf(Events).apply(this, arguments)); | ||
return (0, _possibleConstructorReturn3.default)(this, (Events.__proto__ || Object.getPrototypeOf(Events)).apply(this, arguments)); | ||
} | ||
@@ -83,3 +83,3 @@ | ||
(0, _lodash.each)(eventNames(nameOrNames), function (name) { | ||
(0, _get3.default)(Object.getPrototypeOf(Events.prototype), 'on', _this2).call(_this2, name, handler); | ||
(0, _get3.default)(Events.prototype.__proto__ || Object.getPrototypeOf(Events.prototype), 'on', _this2).call(_this2, name, handler); | ||
}); | ||
@@ -217,3 +217,2 @@ return this; | ||
exports.default = Events; | ||
exports.default = Events; |
@@ -277,12 +277,21 @@ 'use strict'; | ||
var omitPivot = _options$omitPivot === undefined ? false : _options$omitPivot; | ||
var rest = (0, _objectWithoutProperties3.default)(options, ['shallow', 'omitPivot']); | ||
var _options$omitNew = options.omitNew; | ||
var omitNew = _options$omitNew === undefined ? false : _options$omitNew; | ||
var rest = (0, _objectWithoutProperties3.default)(options, ['shallow', 'omitPivot', 'omitNew']); | ||
var attributes = this.attributes; | ||
if (omitNew && this.isNew()) { | ||
return null; | ||
} | ||
if (!shallow) { | ||
var relations = (0, _lodash.mapValues)(this.relations, function (relation, key) { | ||
return relation.toJSON != null ? relation.toJSON((0, _extends3.default)({ shallow: shallow, omitPivot: omitPivot }, rest)) : relation; | ||
return relation.toJSON != null ? relation.toJSON(options) : relation; | ||
}); | ||
// Omit null relations from the omitNew option | ||
relations = _lodash2.default.omitBy(relations, _lodash2.default.isNull); | ||
var pivot = this.pivot && !omitPivot && this.pivot.attributes; | ||
@@ -289,0 +298,0 @@ var pivotAttributes = (0, _lodash.mapKeys)(pivot, function (value, key) { |
@@ -29,3 +29,2 @@ 'use strict'; | ||
// centralizing all logic dealing with type & option handling. | ||
var RelationBase = function () { | ||
@@ -32,0 +31,0 @@ function RelationBase(type, Target, options) { |
@@ -246,3 +246,2 @@ 'use strict'; | ||
*/ | ||
transaction: function transaction() { | ||
@@ -316,5 +315,5 @@ return this.knex.transaction.apply(this, arguments); | ||
if ((0, _lodash.isString)(tableNameOrBuilder)) { | ||
builder = knex(tableNameOrBuilder); | ||
builder = bookshelf.knex(tableNameOrBuilder); | ||
} else if (tableNameOrBuilder == null) { | ||
builder = knex.queryBuilder(); | ||
builder = bookshelf.knex.queryBuilder(); | ||
} else { | ||
@@ -321,0 +320,0 @@ // Assuming here that `tableNameOrBuilder` is a QueryBuilder instance. Not |
@@ -51,4 +51,2 @@ 'use strict'; | ||
// --------------- | ||
var getAttributeUnique = function getAttributeUnique(models, attribute) { | ||
@@ -70,3 +68,3 @@ return _lodash2.default.uniq(_lodash2.default.map(models, function (m) { | ||
(0, _classCallCheck3.default)(this, EagerRelation); | ||
return (0, _possibleConstructorReturn3.default)(this, Object.getPrototypeOf(EagerRelation).apply(this, arguments)); | ||
return (0, _possibleConstructorReturn3.default)(this, (EagerRelation.__proto__ || Object.getPrototypeOf(EagerRelation)).apply(this, arguments)); | ||
} | ||
@@ -73,0 +71,0 @@ |
@@ -137,3 +137,2 @@ 'use strict'; | ||
*/ | ||
hasOne: function hasOne(Target, foreignKey) { | ||
@@ -613,3 +612,3 @@ return this._relation('hasOne', Target, { foreignKey: foreignKey }).init(this); | ||
* }, | ||
* chapters: function{ | ||
* chapters: function() { | ||
* return this.hasMany(Chapter); | ||
@@ -616,0 +615,0 @@ * }, |
@@ -434,3 +434,2 @@ 'use strict'; | ||
*/ | ||
attach: function attach(ids, options) { | ||
@@ -437,0 +436,0 @@ var _this3 = this; |
{ | ||
"name": "bookshelf", | ||
"version": "0.10.0", | ||
"version": "0.10.1", | ||
"description": "A lightweight ORM for PostgreSQL, MySQL, and SQLite3", | ||
@@ -16,3 +16,3 @@ "main": "bookshelf.js", | ||
"prepublish": "npm run build", | ||
"postpublish": "npm run gh-pages" | ||
"postpublish": "./scripts/postpublish.sh" | ||
}, | ||
@@ -53,3 +53,3 @@ "homepage": "http://bookshelfjs.org", | ||
"jsdoc": "^3.4.0", | ||
"knex": "^0.11.0", | ||
"knex": "^0.12.0", | ||
"minimist": "^1.1.0", | ||
@@ -66,3 +66,3 @@ "mocha": "^2.0.1", | ||
"peerDependencies": { | ||
"knex": ">=0.6.10 <0.12.0" | ||
"knex": ">=0.6.10 <0.13.0" | ||
}, | ||
@@ -69,0 +69,0 @@ "author": { |
@@ -103,7 +103,18 @@ # [bookshelf.js](http://bookshelfjs.org) [![Build Status](https://travis-ci.org/tgriesser/bookshelf.svg?branch=master)](https://travis-ci.org/tgriesser/bookshelf) | ||
- [Registry](https://github.com/tgriesser/bookshelf/wiki/Plugin:-Model-Registry): Register models in a central location so that you can refer to them using a string in relations instead of having to require it every time. Helps deal with the challenges of circular module dependencies in Node. | ||
- [Virtuals](https://github.com/tgriesser/bookshelf/wiki/Plugin:-Virtuals): Define virtual properties on your model to compute new values. | ||
- [Visibility](https://github.com/tgriesser/bookshelf/wiki/Plugin:-Visibility): Specify a whitelist/blacklist of model attributes when serialized toJSON. | ||
- [Pagination](https://github.com/tgriesser/bookshelf/wiki/Plugin:-Pagination): Adds `fetchPage` methods to use for pagination in place of `fetch` and `fetchAll` | ||
* [Registry](https://github.com/tgriesser/bookshelf/wiki/Plugin:-Model-Registry): Register models in a central location so that you can refer to them using a string in relations instead of having to require it every time. Helps deal with the challenges of circular module dependencies in Node. | ||
* [Virtuals](https://github.com/tgriesser/bookshelf/wiki/Plugin:-Virtuals): Define virtual properties on your model to compute new values. | ||
* [Visibility](https://github.com/tgriesser/bookshelf/wiki/Plugin:-Visibility): Specify a whitelist/blacklist of model attributes when serialized toJSON. | ||
* [Pagination](https://github.com/tgriesser/bookshelf/wiki/Plugin:-Pagination): Adds `fetchPage` methods to use for pagination in place of `fetch` and `fetchAll`. | ||
## Community plugins | ||
* [bookshelf-cascade-delete](https://github.com/seegno/bookshelf-cascade-delete) - Cascade delete related models on destroy. | ||
* [bookshelf-json-columns](https://github.com/seegno/bookshelf-json-columns) - Parse and stringify JSON columns on save and fetch instead of manually define hooks for each model (PostgreSQL and SQLite). | ||
* [bookshelf-mask](https://github.com/seegno/bookshelf-mask) - Similar to [Visibility](https://github.com/tgriesser/bookshelf/wiki/Plugin:-Visibility) but supporting multiple scopes, masking models and collections using the [json-mask](https://github.com/nemtsov/json-mask) API. | ||
* [bookshelf-schema](https://github.com/bogus34/bookshelf-schema) - A plugin for handling fields, relations, scopes and more. | ||
* [bookshelf-signals](https://github.com/bogus34/bookshelf-signals) - A plugin that translates Bookshelf events to a central hub. | ||
* [bookshelf-paranoia](https://github.com/estate/bookshelf-paranoia) - Protect your database from data loss by soft deleting your rows. | ||
* [bookshelf-uuid](https://github.com/estate/bookshelf-uuid) - Automatically generates UUIDs for your models. | ||
* [bookshelf-modelbase](https://github.com/bsiddiqui/bookshelf-modelbase) - An alternative to extend `Model`, adding timestamps, attribute validation and some native CRUD methods. | ||
## Support | ||
@@ -110,0 +121,0 @@ |
@@ -5,3 +5,3 @@ // Base Collection | ||
// All exernal dependencies required in this scope. | ||
import _, { invokeMap, noop } from 'lodash'; | ||
import _, { invokeMap, noop, negate, isNull } from 'lodash'; | ||
import inherits from 'inherits'; | ||
@@ -137,6 +137,7 @@ | ||
* @param {bool} [options.omitPivot=false] Exclude pivot values. | ||
* @param {bool} [options.omitNew=false] Exclude models that return true for isNew. | ||
* @returns {Object} Serialized model as a plain object. | ||
*/ | ||
CollectionBase.prototype.serialize = function(options) { | ||
return invokeMap(this.models, 'toJSON', options); | ||
return invokeMap(this.models, 'toJSON', options).filter(negate(isNull)); | ||
} | ||
@@ -143,0 +144,0 @@ |
@@ -131,3 +131,1 @@ // Events | ||
} | ||
export default Events; |
@@ -245,13 +245,20 @@ // Base Model | ||
ModelBase.prototype.serialize = function(options = {}) { | ||
const { shallow = false, omitPivot = false, ...rest } = options; | ||
const { shallow = false, omitPivot = false, omitNew = false, ...rest } = options; | ||
const { attributes } = this; | ||
if (omitNew && this.isNew()) { | ||
return null; | ||
} | ||
if (!shallow) { | ||
const relations = mapValues(this.relations, (relation, key) => | ||
let relations = mapValues(this.relations, (relation, key) => | ||
relation.toJSON != null | ||
? relation.toJSON({ shallow, omitPivot, ...rest }) | ||
? relation.toJSON(options) | ||
: relation | ||
); | ||
// Omit null relations from the omitNew option | ||
relations = _.omitBy(relations, _.isNull); | ||
const pivot = this.pivot && !omitPivot && this.pivot.attributes; | ||
@@ -258,0 +265,0 @@ const pivotAttributes = mapKeys(pivot, (value, key) => |
@@ -288,5 +288,5 @@ import { extend, result, isString, isArray, isFunction, each } from 'lodash'; | ||
if (isString(tableNameOrBuilder)) { | ||
builder = knex(tableNameOrBuilder); | ||
builder = bookshelf.knex(tableNameOrBuilder); | ||
} else if (tableNameOrBuilder == null) { | ||
builder = knex.queryBuilder(); | ||
builder = bookshelf.knex.queryBuilder(); | ||
} else { | ||
@@ -293,0 +293,0 @@ // Assuming here that `tableNameOrBuilder` is a QueryBuilder instance. Not |
@@ -570,3 +570,3 @@ import _ from 'lodash'; | ||
* }, | ||
* chapters: function{ | ||
* chapters: function() { | ||
* return this.hasMany(Chapter); | ||
@@ -573,0 +573,0 @@ * }, |
@@ -26,2 +26,4 @@ var _ = require('lodash'); | ||
var SQLite3 = require('../bookshelf')(sqlite3); | ||
var Swapped = require('../bookshelf')(Knex({})); | ||
Swapped.knex = sqlite3; | ||
@@ -33,4 +35,21 @@ it('should allow creating a new Bookshelf instance with "new"', function() { | ||
_.each([MySQL, PostgreSQL, SQLite3], function(bookshelf) { | ||
it('should allow swapping in another knex instance', function() { | ||
var bookshelf = new Bookshelf(Knex({})); | ||
var Models = require('./integration/helpers/objects')(bookshelf).Models; | ||
var site = new Models.Site(); | ||
return require('./integration/helpers/migration')(SQLite3).then(function() { | ||
return site.save() | ||
.then(function() { | ||
expect(false).to.equal(true); | ||
}) | ||
.catch(function() { | ||
bookshelf.knex = sqlite3; | ||
return site.save(); | ||
}); | ||
}); | ||
}); | ||
_.each([MySQL, PostgreSQL, SQLite3, Swapped], function(bookshelf) { | ||
var dialect = bookshelf.knex.client.dialect; | ||
@@ -37,0 +56,0 @@ |
@@ -9,2 +9,3 @@ var _ = require('lodash'); | ||
var deepEqual = require('assert').deepEqual; | ||
var QueryBuilder = require('knex/lib/query/builder') | ||
@@ -159,3 +160,3 @@ module.exports = function(bookshelf) { | ||
it('returns the Knex builder when no arguments are passed', function() { | ||
equal((model.query() instanceof bookshelf.knex.client.QueryBuilder), true); | ||
equal((model.query() instanceof QueryBuilder), true); | ||
}); | ||
@@ -236,2 +237,35 @@ | ||
}); | ||
it('omits new models from collections and relations when {omitNew: true} is passed.', function() { | ||
var model = new bookshelf.Model({id: 1, attr1: 'Test'}); | ||
model.relations = { | ||
someList: new bookshelf.Collection([{id: 2}, {attr2: 'Test'}]), | ||
someRel: new bookshelf.Model({id: 3}), | ||
otherRel: new bookshelf.Model({attr3: 'Test'}) | ||
}; | ||
var coll = new bookshelf.Collection([ | ||
model, | ||
new bookshelf.Model({attr5: 'Test'}), | ||
new bookshelf.Model({id: 4, attr4: 'Test'}) | ||
]) | ||
var json = coll.toJSON(); | ||
equal(json.length, 3); | ||
deepEqual(_.keys(json[0]), ['id', 'attr1', 'someList', 'someRel', 'otherRel']); | ||
deepEqual(_.keys(json[1]), ['attr5']); | ||
deepEqual(_.keys(json[2]), ['id', 'attr4']); | ||
equal(json[0].someList.length, 2); | ||
var omitNew = coll.toJSON({omitNew: true}); | ||
equal(omitNew.length, 2); | ||
deepEqual(_.keys(omitNew[0]), ['id', 'attr1', 'someList', 'someRel']); | ||
deepEqual(_.keys(omitNew[1]), ['id', 'attr4']); | ||
equal(omitNew[0].someList.length, 1); | ||
}); | ||
it('returns null for a new model when {omitNew: true} is passed.', function() { | ||
var model = new bookshelf.Model({attr1: 'Test'}); | ||
var json = model.toJSON(); | ||
deepEqual(_.keys(json), ['attr1']); | ||
var omitNew = model.toJSON({omitNew: true}); | ||
deepEqual(omitNew, null); | ||
}); | ||
}); | ||
@@ -238,0 +272,0 @@ |
Sorry, the diff of this file is not supported yet
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
171
730875
99
18843