Skip to content

Commit

Permalink
Release 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Nogueira committed Apr 21, 2016
1 parent 93a3a4b commit 34359aa
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

## [1.0.0](https://github.com/seegno/bookshelf-cascade-delete/tree/1.0.0)

**Merged pull requests:**

- Add initial code [\#1](https://github.com/seegno/bookshelf-cascade-delete/pull/1) ([ricardogama](https://github.com/ricardogama))



\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
107 changes: 107 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

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

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

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

var _extends4 = _interopRequireDefault(_extends3);

var _bluebird = require('bluebird');

var _bluebird2 = _interopRequireDefault(_bluebird);

var _lodash = require('lodash');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
* Export `bookshelf-cascade-delete` plugin.
*/

/**
* Module dependencies.
*/

exports.default = function (Bookshelf) {
var Model = Bookshelf.Model.prototype;

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

return _bluebird2.default.map(this.constructor.recursiveDeletes(this.get('id'), options), function (query) {
return query(transaction);
}).then(function () {
return Model.destroy.call(_this, (0, _extends4.default)({}, options, {
transacting: transaction
}));
});
},
destroy: function destroy(options) {
var _this2 = this;

options = options || {};

if (options.cascadeDelete === false) {
return Model.destroy.call(this, options);
}

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

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

if (!this.dependents) {
return;
}

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

var foreignKey = _prototype$dependent$.foreignKey;
var target = _prototype$dependent$.target;


return (0, _extends4.default)({}, result, (0, _defineProperty3.default)({}, dependent, {
dependents: target.dependencyMap(),
key: foreignKey,
model: target
}));
}, {});
},
recursiveDeletes: function recursiveDeletes(parent, options) {
// 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.
var queries = (0, _lodash.reduce)(this.dependencyMap(), function (result, dependent) {
var tableName = dependent.model.prototype.tableName;
var whereClause = '"' + dependent.key + '" IN (' + parentValue + ')';
var selectQuery = Bookshelf.knex(tableName).column('id').whereRaw(whereClause);

return [].concat((0, _toConsumableArray3.default)(result), [function (transaction) {
return transaction(tableName).del().whereRaw(whereClause);
}, dependent.model.recursiveDeletes(selectQuery, options)]);
}, []);

return (0, _lodash.flatten)((0, _lodash.compact)(queries)).reverse();
}
});
};
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": "0.0.0",
"version": "1.0.0",
"description": "Cascade delete with Bookshelf.js",
"license": "MIT",
"author": {
Expand Down

0 comments on commit 34359aa

Please sign in to comment.