@entity-access/entity-access 1.2.3 → 1.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/entity-query/EntityType.d.ts +2 -0
- package/dist/entity-query/EntityType.d.ts.map +1 -1
- package/dist/entity-query/EntityType.js +16 -0
- package/dist/entity-query/EntityType.js.map +1 -1
- package/dist/model/EntityContext.d.ts.map +1 -1
- package/dist/model/EntityContext.js +5 -13
- package/dist/model/EntityContext.js.map +1 -1
- package/dist/model/changes/ChangeEntry.d.ts.map +1 -1
- package/dist/model/changes/ChangeEntry.js +66 -42
- package/dist/model/changes/ChangeEntry.js.map +1 -1
- package/dist/model/changes/ChangeSet.d.ts +1 -1
- package/dist/model/changes/ChangeSet.d.ts.map +1 -1
- package/dist/model/changes/ChangeSet.js +18 -13
- package/dist/model/changes/ChangeSet.js.map +1 -1
- package/package.json +1 -1
- package/src/entity-query/EntityType.ts +10 -0
- package/src/model/EntityContext.ts +5 -13
- package/src/model/changes/ChangeEntry.ts +75 -44
- package/src/model/changes/ChangeSet.ts +20 -16
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -47,6 +47,7 @@ export default class ChangeEntry<T = any> implements IChanges {
|
|
|
47
47
|
ce.pending = [];
|
|
48
48
|
ce.dependents = new Set();
|
|
49
49
|
ce.modified = new Map();
|
|
50
|
+
ce.order = 1;
|
|
50
51
|
return ce;
|
|
51
52
|
}
|
|
52
53
|
|
|
@@ -296,12 +297,12 @@ export default class ChangeEntry<T = any> implements IChanges {
|
|
|
296
297
|
}
|
|
297
298
|
|
|
298
299
|
detectDependencies() {
|
|
299
|
-
const { type: {
|
|
300
|
+
const { type: { fkRelations, inverseRelations }, entity } = this;
|
|
300
301
|
// for parent relations.. check if related key is set or not...
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
302
|
+
|
|
303
|
+
let orderChanged = false;
|
|
304
|
+
|
|
305
|
+
for (const iterator of fkRelations) {
|
|
305
306
|
|
|
306
307
|
// get related entry..
|
|
307
308
|
const related = entity[iterator.name];
|
|
@@ -309,64 +310,88 @@ export default class ChangeEntry<T = any> implements IChanges {
|
|
|
309
310
|
continue;
|
|
310
311
|
}
|
|
311
312
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
313
|
+
/**
|
|
314
|
+
* We need to set prototype,
|
|
315
|
+
*
|
|
316
|
+
* Reason:
|
|
317
|
+
* The prototype is not set when object is parsed from
|
|
318
|
+
* JSON and changeSet requires type
|
|
319
|
+
*/
|
|
316
320
|
const prototype = iterator.relatedTypeClass.prototype;
|
|
317
321
|
if (Object.getPrototypeOf(related) !== prototype) {
|
|
318
322
|
Object.setPrototypeOf(related, prototype);
|
|
319
323
|
}
|
|
324
|
+
|
|
320
325
|
const relatedChanges = this.changeSet.getEntry(related);
|
|
321
326
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
if (keyValue === void 0 || keyValue === null) {
|
|
327
|
+
this.order++;
|
|
328
|
+
orderChanged = true;
|
|
325
329
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
+
if (relatedChanges.status !== "inserted") {
|
|
331
|
+
for (const { fkColumn, relatedKeyColumn } of iterator.fkMap) {
|
|
332
|
+
this.entity[fkColumn.name] = related[relatedKeyColumn.name];
|
|
333
|
+
}
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
330
336
|
|
|
331
|
-
|
|
337
|
+
for (const { fkColumn, relatedKeyColumn } of iterator.fkMap) {
|
|
338
|
+
// const keyValue = related[relatedKeyColumn.name];
|
|
339
|
+
// if (keyValue === void 0 || keyValue === null) {
|
|
332
340
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
}
|
|
341
|
+
// if(relatedChanges.dependents.has(this)) {
|
|
342
|
+
// continue;
|
|
343
|
+
// }
|
|
344
|
+
// relatedChanges.dependents.add(this);
|
|
336
345
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
}
|
|
346
|
+
// for (const d of this.dependents) {
|
|
347
|
+
// d.order += this.order + 1;
|
|
348
|
+
// }
|
|
349
|
+
|
|
350
|
+
// if (!fkColumn.columnName) {
|
|
351
|
+
// throw new EntityAccessError(`Configuration error, fk not set for ${fkColumn.name}`);
|
|
352
|
+
// }
|
|
340
353
|
relatedChanges.pending.push(() => {
|
|
341
354
|
this.entity[fkColumn.name] = related[relatedKeyColumn.name];
|
|
342
355
|
});
|
|
343
|
-
if (this.status !== "inserted") {
|
|
344
|
-
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
continue;
|
|
356
|
+
// if (this.status !== "inserted") {
|
|
357
|
+
// this.modified.set(iterator, { column: fkColumn, oldValue: void 0, newValue: void 0});
|
|
358
|
+
// }
|
|
359
|
+
// }
|
|
360
|
+
// continue;
|
|
348
361
|
}
|
|
349
362
|
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
}
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
// if(!relatedChanges.dependents.has(this)) {
|
|
366
|
+
// relatedChanges.dependents.add(this);
|
|
367
|
+
// this.order += relatedChanges.order + 1;
|
|
368
|
+
// for (const d of this.dependents) {
|
|
369
|
+
// d.order += this.order + 1;
|
|
370
|
+
// }
|
|
371
|
+
// }
|
|
372
|
+
// for (const { fkColumn, relatedKeyColumn } of iterator.fkMap) {
|
|
373
|
+
// this.entity[fkColumn.name] = related[relatedKeyColumn.name];
|
|
374
|
+
// }
|
|
360
375
|
// this.entity[iterator.fkColumn.name] = related[rKey.name];
|
|
361
376
|
}
|
|
377
|
+
|
|
378
|
+
if (!orderChanged) {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
for (const element of inverseRelations) {
|
|
383
|
+
const inverseRelation = this[element.name];
|
|
384
|
+
if (!inverseRelation) {
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
// inversely related item can never be an array
|
|
388
|
+
this.changeSet.getEntry(inverseRelation).order++;
|
|
389
|
+
}
|
|
362
390
|
}
|
|
363
391
|
|
|
364
392
|
setupInverseProperties() {
|
|
365
393
|
const deleted = this.status === "deleted";
|
|
366
|
-
for (const iterator of this.type.
|
|
367
|
-
if (!iterator.isInverseRelation) {
|
|
368
|
-
continue;
|
|
369
|
-
}
|
|
394
|
+
for (const iterator of this.type.inverseRelations) {
|
|
370
395
|
const { relatedName } = iterator;
|
|
371
396
|
const related = this.entity[iterator.name];
|
|
372
397
|
if (related === void 0) {
|
|
@@ -374,14 +399,20 @@ export default class ChangeEntry<T = any> implements IChanges {
|
|
|
374
399
|
}
|
|
375
400
|
if (Array.isArray(related)) {
|
|
376
401
|
for (const r of related) {
|
|
377
|
-
const existing = r[
|
|
402
|
+
const existing = r[relatedName];
|
|
378
403
|
if (existing !== this.entity) {
|
|
379
404
|
r[iterator.relatedName] = this.entity;
|
|
405
|
+
// need to update order...
|
|
406
|
+
this.changeSet.getEntry(r).order++;
|
|
380
407
|
}
|
|
381
408
|
}
|
|
382
409
|
continue;
|
|
383
410
|
}
|
|
384
|
-
related[relatedName]
|
|
411
|
+
if (related[relatedName] !== this.entity) {
|
|
412
|
+
related[relatedName] = this.entity;
|
|
413
|
+
// need to update order...
|
|
414
|
+
this.changeSet.getEntry(related).order++;
|
|
415
|
+
}
|
|
385
416
|
if (deleted) {
|
|
386
417
|
this.pending.push(() => delete related[relatedName]);
|
|
387
418
|
}
|
|
@@ -37,23 +37,15 @@ export default class ChangeSet {
|
|
|
37
37
|
|
|
38
38
|
*getChanges(max = 5): Generator<ChangeEntry, any, any> {
|
|
39
39
|
|
|
40
|
-
this.detectChanges();
|
|
40
|
+
const initialChanges = this.detectChanges();
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
// using d = this.addedEvent.listen((ce) => pending.push(ce.detail));
|
|
42
|
+
let copy = this.pending = [] as ChangeEntry[];
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
let copy = this.pending = [];
|
|
47
|
-
|
|
48
|
-
yield * [].concat(this.entries) as any;
|
|
44
|
+
yield *initialChanges;
|
|
49
45
|
|
|
50
46
|
while(copy.length) {
|
|
51
47
|
const next = this.pending = [];
|
|
52
|
-
|
|
53
|
-
iterator.setupInverseProperties();
|
|
54
|
-
iterator.detect();
|
|
55
|
-
yield iterator;
|
|
56
|
-
}
|
|
48
|
+
yield * this.detectChanges(copy);
|
|
57
49
|
copy = next;
|
|
58
50
|
}
|
|
59
51
|
|
|
@@ -125,17 +117,29 @@ export default class ChangeSet {
|
|
|
125
117
|
/**
|
|
126
118
|
* Detect changes will detect and sort the entries as they should be inserted.
|
|
127
119
|
*/
|
|
128
|
-
public detectChanges() {
|
|
120
|
+
public detectChanges(source = this.entries) {
|
|
129
121
|
|
|
130
|
-
|
|
122
|
+
const changes = [] as ChangeEntry[];
|
|
123
|
+
|
|
124
|
+
for (const iterator of source) {
|
|
131
125
|
iterator.setupInverseProperties();
|
|
132
126
|
}
|
|
133
127
|
|
|
134
|
-
for (const iterator of
|
|
128
|
+
for (const iterator of source) {
|
|
135
129
|
iterator.detect();
|
|
130
|
+
switch(iterator.status) {
|
|
131
|
+
case "detached":
|
|
132
|
+
case "unchanged":
|
|
133
|
+
continue;
|
|
134
|
+
default:
|
|
135
|
+
changes.push(iterator);
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
136
138
|
}
|
|
137
139
|
|
|
138
|
-
this.entries.sort((a, b) => a.order - b.order);
|
|
140
|
+
// this.entries.sort((a, b) => a.order - b.order);
|
|
141
|
+
changes.sort((a, b) => a.order - b.order);
|
|
142
|
+
return changes;
|
|
139
143
|
|
|
140
144
|
}
|
|
141
145
|
|