Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
addToRelatedRecords(
cache: SyncRecordAccessor,
op: AddToRelatedRecordsOperation
): RecordOperation | undefined {
const { record, relationship, relatedRecord } = op;
const currentRelatedRecords = cache.getRelatedRecordsSync(
record,
relationship
);
if (
currentRelatedRecords === undefined ||
!recordsInclude(currentRelatedRecords, relatedRecord)
) {
return {
op: 'removeFromRelatedRecords',
record,
relationship,
relatedRecord
};
}
return;
},
op: AddToRelatedRecordsOperation
): Promise {
let currentRecord = await cache.getRecordAsync(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);
await cache.setRecordAsync(record);
}
return record;
},
async removeFromRelatedRecords(
cache: AsyncRecordAccessor,
op: RemoveFromRelatedRecordsOperation
): Promise {
const { record, relationship, relatedRecord } = op;
const currentRelatedRecords = await cache.getRelatedRecordsAsync(
record,
relationship
);
if (
currentRelatedRecords !== undefined &&
recordsInclude(currentRelatedRecords, relatedRecord)
) {
return {
op: 'addToRelatedRecords',
record,
relationship,
relatedRecord
};
}
return;
},
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;
},
async addToRelatedRecords(
cache: AsyncRecordAccessor,
op: AddToRelatedRecordsOperation
): Promise {
const { record, relationship, relatedRecord } = op;
const currentRelatedRecords = await cache.getRelatedRecordsAsync(
record,
relationship
);
if (
currentRelatedRecords === undefined ||
!recordsInclude(currentRelatedRecords, relatedRecord)
) {
return {
op: 'removeFromRelatedRecords',
record,
relationship,
relatedRecord
};
}
return;
},
removeFromRelatedRecords(
cache: SyncRecordAccessor,
op: RemoveFromRelatedRecordsOperation
): RecordOperation | undefined {
const { record, relationship, relatedRecord } = op;
const currentRelatedRecords = cache.getRelatedRecordsSync(
record,
relationship
);
if (
currentRelatedRecords !== undefined &&
recordsInclude(currentRelatedRecords, relatedRecord)
) {
return {
op: 'addToRelatedRecords',
record,
relationship,
relatedRecord
};
}
return;
},