-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathcreate-complex-payload.ts
641 lines (565 loc) · 20.6 KB
/
create-complex-payload.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
There are 10 record types (a ... j). following the naming convention
`complex-record-{type}`.
Each record type has 10 properties (1 ... 10) following the naming
convention `prop_resource_{type}_{number}`.
Record types a, b, c, and d have relationships to each other.
A hasOne B (1:1)
A hasOne C (1:many)
A hasOne D (1:none)
B hasOne A (1:1)
B hasMany C (many:many)
B hasMany D (many:none)
C hasMany A (many:1)
C hasMany B (many:many)
C hasMany D (many:none)
D hasNone A
D hasNone B
D hasMany C (many:none)
relationship names have the convention
<kind>_resource_<type>_<related_type>
Additionally, each record has 10 traits (mixins). following the naming
convention `record-mixin-{trait}`
Each of the first 4 traits (a, b, c, d) has 10 properties (1 ... 10) following the naming
convention `prop_trait_{trait}_{number}`. The remaining traits have just one property still
following this convention.
Traits a, b, c and d have relationships to each other in exactly the same
pattern as the record types; however, these relationships are set as
polymorphic. and thus any record type can satisfy them as all 10 record types
use all 10 traits.
*/
import { styleText } from 'node:util';
import debug from 'debug';
const log = debug('create-complex-payload');
const logRelationship = debug('create-complex-payload:relationships');
const types = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
const fullTraits = ['a', 'b', 'c', 'd'];
const MANY_RELATIONSHIP_SIZE = 4;
const SEEN_IDS_FOR = {
belongsTo_trait_a_b: new Set(),
belongsTo_trait_a_c: new Set(),
belongsTo_trait_a_d: new Set(),
belongsTo_trait_b_a: new Set(),
hasMany_trait_b_c: new Set(),
hasMany_trait_b_d: new Set(),
hasMany_trait_c_a: new Set(),
hasMany_trait_c_b: new Set(),
hasMany_trait_c_d: new Set(),
hasMany_trait_d_c: new Set(),
belongsTo_resource_a_b: new Set(),
belongsTo_resource_a_c: new Set(),
belongsTo_resource_a_d: new Set(),
belongsTo_resource_b_a: new Set(),
hasMany_resource_b_c: new Set(),
hasMany_resource_b_d: new Set(),
hasMany_resource_c_a: new Set(),
hasMany_resource_c_b: new Set(),
hasMany_resource_c_d: new Set(),
hasMany_resource_d_c: new Set(),
};
type RelationshipKey = keyof typeof SEEN_IDS_FOR;
type Ref = { type: string; id: string };
type Resource = {
type: string;
id: string;
attributes: { [key: string]: string };
relationships: { [key in RelationshipKey]?: { data: null | Ref[] | Ref } };
};
type Context = {
RecordsByRef: Map<Ref, Resource>;
RecordsByType: Map<string, Resource[]>;
ALL_Records: Resource[];
totalPerType: number;
PrimaryRecords: Resource[];
OtherRecords: Resource[];
};
type Meta = {
name: string;
kind: 'belongsTo' | 'hasMany';
isRecord: boolean;
isTrait: boolean;
variant: string;
type: string;
inverse: string | null;
};
function generateRecordForType(type: string, id: string): Resource {
// each record will have 56 attributes
const attributes = {};
for (let i = 1; i <= 10; i++) {
attributes[`prop_resource_${type}_${i}`] = `cr:${type}:${id}:${i}`;
}
for (const trait of types) {
for (let i = 1; i <= 10; i++) {
attributes[`prop_trait_${trait}_${i}`] = `trait_:${trait}:${id}:${i}`;
if (!fullTraits.includes(trait)) {
// exit the inner loop if we are not in the first 4 traits
// as only they have 10 properties, the rest have just 1
break;
}
}
}
/*
All records start with 10 relationships via traits a, b, c, and d.
The relationships are polymorphic and can be satisfied by any record type.
Record types a, b, c, and d have additional relationships to each other.
types a, b and c will have 13 total relationships
type d will have 11 total relationships.
We start by populating these relationships as "empty". Then fill them
in during a second pass.
*/
const relationships: { [key in RelationshipKey]?: { data: null | Ref[] | Ref } } = {
belongsTo_trait_a_b: { data: null },
belongsTo_trait_a_c: { data: null },
belongsTo_trait_a_d: { data: null },
belongsTo_trait_b_a: { data: null },
hasMany_trait_b_c: { data: [] },
hasMany_trait_b_d: { data: [] },
hasMany_trait_c_a: { data: [] },
hasMany_trait_c_b: { data: [] },
hasMany_trait_c_d: { data: [] },
hasMany_trait_d_c: { data: [] },
};
if (type === 'a') {
relationships.belongsTo_resource_a_b = { data: null };
relationships.belongsTo_resource_a_c = { data: null };
relationships.belongsTo_resource_a_d = { data: null };
} else if (type === 'b') {
relationships.belongsTo_resource_b_a = { data: null };
relationships.hasMany_resource_b_c = { data: [] };
relationships.hasMany_resource_b_d = { data: [] };
} else if (type === 'c') {
relationships.hasMany_resource_c_a = { data: [] };
relationships.hasMany_resource_c_b = { data: [] };
relationships.hasMany_resource_c_d = { data: [] };
} else if (type === 'd') {
relationships.hasMany_resource_d_c = { data: [] };
}
return {
id,
type: `complex-record-${type}`,
attributes,
relationships,
};
}
const REFS = new Map();
function getRef(record: Resource): Ref {
if (REFS.has(record)) {
return REFS.get(record);
}
const _ref = { type: record.type, id: record.id };
REFS.set(record, _ref);
return _ref;
}
function printObjectRef(ref: Ref) {
return [
styleText('gray', '{ '),
styleText('cyan', 'type'),
styleText('gray', ': '),
styleText('green', `"${ref.type}"`),
styleText('gray', ', '),
styleText('cyan', 'id'),
styleText('gray', ': '),
styleText('green', `"${ref.id}"`),
styleText('gray', ' }'),
].join('');
}
function printStringRef(ref: Ref) {
return [
styleText('gray', '{'),
styleText('cyan', ref.type),
styleText('gray', ':'),
styleText('green', ref.id),
styleText('gray', '}'),
].join('');
}
function logArray(arr) {
console.log('\n\nArray<' + arr.length + '>[');
for (const item of arr) {
console.log(` ${printObjectRef(item)}`);
}
console.log(']\n');
}
function getNextUnseen(context: Context, selfRef: Ref, meta: Meta) {
const records = meta.isTrait ? context.ALL_Records : context.RecordsByType.get(meta.type);
if (!records?.length) {
console.log({
meta,
selfRef,
records,
});
throw new Error('No records from which to find the next unseen');
}
const seen = SEEN_IDS_FOR[meta.name];
for (const record of records) {
const relatedRef = getRef(record);
if (seen.has(relatedRef)) {
continue;
}
if (relatedRef === selfRef) {
continue;
}
seen.add(relatedRef);
return record;
}
if (meta.inverse === null) {
throw new Error(`No unseen records found for ${meta.kind} relationship ${meta.name} with inverse 'null'`);
} else if (meta.kind === 'belongsTo') {
throw new Error(
`No unseen records found for ${meta.kind} relationship ${meta.name} with inverse '${meta.inverse}'`
);
} else {
console.log('\n\nseen', seen.size);
logArray(Array.from(seen));
const inverseSeen = SEEN_IDS_FOR[meta.inverse];
console.log('\n\ninverse seen', inverseSeen.size);
logArray(Array.from(inverseSeen));
console.dir({
meta,
selfRef,
inverseMeta: parseRelationshipMeta(meta.inverse),
});
throw new Error(
`No unseen records found for ${meta.kind} relationship ${meta.name} with inverse '${meta.inverse}'`
);
}
// for hasMany relationships, we are populating
// // validate by printing out the state of every record's relationship
// console.log(`Validating States\n==================`);
// for (const record of all) {
// console.log(stringRef(getRef(record.type, record.id)));
// if (record.relationships[meta.name]) {
// const value = record.relationships[meta.name].data;
// const strValue = !value
// ? 'null'
// : Array.isArray(value)
// ? value.length
// ? value.map((v) => stringRef(v)).join(', ')
// : '<empty>'
// : stringRef(value);
// console.log(`\thas state ${meta.name}: ${strValue}`);
// }
// if (record.relationships[meta.inverse]) {
// const value = record.relationships[meta.inverse].data;
// const strValue = !value
// ? 'null'
// : Array.isArray(value)
// ? value.length
// ? value.map((v) => stringRef(v)).join(', ')
// : '<empty>'
// : stringRef(value);
// console.log(`\thas inverse ${meta.inverse}: ${strValue}`);
// }
// }
// if (meta.kind === 'hasMany') {
// const passes = seen.passes ?? 0;
// if (passes < MANY_RELATIONSHIP_SIZE) {
// SEEN_IDS_FOR[meta.name].passes = passes + 1;
// SEEN_IDS_FOR[meta.name].clear();
// }
// return getNextUnseen(meta, selfRef, records, all);
// }
// throw new Error('No unseen records found');
}
const INVERSE_RELATIONSHIPS = new Map([
['belongsTo_trait_a_b', 'belongsTo_trait_b_a'],
['belongsTo_trait_a_c', 'hasMany_trait_c_a'],
['belongsTo_trait_a_d', null],
['belongsTo_trait_b_a', 'belongsTo_trait_a_b'],
['hasMany_trait_b_c', 'hasMany_trait_c_b'],
['hasMany_trait_b_d', null],
['hasMany_trait_c_a', 'belongsTo_trait_a_c'],
['hasMany_trait_c_b', 'hasMany_trait_b_c'],
['hasMany_trait_c_d', null],
['hasMany_trait_d_c', null],
['belongsTo_resource_a_b', 'belongsTo_resource_b_a'],
['belongsTo_resource_a_c', 'hasMany_resource_c_a'],
['belongsTo_resource_a_d', null],
['belongsTo_resource_b_a', 'belongsTo_resource_a_b'],
['hasMany_resource_b_c', 'hasMany_resource_c_b'],
['hasMany_resource_b_d', null],
['hasMany_resource_c_a', 'belongsTo_resource_a_c'],
['hasMany_resource_c_b', 'hasMany_resource_b_c'],
['hasMany_resource_c_d', null],
['hasMany_resource_d_c', null],
]);
/**
* the property keys follow the pattern
* - <kind>_<isrecord><type>_<related_type>
* - <kind>_<istrait><type>_<related_type>
*/
const METAS = new Map();
function parseRelationshipMeta(fieldName: string): Meta {
if (METAS.has(fieldName)) {
return METAS.get(fieldName);
}
const parts = fieldName.split('_');
const kind = parts[0] as 'belongsTo' | 'hasMany';
const isRecord = parts[1] === 'resource';
const isTrait = parts[1] === 'trait';
const ownType = parts[2];
const typeVariant = parts[3];
const relatedType = isTrait ? `record-mixin-${typeVariant}` : `complex-record-${typeVariant}`;
const inverse = INVERSE_RELATIONSHIPS.get(fieldName);
if (inverse === undefined) {
throw new Error(`No inverse relationship found for ${fieldName}`);
}
if (inverse && INVERSE_RELATIONSHIPS.get(inverse) !== fieldName) {
throw new Error(`Inverse relationship mismatch for ${fieldName}`);
}
const meta = {
name: fieldName,
kind,
isRecord,
isTrait,
ownType,
variant: typeVariant,
type: relatedType,
inverse,
};
METAS.set(fieldName, meta);
return meta;
}
function canAddToRelatedArray(relatedArray: Ref[], ref: Ref) {
return (
relatedArray.length < MANY_RELATIONSHIP_SIZE &&
!relatedArray.some((rel) => rel.id === ref.id && ref.type === ref.type)
);
}
function addRelatedRecord(context: Context, record: Resource, meta: Meta) {
if (meta.kind !== 'belongsTo') {
throw new Error('only use addRelatedRecordForType for belongsTo relationships');
}
// if we've already been assigned, move on
const storage = record.relationships[meta.name];
if (storage.data !== null) {
logRelationship(`\t\t${styleText('cyan', meta.name)} = ${printStringRef(storage.data)} [EXISTING]`);
return;
}
const selfRef = getRef(record);
const candidate = getNextUnseen(context, selfRef, meta);
const ref = getRef(candidate);
// in order to assign, we must also be able to assign to the inverse
if (!meta.inverse) {
logRelationship(`\t\t${styleText('cyan', meta.name)} = ${printStringRef(ref)}`);
storage.data = ref;
return;
}
const inverseMeta = parseRelationshipMeta(meta.inverse);
if (inverseMeta.kind === 'hasMany') {
if (canAddToRelatedArray(candidate.relationships[inverseMeta.name].data, selfRef)) {
logRelationship(`\t\t${styleText('cyan', meta.name)} = ${printStringRef(ref)} (inverse updated)`);
storage.data = ref;
candidate.relationships[inverseMeta.name].data.push(selfRef);
SEEN_IDS_FOR[inverseMeta.name].add(selfRef);
return;
} else {
throw new Error(`Cannot add to ${inverseMeta.name} as it already is populated`);
}
} else if (inverseMeta.kind === 'belongsTo') {
if (candidate.relationships[inverseMeta.name].data === null) {
logRelationship(`\t\t${styleText('cyan', meta.name)} = ${printStringRef(ref)} (inverse updated)`);
storage.data = ref;
candidate.relationships[inverseMeta.name].data = selfRef;
SEEN_IDS_FOR[inverseMeta.name].add(selfRef);
return;
} else {
throw new Error(`Cannot add to ${inverseMeta.name} as it already is populated`);
}
} else {
throw new Error('Unknown inverse relationship kind');
}
}
function addRelatedRecords(context: Context, record: Resource, meta: Meta) {
if (meta.kind !== 'hasMany') {
throw new Error('only use addRelatedRecords for hasMany relationships');
}
// if we've already been assigned, move on
const storage = record.relationships[meta.name];
if (storage.data.length >= MANY_RELATIONSHIP_SIZE) {
if (storage.data.length > MANY_RELATIONSHIP_SIZE) {
throw new Error('Too many relationships');
}
logRelationship(
`\t\t${styleText('cyan', meta.name)} = [${storage.data.map(printStringRef).join(', ')}] [EXISTING]`
);
return;
}
const selfRef = getRef(record);
for (let i = storage.data.length; i < MANY_RELATIONSHIP_SIZE; i++) {
const candidate = getNextUnseen(context, selfRef, meta);
const ref = getRef(candidate);
// in order to assign, we must also be able to assign to the inverse
if (!meta.inverse) {
logRelationship(
`\t\t${styleText('cyan', meta.name)} += ${printStringRef(ref)} (${storage.data.length + 1} total)`
);
storage.data.push(ref);
continue;
}
const inverseMeta = parseRelationshipMeta(meta.inverse);
if (inverseMeta.kind === 'hasMany') {
if (canAddToRelatedArray(candidate.relationships[inverseMeta.name].data, selfRef)) {
logRelationship(
`\t\t${styleText('cyan', meta.name)} += ${printStringRef(ref)} (${storage.data.length + 1} total) (inverse updated)`
);
storage.data.push(ref);
candidate.relationships[inverseMeta.name].data.push(selfRef);
SEEN_IDS_FOR[inverseMeta.name].add(selfRef);
// IF NEEDED
// in a Many:Many situation, we can reset SEEN_IDS in between as the only requirement
// on uniqueness is that its not present in the candidate
continue;
} else {
throw new Error(`Cannot add to ${inverseMeta.name} as it already is populated`);
}
} else if (inverseMeta.kind === 'belongsTo') {
if (candidate.relationships[inverseMeta.name].data === null) {
logRelationship(
`\t\t${styleText('cyan', meta.name)} += ${printStringRef(ref)} (${storage.data.length + 1} total) (inverse updated)`
);
storage.data.push(ref);
candidate.relationships[inverseMeta.name].data = selfRef;
SEEN_IDS_FOR[inverseMeta.name].add(selfRef);
continue;
} else {
throw new Error(
`Cannot add ${printStringRef(selfRef)} to ${printStringRef(ref)} field ${inverseMeta.name} as it already is populated with ${printStringRef(candidate.relationships[inverseMeta.name].data)}`
);
}
} else {
throw new Error('Unknown inverse relationship kind');
}
}
}
async function ensureRelationshipData(context: Context) {
log(
`Generated ${context.ALL_Records.length} total records. Ensuring complete relationship data for ${context.totalPerType} records per type`
);
// for hasMany relationships, we always want to have MANY_RELATIONSHIP_SIZE records.
// for belongsTo relationships, we want to have 1 record.
// for mixin relationships, any record type satisfies the relationship
// for non-mixin relationships, only the specific record type satisfies the relationship
// we process all belongsTo relationships first so that they are most likely to point
// as other primary records
for (const recordsForType of context.RecordsByType.values()) {
let processed = 0;
for (const record of recordsForType) {
logRelationship(`BelongsTo | ${printStringRef(record)} (${++processed} of ${context.totalPerType})`);
for (const fieldName of Object.keys(record.relationships)) {
const meta = parseRelationshipMeta(fieldName);
if (meta.kind === 'belongsTo') {
addRelatedRecord(context, record, meta);
} else if (meta.kind === 'hasMany') {
// do nothing, handled below
} else {
throw new Error('Unknown relationship kind');
}
}
// only add relationships to the first totalPerType records
if (processed >= context.totalPerType) {
break;
}
logRelationship(`\n\n`);
if (logRelationship.enabled) {
await new Promise((resolve) => setTimeout(resolve, 500));
}
}
logRelationship(`\n\n====================\n\n`);
if (logRelationship.enabled) {
await new Promise((resolve) => setTimeout(resolve, 2000));
}
}
// next we process hasMany relationships
for (const recordsForType of context.RecordsByType.values()) {
let processed = 0;
for (const record of recordsForType) {
logRelationship(`HasMany | ${printStringRef(record)} (${++processed} of ${context.totalPerType})`);
for (const fieldName of Object.keys(record.relationships)) {
const meta = parseRelationshipMeta(fieldName);
if (meta.kind === 'belongsTo') {
// do nothing, handled above
} else if (meta.kind === 'hasMany') {
addRelatedRecords(context, record, meta);
} else {
throw new Error('Unknown relationship kind');
}
}
// only add relationships to the first totalPerType records
if (processed >= context.totalPerType) {
break;
}
logRelationship(`\n\n`);
if (logRelationship.enabled) {
await new Promise((resolve) => setTimeout(resolve, 500));
}
}
logRelationship(`\n\n====================\n\n`);
if (logRelationship.enabled) {
await new Promise((resolve) => setTimeout(resolve, 2000));
}
}
// finally we validate
for (const recordsForType of context.RecordsByType.values()) {
let processed = 0;
for (const record of recordsForType) {
for (const fieldName of Object.keys(record.relationships)) {
const meta = parseRelationshipMeta(fieldName);
if (meta.kind === 'belongsTo') {
if (record.relationships[meta.name].data === null) {
console.log(`\tMissing belongsTo relationship ${meta.name} on ${record.type} ${record.id}`);
}
} else if (meta.kind === 'hasMany') {
if (record.relationships[meta.name].data.length !== MANY_RELATIONSHIP_SIZE) {
console.log(
`\tInvalid hasMany relationship ${meta.name} on ${record.type} ${record.id}, expected ${MANY_RELATIONSHIP_SIZE} got ${record.relationships[meta.name].data.length}`
);
}
} else {
throw new Error('Unknown relationship kind');
}
}
processed++;
if (processed >= context.totalPerType) {
break;
}
}
}
}
async function createComplexPayload(totalPerType = 100) {
const RecordsByType = new Map<string, Resource[]>();
const RecordsByRef = new Map<Ref, Resource>();
const ALL_Records: Resource[] = [];
const PrimaryRecords: Resource[] = [];
const OtherRecords: Resource[] = [];
// generate the records
for (const type of types) {
const RecordsOfType: Resource[] = [];
RecordsByType.set(`complex-record-${type}`, RecordsOfType);
// in order to have 1:many and many:many relationships of N > 1, we need
// to generate N * desired total records.
for (let i = 1; i <= totalPerType * (MANY_RELATIONSHIP_SIZE + 1); i++) {
const id = String(i);
const record = generateRecordForType(type, id);
const ref = getRef(record);
RecordsByRef.set(ref, record);
RecordsOfType.push(record);
ALL_Records.push(record);
if (i <= totalPerType) {
PrimaryRecords.push(record);
} else {
OtherRecords.push(record);
}
}
}
// fill in the relationships
await ensureRelationshipData({
RecordsByRef,
RecordsByType,
ALL_Records,
totalPerType,
PrimaryRecords,
OtherRecords,
});
return { data: PrimaryRecords, included: OtherRecords };
}
export { createComplexPayload };