|
| 1 | +import type { |
| 2 | + EagerCollection, |
| 3 | + Json, |
| 4 | + JsonObject, |
| 5 | + Values, |
| 6 | + Mapper, |
| 7 | + DepSafe, |
| 8 | +} from "@skipruntime/core"; |
| 9 | + |
| 10 | +class JoinOneMapper< |
| 11 | + IdProperty extends keyof VLeft, |
| 12 | + JoinedProperty extends string, |
| 13 | + K extends Json, |
| 14 | + VLeft extends JsonObject & { [P in IdProperty]: Json }, |
| 15 | + VRight extends Json, |
| 16 | +> implements |
| 17 | + Mapper< |
| 18 | + K, |
| 19 | + VLeft, |
| 20 | + K, |
| 21 | + Omit<VLeft, IdProperty> & Record<JoinedProperty, VRight> |
| 22 | + > |
| 23 | +{ |
| 24 | + constructor( |
| 25 | + private readonly right: EagerCollection<VLeft[IdProperty], VRight>, |
| 26 | + private readonly on: IdProperty, |
| 27 | + private readonly name: JoinedProperty, |
| 28 | + ) {} |
| 29 | + |
| 30 | + mapEntry( |
| 31 | + key: K, |
| 32 | + values: Values<VLeft>, |
| 33 | + ): Iterable<[K, Omit<VLeft, IdProperty> & Record<JoinedProperty, VRight>]> { |
| 34 | + return values.toArray().map((v: VLeft) => { |
| 35 | + const { [this.on]: key_right, ...value_left } = v; |
| 36 | + const value_right = { |
| 37 | + [this.name]: this.right.getUnique(key_right), |
| 38 | + } as Record<JoinedProperty, VRight>; |
| 39 | + const value_out = { ...value_left, ...value_right } as const; |
| 40 | + return [key, value_out]; |
| 41 | + }); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +export function join_one< |
| 46 | + IdProperty extends keyof V1, |
| 47 | + JoinedProperty extends string, |
| 48 | + K extends Json, |
| 49 | + V1 extends JsonObject & { [P in IdProperty]: Json }, |
| 50 | + V2 extends Json, |
| 51 | +>( |
| 52 | + left: EagerCollection<K, V1>, |
| 53 | + right: EagerCollection<V1[IdProperty], V2>, |
| 54 | + options: { |
| 55 | + on: IdProperty; |
| 56 | + name: JoinedProperty; |
| 57 | + }, |
| 58 | +): EagerCollection<K, Omit<V1, IdProperty> & Record<JoinedProperty, V2>> { |
| 59 | + return left.map( |
| 60 | + JoinOneMapper<IdProperty, JoinedProperty, K, V1, V2>, |
| 61 | + right, |
| 62 | + options.on, |
| 63 | + options.name, |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +class ProjectionMapper< |
| 68 | + ProjectionProperty extends keyof V, |
| 69 | + K extends Json, |
| 70 | + V extends JsonObject & { [P in ProjectionProperty]: Json }, |
| 71 | +> implements Mapper<K, V, V[ProjectionProperty], Omit<V, ProjectionProperty>> |
| 72 | +{ |
| 73 | + constructor(private readonly proj_property: ProjectionProperty) {} |
| 74 | + |
| 75 | + mapEntry( |
| 76 | + _key: K, |
| 77 | + values: Values<V>, |
| 78 | + ): Iterable<[V[ProjectionProperty], Omit<V, ProjectionProperty>]> { |
| 79 | + return values.toArray().map((v: V) => { |
| 80 | + const { [this.proj_property]: new_key, ...new_value } = v; |
| 81 | + return [new_key, new_value]; |
| 82 | + }); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +class JoinManyMapper< |
| 87 | + IdProperty extends keyof VRight, |
| 88 | + JoinedProperty extends string, |
| 89 | + K extends Json, |
| 90 | + VLeft extends JsonObject, |
| 91 | + VRight extends JsonObject & { [P in IdProperty]: Json }, |
| 92 | +> implements |
| 93 | + Mapper< |
| 94 | + VRight[IdProperty], |
| 95 | + VLeft, |
| 96 | + VRight[IdProperty], |
| 97 | + VLeft & Record<JoinedProperty, Omit<VRight, IdProperty>[]> |
| 98 | + > |
| 99 | +{ |
| 100 | + private readonly right: EagerCollection< |
| 101 | + VRight[IdProperty], |
| 102 | + Omit<VRight, IdProperty> |
| 103 | + >; |
| 104 | + |
| 105 | + constructor( |
| 106 | + right: EagerCollection<K, VRight>, |
| 107 | + on: IdProperty, |
| 108 | + private readonly name: JoinedProperty, |
| 109 | + ) { |
| 110 | + this.right = right.map(ProjectionMapper<IdProperty, K, VRight>, on); |
| 111 | + } |
| 112 | + |
| 113 | + mapEntry( |
| 114 | + key: VRight[IdProperty], |
| 115 | + values: Values<VLeft>, |
| 116 | + ): Iterable< |
| 117 | + [ |
| 118 | + VRight[IdProperty], |
| 119 | + VLeft & Record<JoinedProperty, Omit<VRight, IdProperty>[]>, |
| 120 | + ] |
| 121 | + > { |
| 122 | + return values.toArray().map((value_left: VLeft) => { |
| 123 | + const value_right = { [this.name]: this.right.getArray(key) } as Record< |
| 124 | + JoinedProperty, |
| 125 | + (Omit<VRight, IdProperty> & DepSafe)[] |
| 126 | + >; |
| 127 | + const value_out = { ...value_left, ...value_right }; |
| 128 | + return [key, value_out]; |
| 129 | + }); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +export function join_many< |
| 134 | + IdProperty extends keyof V2, |
| 135 | + JoinedProperty extends string, |
| 136 | + K extends Json, |
| 137 | + V1 extends JsonObject, |
| 138 | + V2 extends JsonObject & { [P in IdProperty]: Json }, |
| 139 | +>( |
| 140 | + left: EagerCollection<V2[IdProperty], V1>, |
| 141 | + right: EagerCollection<K, V2>, |
| 142 | + options: { |
| 143 | + on: IdProperty; |
| 144 | + name: JoinedProperty; |
| 145 | + }, |
| 146 | +): EagerCollection< |
| 147 | + V2[IdProperty], |
| 148 | + V1 & Record<JoinedProperty, Omit<V2, IdProperty>[]> |
| 149 | +> { |
| 150 | + return left.map( |
| 151 | + JoinManyMapper<IdProperty, JoinedProperty, K, V1, V2>, |
| 152 | + right, |
| 153 | + options.on, |
| 154 | + options.name, |
| 155 | + ); |
| 156 | +} |
0 commit comments