|
| 1 | +import { Schema as NormalizrSchema } from 'normalizr' |
| 2 | +import { Schema } from '../../../schema/Schema' |
| 3 | +import { Element, Collection } from '../../../data/Data' |
| 4 | +import { Query } from '../../../query/Query' |
| 5 | +import { Model } from '../../Model' |
| 6 | +import { Relation } from './Relation' |
| 7 | + |
| 8 | +export class HasManyBy extends Relation { |
| 9 | + /** |
| 10 | + * The child model instance of the relation. |
| 11 | + */ |
| 12 | + protected child: Model |
| 13 | + |
| 14 | + /** |
| 15 | + * The foreign key of the parent model. |
| 16 | + */ |
| 17 | + protected foreignKey: string |
| 18 | + |
| 19 | + /** |
| 20 | + * The owner key of the parent model. |
| 21 | + */ |
| 22 | + protected ownerKey: string |
| 23 | + |
| 24 | + /** |
| 25 | + * Create a new has-many-by relation instance. |
| 26 | + */ |
| 27 | + constructor( |
| 28 | + parent: Model, |
| 29 | + child: Model, |
| 30 | + foreignKey: string, |
| 31 | + ownerKey: string |
| 32 | + ) { |
| 33 | + super(parent, child) |
| 34 | + this.foreignKey = foreignKey |
| 35 | + this.ownerKey = ownerKey |
| 36 | + |
| 37 | + // In the underlying base relation class, this property is referred to as |
| 38 | + // the "parent" as most relations are not inversed. But, since this |
| 39 | + // one is, we will create a "child" property for improved readability. |
| 40 | + this.child = child |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get all related models for the relationship. |
| 45 | + */ |
| 46 | + getRelateds(): Model[] { |
| 47 | + return [this.child] |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Define the normalizr schema for the relation. |
| 52 | + */ |
| 53 | + define(schema: Schema): NormalizrSchema { |
| 54 | + return schema.many(this.child) |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Attach the relational key to the given relation. |
| 59 | + */ |
| 60 | + attach(record: Element, child: Element): void { |
| 61 | + // If the child doesn't contain the owner key, just skip here. This happens |
| 62 | + // when child items have uid attribute as its primary key, and it's missing |
| 63 | + // when inserting records. Those ids will be generated later and will be |
| 64 | + // looped again. At that time, we can attach the correct owner key value. |
| 65 | + if (child[this.ownerKey] === undefined) { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + if (!record[this.foreignKey]) { |
| 70 | + record[this.foreignKey] = [] |
| 71 | + } |
| 72 | + |
| 73 | + this.attachIfMissing(record[this.foreignKey], child[this.ownerKey]) |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Push owner key to foregin key array if owner key doesn't exist in foreign |
| 78 | + * key array. |
| 79 | + */ |
| 80 | + protected attachIfMissing( |
| 81 | + foreignKey: (string | number)[], |
| 82 | + ownerKey: string | number |
| 83 | + ): void { |
| 84 | + if (foreignKey.indexOf(ownerKey) === -1) { |
| 85 | + foreignKey.push(ownerKey) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Set the constraints for an eager load of the relation. |
| 91 | + */ |
| 92 | + addEagerConstraints(query: Query, models: Collection): void { |
| 93 | + query.whereIn(this.ownerKey, this.getEagerModelKeys(models)) |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Gather the keys from a collection of related models. |
| 98 | + */ |
| 99 | + protected getEagerModelKeys(models: Collection): (string | number)[] { |
| 100 | + return models.reduce<(string | number)[]>((keys, model) => { |
| 101 | + return [...keys, ...model[this.foreignKey]] |
| 102 | + }, []) |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Match the eagerly loaded results to their parents. |
| 107 | + */ |
| 108 | + match(relation: string, models: Collection, results: Collection): void { |
| 109 | + const dictionary = results.reduce<Record<string, Model>>((dic, result) => { |
| 110 | + dic[result[this.ownerKey]] = result |
| 111 | + |
| 112 | + return dic |
| 113 | + }, {}) |
| 114 | + |
| 115 | + models.forEach((model) => { |
| 116 | + const relatedModels = this.getRelatedModels( |
| 117 | + dictionary, |
| 118 | + model[this.foreignKey] |
| 119 | + ) |
| 120 | + |
| 121 | + model.$setRelation(relation, relatedModels) |
| 122 | + }) |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Get all related models from the given dictionary. |
| 127 | + */ |
| 128 | + protected getRelatedModels( |
| 129 | + dictionary: Record<string, Model>, |
| 130 | + keys: (string | number)[] |
| 131 | + ): Model[] { |
| 132 | + return keys.reduce<Model[]>((items, key) => { |
| 133 | + const item = dictionary[key] |
| 134 | + |
| 135 | + item && items.push(item) |
| 136 | + |
| 137 | + return items |
| 138 | + }, []) |
| 139 | + } |
| 140 | +} |
0 commit comments