Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
case 'replaceRelatedRecord':
const updateRelated = operation as
| AddToRelatedRecordsOperation
| RemoveFromRelatedRecordsOperation
| ReplaceRelatedRecordsOperation
| ReplaceRelatedRecordOperation;
transformRecords.push([updateRelated.record, updateRelated.relationship]);
break;
}
for (const [transformRecord, transformRelationship] of transformRecords) {
// check if the transformed record matches record being queried
// if performing a related record query, ensure that the transformed relationship matches the queried
// relationship
if (
equalRecordIdentities(queryRecord, transformRecord) &&
(queryRelationship == null || queryRelationship.name === transformRelationship)
) {
return true;
}
// check if the type of the transformed record matches the type being queried
if (queryType === transformRecord.type) {
return true;
}
// check if the type of the transformed record matches one of the included types
if (includedTypes.has(transformRecord.type)) {
return true;
}
// check if the transformed record is one of the related records being queried
rel =>
!(
equalRecordIdentities(rel.record, r.record) &&
rel.relationship === r.relationship
)
);
if (recRel.data.some(r => equalRecordIdentities(r, record))) {
return true;
transform.operations.forEach((operation: RecordOperation) => {
let request;
let newRequestNeeded = true;
if (
prevRequest &&
equalRecordIdentities(prevRequest.record, operation.record)
) {
if (operation.op === 'removeRecord') {
newRequestNeeded = false;
if (prevRequest.op !== 'removeRecord') {
prevRequest = null;
requests.pop();
}
} else if (
prevRequest.op === 'addRecord' ||
prevRequest.op === 'updateRecord'
) {
if (operation.op === 'replaceAttribute') {
newRequestNeeded = false;
replaceRecordAttribute(
prevRequest.record,
async replaceRelatedRecord(
cache: AsyncRecordAccessor,
op: ReplaceRelatedRecordOperation
): Promise {
const { record, relationship, relatedRecord } = op;
const currentRelatedRecord = await cache.getRelatedRecordAsync(
record,
relationship
);
if (
currentRelatedRecord === undefined ||
!equalRecordIdentities(
currentRelatedRecord as RecordIdentity,
relatedRecord as RecordIdentity
)
) {
return {
op: 'replaceRelatedRecord',
record,
relationship,
relatedRecord: currentRelatedRecord || null
};
}
return;
}
};
private isRelated(recRel: RecordRelationship, record: Record): boolean {
if (recRel.data instanceof Array) {
if (recRel.data.some(r => equalRecordIdentities(r, record))) {
return true;
}
} else if (equalRecordIdentities(recRel.data, record)) {
return true;
}
return false;
}
}
r => !equalRecordIdentities(r, relatedRecord)
);
relatedRecords = relatedRecords.filter(r => !equalRecordIdentities(r, op.relatedRecord));
removeFromRelatedRecords(source: Source, operation: RemoveFromRelatedRecordsOperation) {
let record: Record = source.getRecord(operation.record);
if (record) {
let relationships = deepGet(record, ['relationships', operation.relationship, 'data']) as RecordIdentity[];
if (relationships) {
for (let i = 0, l = relationships.length; i < l; i++) {
if (equalRecordIdentities(relationships[i], operation.relatedRecord)) {
relationships.splice(i, 1);
break;
}
}
return source.putRecord(record);
}
}
},
replaceRelatedRecord(
cache: SyncRecordAccessor,
op: ReplaceRelatedRecordOperation
): RecordOperation | undefined {
const { record, relationship, relatedRecord } = op;
const currentRelatedRecord = cache.getRelatedRecordSync(
record,
relationship
);
if (
currentRelatedRecord === undefined ||
!equalRecordIdentities(
currentRelatedRecord as RecordIdentity,
relatedRecord as RecordIdentity
)
) {
return {
op: 'replaceRelatedRecord',
record,
relationship,
relatedRecord: currentRelatedRecord || null
};
}
return;
}
};