Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function backRefChange(model: PureModel, key: string, change: IArrayChange, refOptions: IReferenceOptions) {
const property = refOptions.property as string;
const oldValue = model[key].length > change.index ? model[key][change.index] : null;
if (change.newValue) {
modelAddReference(change.newValue, property, model);
}
if (oldValue) {
modelRemoveReference(oldValue, property, model);
}
warn(`This shouldn't have happened. Please open an issue: https://github.com/infinum/datx/issues/new`);
return null;
}
export function cloneModel(model: T): T {
const TypeModel = model.constructor as typeof PureModel;
const rawData = modelToJSON(model);
const meta = rawData[META_FIELD] || { };
meta.originalId = meta.id;
delete meta.id;
const clone = new TypeModel(rawData) as T;
const collection = getModelCollection(model);
if (collection) {
collection.add(clone);
} else {
warn(`The model is not in the collection. Referencing the original model won't be possible`);
}
return clone;
}