2019-03-26 04:55:06 +00:00
|
|
|
import { copy } from 'ember-copy';
|
2018-02-12 23:25:21 +00:00
|
|
|
import { get } from '@ember/object';
|
2017-12-15 21:39:18 +00:00
|
|
|
import { makeArray } from '@ember/array';
|
2021-02-17 21:01:44 +00:00
|
|
|
import JSONSerializer from '@ember-data/serializer/json';
|
2019-03-26 04:24:11 +00:00
|
|
|
import { pluralize, singularize } from 'ember-inflector';
|
2018-04-07 01:07:57 +00:00
|
|
|
import removeRecord from '../utils/remove-record';
|
2020-08-20 17:14:49 +00:00
|
|
|
import { assign } from '@ember/polyfills';
|
2022-03-08 17:28:36 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2022-04-07 19:51:41 +00:00
|
|
|
import { camelize, capitalize, dasherize } from '@ember/string';
|
2022-03-08 17:28:36 +00:00
|
|
|
@classic
|
2020-06-10 13:49:16 +00:00
|
|
|
export default class Application extends JSONSerializer {
|
|
|
|
primaryKey = 'ID';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-08-20 17:14:49 +00:00
|
|
|
/**
|
|
|
|
A list of keys that are converted to empty arrays if their value is null.
|
|
|
|
|
|
|
|
arrayNullOverrides = ['Array'];
|
|
|
|
{ Array: null } => { Array: [] }
|
|
|
|
|
|
|
|
@property arrayNullOverrides
|
|
|
|
@type String[]
|
|
|
|
*/
|
|
|
|
arrayNullOverrides = null;
|
|
|
|
|
2020-12-07 20:28:23 +00:00
|
|
|
/**
|
|
|
|
A list of keys that are converted to empty objects if their value is null.
|
|
|
|
|
|
|
|
objectNullOverrides = ['Object'];
|
|
|
|
{ Object: null } => { Object: {} }
|
|
|
|
|
|
|
|
@property objectNullOverrides
|
|
|
|
@type String[]
|
|
|
|
*/
|
|
|
|
objectNullOverrides = null;
|
|
|
|
|
2020-08-20 17:14:49 +00:00
|
|
|
/**
|
|
|
|
A list of keys or objects to convert a map into an array of maps with the original map keys as Name properties.
|
|
|
|
|
|
|
|
mapToArray = ['Map'];
|
|
|
|
{ Map: { a: { x: 1 } } } => { Map: [ { Name: 'a', x: 1 }] }
|
|
|
|
|
|
|
|
mapToArray = [{ beforeName: 'M', afterName: 'Map' }];
|
|
|
|
{ M: { a: { x: 1 } } } => { Map: [ { Name: 'a', x: 1 }] }
|
|
|
|
|
|
|
|
@property mapToArray
|
|
|
|
@type (String|Object)[]
|
|
|
|
*/
|
|
|
|
mapToArray = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
A list of keys for nanosecond timestamps that will be split into two properties: `separateNanos = ['Time']` will
|
|
|
|
produce a `Time` property with a millisecond timestamp and `TimeNanos` with the nanoseconds alone.
|
|
|
|
|
|
|
|
separateNanos = ['Time'];
|
|
|
|
{ Time: 1607839992000100000 } => { Time: 1607839992000, TimeNanos: 100096 }
|
|
|
|
|
|
|
|
@property separateNanos
|
|
|
|
@type String[]
|
|
|
|
*/
|
|
|
|
separateNanos = null;
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
keyForAttribute(attr) {
|
2022-04-07 19:51:41 +00:00
|
|
|
return capitalize(camelize(attr));
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
keyForRelationship(attr, relationshipType) {
|
2022-04-07 19:51:41 +00:00
|
|
|
const key = `${capitalize(camelize(singularize(attr)))}ID`;
|
2019-03-26 04:24:11 +00:00
|
|
|
return relationshipType === 'hasMany' ? pluralize(key) : key;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
// Modeled after the pushPayload for ember-data/serializers/rest
|
|
|
|
pushPayload(store, payload) {
|
|
|
|
const documentHash = {
|
|
|
|
data: [],
|
|
|
|
included: [],
|
|
|
|
};
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
Object.keys(payload).forEach((key) => {
|
2017-09-19 14:47:10 +00:00
|
|
|
const modelName = this.modelNameFromPayloadKey(key);
|
|
|
|
const serializer = store.serializerFor(modelName);
|
|
|
|
const type = store.modelFor(modelName);
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
makeArray(payload[key]).forEach((hash) => {
|
2017-09-19 14:47:10 +00:00
|
|
|
const { data, included } = serializer.normalize(type, hash, key);
|
|
|
|
documentHash.data.push(data);
|
|
|
|
if (included) {
|
|
|
|
documentHash.included.push(...included);
|
|
|
|
}
|
|
|
|
});
|
2018-02-12 23:25:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
store.push(documentHash);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2018-02-12 23:25:21 +00:00
|
|
|
|
2020-08-20 17:14:49 +00:00
|
|
|
normalize(modelClass, hash) {
|
|
|
|
if (hash) {
|
|
|
|
if (this.arrayNullOverrides) {
|
2021-12-28 14:45:20 +00:00
|
|
|
this.arrayNullOverrides.forEach((key) => {
|
2020-08-20 17:14:49 +00:00
|
|
|
if (!hash[key]) {
|
|
|
|
hash[key] = [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-12-07 20:28:23 +00:00
|
|
|
if (this.objectNullOverrides) {
|
2021-12-28 14:45:20 +00:00
|
|
|
this.objectNullOverrides.forEach((key) => {
|
2020-12-07 20:28:23 +00:00
|
|
|
if (!hash[key]) {
|
|
|
|
hash[key] = {};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-08-20 17:14:49 +00:00
|
|
|
|
|
|
|
if (this.mapToArray) {
|
2021-12-28 14:45:20 +00:00
|
|
|
this.mapToArray.forEach((conversion) => {
|
2020-08-20 17:14:49 +00:00
|
|
|
let apiKey, uiKey;
|
|
|
|
|
|
|
|
if (conversion.beforeName) {
|
|
|
|
apiKey = conversion.beforeName;
|
|
|
|
uiKey = conversion.afterName;
|
|
|
|
} else {
|
|
|
|
apiKey = conversion;
|
|
|
|
uiKey = conversion;
|
|
|
|
}
|
|
|
|
|
|
|
|
const map = hash[apiKey] || {};
|
|
|
|
|
2020-09-05 02:44:21 +00:00
|
|
|
hash[uiKey] = Object.keys(map)
|
|
|
|
.sort()
|
2021-12-28 14:45:20 +00:00
|
|
|
.map((mapKey) => {
|
2020-09-05 02:44:21 +00:00
|
|
|
const propertiesForKey = map[mapKey] || {};
|
|
|
|
const convertedMap = { Name: mapKey };
|
2020-08-20 17:14:49 +00:00
|
|
|
|
2020-09-05 02:44:21 +00:00
|
|
|
assign(convertedMap, propertiesForKey);
|
2020-08-20 17:14:49 +00:00
|
|
|
|
2020-09-05 02:44:21 +00:00
|
|
|
return convertedMap;
|
|
|
|
});
|
2020-08-20 17:14:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.separateNanos) {
|
2021-12-28 14:45:20 +00:00
|
|
|
this.separateNanos.forEach((key) => {
|
2020-08-20 17:14:49 +00:00
|
|
|
const timeWithNanos = hash[key];
|
|
|
|
hash[`${key}Nanos`] = timeWithNanos % 1000000;
|
|
|
|
hash[key] = Math.floor(timeWithNanos / 1000000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.normalize(modelClass, hash);
|
|
|
|
}
|
|
|
|
|
2018-02-14 23:42:34 +00:00
|
|
|
normalizeFindAllResponse(store, modelClass) {
|
2020-06-10 13:49:16 +00:00
|
|
|
const result = super.normalizeFindAllResponse(...arguments);
|
2018-02-12 23:25:21 +00:00
|
|
|
this.cullStore(store, modelClass.modelName, result.data);
|
|
|
|
return result;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2018-02-12 23:25:21 +00:00
|
|
|
// When records are removed server-side, and therefore don't show up in requests,
|
|
|
|
// the local copies of those records need to be unloaded from the store.
|
2018-02-14 23:42:34 +00:00
|
|
|
cullStore(store, type, records, storeFilter = () => true) {
|
2021-12-28 14:45:20 +00:00
|
|
|
const newRecords = copy(records).filter((record) => get(record, 'id'));
|
2018-02-12 23:25:21 +00:00
|
|
|
const oldRecords = store.peekAll(type);
|
2018-02-14 23:42:34 +00:00
|
|
|
oldRecords
|
2021-12-28 14:45:20 +00:00
|
|
|
.filter((record) => get(record, 'id'))
|
2018-02-14 23:42:34 +00:00
|
|
|
.filter(storeFilter)
|
2021-12-28 14:45:20 +00:00
|
|
|
.forEach((old) => {
|
2021-12-28 16:08:12 +00:00
|
|
|
const newRecord = newRecords.find(
|
|
|
|
(record) => get(record, 'id') === get(old, 'id')
|
|
|
|
);
|
2018-02-14 23:42:34 +00:00
|
|
|
if (!newRecord) {
|
2018-04-07 01:07:57 +00:00
|
|
|
removeRecord(store, old);
|
2018-02-14 23:42:34 +00:00
|
|
|
} else {
|
|
|
|
newRecords.removeObject(newRecord);
|
|
|
|
}
|
|
|
|
});
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
modelNameFromPayloadKey(key) {
|
2022-04-07 19:51:41 +00:00
|
|
|
return singularize(dasherize(key));
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
}
|