Last active
July 2, 2019 23:49
-
-
Save runspired/e9ee98ccc89fad2a07d9c86f2541a763 to your computer and use it in GitHub Desktop.
Cascade Delete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Promise, defer, resolve } from 'rsvp'; | |
export default class AppAdapter { | |
constructor(options) { | |
Object.assign(this, options); | |
} | |
static create(options) { | |
return new this(options); | |
} | |
deleteRecord(store, modelClass, snapshot) { | |
// usually would be this when extending another adapter | |
// const deletionPromise = this._super(...arguments); | |
const deletionPromise = resolve(); | |
const hasCascade = snapshot.adapterOptions && | |
Array.isArray(snapshot.adapterOptions.cascadeDelete); | |
let data = null; | |
return deletionPromise.then(() => { | |
if (hasCascade) { | |
const toDelete = snapshot.adapterOptions.cascadeDelete; | |
const record = snapshot.record; | |
const clearedRelationships = {}; | |
snapshot.eachRelationship((name, meta) => { | |
if (toDelete.indexOf(name) !== -1) { | |
const isMany = meta.kind === 'hasMany'; | |
clearedRelationships[name] = { | |
data: isMany ? [] : null | |
}; | |
const related = record[meta.kind](name); | |
if (isMany) { | |
const relatedRecords = related.ids().map(id => { | |
return store.peekRecord(meta.type, id); | |
}); | |
relatedRecords.map(relatedRecord => pushDeletion(store, relatedRecord)); | |
} else if (related) { | |
pushDeletion(store, related); | |
} | |
} | |
}); | |
data = { | |
type: snapshot.modelName, | |
id: snapshot.id, | |
relationships: clearedRelationships | |
}; | |
} | |
return { data }; | |
}); | |
} | |
} | |
function pushDeletion(store, record) { | |
if (!record) { return; } | |
if (record.get('isNew')) { | |
record.unloadRecord(); | |
return; | |
} | |
let relationships = {}; | |
let hasRelationships = false; | |
record.eachRelationship((name, { kind }) => { | |
hasRelationships = true; | |
relationships[name] = { | |
data: kind === 'hasMany' ? [] : null | |
}; | |
}); | |
if (hasRelationships) { | |
store.push({ | |
data: { | |
type: record.constructor.modelName, | |
id: record.get('id'), | |
relationships | |
} | |
}); | |
} | |
record.unloadRecord(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
init() { | |
this._super(...arguments); | |
this.allPeople = this.store.peekAll('person'); | |
this.person = this.store.push({ | |
data: { | |
type: 'person', | |
id: '1', | |
attributes: { name: 'John' }, | |
relationships: { | |
friends: { | |
data: [ | |
{ type: 'person', id: '2' }, | |
{ type: 'person', id: '3' } | |
] | |
} | |
} | |
}, | |
included: [ | |
{ | |
type: 'person', | |
id: '2', | |
attributes: { name: 'James' }, | |
}, | |
{ | |
type: 'person', | |
id: '3', | |
attributes: { name: 'Chris' }, | |
} | |
] | |
}); | |
}, | |
actions: { | |
deleteRecord(record) { | |
record.deleteRecord(); | |
record.save({ | |
adapterOptions: { cascadeDelete: ['friends'] } | |
}).then(() => { | |
record.unloadRecord(); | |
// release the retained object | |
this.set('person', null); | |
}); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Model from 'ember-data/model'; | |
import attr from 'ember-data/attr'; | |
import { belongsTo, hasMany } from 'ember-data/relationships'; | |
export default Model.extend({ | |
name: attr(), | |
father: belongsTo('person', { | |
async: false, | |
inverse: null }), | |
friends: hasMany('person', { | |
async: false, | |
inverse: 'friends' | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class MySerializer { | |
constructor(options) { | |
Object.assign(this, options); | |
} | |
static create(options) { | |
return new this(options); | |
} | |
normalizeResponse(_, __, data) { return data; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.15.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
"ember": "3.4.3", | |
"ember-template-compiler": "3.4.3", | |
"ember-testing": "3.4.3" | |
}, | |
"addons": { | |
"ember-data": "3.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment