-
Notifications
You must be signed in to change notification settings - Fork 0
/
JoinTable.ts
234 lines (208 loc) · 7.24 KB
/
JoinTable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import type DynaRecord from "../DynaRecord";
import TransactionBuilder from "../dynamo-utils/TransactWriteBuilder";
import Metadata, {
type TableMetadata,
type JoinTableMetadata
} from "../metadata";
import type { ForeignKey, EntityClass } from "../types";
import { entityToTableItem } from "../utils";
import BelongsToLink from "./BelongsToLink";
/**
* Exclude the type1 type2 instance keys
*/
type ExcludeKeys = "type1" | "type2";
/**
* ForeignKey properties of the join table
*/
type ForeignKeyProperties<T> = {
[P in Exclude<keyof T, ExcludeKeys>]: T[P] extends ForeignKey
? string
: never;
};
/**
* Common props for building transactions
*/
interface TransactionProps {
tableProps: TableMetadata;
entities: {
parentEntity: EntityClass<DynaRecord>;
linkedEntity: EntityClass<DynaRecord>;
};
ids: { parentId: string; linkedEntityId: string };
}
/**
* Abstract class representing a join table for HasAndBelongsToMany relationships.
* This class should be extended for specific join table implementations.
* It is virtual and not persisted to the database but manages the BelongsToLinks
* in each related entity's partition.
*
* Example:
* ```
* class ExampleJoinTable extends JoinTable {
* public exampleId1: ForeignKey;
* public exampleId2: ForeignKey;
* }
* ```
*/
abstract class JoinTable<T extends DynaRecord, K extends DynaRecord> {
constructor(
private readonly type1: EntityClass<T>,
private readonly type2: EntityClass<K>
) {}
/**
* Create a JoinTable entry
* Adds BelongsToLink to each associated Entity's partition
* @param this
* @param keys
*/
public static async create<
ThisClass extends JoinTable<T, K>,
T extends DynaRecord,
K extends DynaRecord
>(
this: new (type1: EntityClass<T>, type2: EntityClass<K>) => ThisClass,
keys: ForeignKeyProperties<ThisClass>
): Promise<void> {
const transactionBuilder = new TransactionBuilder();
const [rel1, rel2] = Metadata.getJoinTable(this.name);
JoinTable.createBelongsToLink(transactionBuilder, keys, rel1, rel2);
JoinTable.createBelongsToLink(transactionBuilder, keys, rel2, rel1);
await transactionBuilder.executeTransaction();
}
/**
* Delete a JoinTable entry
* Deletes BelongsToLink from each associated Entity's partition
* @param this
* @param keys
*/
public static async delete<
ThisClass extends JoinTable<T, K>,
T extends DynaRecord,
K extends DynaRecord
>(
this: new (type1: EntityClass<T>, type2: EntityClass<K>) => ThisClass,
keys: ForeignKeyProperties<ThisClass>
): Promise<void> {
const transactionBuilder = new TransactionBuilder();
const [rel1, rel2] = Metadata.getJoinTable(this.name);
JoinTable.deleteBelongsToLink(transactionBuilder, keys, rel1, rel2);
JoinTable.deleteBelongsToLink(transactionBuilder, keys, rel2, rel1);
await transactionBuilder.executeTransaction();
}
/**
* Creates transactions:
* 1. Create a BelongsToLink in parents partition if its not already linked
* 2. Ensures that the parent EntityExists
* @param transactionBuilder
* @param keys
* @param parentEntityMeta
* @param linkedEntityMeta
*/
private static createBelongsToLink(
transactionBuilder: TransactionBuilder,
keys: ForeignKeyProperties<JoinTable<DynaRecord, DynaRecord>>,
parentEntityMeta: JoinTableMetadata,
linkedEntityMeta: JoinTableMetadata
): void {
const { tableProps, entities, ids } = this.transactionProps(
keys,
parentEntityMeta,
linkedEntityMeta
);
const { name: tableName } = tableProps;
const { alias: partitionKeyAlias } = tableProps.partitionKeyAttribute;
const { alias: sortKeyAlias } = tableProps.sortKeyAttribute;
const { parentEntity, linkedEntity } = entities;
const { parentId, linkedEntityId } = ids;
transactionBuilder.addPut(
{
TableName: tableName,
Item: {
...this.joinTableKey(keys, parentEntityMeta, linkedEntityMeta),
...entityToTableItem(
linkedEntity,
BelongsToLink.build(linkedEntity.name, parentId)
)
},
ConditionExpression: `attribute_not_exists(${partitionKeyAlias})` // Ensure item doesn't already exist
},
`${parentEntity.name} with ID ${linkedEntityId} is already linked to ${linkedEntity.name} with ID ${parentId}`
);
transactionBuilder.addConditionCheck(
{
TableName: tableName,
Key: {
[partitionKeyAlias]: parentEntity.partitionKeyValue(linkedEntityId),
[sortKeyAlias]: parentEntity.name
},
ConditionExpression: `attribute_exists(${partitionKeyAlias})`
},
`${parentEntity.name} with ID ${linkedEntityId} does not exist`
);
}
/**
* Deletes transactions:
* 1. Delete a BelongsToLink in parents partition if its linked
* @param transactionBuilder
* @param keys
* @param parentEntityMeta
* @param linkedEntityMeta
*/
private static deleteBelongsToLink(
transactionBuilder: TransactionBuilder,
keys: ForeignKeyProperties<JoinTable<DynaRecord, DynaRecord>>,
parentEntityMeta: JoinTableMetadata,
linkedEntityMeta: JoinTableMetadata
): void {
const { entity: parentEntity, foreignKey: parentKey } = parentEntityMeta;
const { entity: linkedEntity, foreignKey: linkedKey } = linkedEntityMeta;
const parentId: string = keys[parentKey];
const linkedEntityId: string = keys[linkedKey];
const { name: tableName, partitionKeyAttribute } = Metadata.getEntityTable(
parentEntity.name
);
transactionBuilder.addDelete(
{
TableName: tableName,
Key: this.joinTableKey(keys, parentEntityMeta, linkedEntityMeta),
ConditionExpression: `attribute_exists(${partitionKeyAttribute.alias})`
},
`${parentEntity.name} with ID ${linkedEntityId} is not linked to ${linkedEntity.name} with ID ${parentId}`
);
}
private static joinTableKey(
keys: ForeignKeyProperties<JoinTable<DynaRecord, DynaRecord>>,
parentEntityMeta: JoinTableMetadata,
linkedEntityMeta: JoinTableMetadata
): Record<string, string> {
const { tableProps, entities, ids } = this.transactionProps(
keys,
parentEntityMeta,
linkedEntityMeta
);
const { parentEntity, linkedEntity } = entities;
const { alias: partitionKeyAlias } = tableProps.partitionKeyAttribute;
const { alias: sortKeyAlias } = tableProps.sortKeyAttribute;
return {
[partitionKeyAlias]: parentEntity.partitionKeyValue(ids.linkedEntityId),
[sortKeyAlias]: linkedEntity.partitionKeyValue(ids.parentId)
};
}
private static transactionProps(
keys: ForeignKeyProperties<JoinTable<DynaRecord, DynaRecord>>,
parentEntityMeta: JoinTableMetadata,
linkedEntityMeta: JoinTableMetadata
): TransactionProps {
const { entity: parentEntity, foreignKey: parentFK } = parentEntityMeta;
const { entity: linkedEntity, foreignKey: linkedFK } = linkedEntityMeta;
const tableMetadata = Metadata.getEntityTable(parentEntity.name);
const parentId: string = keys[parentFK];
const linkedEntityId: string = keys[linkedFK];
return {
tableProps: tableMetadata,
entities: { parentEntity, linkedEntity },
ids: { parentId, linkedEntityId }
};
}
}
export default JoinTable;