joist-test-utils 2.4.0-next.37 → 2.4.0-next.38
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.map +1 -1
- package/build/RunPlugin.d.cts +1 -2
- package/build/RunPlugin.d.cts.map +1 -1
- package/build/RunPlugin.d.mts +1 -2
- package/build/RunPlugin.d.mts.map +1 -1
- package/build/RunPlugin.js.map +1 -1
- package/build/context.d.cts +1 -2
- package/build/context.d.cts.map +1 -1
- package/build/context.d.mts +1 -2
- package/build/context.d.mts.map +1 -1
- package/build/index.d.cts +3 -3
- package/build/index.d.cts.map +1 -1
- package/build/index.d.mts +3 -3
- package/build/index.d.mts.map +1 -1
- package/build/run.d.cts +7 -7
- package/build/run.d.cts.map +1 -1
- package/build/run.d.mts +7 -7
- package/build/run.d.mts.map +1 -1
- package/build/seed.d.cts +2 -3
- package/build/seed.d.cts.map +1 -1
- package/build/seed.d.mts +2 -3
- package/build/seed.d.mts.map +1 -1
- package/build/toMatchEntity.d.cts +2 -3
- package/build/toMatchEntity.d.cts.map +1 -1
- package/build/toMatchEntity.d.mts +2 -3
- package/build/toMatchEntity.d.mts.map +1 -1
- package/package.json +4 -4
package/build/RunPlugin.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RunPlugin.cjs","names":["Plugin","getEmInternalApi","#syncEntityData","#syncReferencesAndLazyFields","#syncManyToManys","#syncEnumCollections","#preloadUnloadedRelations","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 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 });\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,IAAa,YAAb,cAA+BA,WAAAA,OAAO;CAGR;CAA5B,YAAY,IAAmC;EAC7C,MAAM;EADoB,KAAA,KAAA;CAE5B;CAEA,qBAAqB;EACnB,OAAO;CACT;CAEA,WAAW,aAAmC,cAA2C;EACvF,MAAM,OAAA,GAAMC,WAAAA,iBAAAA,CAAiB,KAAK,EAAE;EAEpC,IAAI,wBAAwB;EAE5B,IAAI,iBAAiB;EAC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ACb,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","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 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 });\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,IAAa,YAAb,cAA+BA,WAAAA,OAAO;CAGR;CAA5B,YAAY,IAAmC;EAC7C,MAAM;EADoB,KAAA,KAAA;CAE5B;CAEA,qBAAqB;EACnB,OAAO;CACT;CAEA,WAAW,aAAmC,cAA2C;EACvF,MAAM,OAAA,GAAMC,WAAAA,iBAAAA,CAAiB,KAAK,EAAE;EAEpC,IAAI,wBAAwB;EAE5B,IAAI,iBAAiB;EACrB,KAAK,gBAAgB,WAAW;EAChC,KAAK,6BAA6B,WAAW;EAC7C,KAAK,iBAAiB,YAAY;EAClC,KAAK,qBAAqB,YAAY;EACtC,KAAK,0BAA0B,WAAW;CAC5C;CAEA,gBAAgB,aAAmC;EACjD,MAAM,EAAE,OAAO;EACf,OAAO,OAAO,WAAW,CAAC,CAAC,SAAS,SAAS;GAC3C,KAAK,QAAQ,SAAS,cAAc;IAClC,MAAM,OAAA,GAAMC,WAAAA,wBAAAA,CAAwB,WAAW,EAAE,oBAAoB,MAAM,CAAC;IAC5E,GAAG,QAAQ,UAAU,aAAuD,CAAC,GAAG,CAAC;GACnF,CAAC;GACD,KAAK,QAAQ,SAAS,cAAc;IAClC,MAAM,YAAY,GAAG,qBAAqB,UAAU,QAAQ;IAM5D,IAAI,CAAC,WAAW,KAAK,qDAAqD;IAC1E,MAAM,EAAE,MAAM,aAAA,GAAYC,WAAAA,gBAAAA,CAAgB,SAAS;IACnD,MAAM,EAAE,MAAM,aAAA,GAAYA,WAAAA,gBAAAA,CAAgB,SAAS;IACnD,MAAM,QAAA,GAAOC,WAAAA,YAAAA,CAAY,SAAS;IAClC,UAAmB,QAAQ,OACxB,OAAO,CAAC,CACR,QAAQ,cAAsB,aAAa,OAAO,CAAC,CACnD,QAAQ,cAAsB,CAAC,CAAC,KAAK,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AA,yBACb,aAAaC,WAAAA,wBACjB;AACJ;AAEA,SAAS,eACP,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"}
|
package/build/RunPlugin.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EntityManager, JoinRowTodo, Plugin, Todo } from "joist-core";
|
|
2
2
|
//#region src/RunPlugin.d.ts
|
|
3
|
-
declare class RunPlugin extends Plugin {
|
|
3
|
+
export declare class RunPlugin extends Plugin {
|
|
4
4
|
#private;
|
|
5
5
|
readonly em: EntityManager;
|
|
6
6
|
constructor(em: EntityManager);
|
|
@@ -8,5 +8,4 @@ declare class RunPlugin extends Plugin {
|
|
|
8
8
|
afterWrite(entityTodos: Record<string, Todo>, joinRowTodos: Record<string, JoinRowTodo>): void;
|
|
9
9
|
}
|
|
10
10
|
//#endregion
|
|
11
|
-
export { RunPlugin };
|
|
12
11
|
//# 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":";;qBAoCa,kBAAkB;;WAGD,IAAI;EAAhC,YAA4B,IAAI;EAIhC;EAIA,WAAW,aAAa,eAAe,OAAO,cAAc,eAAe"}
|
package/build/RunPlugin.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EntityManager, JoinRowTodo, Plugin, Todo } from "joist-core";
|
|
2
2
|
//#region src/RunPlugin.d.ts
|
|
3
|
-
declare class RunPlugin extends Plugin {
|
|
3
|
+
export declare class RunPlugin extends Plugin {
|
|
4
4
|
#private;
|
|
5
5
|
readonly em: EntityManager;
|
|
6
6
|
constructor(em: EntityManager);
|
|
@@ -8,5 +8,4 @@ declare class RunPlugin extends Plugin {
|
|
|
8
8
|
afterWrite(entityTodos: Record<string, Todo>, joinRowTodos: Record<string, JoinRowTodo>): void;
|
|
9
9
|
}
|
|
10
10
|
//#endregion
|
|
11
|
-
export { RunPlugin };
|
|
12
11
|
//# 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":";;qBAoCa,kBAAkB;;WAGD,IAAI;EAAhC,YAA4B,IAAI;EAIhC;EAIA,WAAW,aAAa,eAAe,OAAO,cAAc,eAAe"}
|
package/build/RunPlugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RunPlugin.js","names":["#syncEntityData","#syncReferencesAndLazyFields","#syncManyToManys","#syncEnumCollections","#preloadUnloadedRelations"],"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 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 });\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,IAAa,YAAb,cAA+B,OAAO;CAGR;CAA5B,YAAY,IAAmC;EAC7C,MAAM;EADoB,KAAA,KAAA;CAE5B;CAEA,qBAAqB;EACnB,OAAO;CACT;CAEA,WAAW,aAAmC,cAA2C;EACvF,MAAM,MAAM,iBAAiB,KAAK,EAAE;EAEpC,IAAI,wBAAwB;EAE5B,IAAI,iBAAiB;EACrB,KAAKA,gBAAgB,WAAW;EAChC,KAAKC,6BAA6B,WAAW;EAC7C,KAAKC,iBAAiB,YAAY;EAClC,KAAKC,qBAAqB,YAAY;EACtC,KAAKC,0BAA0B,WAAW;CAC5C;CAEA,gBAAgB,aAAmC;EACjD,MAAM,EAAE,OAAO;EACf,OAAO,OAAO,WAAW,CAAC,CAAC,SAAS,SAAS;GAC3C,KAAK,QAAQ,SAAS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a,MAAM,CAAC,CACxB,OAAO,CAAC,CACR,QACE,MACC,aAAa,0BACb,aAAa,yBACb,aAAa,wBACjB;AACJ;AAEA,SAAS,eACP,QAKA;CACA,OAAO,aAAa,MAAM,CAAC,CACxB,OAAO,CAAC,CACR,QACE,MACC,aAAa,wBACb,aAAa,0BACb,aAAa,+BACjB;AACJ;AAEA,SAAS,eAAe,QAAgB;CACtC,OAAO,aAAa,MAAM,CAAC,CACxB,OAAO,CAAC,CACR,QACE,MACC,aAAa,wBACb,aAAa,uBACb,aAAa,yBACb,aAAa,0BACb,aAAa,mCACb,aAAa,kBACjB;AACJ"}
|
|
1
|
+
{"version":3,"file":"RunPlugin.js","names":[],"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 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 });\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,IAAa,YAAb,cAA+B,OAAO;CAGR;CAA5B,YAAY,IAAmC;EAC7C,MAAM;EADoB,KAAA,KAAA;CAE5B;CAEA,qBAAqB;EACnB,OAAO;CACT;CAEA,WAAW,aAAmC,cAA2C;EACvF,MAAM,MAAM,iBAAiB,KAAK,EAAE;EAEpC,IAAI,wBAAwB;EAE5B,IAAI,iBAAiB;EACrB,KAAK,gBAAgB,WAAW;EAChC,KAAK,6BAA6B,WAAW;EAC7C,KAAK,iBAAiB,YAAY;EAClC,KAAK,qBAAqB,YAAY;EACtC,KAAK,0BAA0B,WAAW;CAC5C;CAEA,gBAAgB,aAAmC;EACjD,MAAM,EAAE,OAAO;EACf,OAAO,OAAO,WAAW,CAAC,CAAC,SAAS,SAAS;GAC3C,KAAK,QAAQ,SAAS,cAAc;IAClC,MAAM,MAAM,wBAAwB,WAAW,EAAE,oBAAoB,MAAM,CAAC;IAC5E,GAAG,QAAQ,UAAU,aAAuD,CAAC,GAAG,CAAC;GACnF,CAAC;GACD,KAAK,QAAQ,SAAS,cAAc;IAClC,MAAM,YAAY,GAAG,qBAAqB,UAAU,QAAQ;IAM5D,IAAI,CAAC,WAAW,KAAK,qDAAqD;IAC1E,MAAM,EAAE,MAAM,YAAY,gBAAgB,SAAS;IACnD,MAAM,EAAE,MAAM,YAAY,gBAAgB,SAAS;IACnD,MAAM,OAAO,YAAY,SAAS;IAClC,UAAmB,QAAQ,OACxB,OAAO,CAAC,CACR,QAAQ,cAAsB,aAAa,OAAO,CAAC,CACnD,QAAQ,cAAsB,CAAC,CAAC,KAAK,UAAU,U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.ts
|
|
3
3
|
/** A very basic context that has `em` as the Joist `EntityManager`. */
|
|
4
|
-
type Context = {
|
|
4
|
+
export type Context = {
|
|
5
5
|
em: EntityManager;
|
|
6
6
|
};
|
|
7
7
|
//#endregion
|
|
8
|
-
export { Context };
|
|
9
8
|
//# sourceMappingURL=context.d.cts.map
|
package/build/context.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.cts","names":[],"sources":["../src/context.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"context.d.cts","names":[],"sources":["../src/context.ts"],"mappings":";;;YAGY;EAAY,IAAI"}
|
package/build/context.d.mts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { EntityManager } from "joist-core";
|
|
2
2
|
//#region src/context.d.ts
|
|
3
3
|
/** A very basic context that has `em` as the Joist `EntityManager`. */
|
|
4
|
-
type Context = {
|
|
4
|
+
export type Context = {
|
|
5
5
|
em: EntityManager;
|
|
6
6
|
};
|
|
7
7
|
//#endregion
|
|
8
|
-
export { Context };
|
|
9
8
|
//# sourceMappingURL=context.d.mts.map
|
package/build/context.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.mts","names":[],"sources":["../src/context.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"context.d.mts","names":[],"sources":["../src/context.ts"],"mappings":";;;YAGY;EAAY,IAAI"}
|
package/build/index.d.cts
CHANGED
|
@@ -4,7 +4,7 @@ import { MatchedEntity, toMatchEntity } from "./toMatchEntity.cjs";
|
|
|
4
4
|
import { ContextFn, makeRun, makeRunEach, newContext, run, runEach } from "./run.cjs";
|
|
5
5
|
import { SeedConfig, seed } from "./seed.cjs";
|
|
6
6
|
//#region src/index.d.ts
|
|
7
|
-
interface CustomMatcherResult {
|
|
7
|
+
export interface CustomMatcherResult {
|
|
8
8
|
pass: boolean;
|
|
9
9
|
message: () => string;
|
|
10
10
|
}
|
|
@@ -46,7 +46,7 @@ declare module "vitest" {
|
|
|
46
46
|
* made lazy and defined as getters on the prototype, there are no keys on the
|
|
47
47
|
* individual objects left for Jest to see & realize "these are different".
|
|
48
48
|
*/
|
|
49
|
-
declare function areEntitiesEqual(a: unknown, b: unknown): boolean | undefined;
|
|
49
|
+
export declare function areEntitiesEqual(a: unknown, b: unknown): boolean | undefined;
|
|
50
50
|
//#endregion
|
|
51
|
-
export { type Context, type ContextFn,
|
|
51
|
+
export { type Context, type ContextFn, RunPlugin, type SeedConfig, makeRun, makeRunEach, newContext, run, runEach, seed, toMatchEntity };
|
|
52
52
|
//# sourceMappingURL=index.d.cts.map
|
package/build/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;iBASiB;EACf;EACA;;QAGM;YACI;cACE,SAAS,iBAAiB,eAAe;MACjD,cAAc,UAAU,cAAc,KAAK;;;;;YAMrC,SAAS,iBAAiB,eAAe;IACjD,cAAc,UAAU,cAAc,KAAK;;;;YAMnC,SAAS;IACjB,cAAc,UAAU,cAAc,KAAK;;;;YAMnC,SAAS,GAAG;IACpB,cAAc,UAAU,cAAc,KAAK;;YAEnC;IACR,cAAc,UAAU;;;;;;;;;;;;;;;;wBAiBZ,iBAAiB,YAAY"}
|
package/build/index.d.mts
CHANGED
|
@@ -4,7 +4,7 @@ import { MatchedEntity, toMatchEntity } from "./toMatchEntity.mjs";
|
|
|
4
4
|
import { ContextFn, makeRun, makeRunEach, newContext, run, runEach } from "./run.mjs";
|
|
5
5
|
import { SeedConfig, seed } from "./seed.mjs";
|
|
6
6
|
//#region src/index.d.ts
|
|
7
|
-
interface CustomMatcherResult {
|
|
7
|
+
export interface CustomMatcherResult {
|
|
8
8
|
pass: boolean;
|
|
9
9
|
message: () => string;
|
|
10
10
|
}
|
|
@@ -46,7 +46,7 @@ declare module "vitest" {
|
|
|
46
46
|
* made lazy and defined as getters on the prototype, there are no keys on the
|
|
47
47
|
* individual objects left for Jest to see & realize "these are different".
|
|
48
48
|
*/
|
|
49
|
-
declare function areEntitiesEqual(a: unknown, b: unknown): boolean | undefined;
|
|
49
|
+
export declare function areEntitiesEqual(a: unknown, b: unknown): boolean | undefined;
|
|
50
50
|
//#endregion
|
|
51
|
-
export { type Context, type ContextFn,
|
|
51
|
+
export { type Context, type ContextFn, RunPlugin, type SeedConfig, makeRun, makeRunEach, newContext, run, runEach, seed, toMatchEntity };
|
|
52
52
|
//# sourceMappingURL=index.d.mts.map
|
package/build/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;iBASiB;EACf;EACA;;QAGM;YACI;cACE,SAAS,iBAAiB,eAAe;MACjD,cAAc,UAAU,cAAc,KAAK;;;;;YAMrC,SAAS,iBAAiB,eAAe;IACjD,cAAc,UAAU,cAAc,KAAK;;;;YAMnC,SAAS;IACjB,cAAc,UAAU,cAAc,KAAK;;;;YAMnC,SAAS,GAAG;IACpB,cAAc,UAAU,cAAc,KAAK;;YAEnC;IACR,cAAc,UAAU;;;;;;;;;;;;;;;;wBAiBZ,iBAAiB,YAAY"}
|
package/build/run.d.cts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { Context } from "./context.cjs";
|
|
2
2
|
//#region src/run.d.ts
|
|
3
3
|
type MaybePromise<T> = T | Promise<T>;
|
|
4
|
-
type ContextFn<C> = (ctx: C) => MaybePromise<C>;
|
|
4
|
+
export type ContextFn<C> = (ctx: C) => MaybePromise<C>;
|
|
5
5
|
/** Runs the `fn` in a dedicated / non-test Unit of Work . */
|
|
6
|
-
declare function run<C extends Context, T>(ctx: C, fn: (ctx: C) => MaybePromise<T>, contextFn?: ContextFn<C>): Promise<T>;
|
|
6
|
+
export declare function run<C extends Context, T>(ctx: C, fn: (ctx: C) => MaybePromise<T>, contextFn?: ContextFn<C>): Promise<T>;
|
|
7
7
|
/** Creates a `run` with a custom `newContext` method. */
|
|
8
|
-
declare function makeRun<C extends Context>(contextFn: ContextFn<C>): <T>(ctx: C, fn: (ctx: C) => MaybePromise<T>) => Promise<T>;
|
|
8
|
+
export declare function makeRun<C extends Context>(contextFn: ContextFn<C>): <T>(ctx: C, fn: (ctx: C) => MaybePromise<T>) => Promise<T>;
|
|
9
9
|
/** Runs the `fn` in a dedicated / non-test Unit of Work for each value in `values */
|
|
10
|
-
declare function runEach<C extends Context, T, U>(ctx: C, valuesFn: () => U[], fn: (ctx: C, value: U) => MaybePromise<T>, contextFn?: ContextFn<C>): Promise<T[]>;
|
|
10
|
+
export declare function runEach<C extends Context, T, U>(ctx: C, valuesFn: () => U[], fn: (ctx: C, value: U) => MaybePromise<T>, contextFn?: ContextFn<C>): Promise<T[]>;
|
|
11
11
|
/** Creates a `runEach` with a custom `newContext` method. */
|
|
12
|
-
declare function makeRunEach<C extends Context>(contextFn: ContextFn<C>): <T, U>(ctx: C, valuesFn: () => U[], fn: (ctx: C, value: U) => MaybePromise<T>) => Promise<T[]>;
|
|
12
|
+
export declare function makeRunEach<C extends Context>(contextFn: ContextFn<C>): <T, U>(ctx: C, valuesFn: () => U[], fn: (ctx: C, value: U) => MaybePromise<T>) => Promise<T[]>;
|
|
13
13
|
/** A default `newContext` that assumes `ctx.em` is where the `EntityManager` lives. */
|
|
14
|
-
declare function newContext<C extends Context>(ctx: C): C;
|
|
14
|
+
export declare function newContext<C extends Context>(ctx: C): C;
|
|
15
15
|
//#endregion
|
|
16
|
-
export {
|
|
16
|
+
export {};
|
|
17
17
|
//# sourceMappingURL=run.d.cts.map
|
package/build/run.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.d.cts","names":[],"sources":["../src/run.ts"],"mappings":";;KAMK,aAAa,KAAK,IAAI,QAAQ;
|
|
1
|
+
{"version":3,"file":"run.d.cts","names":[],"sources":["../src/run.ts"],"mappings":";;KAMK,aAAa,KAAK,IAAI,QAAQ;YAEvB,UAAU,MAAM,KAAK,MAAM,aAAa;;wBAO9B,IAAI,UAAU,SAAS,GAC3C,KAAK,GACL,KAAK,KAAK,MAAM,aAAa,IAC7B,YAAW,UAAU,KACpB,QAAQ;;wBAsBK,QAAQ,UAAU,SAChC,WAAW,UAAU,MACnB,GAAG,KAAK,GAAG,KAAK,KAAK,MAAM,aAAa,OAAO,QAAQ;;wBAKrC,QAAQ,UAAU,SAAS,GAAG,GAClD,KAAK,GACL,gBAAgB,KAChB,KAAK,KAAK,GAAG,OAAO,MAAM,aAAa,IACvC,YAAW,UAAU,KACpB,QAAQ;;wBAgBK,YAAY,UAAU,SACpC,WAAW,UAAU,MACnB,GAAG,GAAG,KAAK,GAAG,gBAAgB,KAAK,KAAK,KAAK,GAAG,OAAO,MAAM,aAAa,OAAO,QAAQ;;wBAK7E,WAAW,UAAU,SAAS,KAAK,IAAI"}
|
package/build/run.d.mts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import { Context } from "./context.mjs";
|
|
2
2
|
//#region src/run.d.ts
|
|
3
3
|
type MaybePromise<T> = T | Promise<T>;
|
|
4
|
-
type ContextFn<C> = (ctx: C) => MaybePromise<C>;
|
|
4
|
+
export type ContextFn<C> = (ctx: C) => MaybePromise<C>;
|
|
5
5
|
/** Runs the `fn` in a dedicated / non-test Unit of Work . */
|
|
6
|
-
declare function run<C extends Context, T>(ctx: C, fn: (ctx: C) => MaybePromise<T>, contextFn?: ContextFn<C>): Promise<T>;
|
|
6
|
+
export declare function run<C extends Context, T>(ctx: C, fn: (ctx: C) => MaybePromise<T>, contextFn?: ContextFn<C>): Promise<T>;
|
|
7
7
|
/** Creates a `run` with a custom `newContext` method. */
|
|
8
|
-
declare function makeRun<C extends Context>(contextFn: ContextFn<C>): <T>(ctx: C, fn: (ctx: C) => MaybePromise<T>) => Promise<T>;
|
|
8
|
+
export declare function makeRun<C extends Context>(contextFn: ContextFn<C>): <T>(ctx: C, fn: (ctx: C) => MaybePromise<T>) => Promise<T>;
|
|
9
9
|
/** Runs the `fn` in a dedicated / non-test Unit of Work for each value in `values */
|
|
10
|
-
declare function runEach<C extends Context, T, U>(ctx: C, valuesFn: () => U[], fn: (ctx: C, value: U) => MaybePromise<T>, contextFn?: ContextFn<C>): Promise<T[]>;
|
|
10
|
+
export declare function runEach<C extends Context, T, U>(ctx: C, valuesFn: () => U[], fn: (ctx: C, value: U) => MaybePromise<T>, contextFn?: ContextFn<C>): Promise<T[]>;
|
|
11
11
|
/** Creates a `runEach` with a custom `newContext` method. */
|
|
12
|
-
declare function makeRunEach<C extends Context>(contextFn: ContextFn<C>): <T, U>(ctx: C, valuesFn: () => U[], fn: (ctx: C, value: U) => MaybePromise<T>) => Promise<T[]>;
|
|
12
|
+
export declare function makeRunEach<C extends Context>(contextFn: ContextFn<C>): <T, U>(ctx: C, valuesFn: () => U[], fn: (ctx: C, value: U) => MaybePromise<T>) => Promise<T[]>;
|
|
13
13
|
/** A default `newContext` that assumes `ctx.em` is where the `EntityManager` lives. */
|
|
14
|
-
declare function newContext<C extends Context>(ctx: C): C;
|
|
14
|
+
export declare function newContext<C extends Context>(ctx: C): C;
|
|
15
15
|
//#endregion
|
|
16
|
-
export {
|
|
16
|
+
export {};
|
|
17
17
|
//# sourceMappingURL=run.d.mts.map
|
package/build/run.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.d.mts","names":[],"sources":["../src/run.ts"],"mappings":";;KAMK,aAAa,KAAK,IAAI,QAAQ;
|
|
1
|
+
{"version":3,"file":"run.d.mts","names":[],"sources":["../src/run.ts"],"mappings":";;KAMK,aAAa,KAAK,IAAI,QAAQ;YAEvB,UAAU,MAAM,KAAK,MAAM,aAAa;;wBAO9B,IAAI,UAAU,SAAS,GAC3C,KAAK,GACL,KAAK,KAAK,MAAM,aAAa,IAC7B,YAAW,UAAU,KACpB,QAAQ;;wBAsBK,QAAQ,UAAU,SAChC,WAAW,UAAU,MACnB,GAAG,KAAK,GAAG,KAAK,KAAK,MAAM,aAAa,OAAO,QAAQ;;wBAKrC,QAAQ,UAAU,SAAS,GAAG,GAClD,KAAK,GACL,gBAAgB,KAChB,KAAK,KAAK,GAAG,OAAO,MAAM,aAAa,IACvC,YAAW,UAAU,KACpB,QAAQ;;wBAgBK,YAAY,UAAU,SACpC,WAAW,UAAU,MACnB,GAAG,GAAG,KAAK,GAAG,gBAAgB,KAAK,KAAK,KAAK,GAAG,OAAO,MAAM,aAAa,OAAO,QAAQ;;wBAK7E,WAAW,UAAU,SAAS,KAAK,IAAI"}
|
package/build/seed.d.cts
CHANGED
|
@@ -2,7 +2,7 @@ import { Driver, EntityManager } from "joist-core";
|
|
|
2
2
|
import "dotenv/config";
|
|
3
3
|
import pg from "pg";
|
|
4
4
|
//#region src/seed.d.ts
|
|
5
|
-
interface SeedConfig {
|
|
5
|
+
export interface SeedConfig {
|
|
6
6
|
/** A factory function to create a Driver from a pg Pool. */
|
|
7
7
|
createDriver: (pool: pg.Pool) => Driver;
|
|
8
8
|
}
|
|
@@ -29,7 +29,6 @@ interface SeedConfig {
|
|
|
29
29
|
* });
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
declare function seed<E extends EntityManager = EntityManager>(config: SeedConfig, fn: (em: E) => Promise<void>): void;
|
|
32
|
+
export declare function seed<E extends EntityManager = EntityManager>(config: SeedConfig, fn: (em: E) => Promise<void>): void;
|
|
33
33
|
//#endregion
|
|
34
|
-
export { SeedConfig, seed };
|
|
35
34
|
//# sourceMappingURL=seed.d.cts.map
|
package/build/seed.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"seed.d.cts","names":[],"sources":["../src/seed.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"seed.d.cts","names":[],"sources":["../src/seed.ts"],"mappings":";;;;iBAIiB;;EAEf,eAAe,MAAM,GAAG,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;wBA0BnB,KAAK,UAAU,gBAAgB,eAAe,QAAQ,YAAY,KAAK,IAAI,MAAM"}
|
package/build/seed.d.mts
CHANGED
|
@@ -2,7 +2,7 @@ import { Driver, EntityManager } from "joist-core";
|
|
|
2
2
|
import "dotenv/config";
|
|
3
3
|
import pg from "pg";
|
|
4
4
|
//#region src/seed.d.ts
|
|
5
|
-
interface SeedConfig {
|
|
5
|
+
export interface SeedConfig {
|
|
6
6
|
/** A factory function to create a Driver from a pg Pool. */
|
|
7
7
|
createDriver: (pool: pg.Pool) => Driver;
|
|
8
8
|
}
|
|
@@ -29,7 +29,6 @@ interface SeedConfig {
|
|
|
29
29
|
* });
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
declare function seed<E extends EntityManager = EntityManager>(config: SeedConfig, fn: (em: E) => Promise<void>): void;
|
|
32
|
+
export declare function seed<E extends EntityManager = EntityManager>(config: SeedConfig, fn: (em: E) => Promise<void>): void;
|
|
33
33
|
//#endregion
|
|
34
|
-
export { SeedConfig, seed };
|
|
35
34
|
//# sourceMappingURL=seed.d.mts.map
|
package/build/seed.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"seed.d.mts","names":[],"sources":["../src/seed.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"seed.d.mts","names":[],"sources":["../src/seed.ts"],"mappings":";;;;iBAIiB;;EAEf,eAAe,MAAM,GAAG,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;wBA0BnB,KAAK,UAAU,gBAAgB,eAAe,QAAQ,YAAY,KAAK,IAAI,MAAM"}
|
|
@@ -11,7 +11,7 @@ import { Entity, Property, ReadOnlyCollection, Reference } from "joist-core";
|
|
|
11
11
|
* - We prune the expected/actual values to be as minimal as possible, to avoid Jest
|
|
12
12
|
* recursively crawling into connection pools or other misc non-useful things in the diffs
|
|
13
13
|
*/
|
|
14
|
-
declare function toMatchEntity<T>(this: any, actual: unknown, expected: MatchedEntity<T>): CustomMatcherResult;
|
|
14
|
+
export declare function toMatchEntity<T>(this: any, actual: unknown, expected: MatchedEntity<T>): CustomMatcherResult;
|
|
15
15
|
/**
|
|
16
16
|
* Given a Joist entity `T`, "flattens out" the Reference/Collections.
|
|
17
17
|
*
|
|
@@ -21,8 +21,7 @@ declare function toMatchEntity<T>(this: any, actual: unknown, expected: MatchedE
|
|
|
21
21
|
* We allow `| null` so that `toMatchEntity` can work against optional fields
|
|
22
22
|
* that are returned from GraphQL object resolvers.
|
|
23
23
|
*/
|
|
24
|
-
type MatchedEntity<T> = T extends ReadonlyArray<infer U> ? ReadonlyArray<MatchedEntity<U>> : undefined | T | { [K in keyof T]?: T[K] extends Reference<any, infer U, any> ? MatchedEntity<U> | U : T[K] extends ReadOnlyCollection<any, infer U> ? Array<MatchedEntity<U> | U> : T[K] extends Property<any, infer V> ? V : T[K] extends Entity | null | undefined ? MatchedEntity<T[K]> | T[K] | null | undefined : T[K] extends ReadonlyArray<(infer U) | undefined> ? readonly (MatchedEntity<U> | U | undefined)[] : T[K] extends ReadonlyArray<infer U> | null ? readonly (MatchedEntity<U> | U | null)[] // We recurse similar to a DeepPartial
|
|
24
|
+
export type MatchedEntity<T> = T extends ReadonlyArray<infer U> ? ReadonlyArray<MatchedEntity<U>> : undefined | T | { [K in keyof T]?: T[K] extends Reference<any, infer U, any> ? MatchedEntity<U> | U : T[K] extends ReadOnlyCollection<any, infer U> ? Array<MatchedEntity<U> | U> : T[K] extends Property<any, infer V> ? V : T[K] extends Entity | null | undefined ? MatchedEntity<T[K]> | T[K] | null | undefined : T[K] extends ReadonlyArray<(infer U) | undefined> ? readonly (MatchedEntity<U> | U | undefined)[] : T[K] extends ReadonlyArray<infer U> | null ? readonly (MatchedEntity<U> | U | null)[] // We recurse similar to a DeepPartial
|
|
25
25
|
: MatchedEntity<T[K]> | null; };
|
|
26
26
|
//#endregion
|
|
27
|
-
export { MatchedEntity, toMatchEntity };
|
|
28
27
|
//# sourceMappingURL=toMatchEntity.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toMatchEntity.d.cts","names":[],"sources":["../src/toMatchEntity.ts"],"mappings":";;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"toMatchEntity.d.cts","names":[],"sources":["../src/toMatchEntity.ts"],"mappings":";;;;;;;;;;;;;wBAoCgB,cAAc,GAAG,WAAW,iBAAiB,UAAU,cAAc,KAAK;;;;;;;;;;YAmE9E,cAAc,KACxB,UAAU,oBAAoB,KAC1B,cAAc,cAAc,kBAGxB,OAEG,WAAW,KAAK,EAAE,WAAW,qBAAqB,UAC/C,cAAc,KAAK,IACnB,EAAE,WAAW,8BAA8B,KACzC,MAAM,cAAc,KAAK,KACzB,EAAE,WAAW,oBAAoB,KAC/B,IACA,EAAE,WAAW,4BACX,cAAc,EAAE,MAAM,EAAE,wBACxB,EAAE,WAAW,qBAAqB,4BACtB,cAAc,KAAK,mBAC7B,EAAE,WAAW,oBAAoB,sBACrB,cAAc,KAAK;GAE7B,cAAc,EAAE"}
|
|
@@ -11,7 +11,7 @@ import { Entity, Property, ReadOnlyCollection, Reference } from "joist-core";
|
|
|
11
11
|
* - We prune the expected/actual values to be as minimal as possible, to avoid Jest
|
|
12
12
|
* recursively crawling into connection pools or other misc non-useful things in the diffs
|
|
13
13
|
*/
|
|
14
|
-
declare function toMatchEntity<T>(this: any, actual: unknown, expected: MatchedEntity<T>): CustomMatcherResult;
|
|
14
|
+
export declare function toMatchEntity<T>(this: any, actual: unknown, expected: MatchedEntity<T>): CustomMatcherResult;
|
|
15
15
|
/**
|
|
16
16
|
* Given a Joist entity `T`, "flattens out" the Reference/Collections.
|
|
17
17
|
*
|
|
@@ -21,8 +21,7 @@ declare function toMatchEntity<T>(this: any, actual: unknown, expected: MatchedE
|
|
|
21
21
|
* We allow `| null` so that `toMatchEntity` can work against optional fields
|
|
22
22
|
* that are returned from GraphQL object resolvers.
|
|
23
23
|
*/
|
|
24
|
-
type MatchedEntity<T> = T extends ReadonlyArray<infer U> ? ReadonlyArray<MatchedEntity<U>> : undefined | T | { [K in keyof T]?: T[K] extends Reference<any, infer U, any> ? MatchedEntity<U> | U : T[K] extends ReadOnlyCollection<any, infer U> ? Array<MatchedEntity<U> | U> : T[K] extends Property<any, infer V> ? V : T[K] extends Entity | null | undefined ? MatchedEntity<T[K]> | T[K] | null | undefined : T[K] extends ReadonlyArray<(infer U) | undefined> ? readonly (MatchedEntity<U> | U | undefined)[] : T[K] extends ReadonlyArray<infer U> | null ? readonly (MatchedEntity<U> | U | null)[] // We recurse similar to a DeepPartial
|
|
24
|
+
export type MatchedEntity<T> = T extends ReadonlyArray<infer U> ? ReadonlyArray<MatchedEntity<U>> : undefined | T | { [K in keyof T]?: T[K] extends Reference<any, infer U, any> ? MatchedEntity<U> | U : T[K] extends ReadOnlyCollection<any, infer U> ? Array<MatchedEntity<U> | U> : T[K] extends Property<any, infer V> ? V : T[K] extends Entity | null | undefined ? MatchedEntity<T[K]> | T[K] | null | undefined : T[K] extends ReadonlyArray<(infer U) | undefined> ? readonly (MatchedEntity<U> | U | undefined)[] : T[K] extends ReadonlyArray<infer U> | null ? readonly (MatchedEntity<U> | U | null)[] // We recurse similar to a DeepPartial
|
|
25
25
|
: MatchedEntity<T[K]> | null; };
|
|
26
26
|
//#endregion
|
|
27
|
-
export { MatchedEntity, toMatchEntity };
|
|
28
27
|
//# sourceMappingURL=toMatchEntity.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toMatchEntity.d.mts","names":[],"sources":["../src/toMatchEntity.ts"],"mappings":";;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"toMatchEntity.d.mts","names":[],"sources":["../src/toMatchEntity.ts"],"mappings":";;;;;;;;;;;;;wBAoCgB,cAAc,GAAG,WAAW,iBAAiB,UAAU,cAAc,KAAK;;;;;;;;;;YAmE9E,cAAc,KACxB,UAAU,oBAAoB,KAC1B,cAAc,cAAc,kBAGxB,OAEG,WAAW,KAAK,EAAE,WAAW,qBAAqB,UAC/C,cAAc,KAAK,IACnB,EAAE,WAAW,8BAA8B,KACzC,MAAM,cAAc,KAAK,KACzB,EAAE,WAAW,oBAAoB,KAC/B,IACA,EAAE,WAAW,4BACX,cAAc,EAAE,MAAM,EAAE,wBACxB,EAAE,WAAW,qBAAqB,4BACtB,cAAc,KAAK,mBAC7B,EAAE,WAAW,oBAAoB,sBACrB,cAAc,KAAK;GAE7B,cAAc,EAAE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "joist-test-utils",
|
|
3
|
-
"version": "2.4.0-next.
|
|
3
|
+
"version": "2.4.0-next.38",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "build/index.cjs",
|
|
@@ -34,12 +34,12 @@
|
|
|
34
34
|
"dotenv": "^18.0.7"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"joist-core": "2.4.0-next.
|
|
38
|
-
"joist-utils": "2.4.0-next.
|
|
37
|
+
"joist-core": "2.4.0-next.38",
|
|
38
|
+
"joist-utils": "2.4.0-next.38",
|
|
39
39
|
"pg": "^8.23.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@swc/core": "
|
|
42
|
+
"@swc/core": "1.16.18",
|
|
43
43
|
"@swc/jest": "^0.2.39",
|
|
44
44
|
"@types/jest": "^30.0.0",
|
|
45
45
|
"jest": "30.4.2",
|