joist-test-utils 2.3.0-next.7 → 2.3.0-next.71

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.
Files changed (60) hide show
  1. package/build/RunPlugin.cjs +219 -0
  2. package/build/RunPlugin.cjs.map +1 -0
  3. package/build/RunPlugin.d.cts +12 -0
  4. package/build/RunPlugin.d.cts.map +1 -0
  5. package/build/RunPlugin.d.mts +12 -0
  6. package/build/RunPlugin.d.mts.map +1 -0
  7. package/build/RunPlugin.js +203 -341
  8. package/build/RunPlugin.js.map +1 -1
  9. package/build/_virtual/_rolldown/runtime.cjs +23 -0
  10. package/build/context.cjs +2 -0
  11. package/build/context.d.cts +9 -0
  12. package/build/context.d.cts.map +1 -0
  13. package/build/context.d.mts +9 -0
  14. package/build/context.d.mts.map +1 -0
  15. package/build/context.js +2 -3
  16. package/build/index.cjs +38 -0
  17. package/build/index.cjs.map +1 -0
  18. package/build/index.d.cts +52 -0
  19. package/build/index.d.cts.map +1 -0
  20. package/build/index.d.mts +52 -0
  21. package/build/index.d.mts.map +1 -0
  22. package/build/index.js +25 -41
  23. package/build/index.js.map +1 -1
  24. package/build/run.cjs +77 -0
  25. package/build/run.cjs.map +1 -0
  26. package/build/run.d.cts +17 -0
  27. package/build/run.d.cts.map +1 -0
  28. package/build/run.d.mts +17 -0
  29. package/build/run.d.mts.map +1 -0
  30. package/build/run.js +52 -76
  31. package/build/run.js.map +1 -1
  32. package/build/seed.cjs +54 -0
  33. package/build/seed.cjs.map +1 -0
  34. package/build/{seed.d.ts → seed.d.cts} +9 -6
  35. package/build/seed.d.cts.map +1 -0
  36. package/build/seed.d.mts +35 -0
  37. package/build/seed.d.mts.map +1 -0
  38. package/build/seed.js +47 -53
  39. package/build/seed.js.map +1 -1
  40. package/build/toMatchEntity.cjs +94 -0
  41. package/build/toMatchEntity.cjs.map +1 -0
  42. package/build/toMatchEntity.d.cts +28 -0
  43. package/build/toMatchEntity.d.cts.map +1 -0
  44. package/build/toMatchEntity.d.mts +28 -0
  45. package/build/toMatchEntity.d.mts.map +1 -0
  46. package/build/toMatchEntity.js +72 -129
  47. package/build/toMatchEntity.js.map +1 -1
  48. package/package.json +33 -13
  49. package/build/RunPlugin.d.ts +0 -9
  50. package/build/RunPlugin.d.ts.map +0 -1
  51. package/build/context.d.ts +0 -6
  52. package/build/context.d.ts.map +0 -1
  53. package/build/context.js.map +0 -1
  54. package/build/index.d.ts +0 -50
  55. package/build/index.d.ts.map +0 -1
  56. package/build/run.d.ts +0 -15
  57. package/build/run.d.ts.map +0 -1
  58. package/build/seed.d.ts.map +0 -1
  59. package/build/toMatchEntity.d.ts +0 -27
  60. package/build/toMatchEntity.d.ts.map +0 -1
@@ -1,356 +1,218 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RunPlugin = void 0;
4
- const joist_core_1 = require("joist-core");
5
- /*
6
- * `run...` helpers use this plugin to mirror any changes made to their isolated entity manager inside to the original
7
- * entity manager they were passed. This allows changes to be synchronously reflected between the two entity managers
8
- * without needing to query the database.
9
- */
10
- class RunPlugin extends joist_core_1.Plugin {
11
- em;
12
- // The em passed here is the target which we'll mirror changes to. The plugin should not be added to it, but rather
13
- // to the new em `run..` helpers create or by downstream ems created by implementation code
14
- constructor(em) {
15
- super();
16
- this.em = em;
17
- }
18
- maybeCloneForNewEm() {
19
- return this;
20
- }
21
- afterWrite(entityTodos, joinRowTodos) {
22
- const api = (0, joist_core_1.getEmInternalApi)(this.em);
23
- // We don't want any old preloaded data leaking into our changes, so we clear it out before we start as a precaution
24
- api.clearPreloadedRelations();
25
- // Likewise, any data loaders in memory will have stale data, so we clear them out as well
26
- api.clearDataloaders();
27
- this.#syncEntityData(entityTodos);
28
- this.#syncReferences(entityTodos);
29
- this.#syncManyToManys(joinRowTodos);
30
- this.#syncEnumCollections(joinRowTodos);
31
- this.#preloadUnloadedRelations(entityTodos);
32
- }
33
- #syncEntityData(entityTodos) {
34
- const { em } = this;
35
- Object.values(entityTodos).forEach((todo) => {
36
- todo.inserts.forEach((newEntity) => {
37
- const row = (0, joist_core_1.createRowFromEntityData)(newEntity, { preferOriginalData: false });
38
- em.hydrate(newEntity.constructor, [row]);
39
- });
40
- todo.updates.forEach((newEntity) => {
41
- const oldEntity = em.findExistingInstance(newEntity.idTagged);
42
- // This fail shouldn't really be possible, since any entity should either have been created:
43
- // * via factory or em.create in the original em
44
- // * by the new em inside the `run` call, in which case it'd be an insert instead of an update
45
- // * by a previous afterWrite call, which would have already been an insert and added to the original em
46
- // * by a previous `run` call, which would have already been an insert as well
47
- if (!oldEntity)
48
- fail(`Expected to find existing entity for $ {e.idTagged}`);
49
- const { data: newData } = (0, joist_core_1.getInstanceData)(newEntity);
50
- const { data: oldData } = (0, joist_core_1.getInstanceData)(oldEntity);
51
- const meta = (0, joist_core_1.getMetadata)(newEntity);
52
- newEntity.changes.fields
53
- .values()
54
- .filter((fieldName) => fieldName in newData)
55
- .filter((fieldName) => !!meta.allFields[fieldName].serde)
56
- .forEach((fieldName) => {
57
- // We're imitating a round trip to the database here, so we use our field's serde to map the value back
58
- // and forth.
59
- const serde = meta.allFields[fieldName].serde;
60
- let column;
61
- let value;
62
- if (serde instanceof joist_core_1.PolymorphicKeySerde) {
63
- [column, value] =
64
- serde.columns
65
- .values()
66
- .map((c) => [c, c.rowValue(newData)])
67
- .find(([, v]) => v !== undefined) ?? [];
68
- }
69
- else {
70
- [column] = serde.columns;
71
- // the 2nd and 3rd arguments are only used if the 4th argument is defined, so it's OK to pass undefined here
72
- value = column.rowValue(newData);
73
- }
74
- serde.setOnEntity(oldData, column ? { [column.columnName]: value } : {});
75
- });
76
- });
77
- todo.deletes.forEach((newEntity) => {
78
- const existing = em.findExistingInstance(newEntity.idTagged);
79
- // Again it shouldn't really be possible for a deletion to happen without the entity being present in the
80
- // original em, but just in case we'll skip if we don't find it
81
- if (!existing)
82
- fail(`Expected to find existing entity for $ {e.idTagged}`);
83
- (0, joist_core_1.getInstanceData)(existing).markDeletedBecauseNotFound();
84
- });
85
- });
86
- }
87
- // Ensure m2o/rr/poly changes from the new em are reflected in the original em
88
- #syncReferences(entityTodos) {
89
- const { em } = this;
90
- Object.values(entityTodos).forEach((todo) => {
91
- todo.inserts.forEach((newEntity) => {
92
- const oldEntity = em.findExistingInstance(newEntity.idTagged);
93
- getReferences(newEntity).forEach((r) => {
94
- const other = r.isSet ? em.findExistingInstance(r.idTaggedMaybe) : undefined;
95
- preloadReference(em, r.fieldName, oldEntity, other);
96
- if (other)
97
- maybePreloadOtherSide(em, r.fieldName, oldEntity, other);
98
- });
99
- });
100
- todo.updates.forEach((newEntity) => {
101
- const oldEntity = em.findExistingInstance(newEntity.idTagged);
102
- getReferences(newEntity)
103
- .filter((r) => newEntity.changes[r.fieldName].hasChanged)
104
- .forEach((r) => {
105
- const other = r.isSet ? em.findExistingInstance(r.idTaggedMaybe) : undefined;
106
- preloadReference(em, r.fieldName, oldEntity, other);
107
- if (other)
108
- maybePreloadOtherSide(em, r.fieldName, oldEntity, other);
109
- const originalId = newEntity.changes[r.fieldName].originalValue;
110
- const originalOther = originalId ? em.findExistingInstance(originalId) : undefined;
111
- if (originalOther)
112
- maybePreloadOtherSide(em, r.fieldName, oldEntity, originalOther, true);
113
- });
114
- // If the entity was soft-deleted, then we need to clear out the caches for any o2m collections that contain it
115
- if ("deletedAt" in newEntity && newEntity.changes.deletedAt.hasChanged) {
116
- getReferences(oldEntity).forEach((r) => {
117
- const other = r.isSet ? em.findExistingInstance(r.idTaggedMaybe) : undefined;
118
- if (other) {
119
- const otherSide = getOtherSide(r.fieldName, oldEntity, other);
120
- if (otherSide instanceof joist_core_1.OneToManyCollection)
121
- otherSide.resetIsLoaded();
122
- }
123
- });
124
- }
125
- });
126
- todo.deletes.forEach((newEntity) => {
127
- const oldEntity = em.findExistingInstance(newEntity.idTagged);
128
- getReferences(newEntity).forEach((r) => {
129
- // Since both old and new entities are marked as deleted, we can't just go looking through their relations
130
- // for the other side's id since this will error. Likewise, we only care about the collection that this
131
- // entity used to belong to in the old em so we can remove it. So just grab the other id directly from the
132
- // old entity's data and then preload its other side relation.
133
- const entityOrId = (0, joist_core_1.getInstanceData)(oldEntity).data[r.fieldName];
134
- const originalOther = entityOrId
135
- ? em.findExistingInstance((0, joist_core_1.isEntity)(entityOrId) ? entityOrId.idTagged : entityOrId)
136
- : undefined;
137
- if (originalOther)
138
- maybePreloadOtherSide(em, r.fieldName, oldEntity, originalOther, true);
139
- });
140
- });
141
- });
142
- }
143
- // Ensure m2m changes are propagated from the new em to the original em
144
- #syncManyToManys(joinRowTodos) {
145
- const { em } = this;
146
- const api = (0, joist_core_1.getEmInternalApi)(em);
147
- Object.entries(joinRowTodos).forEach(([joinTable, todo]) => {
148
- // Enum m2ms (EnumCollections) aren't entity-to-entity, so there's nothing to mirror between ems.
149
- if (todo.m2m.otherEnum)
150
- return;
151
- const preloads = new Set();
152
- processJoinRows(em, joinTable, todo.newRows, preloads, (oldEntity, entities) => entities.push(oldEntity));
153
- processJoinRows(em, joinTable, todo.deletedRows, preloads, (oldEntity, entities) => entities.splice(entities.indexOf(oldEntity), 1));
154
- // Since multiple join rows could reference the same m2m, our processJoinRows doesn't actually call preload() so
155
- // we need to do it now
156
- preloads.values().forEach((r) => {
157
- const id = r.entity.idTagged;
158
- const entities = api.getPreloadedRelation(id, r.fieldName);
159
- // unique our entities in case somehow an entity was inserted by multiple join rows
160
- api.setPreloadedRelation(id, r.fieldName, [...new Set(entities)]);
161
- r.preload();
162
- });
163
- });
164
- }
165
- // Ensure enum m2m (EnumCollection) membership changes are propagated from the new em to the original em
166
- #syncEnumCollections(joinRowTodos) {
167
- const { em } = this;
168
- const api = (0, joist_core_1.getEmInternalApi)(em);
169
- const touched = new Set();
170
- Object.values(joinRowTodos).forEach((todo) => {
171
- // Only enum m2ms; entity-to-entity m2ms are handled by `#syncManyToManys`.
172
- if (!todo.m2m.otherEnum)
173
- return;
174
- const { columnName, otherColumnName, fieldName } = todo.m2m;
175
- const sync = (rows, add) => {
176
- rows.forEach((row) => {
177
- const newEntity = row.columns[columnName];
178
- const enumId = row.columns[otherColumnName];
179
- const oldEntity = em.findExistingInstance(newEntity.idTagged);
180
- if (!oldEntity || oldEntity.isDeletedEntity)
181
- return;
182
- const oldCollection = oldEntity[fieldName];
183
- // Only mirror collections the original em has loaded (or new entities, whose rows are all
184
- // adds); otherwise a later `.load()` will read the (same-txn) db and see the change itself.
185
- if (!oldCollection.isLoaded && !newEntity.isNewEntity)
186
- return;
187
- const joinRows = api.joinRows(oldCollection);
188
- if (add) {
189
- joinRows.addPreloadedRow(oldCollection, undefined, oldEntity, enumId);
190
- }
191
- else {
192
- joinRows.removePreloadedRow(oldCollection, oldEntity, enumId);
193
- }
194
- touched.add(oldCollection);
195
- });
196
- };
197
- sync(todo.newRows, true);
198
- sync(todo.deletedRows, false);
199
- });
200
- // Re-publish each touched collection's codes and mark it loaded.
201
- touched.forEach((oldCollection) => {
202
- const joinRows = api.joinRows(oldCollection);
203
- const codes = joinRows.getOthers(oldCollection.columnName, oldCollection.entity).map((id) => oldCollection.otherEnum.findById(id).code);
204
- api.setPreloadedRelation(oldCollection.entity.idTagged, oldCollection.fieldName, codes);
205
- oldCollection.preload();
206
- });
207
- }
208
- // Make sure any unloaded relations on net new entities are marked as loaded. We should have already preloaded all
209
- // concrete references in a previous step. So we just need to load collections (plus o2os) if they haven't already
210
- // been preloaded by another step. Since nothing else preloaded them, they must have no data so we can just set
211
- // them to an empty array.
212
- #preloadUnloadedRelations(entityTodos) {
213
- const { em } = this;
214
- Object.entries(entityTodos).forEach(([, todo]) => {
215
- todo.inserts.forEach((newEntity) => {
216
- const oldEntity = em.findExistingInstance(newEntity.idTagged);
217
- getCollections(oldEntity)
218
- .filter((r) => !r.isPreloaded)
219
- .forEach((r) => {
220
- (0, joist_core_1.getEmInternalApi)(em).setPreloadedRelation(oldEntity.idTagged, r.fieldName, []);
221
- r.preload();
222
- });
223
- });
224
- });
225
- }
226
- }
227
- exports.RunPlugin = RunPlugin;
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
+ //#region src/RunPlugin.ts
3
+ var RunPlugin = class extends Plugin {
4
+ em;
5
+ constructor(em) {
6
+ super();
7
+ this.em = em;
8
+ }
9
+ maybeCloneForNewEm() {
10
+ return this;
11
+ }
12
+ afterWrite(entityTodos, joinRowTodos) {
13
+ const api = getEmInternalApi(this.em);
14
+ api.clearPreloadedRelations();
15
+ api.clearDataloaders();
16
+ this.#syncEntityData(entityTodos);
17
+ this.#syncReferences(entityTodos);
18
+ this.#syncManyToManys(joinRowTodos);
19
+ this.#syncEnumCollections(joinRowTodos);
20
+ this.#preloadUnloadedRelations(entityTodos);
21
+ }
22
+ #syncEntityData(entityTodos) {
23
+ const { em } = this;
24
+ Object.values(entityTodos).forEach((todo) => {
25
+ todo.inserts.forEach((newEntity) => {
26
+ const row = createRowFromEntityData(newEntity, { preferOriginalData: false });
27
+ em.hydrate(newEntity.constructor, [row]);
28
+ });
29
+ todo.updates.forEach((newEntity) => {
30
+ const oldEntity = em.findExistingInstance(newEntity.idTagged);
31
+ if (!oldEntity) fail(`Expected to find existing entity for $ {e.idTagged}`);
32
+ const { data: newData } = getInstanceData(newEntity);
33
+ const { data: oldData } = getInstanceData(oldEntity);
34
+ const meta = getMetadata(newEntity);
35
+ newEntity.changes.fields.values().filter((fieldName) => fieldName in newData).filter((fieldName) => !!meta.allFields[fieldName].serde).forEach((fieldName) => {
36
+ const serde = meta.allFields[fieldName].serde;
37
+ let column;
38
+ let value;
39
+ if (serde instanceof PolymorphicKeySerde) [column, value] = serde.columns.values().map((c) => [c, c.rowValue(newData)]).find(([, v]) => v !== void 0) ?? [];
40
+ else {
41
+ [column] = serde.columns;
42
+ value = column.rowValue(newData);
43
+ }
44
+ oldData[fieldName] = serde.fromRow(new PojoRowData([column ? { [column.columnName]: value } : {}]), 0);
45
+ });
46
+ });
47
+ todo.deletes.forEach((newEntity) => {
48
+ const existing = em.findExistingInstance(newEntity.idTagged);
49
+ if (!existing) fail(`Expected to find existing entity for $ {e.idTagged}`);
50
+ getInstanceData(existing).markDeletedBecauseNotFound();
51
+ });
52
+ });
53
+ }
54
+ #syncReferences(entityTodos) {
55
+ const { em } = this;
56
+ Object.values(entityTodos).forEach((todo) => {
57
+ todo.inserts.forEach((newEntity) => {
58
+ const oldEntity = em.findExistingInstance(newEntity.idTagged);
59
+ getReferences(newEntity).forEach((r) => {
60
+ const other = r.isSet ? em.findExistingInstance(r.idTaggedMaybe) : void 0;
61
+ preloadReference(em, r.fieldName, oldEntity, other);
62
+ if (other) maybePreloadOtherSide(em, r.fieldName, oldEntity, other);
63
+ });
64
+ });
65
+ todo.updates.forEach((newEntity) => {
66
+ const oldEntity = em.findExistingInstance(newEntity.idTagged);
67
+ getReferences(newEntity).filter((r) => newEntity.changes[r.fieldName].hasChanged).forEach((r) => {
68
+ const other = r.isSet ? em.findExistingInstance(r.idTaggedMaybe) : void 0;
69
+ preloadReference(em, r.fieldName, oldEntity, other);
70
+ if (other) maybePreloadOtherSide(em, r.fieldName, oldEntity, other);
71
+ const originalId = newEntity.changes[r.fieldName].originalValue;
72
+ const originalOther = originalId ? em.findExistingInstance(originalId) : void 0;
73
+ if (originalOther) maybePreloadOtherSide(em, r.fieldName, oldEntity, originalOther, true);
74
+ });
75
+ if ("deletedAt" in newEntity && newEntity.changes.deletedAt.hasChanged) getReferences(oldEntity).forEach((r) => {
76
+ const other = r.isSet ? em.findExistingInstance(r.idTaggedMaybe) : void 0;
77
+ if (other) {
78
+ const otherSide = getOtherSide(r.fieldName, oldEntity, other);
79
+ if (otherSide instanceof OneToManyCollection) otherSide.resetIsLoaded();
80
+ }
81
+ });
82
+ });
83
+ todo.deletes.forEach((newEntity) => {
84
+ const oldEntity = em.findExistingInstance(newEntity.idTagged);
85
+ getReferences(newEntity).forEach((r) => {
86
+ const entityOrId = getInstanceData(oldEntity).data[r.fieldName];
87
+ const originalOther = entityOrId ? em.findExistingInstance(isEntity(entityOrId) ? entityOrId.idTagged : entityOrId) : void 0;
88
+ if (originalOther) maybePreloadOtherSide(em, r.fieldName, oldEntity, originalOther, true);
89
+ });
90
+ });
91
+ });
92
+ }
93
+ #syncManyToManys(joinRowTodos) {
94
+ const { em } = this;
95
+ const api = getEmInternalApi(em);
96
+ Object.entries(joinRowTodos).forEach(([joinTable, todo]) => {
97
+ if (todo.m2m.otherEnum) return;
98
+ const preloads = /* @__PURE__ */ new Set();
99
+ processJoinRows(em, joinTable, todo.newRows, preloads, (oldEntity, entities) => entities.push(oldEntity));
100
+ processJoinRows(em, joinTable, todo.deletedRows, preloads, (oldEntity, entities) => entities.splice(entities.indexOf(oldEntity), 1));
101
+ preloads.values().forEach((r) => {
102
+ const id = r.entity.idTagged;
103
+ const entities = api.getPreloadedRelation(id, r.fieldName);
104
+ api.setPreloadedRelation(id, r.fieldName, [...new Set(entities)]);
105
+ r.preload();
106
+ });
107
+ });
108
+ }
109
+ #syncEnumCollections(joinRowTodos) {
110
+ const { em } = this;
111
+ const api = getEmInternalApi(em);
112
+ const touched = /* @__PURE__ */ new Set();
113
+ Object.values(joinRowTodos).forEach((todo) => {
114
+ if (!todo.m2m.otherEnum) return;
115
+ const { columnName, otherColumnName, fieldName } = todo.m2m;
116
+ const sync = (rows, add) => {
117
+ rows.forEach((row) => {
118
+ const newEntity = row.columns[columnName];
119
+ const enumId = row.columns[otherColumnName];
120
+ const oldEntity = em.findExistingInstance(newEntity.idTagged);
121
+ if (!oldEntity || oldEntity.isDeletedEntity) return;
122
+ const oldCollection = oldEntity[fieldName];
123
+ if (!oldCollection.isLoaded && !newEntity.isNewEntity) return;
124
+ const joinRows = api.joinRows(oldCollection);
125
+ if (add) joinRows.addPreloadedRow(oldCollection, void 0, oldEntity, enumId);
126
+ else joinRows.removePreloadedRow(oldCollection, oldEntity, enumId);
127
+ touched.add(oldCollection);
128
+ });
129
+ };
130
+ sync(todo.newRows, true);
131
+ sync(todo.deletedRows, false);
132
+ });
133
+ touched.forEach((oldCollection) => {
134
+ const codes = api.joinRows(oldCollection).getOthers(oldCollection.columnName, oldCollection.entity).map((id) => oldCollection.otherEnum.findById(id).code);
135
+ api.setPreloadedRelation(oldCollection.entity.idTagged, oldCollection.fieldName, codes);
136
+ oldCollection.preload();
137
+ });
138
+ }
139
+ #preloadUnloadedRelations(entityTodos) {
140
+ const { em } = this;
141
+ Object.entries(entityTodos).forEach(([, todo]) => {
142
+ todo.inserts.forEach((newEntity) => {
143
+ const oldEntity = em.findExistingInstance(newEntity.idTagged);
144
+ getCollections(oldEntity).filter((r) => !r.isPreloaded).forEach((r) => {
145
+ getEmInternalApi(em).setPreloadedRelation(oldEntity.idTagged, r.fieldName, []);
146
+ r.preload();
147
+ });
148
+ });
149
+ });
150
+ }
151
+ };
228
152
  function preloadReference(em, fieldName, entity, other) {
229
- // Preload for concrete references doesn't make us of the global preloaded cache. It just looks at its owner's data
230
- // 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
231
- // owner's data to have the correct key then we can call preload since all entities should already be in the original
232
- // em
233
- const reference = entity[fieldName];
234
- reference.unload();
235
- const { data } = (0, joist_core_1.getInstanceData)(entity);
236
- data[fieldName] = other?.idTagged;
237
- if (!entity.isDeletedEntity)
238
- reference.preload();
153
+ const reference = entity[fieldName];
154
+ reference.unload();
155
+ const { data } = getInstanceData(entity);
156
+ data[fieldName] = other?.idTagged;
157
+ if (!entity.isDeletedEntity) reference.preload();
239
158
  }
240
159
  function getOtherSide(fieldName, entity, other) {
241
- const meta = (0, joist_core_1.getMetadata)(entity);
242
- const metas = new Set([meta, ...meta.baseTypes]);
243
- return (0, joist_core_1.getRelations)(other)
244
- .values()
245
- .filter((r) => r instanceof joist_core_1.OneToOneReferenceImpl || r instanceof joist_core_1.OneToManyCollection)
246
- .find((r) => metas.has(r.otherMeta) && r.otherFieldName === fieldName);
160
+ const meta = getMetadata(entity);
161
+ const metas = /* @__PURE__ */ new Set([meta, ...meta.baseTypes]);
162
+ return getRelations(other).values().filter((r) => r instanceof OneToOneReferenceImpl || r instanceof OneToManyCollection).find((r) => metas.has(r.otherMeta) && r.otherFieldName === fieldName);
247
163
  }
248
164
  function maybePreloadOtherSide(em, fieldName, entity, other, remove = false) {
249
- const meta = (0, joist_core_1.getMetadata)(entity);
250
- const otherSide = getOtherSide(fieldName, entity, other);
251
- // If our other side is a large o2m then we can just ignore it, since it isn't loadable regardless
252
- if (!otherSide)
253
- return;
254
- // Since our preload scans the entire em, we only need to run it once per collection/o2o. We clear out preloads
255
- // when we start the afterWrite, so if anything is preloaded we know we put it there.
256
- if (otherSide.isPreloaded)
257
- return;
258
- let otherEntities;
259
- if (otherSide instanceof joist_core_1.OneToOneReferenceImpl) {
260
- otherEntities = remove ? [] : [entity];
261
- }
262
- else if (otherSide instanceof joist_core_1.OneToManyCollection) {
263
- // Scan the entire em for (non-deleted) entities of our type that reference the other side
264
- otherEntities = em
265
- .getEntities(meta.cstr)
266
- .values()
267
- .filter((e) => !e.isDeletedEntity)
268
- .filter((e) => e[otherSide.otherFieldName].idTaggedMaybe === other.idTagged)
269
- .toArray();
270
- }
271
- else {
272
- (0, joist_core_1.assertNever)(otherSide);
273
- }
274
- // We don't need to worry about order here because o2ms do their own sorting internally and o2os are inherently
275
- // unordered (since it's just a single entity)
276
- (0, joist_core_1.getEmInternalApi)(em).setPreloadedRelation(other.idTagged, otherSide.fieldName, otherEntities);
277
- otherSide.preload();
278
- // This method is confusingly name, what it actually does is clear out the sorted caches that m2m uses to avoid
279
- // sorting on every `.get` call. Since we just preloaded the collection with new data, we know these caches need to
280
- // be cleared
281
- if (otherSide instanceof joist_core_1.OneToManyCollection)
282
- otherSide.resetIsLoaded();
165
+ const meta = getMetadata(entity);
166
+ const otherSide = getOtherSide(fieldName, entity, other);
167
+ if (!otherSide) return;
168
+ if (otherSide.isPreloaded) return;
169
+ let otherEntities;
170
+ if (otherSide instanceof OneToOneReferenceImpl) otherEntities = remove ? [] : [entity];
171
+ else if (otherSide instanceof OneToManyCollection) otherEntities = em.getEntities(meta.cstr).values().filter((e) => !e.isDeletedEntity).filter((e) => e[otherSide.otherFieldName].idTaggedMaybe === other.idTagged).toArray();
172
+ else assertNever(otherSide);
173
+ getEmInternalApi(em).setPreloadedRelation(other.idTagged, otherSide.fieldName, otherEntities);
174
+ otherSide.preload();
175
+ if (otherSide instanceof OneToManyCollection) otherSide.resetIsLoaded();
283
176
  }
284
177
  function processJoinRows(em, joinTable, rows, preloads, callback) {
285
- const api = (0, joist_core_1.getEmInternalApi)(em);
286
- rows.forEach((row) => {
287
- // A join row's `columns` is always an object with two key/value pairs, one for each side of the m2m.
288
- // This only runs for entity-to-entity m2ms (enum m2ms are skipped by the caller), so both are entities.
289
- const [[col1, e1], [col2, e2]] = Object.entries(row.columns);
290
- [
291
- // Each join row needs to append/remove from both sides of the m2m. So we need to do the same operation once for
292
- // each side.
293
- [col1, e1, e2],
294
- [col2, e2, e1],
295
- ].forEach(([column, newEntity, newOther]) => {
296
- const { idTagged: id } = newEntity;
297
- const oldEntity = em.findExistingInstance(id);
298
- if (oldEntity.isDeletedEntity)
299
- return;
300
- const newM2m = getManyToManys(newEntity).find((r) => r.joinTableName === joinTable && r.columnName === column);
301
- const { fieldName } = newM2m;
302
- const oldM2m = oldEntity[fieldName];
303
- let entities;
304
- if (preloads.has(oldM2m)) {
305
- // If a previous join row has already preloaded this relation, then we can just append/remove from the
306
- // existing preloaded data.
307
- entities = api.getPreloadedRelation(id, fieldName);
308
- }
309
- else if (oldM2m.isLoaded) {
310
- // The order here is important. M2ms are returned in the order their join rows were inserted, so we need to
311
- // make sure we match that behavior here. So we get all the extant rows first and then append each new row
312
- // in order from there. Removals are fine since they'll modify the array in place and the correct order will
313
- // be preserved.
314
- entities = oldM2m.get;
315
- }
316
- else if (newEntity.isNewEntity) {
317
- // If this is a new entity, then all the join rows referencing it must also be inserts. So we can just use an
318
- // empty array initially and inserts will be appended to it.
319
- entities = [];
320
- }
321
- else {
322
- fail(`Expected ${fieldName} to be loaded`);
323
- }
324
- callback(em.findExistingInstance(newOther.idTagged), entities);
325
- api.setPreloadedRelation(id, fieldName, entities);
326
- // We only need to preload the m2m once, but multiple join rows could reference it, so we keep track of which m2ms
327
- // need to be preloaded as we work, then actually call preload() at the end once we're done.
328
- preloads.add(oldM2m);
329
- });
330
- });
178
+ const api = getEmInternalApi(em);
179
+ rows.forEach((row) => {
180
+ const [[col1, e1], [col2, e2]] = Object.entries(row.columns);
181
+ [[
182
+ col1,
183
+ e1,
184
+ e2
185
+ ], [
186
+ col2,
187
+ e2,
188
+ e1
189
+ ]].forEach(([column, newEntity, newOther]) => {
190
+ const { idTagged: id } = newEntity;
191
+ const oldEntity = em.findExistingInstance(id);
192
+ if (oldEntity.isDeletedEntity) return;
193
+ const { fieldName } = getManyToManys(newEntity).find((r) => r.joinTableName === joinTable && r.columnName === column);
194
+ const oldM2m = oldEntity[fieldName];
195
+ let entities;
196
+ if (preloads.has(oldM2m)) entities = api.getPreloadedRelation(id, fieldName);
197
+ else if (oldM2m.isLoaded) entities = oldM2m.get;
198
+ else if (newEntity.isNewEntity) entities = [];
199
+ else fail(`Expected ${fieldName} to be loaded`);
200
+ callback(em.findExistingInstance(newOther.idTagged), entities);
201
+ api.setPreloadedRelation(id, fieldName, entities);
202
+ preloads.add(oldM2m);
203
+ });
204
+ });
331
205
  }
332
206
  function getReferences(entity) {
333
- return (0, joist_core_1.getRelations)(entity)
334
- .values()
335
- .filter((r) => r instanceof joist_core_1.ManyToOneReferenceImpl ||
336
- r instanceof joist_core_1.ReactiveReferenceImpl ||
337
- r instanceof joist_core_1.PolymorphicReferenceImpl);
207
+ return getRelations(entity).values().filter((r) => r instanceof ManyToOneReferenceImpl || r instanceof ReactiveReferenceImpl || r instanceof PolymorphicReferenceImpl);
338
208
  }
339
209
  function getManyToManys(entity) {
340
- return (0, joist_core_1.getRelations)(entity)
341
- .values()
342
- .filter((r) => r instanceof joist_core_1.ManyToManyCollection ||
343
- r instanceof joist_core_1.ReactiveManyToManyImpl ||
344
- r instanceof joist_core_1.ReactiveManyToManyOtherSideImpl);
210
+ return getRelations(entity).values().filter((r) => r instanceof ManyToManyCollection || r instanceof ReactiveManyToManyImpl || r instanceof ReactiveManyToManyOtherSideImpl);
345
211
  }
346
212
  function getCollections(entity) {
347
- return (0, joist_core_1.getRelations)(entity)
348
- .values()
349
- .filter((r) => r instanceof joist_core_1.ManyToManyCollection ||
350
- r instanceof joist_core_1.OneToManyCollection ||
351
- r instanceof joist_core_1.OneToOneReferenceImpl ||
352
- r instanceof joist_core_1.ReactiveManyToManyImpl ||
353
- r instanceof joist_core_1.ReactiveManyToManyOtherSideImpl ||
354
- r instanceof joist_core_1.EnumCollectionImpl);
213
+ return getRelations(entity).values().filter((r) => r instanceof ManyToManyCollection || r instanceof OneToManyCollection || r instanceof OneToOneReferenceImpl || r instanceof ReactiveManyToManyImpl || r instanceof ReactiveManyToManyOtherSideImpl || r instanceof EnumCollectionImpl);
355
214
  }
215
+ //#endregion
216
+ export { RunPlugin };
217
+
356
218
  //# sourceMappingURL=RunPlugin.js.map