Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Object.keys(this.static.refs).forEach((prop) => {
const refs = getModelMetaKey(this, 'refs');
if (prop in refs) {
return;
}
const ref = this.static.refs[prop];
const data = mapItems(this[prop] || this.static.defaults[prop], getModelId);
assignComputed(this, prop);
if (typeof ref === 'object') {
// Back reference
initModelRef(this, prop, {
model: ref.model,
property: ref.property,
type: ReferenceType.TO_ONE_OR_MANY,
}, data);
} else {
// Normal reference
initModelRef(this, prop, {
model: ref,
type: ReferenceType.TO_ONE_OR_MANY,
}, data);
}
});
function getNormalRef(model: PureModel, key: string, refOptions: IReferenceOptions): PureModel|Array|null {
const value: IIdentifier | Array = storage.getModelDataKey(model, key);
const collection = getModelCollection(model);
if (!collection) {
return null;
}
let dataModels = mapItems(value, (id) => id ? collection.findOne(refOptions.model, id) : id);
if (refOptions.type === ReferenceType.TO_MANY && !(dataModels instanceof Array)) {
dataModels = [dataModels];
}
if (dataModels instanceof Array) {
const data: IObservableArray = observable.array(dataModels, { deep: false });
intercept(data, (change: TChange) => partialRefUpdate(model, key, change));
return data;
}
return dataModels;
}
export function updateRef(model: PureModel, key: string, value: TRefValue) {
const refOptions = storage.getModelReferenceOptions(model, key);
const check = refOptions.type === ReferenceType.TO_MANY ? value || [] : value;
const isArray = check instanceof Array || isObservableArray(check);
validateRef(refOptions, isArray, key);
const collection = getModelCollection(model);
startAction(model);
let ids: IIdentifier|Array|null = mapItems(value, (ref: IIdentifier|PureModel) => {
if (ref && collection) {
if (ref instanceof PureModel) {
const refType = getModelType(ref);
if (refType !== getModelType(refOptions.model)) {
endAction(model);
throw error(WRONG_REF_TYPE);
}
}
let instance = collection.findOne(refOptions.model, ref);
if (!instance && typeof ref === 'object') {
instance = collection.add(ref, refOptions.model);
}
endAction(model);
return getModelId(instance || ref);
export function saveRelationship(
model: T,
ref: string,
options?: IRequestOptions,
): Promise {
const collection = getModelCollection(model) as IJsonapiCollection;
const link = getLink(model, ref, 'self');
const href: string = typeof link === 'object' ? link.href : link;
const ids = getRefId(model, ref);
const type = getModelType(getModelMetaKey(model, 'refs')[ref].model);
type ID = IDefinition|Array;
const data: ID = mapItems(ids, (id) => ({ id, type })) as ID;
return update(href, { data }, collection, options && options.headers)
.then(handleResponse(model, ref));
}
Object.keys(data.relationships).forEach((key) => {
const ref = (data.relationships as IDictionary)[key];
if (ref && 'data' in ref && ref.data) {
if ((!(ref.data instanceof Array) || ref.data.length > 0)) {
rawData[key] = mapItems(ref.data, (item: IDefinition) => item.id);
if (!classRefs || !(key in classRefs)) {
refs[key] = {
model: ref.data instanceof Array ? ref.data[0].type : ref.data.type,
type: ref.data instanceof Array ? ReferenceType.TO_MANY : ReferenceType.TO_ONE,
};
}
} else {
rawData[key] = [];
}
}
if (ref && 'links' in ref) {
refLinks[key] = ref.links;
}
if (ref && 'meta' in ref) {
refMeta[key] = ref.meta;
}