joist-test-utils 2.5.0-next.2 → 2.5.0-next.21
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/build/RunPlugin.cjs +56 -37
- package/build/RunPlugin.cjs.map +1 -1
- package/build/RunPlugin.d.cts +0 -2
- package/build/RunPlugin.d.cts.map +1 -1
- package/build/RunPlugin.d.mts +0 -2
- package/build/RunPlugin.d.mts.map +1 -1
- package/build/RunPlugin.js +55 -36
- package/build/RunPlugin.js.map +1 -1
- package/build/_virtual/_rolldown/runtime.cjs +1 -2
- package/build/context.d.cts +0 -2
- package/build/context.d.cts.map +1 -1
- package/build/context.d.mts +0 -2
- package/build/context.d.mts.map +1 -1
- package/build/index.cjs +2 -3
- package/build/index.d.cts +2 -4
- package/build/index.d.cts.map +1 -1
- package/build/index.d.mts +2 -4
- package/build/index.d.mts.map +1 -1
- package/build/index.js +2 -3
- package/build/run.cjs +3 -4
- package/build/run.cjs.map +1 -1
- package/build/run.d.cts +0 -2
- package/build/run.d.cts.map +1 -1
- package/build/run.d.mts +0 -2
- package/build/run.d.mts.map +1 -1
- package/build/run.js +6 -7
- package/build/run.js.map +1 -1
- package/build/seed.cjs +1 -2
- package/build/seed.d.cts +0 -2
- package/build/seed.d.cts.map +1 -1
- package/build/seed.d.mts +0 -2
- package/build/seed.d.mts.map +1 -1
- package/build/seed.js +1 -2
- package/build/toMatchEntity.cjs +1 -2
- package/build/toMatchEntity.d.cts +0 -2
- package/build/toMatchEntity.d.cts.map +1 -1
- package/build/toMatchEntity.d.mts +0 -2
- package/build/toMatchEntity.d.mts.map +1 -1
- package/build/toMatchEntity.js +1 -2
- package/package.json +4 -4
package/build/RunPlugin.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let joist_core = require("joist-core");
|
|
3
|
-
|
|
3
|
+
// src/RunPlugin.ts
|
|
4
4
|
var RunPlugin = class extends joist_core.Plugin {
|
|
5
5
|
em;
|
|
6
6
|
constructor(em) {
|
|
@@ -22,15 +22,16 @@ var RunPlugin = class extends joist_core.Plugin {
|
|
|
22
22
|
}
|
|
23
23
|
#syncEntityData(entityTodos) {
|
|
24
24
|
const { em } = this;
|
|
25
|
-
const
|
|
25
|
+
const api = (0, joist_core.getEmInternalApi)(em);
|
|
26
|
+
const { isLoadedCache } = api;
|
|
26
27
|
Object.values(entityTodos).forEach((todo) => {
|
|
27
28
|
todo.inserts.forEach((newEntity) => {
|
|
28
29
|
const row = (0, joist_core.createRowFromEntityData)(newEntity, { preferOriginalData: false });
|
|
29
30
|
em.hydrate(newEntity.constructor, [row]);
|
|
30
31
|
});
|
|
31
32
|
todo.updates.forEach((newEntity) => {
|
|
32
|
-
const oldEntity =
|
|
33
|
-
if (!oldEntity) fail(`Expected to find existing entity for $ {e.idTagged}`);
|
|
33
|
+
const oldEntity = api.getEntity(newEntity.idTagged);
|
|
34
|
+
if (!oldEntity) (0, joist_core.fail)(`Expected to find existing entity for $ {e.idTagged}`);
|
|
34
35
|
const { data: newData } = (0, joist_core.getInstanceData)(newEntity);
|
|
35
36
|
const { data: oldData } = (0, joist_core.getInstanceData)(oldEntity);
|
|
36
37
|
const meta = (0, joist_core.getMetadata)(newEntity);
|
|
@@ -48,37 +49,38 @@ var RunPlugin = class extends joist_core.Plugin {
|
|
|
48
49
|
});
|
|
49
50
|
});
|
|
50
51
|
todo.deletes.forEach((newEntity) => {
|
|
51
|
-
const existing =
|
|
52
|
-
if (!existing) fail(`Expected to find existing entity for $ {e.idTagged}`);
|
|
52
|
+
const existing = api.getEntity(newEntity.idTagged);
|
|
53
|
+
if (!existing) (0, joist_core.fail)(`Expected to find existing entity for $ {e.idTagged}`);
|
|
53
54
|
(0, joist_core.getInstanceData)(existing).markDeletedBecauseNotFound();
|
|
54
55
|
});
|
|
55
56
|
});
|
|
56
57
|
}
|
|
57
58
|
#syncReferencesAndLazyFields(entityTodos) {
|
|
58
59
|
const { em } = this;
|
|
60
|
+
const api = (0, joist_core.getEmInternalApi)(em);
|
|
59
61
|
Object.values(entityTodos).forEach((todo) => {
|
|
60
62
|
todo.inserts.forEach((newEntity) => {
|
|
61
|
-
const oldEntity =
|
|
63
|
+
const oldEntity = api.getEntity(newEntity.idTagged);
|
|
62
64
|
for (const field of getLazyFields(newEntity)) preloadLazyField(oldEntity, field.fieldName);
|
|
63
65
|
getReferences(newEntity).forEach((r) => {
|
|
64
|
-
const other = r.isSet ?
|
|
66
|
+
const other = r.isSet ? api.getEntity(r.idTaggedMaybe) : void 0;
|
|
65
67
|
preloadReference(em, r.fieldName, oldEntity, other);
|
|
66
68
|
if (other) maybePreloadOtherSide(em, r.fieldName, oldEntity, other);
|
|
67
69
|
});
|
|
68
70
|
});
|
|
69
71
|
todo.updates.forEach((newEntity) => {
|
|
70
|
-
const oldEntity =
|
|
72
|
+
const oldEntity = api.getEntity(newEntity.idTagged);
|
|
71
73
|
for (const field of getLazyFields(newEntity)) if (newEntity.changes[field.fieldName].hasChanged) preloadLazyField(oldEntity, field.fieldName);
|
|
72
74
|
getReferences(newEntity).filter((r) => newEntity.changes[r.fieldName].hasChanged).forEach((r) => {
|
|
73
|
-
const other = r.isSet ?
|
|
75
|
+
const other = r.isSet ? api.getEntity(r.idTaggedMaybe) : void 0;
|
|
74
76
|
preloadReference(em, r.fieldName, oldEntity, other);
|
|
75
77
|
if (other) maybePreloadOtherSide(em, r.fieldName, oldEntity, other);
|
|
76
78
|
const originalId = newEntity.changes[r.fieldName].originalValue;
|
|
77
|
-
const originalOther = originalId ?
|
|
79
|
+
const originalOther = originalId ? api.getEntity(originalId) : void 0;
|
|
78
80
|
if (originalOther) maybePreloadOtherSide(em, r.fieldName, oldEntity, originalOther, true);
|
|
79
81
|
});
|
|
80
82
|
if ("deletedAt" in newEntity && newEntity.changes.deletedAt.hasChanged) getReferences(oldEntity).forEach((r) => {
|
|
81
|
-
const other = r.isSet ?
|
|
83
|
+
const other = r.isSet ? api.getEntity(r.idTaggedMaybe) : void 0;
|
|
82
84
|
if (other) {
|
|
83
85
|
const otherSide = getOtherSide(r.fieldName, oldEntity, other);
|
|
84
86
|
if (otherSide instanceof joist_core.OneToManyCollection) otherSide.resetIsLoaded();
|
|
@@ -86,10 +88,10 @@ var RunPlugin = class extends joist_core.Plugin {
|
|
|
86
88
|
});
|
|
87
89
|
});
|
|
88
90
|
todo.deletes.forEach((newEntity) => {
|
|
89
|
-
const oldEntity =
|
|
91
|
+
const oldEntity = api.getEntity(newEntity.idTagged);
|
|
90
92
|
getReferences(newEntity).forEach((r) => {
|
|
91
93
|
const entityOrId = (0, joist_core.getInstanceData)(oldEntity).data[r.fieldName];
|
|
92
|
-
const originalOther = entityOrId ?
|
|
94
|
+
const originalOther = entityOrId ? api.getEntity((0, joist_core.isEntity)(entityOrId) ? entityOrId.idTagged : entityOrId) : void 0;
|
|
93
95
|
if (originalOther) maybePreloadOtherSide(em, r.fieldName, oldEntity, originalOther, true);
|
|
94
96
|
});
|
|
95
97
|
});
|
|
@@ -100,10 +102,10 @@ var RunPlugin = class extends joist_core.Plugin {
|
|
|
100
102
|
const api = (0, joist_core.getEmInternalApi)(em);
|
|
101
103
|
Object.entries(joinRowTodos).forEach(([joinTable, todo]) => {
|
|
102
104
|
if (todo.m2m.otherEnum) return;
|
|
103
|
-
const
|
|
104
|
-
processJoinRows(em, joinTable, todo.newRows,
|
|
105
|
-
processJoinRows(em, joinTable, todo.deletedRows,
|
|
106
|
-
|
|
105
|
+
const toPreload = /* @__PURE__ */ new Set();
|
|
106
|
+
processJoinRows(em, joinTable, todo.newRows, toPreload, true);
|
|
107
|
+
processJoinRows(em, joinTable, todo.deletedRows, toPreload, false);
|
|
108
|
+
toPreload.values().forEach((r) => {
|
|
107
109
|
const id = r.entity.idTagged;
|
|
108
110
|
const entities = api.getPreloadedRelation(id, r.fieldName);
|
|
109
111
|
api.setPreloadedRelation(id, r.fieldName, [...new Set(entities)]);
|
|
@@ -122,7 +124,7 @@ var RunPlugin = class extends joist_core.Plugin {
|
|
|
122
124
|
rows.forEach((row) => {
|
|
123
125
|
const newEntity = row.columns[columnName];
|
|
124
126
|
const enumId = row.columns[otherColumnName];
|
|
125
|
-
const oldEntity =
|
|
127
|
+
const oldEntity = api.getEntity(newEntity.idTagged);
|
|
126
128
|
if (!oldEntity || oldEntity.isDeletedEntity) return;
|
|
127
129
|
const oldCollection = oldEntity[fieldName];
|
|
128
130
|
if (!oldCollection.isLoaded && !newEntity.isNewEntity) return;
|
|
@@ -143,11 +145,12 @@ var RunPlugin = class extends joist_core.Plugin {
|
|
|
143
145
|
}
|
|
144
146
|
#preloadUnloadedRelations(entityTodos) {
|
|
145
147
|
const { em } = this;
|
|
148
|
+
const api = (0, joist_core.getEmInternalApi)(em);
|
|
146
149
|
Object.entries(entityTodos).forEach(([, todo]) => {
|
|
147
150
|
todo.inserts.forEach((newEntity) => {
|
|
148
|
-
const oldEntity =
|
|
149
|
-
getCollections(oldEntity).filter((r) => !r.isPreloaded).forEach((r) => {
|
|
150
|
-
|
|
151
|
+
const oldEntity = api.getEntity(newEntity.idTagged);
|
|
152
|
+
getCollections(oldEntity).filter((r) => !r.isLoaded && !r.isPreloaded).forEach((r) => {
|
|
153
|
+
api.setPreloadedRelation(oldEntity.idTagged, r.fieldName, []);
|
|
151
154
|
r.preload();
|
|
152
155
|
});
|
|
153
156
|
});
|
|
@@ -183,11 +186,17 @@ function maybePreloadOtherSide(em, fieldName, entity, other, remove = false) {
|
|
|
183
186
|
otherSide.preload();
|
|
184
187
|
if (otherSide instanceof joist_core.OneToManyCollection) otherSide.resetIsLoaded();
|
|
185
188
|
}
|
|
186
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Mirrors persisted join rows without deriving membership or queuing fixture reactions.
|
|
191
|
+
*
|
|
192
|
+
* Used only by RunPlugin to apply changes that the isolated EntityManager has already flushed.
|
|
193
|
+
* Factory-created collections have complete membership in their index; other unloaded collections stay unloaded.
|
|
194
|
+
*/
|
|
195
|
+
function processJoinRows(em, joinTable, rows, toPreload, add) {
|
|
187
196
|
const api = (0, joist_core.getEmInternalApi)(em);
|
|
188
|
-
|
|
197
|
+
for (const row of rows) {
|
|
189
198
|
const [[col1, e1], [col2, e2]] = Object.entries(row.columns);
|
|
190
|
-
[[
|
|
199
|
+
for (const [column, newEntity, newOther] of [[
|
|
191
200
|
col1,
|
|
192
201
|
e1,
|
|
193
202
|
e2
|
|
@@ -195,22 +204,33 @@ function processJoinRows(em, joinTable, rows, preloads, callback) {
|
|
|
195
204
|
col2,
|
|
196
205
|
e2,
|
|
197
206
|
e1
|
|
198
|
-
]]
|
|
207
|
+
]]) {
|
|
199
208
|
const { idTagged: id } = newEntity;
|
|
200
|
-
const oldEntity =
|
|
201
|
-
if (oldEntity.isDeletedEntity)
|
|
209
|
+
const oldEntity = api.getEntity(id);
|
|
210
|
+
if (oldEntity.isDeletedEntity) continue;
|
|
202
211
|
const { fieldName } = getManyToManys(newEntity).find((r) => r.joinTableName === joinTable && r.columnName === column);
|
|
203
212
|
const oldM2m = oldEntity[fieldName];
|
|
213
|
+
const oldOther = api.getEntity(newOther.idTagged);
|
|
214
|
+
const joinRows = api.joinRows(oldM2m);
|
|
204
215
|
let entities;
|
|
205
|
-
if (
|
|
206
|
-
else if (oldM2m.isLoaded) entities = oldM2m.
|
|
207
|
-
else if (newEntity.isNewEntity) entities =
|
|
208
|
-
|
|
209
|
-
|
|
216
|
+
if (toPreload.has(oldM2m)) entities = api.getPreloadedRelation(id, fieldName);
|
|
217
|
+
else if (oldM2m.isLoaded) entities = [...oldM2m instanceof joist_core.ReactiveManyToManyImpl ? oldM2m.current() : oldM2m.getWithDeleted];
|
|
218
|
+
else if (newEntity.isNewEntity || (0, joist_core.getInstanceData)(oldEntity).isOrWasNew) entities = joinRows.getOthers(column, oldEntity);
|
|
219
|
+
if (entities && !toPreload.has(oldM2m)) for (const entity of entities) joinRows.addPreloadedRow(oldM2m, void 0, oldEntity, entity);
|
|
220
|
+
joinRows.removePreloadedRow(oldM2m, oldEntity, oldOther);
|
|
221
|
+
if (add) joinRows.addPreloadedRow(oldM2m, row.id, oldEntity, oldOther);
|
|
222
|
+
api.isLoadedCache.resetIsLoaded(oldEntity, fieldName);
|
|
223
|
+
if (!entities) continue;
|
|
224
|
+
if (add) {
|
|
225
|
+
if (!entities.includes(oldOther)) entities.push(oldOther);
|
|
226
|
+
} else {
|
|
227
|
+
const index = entities.indexOf(oldOther);
|
|
228
|
+
if (index !== -1) entities.splice(index, 1);
|
|
229
|
+
}
|
|
210
230
|
api.setPreloadedRelation(id, fieldName, entities);
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
}
|
|
231
|
+
toPreload.add(oldM2m);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
214
234
|
}
|
|
215
235
|
/** Returns the lazy columns on an entity, including inherited fields. */
|
|
216
236
|
function getLazyFields(entity) {
|
|
@@ -225,7 +245,6 @@ function getManyToManys(entity) {
|
|
|
225
245
|
function getCollections(entity) {
|
|
226
246
|
return (0, joist_core.getRelations)(entity).values().filter((r) => r instanceof joist_core.ManyToManyCollection || r instanceof joist_core.OneToManyCollection || r instanceof joist_core.OneToOneReferenceImpl || r instanceof joist_core.ReactiveManyToManyImpl || r instanceof joist_core.ReactiveManyToManyOtherSideImpl || r instanceof joist_core.EnumCollectionImpl);
|
|
227
247
|
}
|
|
228
|
-
//#endregion
|
|
229
248
|
exports.RunPlugin = RunPlugin;
|
|
230
249
|
|
|
231
250
|
//# sourceMappingURL=RunPlugin.cjs.map
|
package/build/RunPlugin.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RunPlugin.cjs","names":["Plugin","getEmInternalApi","createRowFromEntityData","getInstanceData","getMetadata","PolymorphicKeySerde","PojoRowData","OneToManyCollection","isEntity","getRelations","OneToOneReferenceImpl","ManyToOneReferenceImpl","ReactiveReferenceImpl","PolymorphicReferenceImpl","ManyToManyCollection","ReactiveManyToManyImpl","ReactiveManyToManyOtherSideImpl","EnumCollectionImpl"],"sources":["../src/RunPlugin.ts"],"sourcesContent":["import {\n type Entity,\n type EntityManager,\n EnumCollectionImpl,\n type FieldColumn,\n type JoinRow,\n type JoinRowTodo,\n type LazyFieldImpl,\n ManyToManyCollection,\n ManyToOneReferenceImpl,\n type MaybeAbstractEntityConstructor,\n OneToManyCollection,\n OneToOneReferenceImpl,\n Plugin,\n PojoRowData,\n PolymorphicKeySerde,\n PolymorphicReferenceImpl,\n type PrimitiveField,\n ReactiveManyToManyImpl,\n ReactiveManyToManyOtherSideImpl,\n ReactiveReferenceImpl,\n type Todo,\n assertNever,\n createRowFromEntityData,\n getEmInternalApi,\n getInstanceData,\n getMetadata,\n getRelations,\n isEntity,\n} from \"joist-core\";\n\n/*\n * `run...` helpers use this plugin to mirror changes from their isolated entity manager to the original\n * entity manager they were passed. This allows changes to be synchronously reflected between the two entity managers\n * without needing to query the database.\n */\nexport class RunPlugin extends Plugin {\n // The em passed here is the target which we'll mirror changes to. The plugin should not be added to it, but rather\n // to the new em `run..` helpers create or by downstream ems created by implementation code\n constructor(public readonly em: EntityManager) {\n super();\n }\n\n maybeCloneForNewEm() {\n return this;\n }\n\n afterWrite(entityTodos: Record<string, Todo>, joinRowTodos: Record<string, JoinRowTodo>) {\n const api = getEmInternalApi(this.em);\n // We don't want any old preloaded data leaking into our changes, so we clear it out before we start as a precaution\n api.clearPreloadedRelations();\n // Likewise, any data loaders in memory will have stale data, so we clear them out as well\n api.clearDataloaders();\n this.#syncEntityData(entityTodos);\n this.#syncReferencesAndLazyFields(entityTodos);\n this.#syncManyToManys(joinRowTodos);\n this.#syncEnumCollections(joinRowTodos);\n this.#preloadUnloadedRelations(entityTodos);\n }\n\n #syncEntityData(entityTodos: Record<string, Todo>) {\n const { em } = this;\n const { isLoadedCache } = getEmInternalApi(em);\n Object.values(entityTodos).forEach((todo) => {\n todo.inserts.forEach((newEntity) => {\n const row = createRowFromEntityData(newEntity, { preferOriginalData: false });\n em.hydrate(newEntity.constructor as MaybeAbstractEntityConstructor<Entity>, [row]);\n });\n todo.updates.forEach((newEntity) => {\n const oldEntity = em.findExistingInstance(newEntity.idTagged);\n // This fail shouldn't really be possible, since any entity should either have been created:\n // * via factory or em.create in the original em\n // * by the new em inside the `run` call, in which case it'd be an insert instead of an update\n // * by a previous afterWrite call, which would have already been an insert and added to the original em\n // * by a previous `run` call, which would have already been an insert as well\n if (!oldEntity) fail(`Expected to find existing entity for $ {e.idTagged}`);\n const { data: newData } = getInstanceData(newEntity);\n const { data: oldData } = getInstanceData(oldEntity);\n const meta = getMetadata(newEntity);\n (newEntity as any).changes.fields\n .values()\n .filter((fieldName: string) => fieldName in newData)\n .filter((fieldName: string) => !!meta.allFields[fieldName].serde)\n .forEach((fieldName: string) => {\n // We're imitating a round trip to the database here, so we use our field's serde to map the value back\n // and forth.\n const serde = meta.allFields[fieldName].serde!;\n let column: FieldColumn | undefined;\n let value: any;\n if (serde instanceof PolymorphicKeySerde) {\n [column, value] =\n serde.columns\n .values()\n .map((c) => [c, c.rowValue(newData)] as const)\n .find(([, v]) => v !== undefined) ?? [];\n } else {\n [column] = serde.columns;\n // the 2nd and 3rd arguments are only used if the 4th argument is defined, so it's OK to pass undefined here\n value = column.rowValue(newData);\n }\n oldData[fieldName] = serde.fromRow(new PojoRowData([column ? { [column.columnName]: value } : {}]), 0);\n // Mirror setField's cache invalidation for these changed fields without making test entities dirty\n // or queuing business reactions that the isolated EM has already processed.\n isLoadedCache.resetIsLoaded(oldEntity, fieldName);\n });\n });\n todo.deletes.forEach((newEntity) => {\n const existing = em.findExistingInstance(newEntity.idTagged);\n // Again it shouldn't really be possible for a deletion to happen without the entity being present in the\n // original em, but just in case we'll skip if we don't find it\n if (!existing) fail(`Expected to find existing entity for $ {e.idTagged}`);\n getInstanceData(existing).markDeletedBecauseNotFound();\n });\n });\n }\n\n // Ensure m2o/rr/poly changes and known lazy values from the new em are loaded in the original em\n // Run is a test helper, so we eagerly preload the test entity graph for synchronous assertions. LazyFields are an\n // exception: they can hold large payloads, so we only preload values known from inserts or changed columns. Untouched\n // lazy fields keep their loaded state instead of fetching data that the production code did not write.\n #syncReferencesAndLazyFields(entityTodos: Record<string, Todo>) {\n const { em } = this;\n Object.values(entityTodos).forEach((todo) => {\n todo.inserts.forEach((newEntity) => {\n const oldEntity = em.findExistingInstance(newEntity.idTagged)!;\n // New entities always have the lazy field's initial value, so always preload them\n for (const field of getLazyFields(newEntity)) {\n preloadLazyField(oldEntity, field.fieldName);\n }\n\n getReferences(newEntity).forEach((r) => {\n const other = r.isSet ? em.findExistingInstance(r.idTaggedMaybe) : undefined;\n preloadReference(em, r.fieldName, oldEntity, other);\n if (other) maybePreloadOtherSide(em, r.fieldName, oldEntity, other);\n });\n });\n todo.updates.forEach((newEntity) => {\n const oldEntity = em.findExistingInstance(newEntity.idTagged)!;\n // Only preload lazy fields that were actively changed, b/c they're already available/loaded in the entity we're copying from\n for (const field of getLazyFields(newEntity)) {\n if ((newEntity as any).changes[field.fieldName].hasChanged) {\n preloadLazyField(oldEntity, field.fieldName);\n }\n }\n\n getReferences(newEntity)\n .filter((r) => (newEntity as any).changes[r.fieldName].hasChanged)\n .forEach((r) => {\n const other = r.isSet ? em.findExistingInstance(r.idTaggedMaybe) : undefined;\n preloadReference(em, r.fieldName, oldEntity, other);\n if (other) maybePreloadOtherSide(em, r.fieldName, oldEntity, other);\n const originalId = (newEntity as any).changes[r.fieldName].originalValue;\n const originalOther = originalId ? em.findExistingInstance(originalId) : undefined;\n if (originalOther) maybePreloadOtherSide(em, r.fieldName, oldEntity, originalOther, true);\n });\n // If the entity was soft-deleted, then we need to clear out the caches for any o2m collections that contain it\n if (\"deletedAt\" in newEntity && (newEntity as any).changes.deletedAt.hasChanged) {\n getReferences(oldEntity).forEach((r) => {\n const other = r.isSet ? em.findExistingInstance(r.idTaggedMaybe) : undefined;\n if (other) {\n const otherSide = getOtherSide(r.fieldName, oldEntity, other);\n if (otherSide instanceof OneToManyCollection) otherSide.resetIsLoaded();\n }\n });\n }\n });\n todo.deletes.forEach((newEntity) => {\n const oldEntity = em.findExistingInstance(newEntity.idTagged)!;\n getReferences(newEntity).forEach((r) => {\n // Since both old and new entities are marked as deleted, we can't just go looking through their relations\n // for the other side's id since this will error. Likewise, we only care about the collection that this\n // entity used to belong to in the old em so we can remove it. So just grab the other id directly from the\n // old entity's data and then preload its other side relation.\n const entityOrId = getInstanceData(oldEntity).data[r.fieldName];\n const originalOther = entityOrId\n ? em.findExistingInstance(isEntity(entityOrId) ? entityOrId.idTagged : entityOrId)\n : undefined;\n if (originalOther) maybePreloadOtherSide(em, r.fieldName, oldEntity, originalOther, true);\n });\n });\n });\n }\n\n // Ensure m2m changes are propagated from the new em to the original em\n #syncManyToManys(joinRowTodos: Record<string, JoinRowTodo>) {\n const { em } = this;\n const api = getEmInternalApi(em);\n Object.entries(joinRowTodos).forEach(([joinTable, todo]) => {\n // Enum m2ms (EnumCollections) aren't entity-to-entity, so there's nothing to mirror between ems.\n if (todo.m2m.otherEnum) return;\n const preloads = new Set<ManyToManyCollection<Entity, Entity>>();\n processJoinRows(em, joinTable, todo.newRows, preloads, (oldEntity, entities) => entities.push(oldEntity));\n processJoinRows(em, joinTable, todo.deletedRows, preloads, (oldEntity, entities) =>\n entities.splice(entities.indexOf(oldEntity), 1),\n );\n // Since multiple join rows could reference the same m2m, our processJoinRows doesn't actually call preload() so\n // we need to do it now\n preloads.values().forEach((r) => {\n const id = r.entity.idTagged;\n const entities = api.getPreloadedRelation(id, r.fieldName) as Entity[];\n // unique our entities in case somehow an entity was inserted by multiple join rows\n api.setPreloadedRelation(id, r.fieldName, [...new Set(entities)]);\n r.preload();\n });\n });\n }\n\n // Ensure enum m2m (EnumCollection) membership changes are propagated from the new em to the original em\n #syncEnumCollections(joinRowTodos: Record<string, JoinRowTodo>) {\n const { em } = this;\n const api = getEmInternalApi(em);\n const touched = new Set<EnumCollectionImpl<Entity, any>>();\n Object.values(joinRowTodos).forEach((todo) => {\n // Only enum m2ms; entity-to-entity m2ms are handled by `#syncManyToManys`.\n if (!todo.m2m.otherEnum) return;\n const { columnName, otherColumnName, fieldName } = todo.m2m;\n const sync = (rows: JoinRow[], add: boolean) => {\n rows.forEach((row) => {\n const newEntity = row.columns[columnName] as Entity;\n const enumId = row.columns[otherColumnName] as number;\n const oldEntity = em.findExistingInstance(newEntity.idTagged);\n if (!oldEntity || oldEntity.isDeletedEntity) return;\n const oldCollection = (oldEntity as any)[fieldName] as EnumCollectionImpl<Entity, any>;\n // Only mirror collections the original em has loaded (or new entities, whose rows are all\n // adds); otherwise a later `.load()` will read the (same-txn) db and see the change itself.\n if (!oldCollection.isLoaded && !newEntity.isNewEntity) return;\n const joinRows = api.joinRows(oldCollection);\n if (add) {\n joinRows.addPreloadedRow(oldCollection, undefined, oldEntity, enumId);\n } else {\n joinRows.removePreloadedRow(oldCollection, oldEntity, enumId);\n }\n touched.add(oldCollection);\n });\n };\n sync(todo.newRows, true);\n sync(todo.deletedRows, false);\n });\n // Re-publish each touched collection's codes and mark it loaded.\n touched.forEach((oldCollection) => {\n const joinRows = api.joinRows(oldCollection);\n const codes = (joinRows.getOthers(oldCollection.columnName, oldCollection.entity) as number[]).map(\n (id) => oldCollection.otherEnum!.findById(id)!.code,\n );\n api.setPreloadedRelation(oldCollection.entity.idTagged, oldCollection.fieldName, codes);\n oldCollection.preload();\n });\n }\n\n // Make sure any unloaded relations on net new entities are marked as loaded. We should have already preloaded all\n // concrete references in a previous step. So we just need to load collections (plus o2os) if they haven't already\n // been preloaded by another step. Since nothing else preloaded them, they must have no data so we can just set\n // them to an empty array.\n #preloadUnloadedRelations(entityTodos: Record<string, Todo>) {\n const { em } = this;\n Object.entries(entityTodos).forEach(([, todo]) => {\n todo.inserts.forEach((newEntity) => {\n const oldEntity = em.findExistingInstance(newEntity.idTagged)!;\n getCollections(oldEntity)\n .filter((r) => !r.isPreloaded)\n .forEach((r) => {\n getEmInternalApi(em).setPreloadedRelation(oldEntity.idTagged, r.fieldName, []);\n r.preload();\n });\n });\n });\n }\n}\n\n// References that represent one (or more) foreign keys directly on the entity\ntype ConcreteReference =\n | ManyToOneReferenceImpl<Entity, Entity, any>\n | ReactiveReferenceImpl<Entity, Entity, any, any>\n | PolymorphicReferenceImpl<Entity, Entity, any>;\n\n/** Preloads a mirrored lazy value without treating the database write as a test-entity mutation. */\nfunction preloadLazyField(entity: Entity, fieldName: string): void {\n const field = (entity as any)[fieldName] as LazyFieldImpl<Entity, unknown>;\n field.preload();\n}\n\nfunction preloadReference(em: EntityManager, fieldName: string, entity: Entity, other: Entity | undefined) {\n // Preload for concrete references doesn't make us of the global preloaded cache. It just looks at its owner's data\n // to get a key, then checks if that entity is already in the em and uses that if so. So we just need to set the\n // owner's data to have the correct key then we can call preload since all entities should already be in the original\n // em\n const reference = (entity as any)[fieldName] as ConcreteReference;\n (reference as any).unload();\n const { data } = getInstanceData(entity);\n data[fieldName] = other?.idTagged;\n if (!entity.isDeletedEntity) reference.preload();\n}\n\nfunction getOtherSide(fieldName: string, entity: Entity, other: Entity) {\n const meta = getMetadata(entity);\n const metas = new Set([meta, ...meta.baseTypes]);\n return getRelations(other)\n .values()\n .filter((r) => r instanceof OneToOneReferenceImpl || r instanceof OneToManyCollection)\n .find((r) => metas.has(r.otherMeta) && r.otherFieldName === fieldName);\n}\n\nfunction maybePreloadOtherSide(\n em: EntityManager,\n fieldName: string,\n entity: Entity,\n other: Entity,\n remove: boolean = false,\n): void {\n const meta = getMetadata(entity);\n const otherSide = getOtherSide(fieldName, entity, other);\n // If our other side is a large o2m then we can just ignore it, since it isn't loadable regardless\n if (!otherSide) return;\n // Since our preload scans the entire em, we only need to run it once per collection/o2o. We clear out preloads\n // when we start the afterWrite, so if anything is preloaded we know we put it there.\n if (otherSide.isPreloaded) return;\n let otherEntities: Entity[];\n if (otherSide instanceof OneToOneReferenceImpl) {\n otherEntities = remove ? [] : [entity];\n } else if (otherSide instanceof OneToManyCollection) {\n // Scan the entire em for (non-deleted) entities of our type that reference the other side\n otherEntities = em\n .getEntities(meta.cstr)\n .values()\n .filter((e) => !e.isDeletedEntity)\n .filter((e) => ((e as any)[otherSide.otherFieldName] as ConcreteReference).idTaggedMaybe === other.idTagged)\n .toArray();\n } else {\n assertNever(otherSide);\n }\n // We don't need to worry about order here because o2ms do their own sorting internally and o2os are inherently\n // unordered (since it's just a single entity)\n getEmInternalApi(em).setPreloadedRelation(other.idTagged, otherSide.fieldName, otherEntities);\n otherSide.preload();\n // This method is confusingly name, what it actually does is clear out the sorted caches that m2m uses to avoid\n // sorting on every `.get` call. Since we just preloaded the collection with new data, we know these caches need to\n // be cleared\n if (otherSide instanceof OneToManyCollection) otherSide.resetIsLoaded();\n}\n\nfunction processJoinRows(\n em: EntityManager,\n joinTable: string,\n rows: JoinRow[],\n preloads: Set<ManyToManyCollection<Entity, Entity>>,\n callback: (oldEntity: Entity, entities: Entity[]) => void,\n) {\n const api = getEmInternalApi(em);\n rows.forEach((row) => {\n // A join row's `columns` is always an object with two key/value pairs, one for each side of the m2m.\n // This only runs for entity-to-entity m2ms (enum m2ms are skipped by the caller), so both are entities.\n const [[col1, e1], [col2, e2]] = Object.entries(row.columns) as [[string, Entity], [string, Entity]];\n (\n [\n // Each join row needs to append/remove from both sides of the m2m. So we need to do the same operation once for\n // each side.\n [col1, e1, e2],\n [col2, e2, e1],\n ] as const\n ).forEach(([column, newEntity, newOther]) => {\n const { idTagged: id } = newEntity;\n const oldEntity = em.findExistingInstance(id)!;\n if (oldEntity.isDeletedEntity) return;\n const newM2m = getManyToManys(newEntity).find((r) => r.joinTableName === joinTable && r.columnName === column)!;\n const { fieldName } = newM2m;\n const oldM2m = (oldEntity as any)[fieldName] as ManyToManyCollection<Entity, Entity>;\n let entities: Entity[];\n if (preloads.has(oldM2m)) {\n // If a previous join row has already preloaded this relation, then we can just append/remove from the\n // existing preloaded data.\n entities = api.getPreloadedRelation(id, fieldName) as Entity[];\n } else if (oldM2m.isLoaded) {\n // The order here is important. M2ms are returned in the order their join rows were inserted, so we need to\n // make sure we match that behavior here. So we get all the extant rows first and then append each new row\n // in order from there. Removals are fine since they'll modify the array in place and the correct order will\n // be preserved.\n entities = oldM2m.get;\n } else if (newEntity.isNewEntity) {\n // If this is a new entity, then all the join rows referencing it must also be inserts. So we can just use an\n // empty array initially and inserts will be appended to it.\n entities = [];\n } else {\n fail(`Expected ${fieldName} to be loaded`);\n }\n callback(em.findExistingInstance(newOther.idTagged)!, entities);\n api.setPreloadedRelation(id, fieldName, entities);\n // We only need to preload the m2m once, but multiple join rows could reference it, so we keep track of which m2ms\n // need to be preloaded as we work, then actually call preload() at the end once we're done.\n preloads.add(oldM2m);\n });\n });\n}\n\n/** Returns the lazy columns on an entity, including inherited fields. */\nfunction getLazyFields(entity: Entity): IteratorObject<PrimitiveField> {\n return Object.values(getMetadata(entity).allFields)\n .values()\n .filter((field) => field.kind === \"primitive\")\n .filter((field) => !!field.lazy);\n}\n\nfunction getReferences(entity: Entity): IteratorObject<ConcreteReference> {\n return getRelations(entity)\n .values()\n .filter(\n (r) =>\n r instanceof ManyToOneReferenceImpl ||\n r instanceof ReactiveReferenceImpl ||\n r instanceof PolymorphicReferenceImpl,\n );\n}\n\nfunction getManyToManys(\n entity: Entity,\n): IteratorObject<\n | ManyToManyCollection<Entity, Entity>\n | ReactiveManyToManyImpl<any, any, any>\n | ReactiveManyToManyOtherSideImpl<any, any>\n> {\n return getRelations(entity)\n .values()\n .filter(\n (r) =>\n r instanceof ManyToManyCollection ||\n r instanceof ReactiveManyToManyImpl ||\n r instanceof ReactiveManyToManyOtherSideImpl,\n );\n}\n\nfunction getCollections(entity: Entity) {\n return getRelations(entity)\n .values()\n .filter(\n (r) =>\n r instanceof ManyToManyCollection ||\n r instanceof OneToManyCollection ||\n r instanceof OneToOneReferenceImpl ||\n r instanceof ReactiveManyToManyImpl ||\n r instanceof ReactiveManyToManyOtherSideImpl ||\n r instanceof EnumCollectionImpl,\n );\n}\n"],"mappings":";;;AAoCA,IA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QAKA;CACA,QAAA,GAAOJ,WAAAA,aAAAA,CAAa,MAAM,CAAC,CACxB,OAAO,CAAC,CACR,QACE,MACC,aAAaK,WAAAA,wBACb,aAAaC,WAAAA,0BACb,aAAaC,WAAAA,+BACjB;AACJ;AAEA,SAAS,eAAe,QAAgB;CACtC,QAAA,GAAOP,WAAAA,aAAAA,CAAa,MAAM,CAAC,CACxB,OAAO,CAAC,CACR,QACE,MACC,aAAaK,WAAAA,wBACb,aAAaP,WAAAA,uBACb,aAAaG,WAAAA,yBACb,aAAaK,WAAAA,0BACb,aAAaC,WAAAA,mCACb,aAAaC,WAAAA,kBACjB;AACJ"}
|
|
1
|
+
{"version":3,"file":"RunPlugin.cjs","names":["Plugin","getEmInternalApi","createRowFromEntityData","getInstanceData","getMetadata","PolymorphicKeySerde","PojoRowData","OneToManyCollection","isEntity","getRelations","OneToOneReferenceImpl","ReactiveManyToManyImpl","ManyToOneReferenceImpl","ReactiveReferenceImpl","PolymorphicReferenceImpl","ManyToManyCollection","ReactiveManyToManyOtherSideImpl","EnumCollectionImpl"],"sources":["../src/RunPlugin.ts"],"sourcesContent":["import {\n type Entity,\n type EntityManager,\n EnumCollectionImpl,\n type FieldColumn,\n type JoinRow,\n type JoinRowTodo,\n type LazyFieldImpl,\n ManyToManyCollection,\n ManyToOneReferenceImpl,\n type MaybeAbstractEntityConstructor,\n OneToManyCollection,\n OneToOneReferenceImpl,\n Plugin,\n PojoRowData,\n PolymorphicKeySerde,\n PolymorphicReferenceImpl,\n type PrimitiveField,\n ReactiveManyToManyImpl,\n ReactiveManyToManyOtherSideImpl,\n ReactiveReferenceImpl,\n type Todo,\n assertNever,\n createRowFromEntityData,\n fail,\n getEmInternalApi,\n getInstanceData,\n getMetadata,\n getRelations,\n isEntity,\n} from \"joist-core\";\n\n/*\n * `run...` helpers use this plugin to mirror changes from their isolated entity manager to the original\n * entity manager they were passed. This allows changes to be synchronously reflected between the two entity managers\n * without needing to query the database.\n */\nexport class RunPlugin extends Plugin {\n // The em passed here is the target which we'll mirror changes to. The plugin should not be added to it, but rather\n // to the new em `run..` helpers create or by downstream ems created by implementation code\n constructor(public readonly em: EntityManager) {\n super();\n }\n\n maybeCloneForNewEm() {\n return this;\n }\n\n afterWrite(entityTodos: Record<string, Todo>, joinRowTodos: Record<string, JoinRowTodo>) {\n const api = getEmInternalApi(this.em);\n // We don't want any old preloaded data leaking into our changes, so we clear it out before we start as a precaution\n api.clearPreloadedRelations();\n // Likewise, any data loaders in memory will have stale data, so we clear them out as well\n api.clearDataloaders();\n this.#syncEntityData(entityTodos);\n this.#syncReferencesAndLazyFields(entityTodos);\n this.#syncManyToManys(joinRowTodos);\n this.#syncEnumCollections(joinRowTodos);\n this.#preloadUnloadedRelations(entityTodos);\n }\n\n #syncEntityData(entityTodos: Record<string, Todo>) {\n const { em } = this;\n const api = getEmInternalApi(em);\n const { isLoadedCache } = api;\n Object.values(entityTodos).forEach((todo) => {\n todo.inserts.forEach((newEntity) => {\n const row = createRowFromEntityData(newEntity, { preferOriginalData: false });\n em.hydrate(newEntity.constructor as MaybeAbstractEntityConstructor<Entity>, [row]);\n });\n todo.updates.forEach((newEntity) => {\n const oldEntity = api.getEntity(newEntity.idTagged);\n // This fail shouldn't really be possible, since any entity should either have been created:\n // * via factory or em.create in the original em\n // * by the new em inside the `run` call, in which case it'd be an insert instead of an update\n // * by a previous afterWrite call, which would have already been an insert and added to the original em\n // * by a previous `run` call, which would have already been an insert as well\n if (!oldEntity) fail(`Expected to find existing entity for $ {e.idTagged}`);\n const { data: newData } = getInstanceData(newEntity);\n const { data: oldData } = getInstanceData(oldEntity);\n const meta = getMetadata(newEntity);\n (newEntity as any).changes.fields\n .values()\n .filter((fieldName: string) => fieldName in newData)\n .filter((fieldName: string) => !!meta.allFields[fieldName].serde)\n .forEach((fieldName: string) => {\n // We're imitating a round trip to the database here, so we use our field's serde to map the value back\n // and forth.\n const serde = meta.allFields[fieldName].serde!;\n let column: FieldColumn | undefined;\n let value: any;\n if (serde instanceof PolymorphicKeySerde) {\n [column, value] =\n serde.columns\n .values()\n .map((c) => [c, c.rowValue(newData)] as const)\n .find(([, v]) => v !== undefined) ?? [];\n } else {\n [column] = serde.columns;\n // the 2nd and 3rd arguments are only used if the 4th argument is defined, so it's OK to pass undefined here\n value = column.rowValue(newData);\n }\n oldData[fieldName] = serde.fromRow(new PojoRowData([column ? { [column.columnName]: value } : {}]), 0);\n // Mirror setField's cache invalidation for these changed fields without making test entities dirty\n // or queuing business reactions that the isolated EM has already processed.\n isLoadedCache.resetIsLoaded(oldEntity, fieldName);\n });\n });\n todo.deletes.forEach((newEntity) => {\n const existing = api.getEntity(newEntity.idTagged);\n // Again it shouldn't really be possible for a deletion to happen without the entity being present in the\n // original em, but just in case we'll skip if we don't find it\n if (!existing) fail(`Expected to find existing entity for $ {e.idTagged}`);\n getInstanceData(existing).markDeletedBecauseNotFound();\n });\n });\n }\n\n // Ensure m2o/rr/poly changes and known lazy values from the new em are loaded in the original em\n // Run is a test helper, so we eagerly preload the test entity graph for synchronous assertions. LazyFields are an\n // exception: they can hold large payloads, so we only preload values known from inserts or changed columns. Untouched\n // lazy fields keep their loaded state instead of fetching data that the production code did not write.\n #syncReferencesAndLazyFields(entityTodos: Record<string, Todo>) {\n const { em } = this;\n const api = getEmInternalApi(em);\n Object.values(entityTodos).forEach((todo) => {\n todo.inserts.forEach((newEntity) => {\n const oldEntity = api.getEntity(newEntity.idTagged)!;\n // New entities always have the lazy field's initial value, so always preload them\n for (const field of getLazyFields(newEntity)) {\n preloadLazyField(oldEntity, field.fieldName);\n }\n\n getReferences(newEntity).forEach((r) => {\n const other = r.isSet ? api.getEntity(r.idTaggedMaybe) : undefined;\n preloadReference(em, r.fieldName, oldEntity, other);\n if (other) maybePreloadOtherSide(em, r.fieldName, oldEntity, other);\n });\n });\n todo.updates.forEach((newEntity) => {\n const oldEntity = api.getEntity(newEntity.idTagged)!;\n // Only preload lazy fields that were actively changed, b/c they're already available/loaded in the entity we're copying from\n for (const field of getLazyFields(newEntity)) {\n if ((newEntity as any).changes[field.fieldName].hasChanged) {\n preloadLazyField(oldEntity, field.fieldName);\n }\n }\n\n getReferences(newEntity)\n .filter((r) => (newEntity as any).changes[r.fieldName].hasChanged)\n .forEach((r) => {\n const other = r.isSet ? api.getEntity(r.idTaggedMaybe) : undefined;\n preloadReference(em, r.fieldName, oldEntity, other);\n if (other) maybePreloadOtherSide(em, r.fieldName, oldEntity, other);\n const originalId = (newEntity as any).changes[r.fieldName].originalValue;\n const originalOther = originalId ? api.getEntity(originalId) : undefined;\n if (originalOther) maybePreloadOtherSide(em, r.fieldName, oldEntity, originalOther, true);\n });\n // If the entity was soft-deleted, then we need to clear out the caches for any o2m collections that contain it\n if (\"deletedAt\" in newEntity && (newEntity as any).changes.deletedAt.hasChanged) {\n getReferences(oldEntity).forEach((r) => {\n const other = r.isSet ? api.getEntity(r.idTaggedMaybe) : undefined;\n if (other) {\n const otherSide = getOtherSide(r.fieldName, oldEntity, other);\n if (otherSide instanceof OneToManyCollection) otherSide.resetIsLoaded();\n }\n });\n }\n });\n todo.deletes.forEach((newEntity) => {\n const oldEntity = api.getEntity(newEntity.idTagged)!;\n getReferences(newEntity).forEach((r) => {\n // Since both old and new entities are marked as deleted, we can't just go looking through their relations\n // for the other side's id since this will error. Likewise, we only care about the collection that this\n // entity used to belong to in the old em so we can remove it. So just grab the other id directly from the\n // old entity's data and then preload its other side relation.\n const entityOrId = getInstanceData(oldEntity).data[r.fieldName];\n const originalOther = entityOrId\n ? api.getEntity(isEntity(entityOrId) ? entityOrId.idTagged : entityOrId)\n : undefined;\n if (originalOther) maybePreloadOtherSide(em, r.fieldName, oldEntity, originalOther, true);\n });\n });\n });\n }\n\n // Ensure m2m changes are propagated from the new em to the original em\n #syncManyToManys(joinRowTodos: Record<string, JoinRowTodo>) {\n const { em } = this;\n const api = getEmInternalApi(em);\n Object.entries(joinRowTodos).forEach(([joinTable, todo]) => {\n // Enum m2ms (EnumCollections) aren't entity-to-entity, so there's nothing to mirror between ems.\n if (todo.m2m.otherEnum) return;\n const toPreload = new Set<ManyToMany>();\n processJoinRows(em, joinTable, todo.newRows, toPreload, true);\n processJoinRows(em, joinTable, todo.deletedRows, toPreload, false);\n // Since multiple join rows could reference the same m2m, our processJoinRows doesn't actually call preload() so\n // we need to do it now\n toPreload.values().forEach((r) => {\n const id = r.entity.idTagged;\n const entities = api.getPreloadedRelation(id, r.fieldName) as Entity[];\n // unique our entities in case somehow an entity was inserted by multiple join rows\n api.setPreloadedRelation(id, r.fieldName, [...new Set(entities)]);\n r.preload();\n });\n });\n }\n\n // Ensure enum m2m (EnumCollection) membership changes are propagated from the new em to the original em\n #syncEnumCollections(joinRowTodos: Record<string, JoinRowTodo>) {\n const { em } = this;\n const api = getEmInternalApi(em);\n const touched = new Set<EnumCollectionImpl<Entity, any>>();\n Object.values(joinRowTodos).forEach((todo) => {\n // Only enum m2ms; entity-to-entity m2ms are handled by `#syncManyToManys`.\n if (!todo.m2m.otherEnum) return;\n const { columnName, otherColumnName, fieldName } = todo.m2m;\n const sync = (rows: JoinRow[], add: boolean) => {\n rows.forEach((row) => {\n const newEntity = row.columns[columnName] as Entity;\n const enumId = row.columns[otherColumnName] as number;\n const oldEntity = api.getEntity(newEntity.idTagged);\n if (!oldEntity || oldEntity.isDeletedEntity) return;\n const oldCollection = (oldEntity as any)[fieldName] as EnumCollectionImpl<Entity, any>;\n // Only mirror collections the original em has loaded (or new entities, whose rows are all\n // adds); otherwise a later `.load()` will read the (same-txn) db and see the change itself.\n if (!oldCollection.isLoaded && !newEntity.isNewEntity) return;\n const joinRows = api.joinRows(oldCollection);\n if (add) {\n joinRows.addPreloadedRow(oldCollection, undefined, oldEntity, enumId);\n } else {\n joinRows.removePreloadedRow(oldCollection, oldEntity, enumId);\n }\n touched.add(oldCollection);\n });\n };\n sync(todo.newRows, true);\n sync(todo.deletedRows, false);\n });\n // Re-publish each touched collection's codes and mark it loaded.\n touched.forEach((oldCollection) => {\n const joinRows = api.joinRows(oldCollection);\n const codes = (joinRows.getOthers(oldCollection.columnName, oldCollection.entity) as number[]).map(\n (id) => oldCollection.otherEnum!.findById(id)!.code,\n );\n api.setPreloadedRelation(oldCollection.entity.idTagged, oldCollection.fieldName, codes);\n oldCollection.preload();\n });\n }\n\n // Make sure any unloaded relations on net new entities are marked as loaded. We should have already preloaded all\n // concrete references in a previous step. So we just need to load collections (plus o2os) if they haven't already\n // been preloaded by another step. Since nothing else preloaded them, they must have no data so we can just set\n // them to an empty array.\n #preloadUnloadedRelations(entityTodos: Record<string, Todo>) {\n const { em } = this;\n const api = getEmInternalApi(em);\n Object.entries(entityTodos).forEach(([, todo]) => {\n todo.inserts.forEach((newEntity) => {\n const oldEntity = api.getEntity(newEntity.idTagged)!;\n getCollections(oldEntity)\n .filter((r) => !r.isLoaded && !r.isPreloaded)\n .forEach((r) => {\n api.setPreloadedRelation(oldEntity.idTagged, r.fieldName, []);\n r.preload();\n });\n });\n });\n }\n}\n\n// References that represent one (or more) foreign keys directly on the entity\ntype ConcreteReference =\n | ManyToOneReferenceImpl<Entity, Entity, any>\n | ReactiveReferenceImpl<Entity, Entity, any, any>\n | PolymorphicReferenceImpl<Entity, Entity, any>;\n\ntype ManyToMany =\n | ManyToManyCollection<Entity, Entity>\n | ReactiveManyToManyImpl<any, any, any>\n | ReactiveManyToManyOtherSideImpl<any, any>;\n\n/** Preloads a mirrored lazy value without treating the database write as a test-entity mutation. */\nfunction preloadLazyField(entity: Entity, fieldName: string): void {\n const field = (entity as any)[fieldName] as LazyFieldImpl<Entity, unknown>;\n field.preload();\n}\n\nfunction preloadReference(em: EntityManager, fieldName: string, entity: Entity, other: Entity | undefined) {\n // Preload for concrete references doesn't make us of the global preloaded cache. It just looks at its owner's data\n // to get a key, then checks if that entity is already in the em and uses that if so. So we just need to set the\n // owner's data to have the correct key then we can call preload since all entities should already be in the original\n // em\n const reference = (entity as any)[fieldName] as ConcreteReference;\n (reference as any).unload();\n const { data } = getInstanceData(entity);\n data[fieldName] = other?.idTagged;\n if (!entity.isDeletedEntity) reference.preload();\n}\n\nfunction getOtherSide(fieldName: string, entity: Entity, other: Entity) {\n const meta = getMetadata(entity);\n const metas = new Set([meta, ...meta.baseTypes]);\n return getRelations(other)\n .values()\n .filter((r) => r instanceof OneToOneReferenceImpl || r instanceof OneToManyCollection)\n .find((r) => metas.has(r.otherMeta) && r.otherFieldName === fieldName);\n}\n\nfunction maybePreloadOtherSide(\n em: EntityManager,\n fieldName: string,\n entity: Entity,\n other: Entity,\n remove: boolean = false,\n): void {\n const meta = getMetadata(entity);\n const otherSide = getOtherSide(fieldName, entity, other);\n // If our other side is a large o2m then we can just ignore it, since it isn't loadable regardless\n if (!otherSide) return;\n // Since our preload scans the entire em, we only need to run it once per collection/o2o. We clear out preloads\n // when we start the afterWrite, so if anything is preloaded we know we put it there.\n if (otherSide.isPreloaded) return;\n let otherEntities: Entity[];\n if (otherSide instanceof OneToOneReferenceImpl) {\n otherEntities = remove ? [] : [entity];\n } else if (otherSide instanceof OneToManyCollection) {\n // Scan the entire em for (non-deleted) entities of our type that reference the other side\n otherEntities = em\n .getEntities(meta.cstr)\n .values()\n .filter((e) => !e.isDeletedEntity)\n .filter((e) => ((e as any)[otherSide.otherFieldName] as ConcreteReference).idTaggedMaybe === other.idTagged)\n .toArray();\n } else {\n assertNever(otherSide);\n }\n // We don't need to worry about order here because o2ms do their own sorting internally and o2os are inherently\n // unordered (since it's just a single entity)\n getEmInternalApi(em).setPreloadedRelation(other.idTagged, otherSide.fieldName, otherEntities);\n otherSide.preload();\n // This method is confusingly name, what it actually does is clear out the sorted caches that m2m uses to avoid\n // sorting on every `.get` call. Since we just preloaded the collection with new data, we know these caches need to\n // be cleared\n if (otherSide instanceof OneToManyCollection) otherSide.resetIsLoaded();\n}\n\n/**\n * Mirrors persisted join rows without deriving membership or queuing fixture reactions.\n *\n * Used only by RunPlugin to apply changes that the isolated EntityManager has already flushed.\n * Factory-created collections have complete membership in their index; other unloaded collections stay unloaded.\n */\nfunction processJoinRows(\n em: EntityManager,\n joinTable: string,\n rows: JoinRow[],\n toPreload: Set<ManyToMany>,\n add: boolean,\n): void {\n const api = getEmInternalApi(em);\n for (const row of rows) {\n // A join row's `columns` is always an object with two key/value pairs, one for each side of the m2m.\n // This only runs for entity-to-entity m2ms (enum m2ms are skipped by the caller), so both are entities.\n const [[col1, e1], [col2, e2]] = Object.entries(row.columns) as [[string, Entity], [string, Entity]];\n for (const [column, newEntity, newOther] of [\n // Each join row needs to append/remove from both sides of the m2m. So we need to do the same operation once for\n // each side.\n [col1, e1, e2],\n [col2, e2, e1],\n ] as const) {\n const { idTagged: id } = newEntity;\n const oldEntity = api.getEntity(id)!;\n if (oldEntity.isDeletedEntity) continue;\n const newM2m = getManyToManys(newEntity).find((r) => r.joinTableName === joinTable && r.columnName === column)!;\n const { fieldName } = newM2m;\n const oldM2m = (oldEntity as any)[fieldName] as ManyToMany;\n const oldOther = api.getEntity(newOther.idTagged)!;\n const joinRows = api.joinRows(oldM2m);\n let entities: Entity[] | undefined;\n if (toPreload.has(oldM2m)) {\n // If a previous join row has already preloaded this relation, then we can just append/remove from the\n // existing preloaded data.\n entities = api.getPreloadedRelation(id, fieldName) as Entity[];\n } else if (oldM2m.isLoaded) {\n // The order here is important. M2ms are returned in the order their join rows were inserted, so we need to\n // make sure we match that behavior here. So we get all the extant rows first and then append each new row\n // in order from there. Removals are fine since they'll modify the array in place and the correct order will\n // be preserved.\n // A reactive collection's getter can derive again; mirroring must use its stored value instead.\n entities = [...(oldM2m instanceof ReactiveManyToManyImpl ? oldM2m.current() : oldM2m.getWithDeleted)];\n } else if (newEntity.isNewEntity || getInstanceData(oldEntity).isOrWasNew) {\n // Newly-created mirrored entities have no membership from before this run. Factory-created entities\n // can also have an unloaded reactive inverse; their index contains all membership since creation.\n entities = joinRows.getOthers(column, oldEntity) as Entity[];\n }\n\n // Forked collections can have a stored snapshot without join rows in this EM. Seed that snapshot\n // before applying the mirrored delta so later derivations see the complete persisted membership.\n if (entities && !toPreload.has(oldM2m)) {\n for (const entity of entities) joinRows.addPreloadedRow(oldM2m, undefined, oldEntity, entity);\n }\n // Replace any completed deletion tombstone when a later run re-adds the same membership.\n joinRows.removePreloadedRow(oldM2m, oldEntity, oldOther);\n if (add) joinRows.addPreloadedRow(oldM2m, row.id, oldEntity, oldOther);\n api.isLoadedCache.resetIsLoaded(oldEntity, fieldName);\n\n // A persisted, unloaded collection may have other members we do not know about. Keep it unloaded;\n // its next load will read the full membership from the database instead of publishing a partial list.\n if (!entities) continue;\n if (add) {\n if (!entities.includes(oldOther)) entities.push(oldOther);\n } else {\n const index = entities.indexOf(oldOther);\n if (index !== -1) entities.splice(index, 1);\n }\n api.setPreloadedRelation(id, fieldName, entities);\n // We only need to preload the m2m once, but multiple join rows could reference it, so we keep track of which m2ms\n // need to be preloaded as we work, then actually call preload() at the end once we're done.\n toPreload.add(oldM2m);\n }\n }\n}\n\n/** Returns the lazy columns on an entity, including inherited fields. */\nfunction getLazyFields(entity: Entity): IteratorObject<PrimitiveField> {\n return Object.values(getMetadata(entity).allFields)\n .values()\n .filter((field) => field.kind === \"primitive\")\n .filter((field) => !!field.lazy);\n}\n\nfunction getReferences(entity: Entity): IteratorObject<ConcreteReference> {\n return getRelations(entity)\n .values()\n .filter(\n (r) =>\n r instanceof ManyToOneReferenceImpl ||\n r instanceof ReactiveReferenceImpl ||\n r instanceof PolymorphicReferenceImpl,\n );\n}\n\nfunction getManyToManys(entity: Entity): IteratorObject<ManyToMany> {\n return getRelations(entity)\n .values()\n .filter(\n (r) =>\n r instanceof ManyToManyCollection ||\n r instanceof ReactiveManyToManyImpl ||\n r instanceof ReactiveManyToManyOtherSideImpl,\n );\n}\n\nfunction getCollections(entity: Entity) {\n return getRelations(entity)\n .values()\n .filter(\n (r) =>\n r instanceof ManyToManyCollection ||\n r instanceof OneToManyCollection ||\n r instanceof OneToOneReferenceImpl ||\n r instanceof ReactiveManyToManyImpl ||\n r instanceof ReactiveManyToManyOtherSideImpl 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|
export declare class RunPlugin extends Plugin {
|
|
4
3
|
#private;
|
|
5
4
|
readonly em: EntityManager;
|
|
@@ -7,5 +6,4 @@ export declare class RunPlugin extends Plugin {
|
|
|
7
6
|
maybeCloneForNewEm(): this;
|
|
8
7
|
afterWrite(entityTodos: Record<string, Todo>, joinRowTodos: Record<string, JoinRowTodo>): void;
|
|
9
8
|
}
|
|
10
|
-
//#endregion
|
|
11
9
|
//# sourceMappingURL=RunPlugin.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RunPlugin.d.cts","names":[],"sources":["../src/RunPlugin.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"RunPlugin.d.cts","names":[],"sources":["../src/RunPlugin.ts"],"mappings":";qBAqCa,kBAAkB;;WAGD,IAAI;EAAhC,YAA4B,IAAI;EAIhC;EAIA,WAAW,aAAa,eAAe,OAAO,cAAc,eAAe"}
|
package/build/RunPlugin.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { EntityManager, JoinRowTodo, Plugin, Todo } from "joist-core";
|
|
2
|
-
//#region src/RunPlugin.d.ts
|
|
3
2
|
export declare class RunPlugin extends Plugin {
|
|
4
3
|
#private;
|
|
5
4
|
readonly em: EntityManager;
|
|
@@ -7,5 +6,4 @@ export declare class RunPlugin extends Plugin {
|
|
|
7
6
|
maybeCloneForNewEm(): this;
|
|
8
7
|
afterWrite(entityTodos: Record<string, Todo>, joinRowTodos: Record<string, JoinRowTodo>): void;
|
|
9
8
|
}
|
|
10
|
-
//#endregion
|
|
11
9
|
//# sourceMappingURL=RunPlugin.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RunPlugin.d.mts","names":[],"sources":["../src/RunPlugin.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"RunPlugin.d.mts","names":[],"sources":["../src/RunPlugin.ts"],"mappings":";qBAqCa,kBAAkB;;WAGD,IAAI;EAAhC,YAA4B,IAAI;EAIhC;EAIA,WAAW,aAAa,eAAe,OAAO,cAAc,eAAe"}
|
package/build/RunPlugin.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { EnumCollectionImpl, ManyToManyCollection, ManyToOneReferenceImpl, OneToManyCollection, OneToOneReferenceImpl, Plugin, PojoRowData, PolymorphicKeySerde, PolymorphicReferenceImpl, ReactiveManyToManyImpl, ReactiveManyToManyOtherSideImpl, ReactiveReferenceImpl, assertNever, createRowFromEntityData, getEmInternalApi, getInstanceData, getMetadata, getRelations, isEntity } from "joist-core";
|
|
2
|
-
|
|
1
|
+
import { EnumCollectionImpl, ManyToManyCollection, ManyToOneReferenceImpl, OneToManyCollection, OneToOneReferenceImpl, Plugin, PojoRowData, PolymorphicKeySerde, PolymorphicReferenceImpl, ReactiveManyToManyImpl, ReactiveManyToManyOtherSideImpl, ReactiveReferenceImpl, assertNever, createRowFromEntityData, fail, getEmInternalApi, getInstanceData, getMetadata, getRelations, isEntity } from "joist-core";
|
|
2
|
+
// src/RunPlugin.ts
|
|
3
3
|
var RunPlugin = class extends Plugin {
|
|
4
4
|
em;
|
|
5
5
|
constructor(em) {
|
|
@@ -21,14 +21,15 @@ var RunPlugin = class extends Plugin {
|
|
|
21
21
|
}
|
|
22
22
|
#syncEntityData(entityTodos) {
|
|
23
23
|
const { em } = this;
|
|
24
|
-
const
|
|
24
|
+
const api = getEmInternalApi(em);
|
|
25
|
+
const { isLoadedCache } = api;
|
|
25
26
|
Object.values(entityTodos).forEach((todo) => {
|
|
26
27
|
todo.inserts.forEach((newEntity) => {
|
|
27
28
|
const row = createRowFromEntityData(newEntity, { preferOriginalData: false });
|
|
28
29
|
em.hydrate(newEntity.constructor, [row]);
|
|
29
30
|
});
|
|
30
31
|
todo.updates.forEach((newEntity) => {
|
|
31
|
-
const oldEntity =
|
|
32
|
+
const oldEntity = api.getEntity(newEntity.idTagged);
|
|
32
33
|
if (!oldEntity) fail(`Expected to find existing entity for $ {e.idTagged}`);
|
|
33
34
|
const { data: newData } = getInstanceData(newEntity);
|
|
34
35
|
const { data: oldData } = getInstanceData(oldEntity);
|
|
@@ -47,7 +48,7 @@ var RunPlugin = class extends Plugin {
|
|
|
47
48
|
});
|
|
48
49
|
});
|
|
49
50
|
todo.deletes.forEach((newEntity) => {
|
|
50
|
-
const existing =
|
|
51
|
+
const existing = api.getEntity(newEntity.idTagged);
|
|
51
52
|
if (!existing) fail(`Expected to find existing entity for $ {e.idTagged}`);
|
|
52
53
|
getInstanceData(existing).markDeletedBecauseNotFound();
|
|
53
54
|
});
|
|
@@ -55,29 +56,30 @@ var RunPlugin = class extends Plugin {
|
|
|
55
56
|
}
|
|
56
57
|
#syncReferencesAndLazyFields(entityTodos) {
|
|
57
58
|
const { em } = this;
|
|
59
|
+
const api = getEmInternalApi(em);
|
|
58
60
|
Object.values(entityTodos).forEach((todo) => {
|
|
59
61
|
todo.inserts.forEach((newEntity) => {
|
|
60
|
-
const oldEntity =
|
|
62
|
+
const oldEntity = api.getEntity(newEntity.idTagged);
|
|
61
63
|
for (const field of getLazyFields(newEntity)) preloadLazyField(oldEntity, field.fieldName);
|
|
62
64
|
getReferences(newEntity).forEach((r) => {
|
|
63
|
-
const other = r.isSet ?
|
|
65
|
+
const other = r.isSet ? api.getEntity(r.idTaggedMaybe) : void 0;
|
|
64
66
|
preloadReference(em, r.fieldName, oldEntity, other);
|
|
65
67
|
if (other) maybePreloadOtherSide(em, r.fieldName, oldEntity, other);
|
|
66
68
|
});
|
|
67
69
|
});
|
|
68
70
|
todo.updates.forEach((newEntity) => {
|
|
69
|
-
const oldEntity =
|
|
71
|
+
const oldEntity = api.getEntity(newEntity.idTagged);
|
|
70
72
|
for (const field of getLazyFields(newEntity)) if (newEntity.changes[field.fieldName].hasChanged) preloadLazyField(oldEntity, field.fieldName);
|
|
71
73
|
getReferences(newEntity).filter((r) => newEntity.changes[r.fieldName].hasChanged).forEach((r) => {
|
|
72
|
-
const other = r.isSet ?
|
|
74
|
+
const other = r.isSet ? api.getEntity(r.idTaggedMaybe) : void 0;
|
|
73
75
|
preloadReference(em, r.fieldName, oldEntity, other);
|
|
74
76
|
if (other) maybePreloadOtherSide(em, r.fieldName, oldEntity, other);
|
|
75
77
|
const originalId = newEntity.changes[r.fieldName].originalValue;
|
|
76
|
-
const originalOther = originalId ?
|
|
78
|
+
const originalOther = originalId ? api.getEntity(originalId) : void 0;
|
|
77
79
|
if (originalOther) maybePreloadOtherSide(em, r.fieldName, oldEntity, originalOther, true);
|
|
78
80
|
});
|
|
79
81
|
if ("deletedAt" in newEntity && newEntity.changes.deletedAt.hasChanged) getReferences(oldEntity).forEach((r) => {
|
|
80
|
-
const other = r.isSet ?
|
|
82
|
+
const other = r.isSet ? api.getEntity(r.idTaggedMaybe) : void 0;
|
|
81
83
|
if (other) {
|
|
82
84
|
const otherSide = getOtherSide(r.fieldName, oldEntity, other);
|
|
83
85
|
if (otherSide instanceof OneToManyCollection) otherSide.resetIsLoaded();
|
|
@@ -85,10 +87,10 @@ var RunPlugin = class extends Plugin {
|
|
|
85
87
|
});
|
|
86
88
|
});
|
|
87
89
|
todo.deletes.forEach((newEntity) => {
|
|
88
|
-
const oldEntity =
|
|
90
|
+
const oldEntity = api.getEntity(newEntity.idTagged);
|
|
89
91
|
getReferences(newEntity).forEach((r) => {
|
|
90
92
|
const entityOrId = getInstanceData(oldEntity).data[r.fieldName];
|
|
91
|
-
const originalOther = entityOrId ?
|
|
93
|
+
const originalOther = entityOrId ? api.getEntity(isEntity(entityOrId) ? entityOrId.idTagged : entityOrId) : void 0;
|
|
92
94
|
if (originalOther) maybePreloadOtherSide(em, r.fieldName, oldEntity, originalOther, true);
|
|
93
95
|
});
|
|
94
96
|
});
|
|
@@ -99,10 +101,10 @@ var RunPlugin = class extends Plugin {
|
|
|
99
101
|
const api = getEmInternalApi(em);
|
|
100
102
|
Object.entries(joinRowTodos).forEach(([joinTable, todo]) => {
|
|
101
103
|
if (todo.m2m.otherEnum) return;
|
|
102
|
-
const
|
|
103
|
-
processJoinRows(em, joinTable, todo.newRows,
|
|
104
|
-
processJoinRows(em, joinTable, todo.deletedRows,
|
|
105
|
-
|
|
104
|
+
const toPreload = /* @__PURE__ */ new Set();
|
|
105
|
+
processJoinRows(em, joinTable, todo.newRows, toPreload, true);
|
|
106
|
+
processJoinRows(em, joinTable, todo.deletedRows, toPreload, false);
|
|
107
|
+
toPreload.values().forEach((r) => {
|
|
106
108
|
const id = r.entity.idTagged;
|
|
107
109
|
const entities = api.getPreloadedRelation(id, r.fieldName);
|
|
108
110
|
api.setPreloadedRelation(id, r.fieldName, [...new Set(entities)]);
|
|
@@ -121,7 +123,7 @@ var RunPlugin = class extends Plugin {
|
|
|
121
123
|
rows.forEach((row) => {
|
|
122
124
|
const newEntity = row.columns[columnName];
|
|
123
125
|
const enumId = row.columns[otherColumnName];
|
|
124
|
-
const oldEntity =
|
|
126
|
+
const oldEntity = api.getEntity(newEntity.idTagged);
|
|
125
127
|
if (!oldEntity || oldEntity.isDeletedEntity) return;
|
|
126
128
|
const oldCollection = oldEntity[fieldName];
|
|
127
129
|
if (!oldCollection.isLoaded && !newEntity.isNewEntity) return;
|
|
@@ -142,11 +144,12 @@ var RunPlugin = class extends Plugin {
|
|
|
142
144
|
}
|
|
143
145
|
#preloadUnloadedRelations(entityTodos) {
|
|
144
146
|
const { em } = this;
|
|
147
|
+
const api = getEmInternalApi(em);
|
|
145
148
|
Object.entries(entityTodos).forEach(([, todo]) => {
|
|
146
149
|
todo.inserts.forEach((newEntity) => {
|
|
147
|
-
const oldEntity =
|
|
148
|
-
getCollections(oldEntity).filter((r) => !r.isPreloaded).forEach((r) => {
|
|
149
|
-
|
|
150
|
+
const oldEntity = api.getEntity(newEntity.idTagged);
|
|
151
|
+
getCollections(oldEntity).filter((r) => !r.isLoaded && !r.isPreloaded).forEach((r) => {
|
|
152
|
+
api.setPreloadedRelation(oldEntity.idTagged, r.fieldName, []);
|
|
150
153
|
r.preload();
|
|
151
154
|
});
|
|
152
155
|
});
|
|
@@ -182,11 +185,17 @@ function maybePreloadOtherSide(em, fieldName, entity, other, remove = false) {
|
|
|
182
185
|
otherSide.preload();
|
|
183
186
|
if (otherSide instanceof OneToManyCollection) otherSide.resetIsLoaded();
|
|
184
187
|
}
|
|
185
|
-
|
|
188
|
+
/**
|
|
189
|
+
* Mirrors persisted join rows without deriving membership or queuing fixture reactions.
|
|
190
|
+
*
|
|
191
|
+
* Used only by RunPlugin to apply changes that the isolated EntityManager has already flushed.
|
|
192
|
+
* Factory-created collections have complete membership in their index; other unloaded collections stay unloaded.
|
|
193
|
+
*/
|
|
194
|
+
function processJoinRows(em, joinTable, rows, toPreload, add) {
|
|
186
195
|
const api = getEmInternalApi(em);
|
|
187
|
-
|
|
196
|
+
for (const row of rows) {
|
|
188
197
|
const [[col1, e1], [col2, e2]] = Object.entries(row.columns);
|
|
189
|
-
[[
|
|
198
|
+
for (const [column, newEntity, newOther] of [[
|
|
190
199
|
col1,
|
|
191
200
|
e1,
|
|
192
201
|
e2
|
|
@@ -194,22 +203,33 @@ function processJoinRows(em, joinTable, rows, preloads, callback) {
|
|
|
194
203
|
col2,
|
|
195
204
|
e2,
|
|
196
205
|
e1
|
|
197
|
-
]]
|
|
206
|
+
]]) {
|
|
198
207
|
const { idTagged: id } = newEntity;
|
|
199
|
-
const oldEntity =
|
|
200
|
-
if (oldEntity.isDeletedEntity)
|
|
208
|
+
const oldEntity = api.getEntity(id);
|
|
209
|
+
if (oldEntity.isDeletedEntity) continue;
|
|
201
210
|
const { fieldName } = getManyToManys(newEntity).find((r) => r.joinTableName === joinTable && r.columnName === column);
|
|
202
211
|
const oldM2m = oldEntity[fieldName];
|
|
212
|
+
const oldOther = api.getEntity(newOther.idTagged);
|
|
213
|
+
const joinRows = api.joinRows(oldM2m);
|
|
203
214
|
let entities;
|
|
204
|
-
if (
|
|
205
|
-
else if (oldM2m.isLoaded) entities = oldM2m.
|
|
206
|
-
else if (newEntity.isNewEntity) entities =
|
|
207
|
-
|
|
208
|
-
|
|
215
|
+
if (toPreload.has(oldM2m)) entities = api.getPreloadedRelation(id, fieldName);
|
|
216
|
+
else if (oldM2m.isLoaded) entities = [...oldM2m instanceof ReactiveManyToManyImpl ? oldM2m.current() : oldM2m.getWithDeleted];
|
|
217
|
+
else if (newEntity.isNewEntity || getInstanceData(oldEntity).isOrWasNew) entities = joinRows.getOthers(column, oldEntity);
|
|
218
|
+
if (entities && !toPreload.has(oldM2m)) for (const entity of entities) joinRows.addPreloadedRow(oldM2m, void 0, oldEntity, entity);
|
|
219
|
+
joinRows.removePreloadedRow(oldM2m, oldEntity, oldOther);
|
|
220
|
+
if (add) joinRows.addPreloadedRow(oldM2m, row.id, oldEntity, oldOther);
|
|
221
|
+
api.isLoadedCache.resetIsLoaded(oldEntity, fieldName);
|
|
222
|
+
if (!entities) continue;
|
|
223
|
+
if (add) {
|
|
224
|
+
if (!entities.includes(oldOther)) entities.push(oldOther);
|
|
225
|
+
} else {
|
|
226
|
+
const index = entities.indexOf(oldOther);
|
|
227
|
+
if (index !== -1) entities.splice(index, 1);
|
|
228
|
+
}
|
|
209
229
|
api.setPreloadedRelation(id, fieldName, entities);
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
}
|
|
230
|
+
toPreload.add(oldM2m);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
213
233
|
}
|
|
214
234
|
/** Returns the lazy columns on an entity, including inherited fields. */
|
|
215
235
|
function getLazyFields(entity) {
|
|
@@ -224,7 +244,6 @@ function getManyToManys(entity) {
|
|
|
224
244
|
function getCollections(entity) {
|
|
225
245
|
return getRelations(entity).values().filter((r) => r instanceof ManyToManyCollection || r instanceof OneToManyCollection || r instanceof OneToOneReferenceImpl || r instanceof ReactiveManyToManyImpl || r instanceof ReactiveManyToManyOtherSideImpl || r instanceof EnumCollectionImpl);
|
|
226
246
|
}
|
|
227
|
-
//#endregion
|
|
228
247
|
export { RunPlugin };
|
|
229
248
|
|
|
230
249
|
//# sourceMappingURL=RunPlugin.js.map
|