Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function replaceRecordHasOne(
record: RecordIdentity,
relationship: string,
relatedRecord: RecordIdentity
) {
deepSet(
record,
['relationships', relationship, 'data'],
relatedRecord ? cloneRecordIdentity(relatedRecord) : null
);
}
addToRelatedRecords(
cache: SyncRecordAccessor,
op: AddToRelatedRecordsOperation
): PatchResultData {
let currentRecord = cache.getRecordSync(op.record);
let record: Record;
const { relationship, relatedRecord } = op;
if (currentRecord) {
record = clone(currentRecord);
} else {
record = cloneRecordIdentity(op.record);
}
const relatedRecords: RecordIdentity[] =
deepGet(record, ['relationships', relationship, 'data']) || [];
if (!recordsInclude(relatedRecords, relatedRecord)) {
relatedRecords.push(relatedRecord);
deepSet(record, ['relationships', relationship, 'data'], relatedRecords);
cache.setRecordSync(record);
}
return record;
},
relatedRecords.map(r => cloneRecordIdentity(r))
);
replaceAttribute(source: Source, operation: ReplaceAttributeOperation) {
let record: Record = source.getRecord(operation.record) || cloneRecordIdentity(operation.record);
record.attributes = record.attributes || {};
record.attributes[operation.attribute] = operation.value;
source.putRecord(record);
},
private clearInverseRelationshipOps(record: RecordIdentity): RecordOperation[] {
const ops: RecordOperation[] = [];
const inverseRels = this.cache.inverseRelationships.all(record);
if (inverseRels.length > 0) {
const recordIdentity = cloneRecordIdentity(record);
inverseRels.forEach(rel => {
const relationshipDef = this.cache.schema.getModel(rel.record.type).relationships[rel.relationship];
if (relationshipDef.type === 'hasMany') {
ops.push({
op: 'removeFromRelatedRecords',
record: rel.record,
relationship: rel.relationship,
relatedRecord: recordIdentity
});
} else {
ops.push({
op: 'replaceRelatedRecord',
record: rel.record,
relationship: rel.relationship,
relatedRecord: null
});
addToRelatedRecords(
operation: AddToRelatedRecordsOperation
): TransformRecordRequest {
return {
op: 'addToRelatedRecords',
record: cloneRecordIdentity(operation.record),
relationship: operation.relationship,
relatedRecords: [cloneRecordIdentity(operation.relatedRecord)]
} as AddToRelatedRecordsRequest;
},
addToRelatedRecords(source: Source, operation: AddToRelatedRecordsOperation) {
let record: Record = source.getRecord(operation.record) || cloneRecordIdentity(operation.record);
let relationships = deepGet(record, ['relationships', operation.relationship, 'data']);
if (relationships) {
relationships.push(operation.relatedRecord);
} else {
deepSet(record, ['relationships', operation.relationship, 'data'], [operation.relatedRecord]);
}
source.putRecord(record);
},
export function recordRemoved(
schema: Schema,
record?: Record
): RecordOperation[] {
const ops: RecordOperation[] = [];
if (record && record.relationships) {
const recordIdentity = cloneRecordIdentity(record);
schema.eachRelationship(record.type, (relationship, relationshipDef) => {
const relationshipData = deepGet(record, [
'relationships',
relationship,
'data'
]) as RecordIdentity | RecordIdentity[] | null | undefined;
if (relationshipData) {
const relatedRecords = recordArrayFromData(
relationshipData
) as RecordIdentity[];
Array.prototype.push.apply(
ops,
removeRelatedRecordsOps(
create(identity: RecordIdentity): Model {
const modelFactory = this.modelFactoryFor(identity.type);
return modelFactory.create({
identity: cloneRecordIdentity(identity),
_store: this._store
});
}
async removeRecord(record: RecordIdentity, options?: object): Promise {
const identity = cloneRecordIdentity(record);
await this.update(t => t.removeRecord(identity), options);
}