Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
content.data.forEach((record) => {
const type = record.type;
const adapter = store.adapterFor(singularize(type));
adapter._getIndex(type).forEach((storageKey) => {
delete get(adapter, '_storage')[storageKey];
});
adapter._getIndex(type).reset();
// unload from store
store.unloadAll(singularize(type));
});
}
const promises = content.data.map((record) => {
const adapter = store.adapterFor(singularize(record.type));
// collect types to reload
reloadTypes.push(singularize(record.type));
return adapter._handleStorageRequest(null, 'POST', {
data: {data: record}
});
});
source(src, configuration) {
const [, , , model] = src.split('/');
const owner = getOwner(this);
const route = match(src);
const find = route.cb(route.params, owner);
const repo = this[model] || owner.lookup(`service:repository/${singularize(model)}`);
configuration.createEvent = function(result = {}, configuration) {
const event = {
type: 'message',
data: result,
};
const meta = get(event, 'data.meta') || {};
if (typeof meta.range === 'undefined') {
repo.reconcile(meta);
}
return event;
};
return this.type.source(find, configuration);
}
}
export function osfNestedResource(
server: Server,
parentModelName: K,
relationshipName: string & RelationshipsFor,
options?: Partial,
) {
const opts: NestedResourceOptions = Object.assign({
path: `/${pluralize(underscore(parentModelName))}/:parentID/${underscore(relationshipName)}`,
relatedModelName: singularize(relationshipName),
defaultSortKey: '-id',
}, options);
const mirageParentModelName = pluralize(camelize(parentModelName));
const mirageRelatedModelName = pluralize(camelize(opts.relatedModelName));
const detailPath = `${opts.path}/:id`;
const actions = gatherResourceActions(opts);
if (actions.includes('index')) {
server.get(opts.path, function(schema, request) {
const data = schema[mirageParentModelName]
.find(request.params.parentID)[relationshipName]
.models
.filter((m: ModelInstance) => filter(m, request))
.map((model: ModelInstance) => this.serialize(model).data);
return process(schema, request, this, data, opts);
});
getQuery(subject, object, objectId, school){
const query = {
filters: {}
};
if(object && objectId){
let what = pluralize(object.camelize());
if(object === 'mesh term'){
what = 'meshDescriptors';
}
if(subject === 'session'){
const sessionSingulars = ['sessionTypes', 'courses'];
if(sessionSingulars.includes(what)){
what = singularize(what);
}
}
if(subject === 'instructor'){
const specialInstructed = ['learningMaterials', 'sessionTypes', 'courses', 'sessions'];
if(specialInstructed.includes(what)){
what = 'instructed' + what.capitalize();
}
}
if(subject === 'learning material' && object === 'course'){
what = 'fullCourses';
}
query.filters[what] = objectId;
} else {
if(subject !== 'mesh term' && subject !== 'instructor' && subject !== 'learning material' && school){
query.filters['schools'] = [school.id];
}
server.patch(opts.path, (schema: Schema, request: Request) => {
const { parentID } = request.params;
const parentModel = schema[mirageParentModelName].find(parentID);
const { data: relatedRefs } = JSON.parse(request.requestBody) as {
data: Array>,
};
const relatedIdsKey = `${singularize(relationshipName)}Ids`;
parentModel.update({
[relatedIdsKey]: relatedRefs.mapBy('id'),
});
return { data: relatedRefs };
});
}
return this._doRelatedRequest(store, snapshot, updatedSnapshots, relationship, url, 'PATCH', isBulk).then(res => {
var relatedType = singularize(snapshot.record[relationship].meta().type);
res.data.forEach(item => {
var record = store.push(store.normalize(relatedType, item));
snapshot.record.resolveRelationship(relationship).addCanonicalRecord(record._internalModel);
});
return res;
});
},
return A(rawPayload.data.map(model => {
let ModelClass = store.modelFor(singularize(model.type));
let jsonApiPayload = serializer.normalizeResponse(store, ModelClass, Object.assign({}, rawPayload, {
data: model
}), null, 'query');
return store.push(jsonApiPayload);
}));
}
get name(): string {
const { entity } = this;
return entity ? singularize(entity.displayName) : '';
}
get referentType() {
const { relationships } = this.links;
if (relationships &&
'data' in relationships.referent &&
relationships.referent.data &&
'type' in relationships.referent.data) {
return singularize(relationships.referent.data.type) as ReferentModelName;
}
return undefined;
}