Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable deepDelete config per relation #16

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ To use with your Models add the `mixins` attribute to the definition object of y
}
```

If you want a deep delete on some but not on all relation, you can provide an object with the `relation` and the `deepDelete` option into the array. The following uses deep delete on the relation `properties` and not a deep deletion on the relation `description`.

```json
{
"name": "Product",
"properties": {
"name": {
"type": "string",
}
},
"relations": {
"properties": {
"type": "hasMany",
"model": "Property",
"foreignKey": ""
}
},
"mixins": {
"CascadeDelete": {
"relations": [{"relation": "properties", "deepDelete": false}, "description"],
"deepDelete": true
}
}
}
```

**options**

| option | type | description | required |
Expand Down
24 changes: 15 additions & 9 deletions cascade-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,31 @@ module.exports = function (Model, options) {
function cascadeDeletes() {
return _.map(options.relations, function (relation) {
return function (modelId) {
debug('Relation ' + relation + ' model ' + Model.definition.name);
var theRelation = relation;
var theDeepDelete = options.deepDelete;
if (typeof(relation) === 'object') {
theRelation = relation.relation;
theDeepDelete = relation.deepDelete || false;
}
debug('Relation ' + theRelation + ' model ' + Model.definition.name);

if (!Model.relations[relation]) {
debug('Relation ' + relation + ' not found for model ' + Model.definition.name);
throw 'Relation ' + relation + ' not found for model ' + Model.definition.name;
if (!Model.relations[theRelation]) {
debug('Relation ' + theRelation + ' not found for model ' + Model.definition.name);
throw 'Relation ' + theRelation + ' not found for model ' + Model.definition.name;
}


var relationModel = Model.relations[relation].modelTo;
var relationKey = Model.relations[relation].keyTo;
var relationModel = Model.relations[theRelation].modelTo;
var relationKey = Model.relations[theRelation].keyTo;

if (Model.relations[relation].modelThrough) {
relationModel = Model.relations[relation].modelThrough;
if (Model.relations[theRelation].modelThrough) {
relationModel = Model.relations[theRelation].modelThrough;
}

var where = {};
where[relationKey] = modelId;

if (options.deepDelete) {
if (theDeepDelete) {
let relationModelIdName = relationModel.getIdName();
let fields = {};
fields[relationModelIdName] = true;
Expand Down