2016-06-30 13:21:47 +03:00
|
|
|
import get from 'ember-metal/get';
|
|
|
|
import {isNone} from 'ember-utils';
|
2016-05-24 15:06:59 +03:00
|
|
|
import BaseAdapter from 'ghost-admin/adapters/base';
|
2014-07-21 01:48:24 +04:00
|
|
|
|
|
|
|
// EmbeddedRelationAdapter will augment the query object in calls made to
|
2015-09-03 14:06:50 +03:00
|
|
|
// DS.Store#findRecord, findAll, query, and queryRecord with the correct "includes"
|
2014-07-21 01:48:24 +04:00
|
|
|
// (?include=relatedType) by introspecting on the provided subclass of the DS.Model.
|
2014-12-26 03:01:02 +03:00
|
|
|
// In cases where there is no query object (DS.Model#save, or simple finds) the URL
|
|
|
|
// that is built will be augmented with ?include=... where appropriate.
|
2014-07-21 01:48:24 +04:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
// If a model has an embedded hasMany relation, the related type will be included:
|
|
|
|
// roles: DS.hasMany('role', { embedded: 'always' }) => ?include=roles
|
|
|
|
|
2015-08-19 14:55:40 +03:00
|
|
|
export default BaseAdapter.extend({
|
2015-10-28 14:36:45 +03:00
|
|
|
find(store, type, id, snapshot) {
|
2015-09-03 14:06:50 +03:00
|
|
|
return this.ajax(this.buildIncludeURL(store, type.modelName, id, snapshot, 'find'), 'GET');
|
2014-07-21 01:48:24 +04:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
findRecord(store, type, id, snapshot) {
|
2015-09-03 14:06:50 +03:00
|
|
|
return this.ajax(this.buildIncludeURL(store, type.modelName, id, snapshot, 'findRecord'), 'GET');
|
2014-07-21 01:48:24 +04:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
findAll(store, type, sinceToken) {
|
|
|
|
let query, url;
|
2014-12-26 03:01:02 +03:00
|
|
|
|
|
|
|
if (sinceToken) {
|
2015-09-03 14:06:50 +03:00
|
|
|
query = {since: sinceToken};
|
2014-12-26 03:01:02 +03:00
|
|
|
}
|
|
|
|
|
2015-09-03 14:06:50 +03:00
|
|
|
url = this.buildIncludeURL(store, type.modelName, null, null, 'findAll');
|
|
|
|
|
|
|
|
return this.ajax(url, 'GET', {data: query});
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
query(store, type, query) {
|
2015-09-03 14:06:50 +03:00
|
|
|
return this._super(store, type, this.buildQuery(store, type.modelName, query));
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
queryRecord(store, type, query) {
|
2015-09-03 14:06:50 +03:00
|
|
|
return this._super(store, type, this.buildQuery(store, type.modelName, query));
|
2014-07-21 01:48:24 +04:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
createRecord(store, type, snapshot) {
|
2016-01-13 17:47:10 +03:00
|
|
|
return this.saveRecord(store, type, snapshot, {method: 'POST'}, 'createRecord');
|
2014-12-26 03:01:02 +03:00
|
|
|
},
|
2014-07-21 01:48:24 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
updateRecord(store, type, snapshot) {
|
|
|
|
let options = {
|
2014-12-26 03:01:02 +03:00
|
|
|
method: 'PUT',
|
2015-10-28 14:36:45 +03:00
|
|
|
id: get(snapshot, 'id')
|
2014-12-26 03:01:02 +03:00
|
|
|
};
|
2014-07-21 01:48:24 +04:00
|
|
|
|
2016-01-13 17:47:10 +03:00
|
|
|
return this.saveRecord(store, type, snapshot, options, 'updateRecord');
|
2014-12-26 03:01:02 +03:00
|
|
|
},
|
|
|
|
|
2016-01-13 17:47:10 +03:00
|
|
|
saveRecord(store, type, snapshot, options, requestType) {
|
2015-10-28 14:36:45 +03:00
|
|
|
let _options = options || {};
|
2016-01-13 17:47:10 +03:00
|
|
|
let url = this.buildIncludeURL(store, type.modelName, _options.id, snapshot, requestType);
|
2015-10-28 14:36:45 +03:00
|
|
|
let payload = this.preparePayload(store, type, snapshot);
|
2014-12-26 03:01:02 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return this.ajax(url, _options.method, payload);
|
2014-12-26 03:01:02 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
preparePayload(store, type, snapshot) {
|
|
|
|
let serializer = store.serializerFor(type.modelName);
|
|
|
|
let payload = {};
|
2014-12-26 03:01:02 +03:00
|
|
|
|
2015-09-03 14:06:50 +03:00
|
|
|
serializer.serializeIntoHash(payload, type, snapshot);
|
2014-12-26 03:01:02 +03:00
|
|
|
|
|
|
|
return {data: payload};
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
buildIncludeURL(store, modelName, id, snapshot, requestType, query) {
|
|
|
|
let includes = this.getEmbeddedRelations(store, modelName);
|
|
|
|
let url = this.buildURL(modelName, id, snapshot, requestType, query);
|
2014-12-26 03:01:02 +03:00
|
|
|
|
|
|
|
if (includes.length) {
|
2015-10-28 14:36:45 +03:00
|
|
|
url += `?include=${includes.join(',')}`;
|
2014-12-26 03:01:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
buildQuery(store, modelName, options) {
|
|
|
|
let deDupe = {};
|
|
|
|
let toInclude = this.getEmbeddedRelations(store, modelName);
|
|
|
|
let query = options || {};
|
2014-07-21 01:48:24 +04:00
|
|
|
|
|
|
|
if (toInclude.length) {
|
|
|
|
// If this is a find by id, build a query object and attach the includes
|
|
|
|
if (typeof options === 'string' || typeof options === 'number') {
|
2014-12-26 03:01:02 +03:00
|
|
|
query = {};
|
2014-07-21 01:48:24 +04:00
|
|
|
query.id = options;
|
|
|
|
query.include = toInclude.join(',');
|
2015-10-28 14:36:45 +03:00
|
|
|
} else if (typeof options === 'object' || isNone(options)) {
|
2014-10-25 01:09:50 +04:00
|
|
|
// If this is a find all (no existing query object) build one and attach
|
|
|
|
// the includes.
|
|
|
|
// If this is a find with an existing query object then merge the includes
|
|
|
|
// into the existing object. Existing properties and includes are preserved.
|
2014-12-26 03:01:02 +03:00
|
|
|
query = query || {};
|
2014-07-21 01:48:24 +04:00
|
|
|
toInclude = toInclude.concat(query.include ? query.include.split(',') : []);
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
toInclude.forEach((include) => {
|
2014-07-21 01:48:24 +04:00
|
|
|
deDupe[include] = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
query.include = Object.keys(deDupe).join(',');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
2014-12-26 03:01:02 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
getEmbeddedRelations(store, modelName) {
|
|
|
|
let model = store.modelFor(modelName);
|
|
|
|
let ret = [];
|
2014-12-26 03:01:02 +03:00
|
|
|
|
|
|
|
// Iterate through the model's relationships and build a list
|
|
|
|
// of those that need to be pulled in via "include" from the API
|
|
|
|
model.eachRelationship(function (name, meta) {
|
2016-11-14 16:16:51 +03:00
|
|
|
if (meta.kind === 'hasMany'
|
|
|
|
&& Object.prototype.hasOwnProperty.call(meta.options, 'embedded')
|
|
|
|
&& meta.options.embedded === 'always') {
|
2014-12-26 03:01:02 +03:00
|
|
|
ret.push(name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
2014-07-21 01:48:24 +04:00
|
|
|
}
|
|
|
|
});
|