|
| 1 | +import { BaseRecord } from '../../adapters' |
| 2 | +import PropertyDecorator from '../../decorators/property/property-decorator' |
| 3 | + |
| 4 | +export type PopulatorNarrowedProperty = Pick< |
| 5 | + InstanceType<typeof PropertyDecorator>, |
| 6 | + 'resource' | 'reference' | 'property' | 'path' |
| 7 | +> |
| 8 | + |
| 9 | +/** |
| 10 | + * It populates one property in given records |
| 11 | + * |
| 12 | + * @param {Array<BaseRecord>} records array of records to populate |
| 13 | + * @param {PropertyDecorator} property Decorator for the reference property to populate |
| 14 | + */ |
| 15 | +export const populateProperty = async ( |
| 16 | + records: Array<BaseRecord> | null, |
| 17 | + property: PopulatorNarrowedProperty, |
| 18 | +): Promise<Array<BaseRecord> | null> => { |
| 19 | + const decoratedResource = property.resource() |
| 20 | + |
| 21 | + if (!records || !records.length) { |
| 22 | + return records |
| 23 | + } |
| 24 | + |
| 25 | + const referencedResource = property.reference() |
| 26 | + |
| 27 | + if (!referencedResource) { |
| 28 | + throw new Error([ |
| 29 | + `There is no reference resource named: "${property.property.reference}"`, |
| 30 | + `for property: "${decoratedResource.id()}.properties.${property.path}"`, |
| 31 | + ].join('\n')) |
| 32 | + } |
| 33 | + |
| 34 | + // I will describe the process for following data: |
| 35 | + // - decoratedResource = 'Comment' |
| 36 | + // - referenceResource = 'User' |
| 37 | + // property.path = 'userId' |
| 38 | + |
| 39 | + // first, we create externalIdsMap[1] = null where 1 is userId. This make keys unique and assign |
| 40 | + // nulls to each of them |
| 41 | + const externalIdsMap = records.reduce((memo, baseRecord) => { |
| 42 | + const foreignKeyValue = baseRecord.param(property.path) |
| 43 | + // when foreign key is not filled (like null) - don't add this because |
| 44 | + // BaseResource#findMany (which we will use) might break for nulls |
| 45 | + if (!foreignKeyValue) { |
| 46 | + return memo |
| 47 | + } |
| 48 | + return { |
| 49 | + ...memo, |
| 50 | + [foreignKeyValue]: null, |
| 51 | + } |
| 52 | + }, {}) |
| 53 | + |
| 54 | + const uniqueExternalIds = Object.keys(externalIdsMap) |
| 55 | + |
| 56 | + // when no record has `userId` filled = return input `records` |
| 57 | + if (!uniqueExternalIds.length) { |
| 58 | + return records |
| 59 | + } |
| 60 | + |
| 61 | + // now find all referenced records: all users |
| 62 | + const referenceRecords = await referencedResource.findMany(uniqueExternalIds) |
| 63 | + |
| 64 | + // |
| 65 | + if (!referenceRecords || !referenceRecords.length) { |
| 66 | + return records |
| 67 | + } |
| 68 | + |
| 69 | + // now assign these users to `externalIdsMap` instead of the empty object we had. To speed up |
| 70 | + // assigning them to record#populated we will do in the next step |
| 71 | + referenceRecords.forEach((referenceRecord) => { |
| 72 | + // example: externalIds[1] = { ...userRecord } | null (if not found) |
| 73 | + const foreignKeyValue = referenceRecord.id() |
| 74 | + externalIdsMap[foreignKeyValue] = referenceRecord |
| 75 | + }) |
| 76 | + |
| 77 | + return records.map((record) => { |
| 78 | + // we set record.populated['userId'] = externalIdsMap[record.param('userId)] |
| 79 | + const foreignKeyValue = record.param(property.path) |
| 80 | + record.populate(property.path, externalIdsMap[foreignKeyValue]) |
| 81 | + return record |
| 82 | + }) |
| 83 | +} |
0 commit comments