Cascade delete your relationships
MIT License
Sometimes we have a "Composition" kind of relationship (per UML terms) where the lifecyle of one class is closely related to another.
Composition relationships represents a "part-whole" relationship such that class B is an integral part of class A. This relationship is typically used if objects of class A can't logically exist without having a class B object.
E.g, if a Human
model "has many" Leg
s, we want to destroy the legs if/when the Human is destroyed.
For such cases, we need to "cascade delete" the related models along with the parent model. This addon provides a Mixin with this functionality for you to add to any adapter in your ember app.
Just run
ember install ember-data-cascade-delete
If we want to add this feature globally, we add the Mixin to the application adapter:
// app/adapters/application.js
import JSONAPIAdapter from '@ember-data/adapter/json-api';
import CascadeDeleteMixin from 'ember-data-cascade-delete';
export default class ApplicationAdapter extends JSONAPIAdapter.extend(CascadeDeleteMixin) {
}
You can also add this mixin to any other more specific (model-scoped) adapter.
Now we can specify a cascadeDelete
option in our relationships like:
// app/models/human.js
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
export default class Human extends Model {
@attr('string')
name;
@belongsTo('heart', { async: true, inverse: 'human', cascadeDelete: true })
heart;
@hasMany('leg', { async: true, inverse: 'human', cascadeDelete: true })
legs;
}
Now when we call human.destroyRecord()
the related records marked with cascadeDelete: true
will be unloaded after the main record is destroyed.
Two things to note:
Leg
could have a hasMany
relationship to Finger
andcascadeDelete: true
on both ends of a relationship, you will get infinite loops.This addon is essentially the addonified version of this blog post, with the additional feature of nested cascade delete relationships.
ember serve
npm test
(Runs ember try:each
to test your addon against multiple Ember versions)ember test
ember test --server
ember build
For more information on using ember-cli, visit https://ember-cli.com/.