Skip to content

Commit

Permalink
Release 2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Gama committed Dec 13, 2016
1 parent 865cb5e commit 1b43ec5
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 66 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## [2.0.1](https://github.com/seegno/bookshelf-cascade-delete/tree/2.0.1) (2016-12-13)
[Full Changelog](https://github.com/seegno/bookshelf-cascade-delete/compare/2.0.0...2.0.1)

**Closed issues:**

- Self relations [\#30](https://github.com/seegno/bookshelf-cascade-delete/issues/30)
- Can't attach on destroy events of cascaded delete [\#10](https://github.com/seegno/bookshelf-cascade-delete/issues/10)

**Merged pull requests:**

- Add user to postgres knexfile [\#33](https://github.com/seegno/bookshelf-cascade-delete/pull/33) ([ricardogama](https://github.com/ricardogama))
- Add class syntax documentation [\#32](https://github.com/seegno/bookshelf-cascade-delete/pull/32) ([ricardogama](https://github.com/ricardogama))
- Add node 6 to travis configuration file [\#29](https://github.com/seegno/bookshelf-cascade-delete/pull/29) ([ricardogama](https://github.com/ricardogama))
- Update test coverage scripts to use nyc [\#28](https://github.com/seegno/bookshelf-cascade-delete/pull/28) ([ricardogama](https://github.com/ricardogama))
- Add timeout option to test script [\#27](https://github.com/seegno/bookshelf-cascade-delete/pull/27) ([ricardogama](https://github.com/ricardogama))
- Add cascadeDelete option documentation [\#26](https://github.com/seegno/bookshelf-cascade-delete/pull/26) ([ricardogama](https://github.com/ricardogama))
- Remove auxiliar methods from Model prototype [\#24](https://github.com/seegno/bookshelf-cascade-delete/pull/24) ([ricardogama](https://github.com/ricardogama))

## [2.0.0](https://github.com/seegno/bookshelf-cascade-delete/tree/2.0.0) (2016-08-29)
[Full Changelog](https://github.com/seegno/bookshelf-cascade-delete/compare/1.0.5...2.0.0)

Expand Down
151 changes: 86 additions & 65 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _bluebird = require('bluebird');

var _lodash = require('lodash');
Expand All @@ -28,24 +28,89 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

exports.default = function (Bookshelf) {
var Model = Bookshelf.Model.prototype;
var client = Bookshelf.knex.client.config.client;
var knex = Bookshelf.knex;
var client = knex.client.config.client;
var quoteColumns = client === 'postgres' || client === 'postgresql' || client === 'pg';

Bookshelf.Model = Bookshelf.Model.extend({
cascadeDelete: function cascadeDelete(transaction, options) {
var _this = this;
/**
* Dependency map.
*/

var queries = this.constructor.recursiveDeletes(this.get(this.idAttribute) || this._knex.column(this.idAttribute), options);
function dependencyMap() {
var _this = this;

return (0, _bluebird.mapSeries)((0, _lodash.flattenDeep)(queries).reverse(), function (query) {
return query(transaction);
}).then(function () {
return Model.destroy.call(_this, (0, _extends3.default)({}, options, {
transacting: transaction
}));
});
},
var skipDependents = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;

if (skipDependents || !this.dependents) {
return;
}

return (0, _lodash.reduce)(this.dependents, function (result, dependent) {
var _prototype$dependent = _this.prototype[dependent](),
relatedData = _prototype$dependent.relatedData;

var skipDependents = relatedData.type === 'belongsToMany';

return [].concat((0, _toConsumableArray3.default)(result), [{
dependents: dependencyMap.call(relatedData.target, skipDependents),
key: relatedData.key('foreignKey'),
model: relatedData.target,
skipDependents: skipDependents,
tableName: skipDependents ? relatedData.joinTable() : relatedData.target.prototype.tableName
}]);
}, []);
}

/**
* Recursive deletes.
*/

function recursiveDeletes(parent) {
// Stringify in case of parent being an instance of query.
var parentValue = typeof parent === 'number' || typeof parent === 'string' ? '\'' + parent + '\'' : parent.toString();
var dependencies = dependencyMap.call(this);

// Build delete queries for each dependent.
return (0, _lodash.reduce)(dependencies, function (result, _ref) {
var tableName = _ref.tableName,
key = _ref.key,
model = _ref.model,
skipDependents = _ref.skipDependents;

var whereClause = (quoteColumns ? '"' + key + '"' : key) + ' IN (' + parentValue + ')';

return [].concat((0, _toConsumableArray3.default)(result), [function (transaction) {
return transaction(tableName).del().whereRaw(whereClause);
}, skipDependents ? [] : recursiveDeletes.call(model, knex(tableName).column(model.prototype.idAttribute).whereRaw(whereClause))]);
}, []);
}

/**
* Cascade delete.
*/

function cascadeDelete(transacting, options) {
var _this2 = this;

var id = this.get(this.idAttribute) || this._knex.column(this.idAttribute);
var queries = recursiveDeletes.call(this.constructor, id);

return (0, _bluebird.mapSeries)((0, _lodash.flattenDeep)(queries).reverse(), function (query) {
return query(transacting);
}).then(function () {
return Model.destroy.call(_this2, (0, _extends3.default)({}, options, {
transacting: transacting
}));
});
}

/**
* Extend Model `destroy` method.
*/

Bookshelf.Model = Bookshelf.Model.extend({
destroy: function destroy(options) {
var _this2 = this;
var _this3 = this;

options = options || {};

Expand All @@ -54,57 +119,13 @@ exports.default = function (Bookshelf) {
}

if (options.transacting) {
return this.cascadeDelete(options.transacting, options);
return cascadeDelete.call(this, options.transacting, options);
}

return Bookshelf.knex.transaction(function (transaction) {
return _this2.cascadeDelete(transaction, options);
return Bookshelf.knex.transaction(function (transacting) {
return cascadeDelete.call(_this3, transacting, options);
});
}
}, {
dependencyMap: function dependencyMap() {
var _this3 = this;

var skipDependents = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];

if (skipDependents || !this.dependents) {
return;
}

return (0, _lodash.reduce)(this.dependents, function (result, dependent) {
var _prototype$dependent = _this3.prototype[dependent]();

var relatedData = _prototype$dependent.relatedData;

var skipDependents = relatedData.type === 'belongsToMany';

return [].concat((0, _toConsumableArray3.default)(result), [{
dependents: relatedData.target.dependencyMap(skipDependents),
key: relatedData.key('foreignKey'),
model: relatedData.target,
skipDependents: skipDependents,
tableName: skipDependents ? relatedData.joinTable() : relatedData.target.prototype.tableName
}]);
}, []);
},
recursiveDeletes: function recursiveDeletes(parent) {
// Stringify in case of parent being an instance of query.
var parentValue = typeof parent === 'number' || typeof parent === 'string' ? '\'' + parent + '\'' : parent.toString();

// Build delete queries for each dependent.
return (0, _lodash.reduce)(this.dependencyMap(), function (result, _ref) {
var tableName = _ref.tableName;
var key = _ref.key;
var model = _ref.model;
var skipDependents = _ref.skipDependents;

var whereClause = (client === 'postgres' ? '"' + key + '"' : key) + ' IN (' + parentValue + ')';

return [].concat((0, _toConsumableArray3.default)(result), [function (transaction) {
return transaction(tableName).del().whereRaw(whereClause);
}, skipDependents ? [] : model.recursiveDeletes(Bookshelf.knex(tableName).column(model.prototype.idAttribute).whereRaw(whereClause))]);
}, []);
}
});
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bookshelf-cascade-delete",
"version": "2.0.0",
"version": "2.0.1",
"description": "Cascade delete with Bookshelf.js",
"license": "MIT",
"author": {
Expand Down

0 comments on commit 1b43ec5

Please sign in to comment.