joist-core 2.3.0-next.45 → 2.3.0-next.47

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.
@@ -159,7 +159,12 @@ var ManyToOneReferenceImpl = class extends require_relations_AbstractRelationImp
159
159
  maybeCascadeDelete() {
160
160
  if (this.isCascadeDelete) {
161
161
  const current = this.current({ withDeleted: true });
162
- if (current !== void 0 && typeof current !== "string") this.entity.em.delete(current);
162
+ if (current !== void 0) {
163
+ if (typeof current === "string") {
164
+ const other = this.entity.em.getEntity(current);
165
+ if (other) this.entity.em.delete(other);
166
+ } else this.entity.em.delete(current);
167
+ }
163
168
  }
164
169
  }
165
170
  async cleanupOnEntityDeleted() {
@@ -1 +1 @@
1
- {"version":3,"file":"ManyToOneReference.cjs","names":["lazyField","getMetadata","AbstractRelationImpl","#field","#state","getInstanceData","isEntity","#loadPromise","sameEntity","#setImpl","toTaggedId","setField","#percolateRemove","#percolateAdd","toIdOf","maybeResolveReferenceToId","deTagId","#hasBeenSet","ensureTagged","OneToManyCollection","OneToManyLargeCollection","OneToOneReferenceImpl","getEmInternalApi","getField","isCascadeDelete","RelationT","RelationU","ReferenceN","BaseEntity","NoIdError","#m2o","#loaded"],"sources":["../../src/relations/ManyToOneReference.ts"],"sourcesContent":["import { type Entity, isEntity } from \"../Entity.ts\";\nimport { type IdOf, type TaggedId, getEmInternalApi, sameEntity } from \"../EntityManager.ts\";\nimport { type EntityMetadata, type ManyToOneField, getMetadata } from \"../EntityMetadata.ts\";\nimport { getField, setField } from \"../fields.ts\";\nimport {\n BaseEntity,\n NoIdError,\n OneToManyLargeCollection,\n OneToOneReferenceImpl,\n type Reference,\n deTagId,\n ensureNotDeleted,\n ensureTagged,\n fail,\n getInstanceData,\n maybeResolveReferenceToId,\n toIdOf,\n toTaggedId,\n} from \"../index.ts\";\nimport { lazyField } from \"../newEntity.ts\";\nimport { maybeAdd, maybeRemove } from \"../utils.ts\";\nimport { AbstractRelationImpl, isCascadeDelete } from \"./AbstractRelationImpl.ts\";\nimport { OneToManyCollection } from \"./OneToManyCollection.ts\";\nimport { ReferenceN } from \"./ReferenceSymbols.ts\";\nimport { RelationT, RelationU } from \"./RelationSymbols.ts\";\n\n/** An alias for creating `ManyToOneReference`s. */\nexport function hasOne<T extends Entity, U extends Entity, N extends never | undefined>(): ManyToOneReference<T, U, N> {\n return lazyField((entity: T, fieldName) => {\n const m2o = getMetadata(entity).allFields[fieldName] as ManyToOneField;\n return new ManyToOneReferenceImpl<T, U, N>(entity, m2o);\n });\n}\n\n/** Type guard utility for determining if an entity field is a ManyToOneReference. */\nexport function isManyToOneReference(maybeReference: any): maybeReference is ManyToOneReference<any, any, any> {\n return maybeReference instanceof ManyToOneReferenceImpl;\n}\n\nexport interface ManyToOneReference<T extends Entity, U extends Entity, N extends never | undefined> extends Reference<\n T,\n U,\n N\n> {\n /** Returns the id of the current assigned entity, or a runtime error if either 1) unset or 2) set to a new entity that doesn't have an `id` yet. */\n id: IdOf<U>;\n\n /** Returns the id of the current assigned entity, undefined if unset, or a runtime error if set to a new entity. */\n idIfSet: IdOf<U> | undefined;\n\n /** Returns the id of the current assigned entity, undefined if unset, or undefined if set to a new entity. */\n idMaybe: IdOf<U> | undefined;\n\n idUntagged: string;\n\n idUntaggedIfSet: string | undefined;\n\n /** Returns `true` if this relation is currently set (i.e. regardless of whether it's loaded, or if it is set but the assigned entity doesn't have an id saved. */\n readonly isSet: boolean;\n}\n\n/**\n * Manages a foreign key from one entity to another, i.e. `Book.author --> Author`.\n *\n * We keep the current `author` / `author_id` value in the `__orm.data` hash, where the\n * current value could be either the (string) author id from the database, or an entity\n * `Author` that the user has set.\n *\n * Note that if our `images.author_id` column is unique, this `ManyToOneReference` will essentially\n * be half of a one-to-one relationship, but we'll keep using this `ManyToOneReference` on the \"many\"\n * side, and the other side, i.e. `Author.image` will use a `OneToOneReference` to point back to us.\n */\nexport class ManyToOneReferenceImpl<T extends Entity, U extends Entity, N extends never | undefined>\n extends AbstractRelationImpl<T, U>\n implements ManyToOneReference<T, U, N>\n{\n readonly #field: ManyToOneField;\n #state: M2OState<U, N>;\n #loadPromise: Promise<void> | undefined;\n #hasBeenSet = false;\n\n constructor(entity: T, field: ManyToOneField) {\n super(entity);\n this.#field = field;\n this.#state = getInstanceData(entity).isOrWasNew\n ? new M2OLoadedState<U, N>(this, undefined)\n : new M2OUnloadedState<U, N>(this);\n }\n\n async load(opts: { withDeleted?: boolean; forceReload?: boolean } = {}): Promise<U | N> {\n ensureNotDeleted(this.entity, \"pending\");\n this.#state = this.#state.maybeInstantLoad();\n if (!this.#state.isLoaded || opts.forceReload) {\n const current = this.current();\n if (isEntity(current) || current === undefined) {\n this.#state = new M2OLoadedState<U, N>(this, current as U | N | undefined);\n } else {\n await (this.#loadPromise ??= this.entity.em.load(this.otherMeta.cstr, current).then((entity) => {\n this.#loadPromise = undefined;\n // In extremely rare cases, someone might have called `set` while this promise was in-flight,\n // so make sure our current value is still the same as the fetched entity.\n if (sameEntity(entity, this.current())) {\n this.#state = this.#state.applyLoad(entity);\n }\n }));\n }\n }\n return this.filterDeleted(this.#state.doGet() as U | N, opts);\n }\n\n set(other: U | N): void {\n this.#setImpl(other);\n }\n\n get isSet(): boolean {\n return this.current() !== undefined;\n }\n\n get isLoaded(): boolean {\n // Even if `.load()` has not been called, `.get` will detect\n this.#state = this.#state.maybeInstantLoad();\n return this.#state.isLoaded;\n }\n\n get isPreloaded(): boolean {\n return !!this.maybeFindEntity();\n }\n\n preload(): void {\n this.#state = new M2OLoadedState<U, N>(this, this.maybeFindEntity());\n }\n\n private unload(): void {\n this.#state = new M2OUnloadedState<U, N>(this);\n this.#loadPromise = undefined;\n }\n\n /** Copy another em's `source` relation into our state. */\n import(source: ManyToOneReferenceImpl<T, U, N>, findEntity: (e: U) => U): void {\n this.#state = source.#state.import(this, findEntity);\n }\n\n private doGet(opts?: { withDeleted?: boolean }): U | N {\n ensureNotDeleted(this.entity, \"pending\");\n this.#state = this.#state.maybeInstantLoad();\n return this.filterDeleted(this.#state.doGet() as U | N, opts);\n }\n\n get getWithDeleted(): U | N {\n return this.doGet({ withDeleted: true });\n }\n\n get get(): U | N {\n return this.doGet({ withDeleted: false });\n }\n\n /** Returns the id of the current value. */\n get id(): IdOf<U> {\n return this.idMaybe || failNoId(this.entity, this.fieldName, this.current());\n }\n\n /** Sets the m2o to `id`, and allows accepting `undefined` (`N`) if this is a nullable relation. */\n set id(id: IdOf<U> | N) {\n ensureNotDeleted(this.entity, \"pending\");\n const newId = toTaggedId(this.otherMeta, id);\n\n const previousId = this.idTaggedMaybe;\n const previous = this.maybeFindEntity();\n const changed = setField(this.entity, this.fieldName, id);\n if (!changed) {\n return;\n }\n\n const loaded = newId ? (this.entity.em.getEntity(newId) as U) : undefined;\n this.#state = loaded ? new M2OLoadedState<U, N>(this, loaded) : new M2OUnloadedState<U, N>(this);\n this.#loadPromise = undefined;\n this.#percolateRemove(previousId, previous);\n this.#percolateAdd();\n }\n\n get idIfSet(): IdOf<U> | N | undefined {\n return this.idMaybe || failIfNewEntity(this.entity, this.fieldName, this.current());\n }\n\n get idUntagged(): string {\n return this.idUntaggedMaybe || failNoId(this.entity, this.fieldName, this.current());\n }\n\n get idUntaggedIfSet(): string | undefined {\n return this.idUntaggedMaybe || failIfNewEntity(this.entity, this.fieldName, this.current());\n }\n\n get idMaybe(): IdOf<U> | N | undefined {\n ensureNotDeleted(this.entity, \"pending\");\n return toIdOf(this.otherMeta, this.idTaggedMaybe);\n }\n\n /** Returns the tagged id of the current value, or undefined if unset or a new entity. */\n get idTaggedMaybe(): TaggedId | undefined {\n // Skip the deleted check so that `isPreloaded` doesn't blow up during em.refreshes/populates\n // ensureNotDeleted(this.entity, \"pending\");\n return maybeResolveReferenceToId(this.current());\n }\n\n get fieldName(): string {\n return this.#field.fieldName;\n }\n\n get otherFieldName(): keyof U & string {\n return this.#field.otherFieldName as keyof U & string;\n }\n\n private get idUntaggedMaybe(): string | undefined {\n return deTagId(this.otherMeta, this.idMaybe);\n }\n\n #setImpl(_other: U | IdOf<U> | N): void {\n ensureNotDeleted(this.entity, \"pending\");\n this.#hasBeenSet = true;\n\n // If the project is not using tagged ids, we still want it tagged internally\n const other = ensureTagged(this.otherMeta, _other);\n if (sameEntity(other, this.current({ withDeleted: true }))) {\n return;\n }\n\n const previousId = this.idTaggedMaybe;\n const previous = this.maybeFindEntity();\n // Prefer to keep the id in our data hash, but if this is a new entity w/o an id, use the entity itself\n setField(this.entity, this.fieldName, isEntity(other) ? (other?.idTaggedMaybe ?? other) : other);\n\n // Setting to an entity | undefined => loaded, setting to an id might reverse to unloaded\n const maybeEntity = isEntity(other) ? (other as U) : other ? this.entity.em.getEntity(other) : undefined;\n if (maybeEntity || other === undefined) {\n this.#state = new M2OLoadedState<U, N>(this, (maybeEntity ?? other) as U | N);\n } else {\n this.#state = new M2OUnloadedState<U, N>(this);\n }\n this.#loadPromise = undefined;\n this.#percolateRemove(previousId, previous);\n this.#percolateAdd();\n }\n\n // private impl\n\n setFromOpts(other: U | IdOf<U> | N): void {\n this.#setImpl(other);\n }\n\n maybeCascadeDelete(): void {\n if (this.isCascadeDelete) {\n const current = this.current({ withDeleted: true });\n if (current !== undefined && typeof current !== \"string\") {\n this.entity.em.delete(current as U);\n }\n }\n }\n\n async cleanupOnEntityDeleted(): Promise<void> {\n // if we are going to delete this relation as well, then we don't need to clean it up\n if (this.isCascadeDelete) return;\n const current = await this.load({ withDeleted: true });\n if (current !== undefined) {\n const o2m = this.getOtherRelation(current);\n if (o2m instanceof OneToManyCollection) {\n o2m.remove(this.entity, { requireLoaded: false });\n } else if (o2m instanceof OneToManyLargeCollection) {\n o2m.remove(this.entity);\n } else if (o2m instanceof OneToOneReferenceImpl) {\n o2m.set(undefined as any);\n } else {\n throw new Error(`Unhandled ${o2m}`);\n }\n }\n setField(this.entity, this.fieldName, undefined);\n this.#state = new M2OLoadedState<U, N>(this, undefined as any);\n }\n\n // Called on `book.author.set(...)` with our previous value, to percolate a `author.books.remove`. */\n #percolateRemove(prevId: string | undefined, prevEntity: U | undefined) {\n if (prevEntity) {\n const prevRelation = this.getOtherRelation(prevEntity);\n if (prevRelation instanceof OneToManyCollection) {\n prevRelation.removeIfLoaded(this.entity);\n } else if (prevRelation instanceof OneToManyLargeCollection) {\n prevRelation.remove(this.entity);\n } else {\n prevRelation.set(undefined as any, { percolating: true });\n }\n } else if (prevId) {\n // prevEntity is not loaded in memory, but cache it in case our other side is later loaded\n const { em } = this.entity;\n let map = getEmInternalApi(em).pendingPercolate.get(prevId);\n if (!map) {\n map = new Map();\n getEmInternalApi(em).pendingPercolate.set(prevId, map);\n }\n let pending = map.get(this.otherFieldName);\n if (!pending) {\n pending = { adds: [], removes: [] };\n map.set(this.otherFieldName, pending);\n }\n maybeAdd(pending.removes, this.entity);\n maybeRemove(pending.adds, this.entity);\n }\n }\n\n #percolateAdd() {\n const id = this.current();\n const other = this.maybeFindEntity();\n if (other) {\n // Other is already loaded in memory, immediately hook it up\n const newRelation = this.getOtherRelation(other);\n if (newRelation instanceof OneToManyCollection) {\n newRelation.add(this.entity);\n } else if (newRelation instanceof OneToManyLargeCollection) {\n newRelation.add(this.entity);\n } else if (newRelation) {\n newRelation.set(this.entity, { percolating: true });\n } else {\n // Something is wrong, we should always have a relation, but instead\n // of blowing up here, let this finish b/c it will probably turn into\n // a validation error.\n }\n } else if (typeof id === \"string\") {\n // Other is not loaded in memory, but cache it in case our other side is later loaded\n const { em } = this.entity;\n let map = getEmInternalApi(em).pendingPercolate.get(id);\n if (!map) {\n map = new Map();\n getEmInternalApi(em).pendingPercolate.set(id, map);\n }\n let pending = map.get(this.otherFieldName);\n if (!pending) {\n pending = { adds: [], removes: [] };\n map.set(this.otherFieldName, pending);\n }\n maybeAdd(pending.adds, this.entity);\n maybeRemove(pending.removes, this.entity);\n }\n }\n\n // We need to keep U in data[fieldName] to handle entities without an id assigned yet.\n current(opts?: { withDeleted?: boolean }): U | TaggedId | N {\n const current = getField(this.entity, this.fieldName);\n if (current !== undefined && isEntity(current)) {\n return this.filterDeleted(current as U, opts);\n }\n return current;\n }\n\n get otherMeta(): EntityMetadata<U> {\n return this.#field.otherMetadata();\n }\n\n get hasBeenSet(): boolean {\n return this.#hasBeenSet;\n }\n\n public toString(): string {\n return `ManyToOneReference(entity: ${this.entity}, fieldName: ${this.fieldName}, otherType: ${this.otherMeta.type}, otherFieldName: ${this.otherFieldName}, id: ${this.idMaybe})`;\n }\n\n /**\n * Removes pending-hard-delete (but not soft-deleted), unless explicitly asked for.\n *\n * Note that we leave soft-deleted entities b/c the call signature of a `book.author.get`\n * very likely does not expect a soft-deleted entity to result in `undefined`.\n *\n * (Contrasted with `author.books.get` which is more intuitive to have soft-deleted\n * entities filtered out, i.e. it doesn't fundamentally change the return type.)\n */\n private filterDeleted(entity: U | N, opts?: { withDeleted?: boolean }): U | N {\n if (entity && (!opts || !opts.withDeleted) && entity.isDeletedEntity) {\n return undefined!;\n }\n return entity;\n }\n\n /** Returns the other relation that points back at us, i.e. we're `book.author_id` and this is `Author.books`. */\n private getOtherRelation(\n other: U,\n ): OneToManyCollection<U, T> | OneToOneReferenceImpl<U, T> | OneToManyLargeCollection<U, T> {\n return (other as U)[this.otherFieldName] as any;\n }\n\n private get isCascadeDelete(): boolean {\n return isCascadeDelete(this, this.fieldName);\n }\n\n /**\n * Looks for an entity in `EntityManager`, b/c we may have it in memory even if\n * our reference is not specifically loaded.\n */\n maybeFindEntity(): U | undefined {\n // Check current() first b/c a new entity won't have an id yet\n const current = this.current();\n if (isEntity(current)) return current as U;\n const { idTaggedMaybe } = this;\n return idTaggedMaybe !== undefined ? (this.entity.em.getEntity(idTaggedMaybe) as U) : undefined;\n }\n\n [RelationT]: T = null!;\n [RelationU]: U = null!;\n [ReferenceN]: N = null!;\n}\n\n/**\n * Fails when we can't return an `.id` for a reference, i.e. it's unset or a new entity.\n *\n * We strictly type `book.author.id` as `AuthorId`, even if `book.author` is nullable, to\n * avoid users calling `.id` and getting back `undefined`, but not because \"there is no\n * author\", but only because \"the assigned author doesn't have an id assigned yet\".\n *\n * To handle nullable references, set `.idIfSet`.\n *\n * Assumes being called like `return idMaybe ?? failNoId()`, i.e. if we're called we\n * know the id is not available.\n */\nexport function failNoId(entity: Entity, fieldName: string, current: string | Entity | undefined): never {\n if (!current) fail(`Reference ${entity}.${fieldName} is unset`);\n // Throw NoIdError, which lets ReactionsManager will handle reactive fields gracefully\n if (current instanceof BaseEntity)\n throw new NoIdError(`Reference ${entity}.${fieldName} is assigned to a new entity`);\n fail(`Unreachable`);\n}\n\n/**\n * Fails when we can't return an `.idIfSet` for a reference, i.e. it's a new entity.\n *\n * Assumes being called like `return idMaybe ?? failIfNewEntity()`, i.e. if we're called we\n * know the id is not available.\n */\nexport function failIfNewEntity(entity: Entity, fieldName: string, current: string | Entity | undefined): undefined {\n if (current instanceof BaseEntity) {\n throw new NoIdError(`Reference ${entity}.${fieldName} is assigned to a new entity`);\n }\n // Since this is a `.idIfSet`, having no `current` is fine, return undefined.\n return undefined;\n}\n\ninterface M2OState<U extends Entity, N extends never | undefined> {\n readonly isLoaded: boolean;\n maybeInstantLoad(): M2OState<U, N>;\n applyLoad(loaded: U | N | undefined): M2OLoadedState<U, N>;\n import(target: ManyToOneReferenceImpl<any, any, any>, findEntity: (e: U) => U): M2OState<U, N>;\n doGet(): U | N | undefined;\n}\n\nclass M2OUnloadedState<U extends Entity, N extends never | undefined> implements M2OState<U, N> {\n readonly isLoaded = false;\n #m2o: ManyToOneReferenceImpl<any, any, any>;\n constructor(m2o: ManyToOneReferenceImpl<any, any, any>) {\n this.#m2o = m2o;\n }\n applyLoad(loaded: U | N | undefined): M2OLoadedState<U, N> {\n return new M2OLoadedState<U, N>(this.#m2o, loaded);\n }\n import(target: ManyToOneReferenceImpl<any, any, any>): M2OState<U, N> {\n return new M2OUnloadedState<U, N>(target);\n }\n maybeInstantLoad(): M2OState<U, N> {\n const current = this.#m2o.current();\n if (current === undefined || isEntity(current)) {\n return new M2OLoadedState<U, N>(this.#m2o, current as U | undefined);\n } else {\n const maybeFound = this.#m2o.entity.em.getEntity(current);\n if (maybeFound) {\n return new M2OLoadedState<U, N>(this.#m2o, maybeFound as U);\n }\n }\n return this;\n }\n doGet(): U | N | undefined {\n throw new Error(`${this.#m2o.entity}.${this.#m2o.fieldName} was not loaded`);\n }\n}\n\n/** The when loaded, with either the loaded entity or undefined. */\nclass M2OLoadedState<U extends Entity, N extends never | undefined> implements M2OState<U, N> {\n readonly isLoaded = true;\n #m2o: ManyToOneReferenceImpl<any, any, any>;\n #loaded: U | N | undefined;\n constructor(m2o: ManyToOneReferenceImpl<any, any, any>, loaded: U | N | undefined) {\n this.#loaded = loaded;\n this.#m2o = m2o;\n }\n applyLoad(loaded: U | N | undefined): M2OLoadedState<U, N> {\n return new M2OLoadedState<U, N>(this.#m2o, loaded);\n }\n import(target: ManyToOneReferenceImpl<any, any, any>, findEntity: (e: U) => U): M2OState<U, N> {\n return new M2OLoadedState<U, N>(target, isEntity(this.#loaded) ? findEntity(this.#loaded) : undefined);\n }\n maybeInstantLoad(): M2OState<U, N> {\n return this;\n }\n doGet(): U | N | undefined {\n return this.#loaded;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA2BA,SAAgB,SAAuG;CACrH,OAAOA,kBAAAA,WAAW,QAAW,cAAc;EACzC,MAAM,MAAMC,uBAAAA,YAAY,MAAM,CAAC,CAAC,UAAU;EAC1C,OAAO,IAAI,uBAAgC,QAAQ,GAAG;CACxD,CAAC;AACH;;AAGA,SAAgB,qBAAqB,gBAA0E;CAC7G,OAAO,0BAA0B;AACnC;;;;;;;;;;;;AAmCA,IAAa,yBAAb,cACUC,uCAAAA,qBAEV;CACE;CACA;CACA;CACA,cAAc;CAEd,YAAY,QAAW,OAAuB;EAC5C,MAAM,MAAM;EACZ,KAAKC,SAAS;EACd,KAAKC,SAASC,mBAAAA,gBAAgB,MAAM,CAAC,CAAC,aAClC,IAAI,eAAqB,MAAM,KAAA,CAAS,IACxC,IAA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;CAC1C;CACA,mBAAmC;EACjC,MAAM,UAAU,KAAKA,KAAK,QAAQ;EAClC,IAAI,YAAY,KAAA,KAAaxB,eAAAA,SAAS,OAAO,GAC3C,OAAO,IAAI,eAAqB,KAAKwB,MAAM,OAAwB;OAC9D;GACL,MAAM,aAAa,KAAKA,KAAK,OAAO,GAAG,UAAU,OAAO;GACxD,IAAI,YACF,OAAO,IAAI,eAAqB,KAAKA,MAAM,UAAe;EAE9D;EACA,OAAO;CACT;CACA,QAA2B;EACzB,MAAM,IAAI,MAAM,GAAG,KAAKA,KAAK,OAAO,GAAG,KAAKA,KAAK,UAAU,gBAAgB;CAC7E;AACF;;AAGA,IAAM,iBAAN,MAAM,eAAwF;CAC5F,WAAoB;CACpB;CACA;CACA,YAAY,KAA4C,QAA2B;EACjF,KAAKC,UAAU;EACf,KAAKD,OAAO;CACd;CACA,UAAU,QAAiD;EACzD,OAAO,IAAI,eAAqB,KAAKA,MAAM,MAAM;CACnD;CACA,OAAO,QAA+C,YAAyC;EAC7F,OAAO,IAAI,eAAqB,QAAQxB,eAAAA,SAAS,KAAKyB,OAAO,IAAI,WAAW,KAAKA,OAAO,IAAI,KAAA,CAAS;CACvG;CACA,mBAAmC;EACjC,OAAO;CACT;CACA,QAA2B;EACzB,OAAO,KAAKA;CACd;AACF"}
1
+ {"version":3,"file":"ManyToOneReference.cjs","names":["lazyField","getMetadata","AbstractRelationImpl","#field","#state","getInstanceData","isEntity","#loadPromise","sameEntity","#setImpl","toTaggedId","setField","#percolateRemove","#percolateAdd","toIdOf","maybeResolveReferenceToId","deTagId","#hasBeenSet","ensureTagged","OneToManyCollection","OneToManyLargeCollection","OneToOneReferenceImpl","getEmInternalApi","getField","isCascadeDelete","RelationT","RelationU","ReferenceN","BaseEntity","NoIdError","#m2o","#loaded"],"sources":["../../src/relations/ManyToOneReference.ts"],"sourcesContent":["import { type Entity, isEntity } from \"../Entity.ts\";\nimport { type IdOf, type TaggedId, getEmInternalApi, sameEntity } from \"../EntityManager.ts\";\nimport { type EntityMetadata, type ManyToOneField, getMetadata } from \"../EntityMetadata.ts\";\nimport { getField, setField } from \"../fields.ts\";\nimport {\n BaseEntity,\n NoIdError,\n OneToManyLargeCollection,\n OneToOneReferenceImpl,\n type Reference,\n deTagId,\n ensureNotDeleted,\n ensureTagged,\n fail,\n getInstanceData,\n maybeResolveReferenceToId,\n toIdOf,\n toTaggedId,\n} from \"../index.ts\";\nimport { lazyField } from \"../newEntity.ts\";\nimport { maybeAdd, maybeRemove } from \"../utils.ts\";\nimport { AbstractRelationImpl, isCascadeDelete } from \"./AbstractRelationImpl.ts\";\nimport { OneToManyCollection } from \"./OneToManyCollection.ts\";\nimport { ReferenceN } from \"./ReferenceSymbols.ts\";\nimport { RelationT, RelationU } from \"./RelationSymbols.ts\";\n\n/** An alias for creating `ManyToOneReference`s. */\nexport function hasOne<T extends Entity, U extends Entity, N extends never | undefined>(): ManyToOneReference<T, U, N> {\n return lazyField((entity: T, fieldName) => {\n const m2o = getMetadata(entity).allFields[fieldName] as ManyToOneField;\n return new ManyToOneReferenceImpl<T, U, N>(entity, m2o);\n });\n}\n\n/** Type guard utility for determining if an entity field is a ManyToOneReference. */\nexport function isManyToOneReference(maybeReference: any): maybeReference is ManyToOneReference<any, any, any> {\n return maybeReference instanceof ManyToOneReferenceImpl;\n}\n\nexport interface ManyToOneReference<T extends Entity, U extends Entity, N extends never | undefined> extends Reference<\n T,\n U,\n N\n> {\n /** Returns the id of the current assigned entity, or a runtime error if either 1) unset or 2) set to a new entity that doesn't have an `id` yet. */\n id: IdOf<U>;\n\n /** Returns the id of the current assigned entity, undefined if unset, or a runtime error if set to a new entity. */\n idIfSet: IdOf<U> | undefined;\n\n /** Returns the id of the current assigned entity, undefined if unset, or undefined if set to a new entity. */\n idMaybe: IdOf<U> | undefined;\n\n idUntagged: string;\n\n idUntaggedIfSet: string | undefined;\n\n /** Returns `true` if this relation is currently set (i.e. regardless of whether it's loaded, or if it is set but the assigned entity doesn't have an id saved. */\n readonly isSet: boolean;\n}\n\n/**\n * Manages a foreign key from one entity to another, i.e. `Book.author --> Author`.\n *\n * We keep the current `author` / `author_id` value in the `__orm.data` hash, where the\n * current value could be either the (string) author id from the database, or an entity\n * `Author` that the user has set.\n *\n * Note that if our `images.author_id` column is unique, this `ManyToOneReference` will essentially\n * be half of a one-to-one relationship, but we'll keep using this `ManyToOneReference` on the \"many\"\n * side, and the other side, i.e. `Author.image` will use a `OneToOneReference` to point back to us.\n */\nexport class ManyToOneReferenceImpl<T extends Entity, U extends Entity, N extends never | undefined>\n extends AbstractRelationImpl<T, U>\n implements ManyToOneReference<T, U, N>\n{\n readonly #field: ManyToOneField;\n #state: M2OState<U, N>;\n #loadPromise: Promise<void> | undefined;\n #hasBeenSet = false;\n\n constructor(entity: T, field: ManyToOneField) {\n super(entity);\n this.#field = field;\n this.#state = getInstanceData(entity).isOrWasNew\n ? new M2OLoadedState<U, N>(this, undefined)\n : new M2OUnloadedState<U, N>(this);\n }\n\n async load(opts: { withDeleted?: boolean; forceReload?: boolean } = {}): Promise<U | N> {\n ensureNotDeleted(this.entity, \"pending\");\n this.#state = this.#state.maybeInstantLoad();\n if (!this.#state.isLoaded || opts.forceReload) {\n const current = this.current();\n if (isEntity(current) || current === undefined) {\n this.#state = new M2OLoadedState<U, N>(this, current as U | N | undefined);\n } else {\n await (this.#loadPromise ??= this.entity.em.load(this.otherMeta.cstr, current).then((entity) => {\n this.#loadPromise = undefined;\n // In extremely rare cases, someone might have called `set` while this promise was in-flight,\n // so make sure our current value is still the same as the fetched entity.\n if (sameEntity(entity, this.current())) {\n this.#state = this.#state.applyLoad(entity);\n }\n }));\n }\n }\n return this.filterDeleted(this.#state.doGet() as U | N, opts);\n }\n\n set(other: U | N): void {\n this.#setImpl(other);\n }\n\n get isSet(): boolean {\n return this.current() !== undefined;\n }\n\n get isLoaded(): boolean {\n // Even if `.load()` has not been called, `.get` will detect\n this.#state = this.#state.maybeInstantLoad();\n return this.#state.isLoaded;\n }\n\n get isPreloaded(): boolean {\n return !!this.maybeFindEntity();\n }\n\n preload(): void {\n this.#state = new M2OLoadedState<U, N>(this, this.maybeFindEntity());\n }\n\n private unload(): void {\n this.#state = new M2OUnloadedState<U, N>(this);\n this.#loadPromise = undefined;\n }\n\n /** Copy another em's `source` relation into our state. */\n import(source: ManyToOneReferenceImpl<T, U, N>, findEntity: (e: U) => U): void {\n this.#state = source.#state.import(this, findEntity);\n }\n\n private doGet(opts?: { withDeleted?: boolean }): U | N {\n ensureNotDeleted(this.entity, \"pending\");\n this.#state = this.#state.maybeInstantLoad();\n return this.filterDeleted(this.#state.doGet() as U | N, opts);\n }\n\n get getWithDeleted(): U | N {\n return this.doGet({ withDeleted: true });\n }\n\n get get(): U | N {\n return this.doGet({ withDeleted: false });\n }\n\n /** Returns the id of the current value. */\n get id(): IdOf<U> {\n return this.idMaybe || failNoId(this.entity, this.fieldName, this.current());\n }\n\n /** Sets the m2o to `id`, and allows accepting `undefined` (`N`) if this is a nullable relation. */\n set id(id: IdOf<U> | N) {\n ensureNotDeleted(this.entity, \"pending\");\n const newId = toTaggedId(this.otherMeta, id);\n\n const previousId = this.idTaggedMaybe;\n const previous = this.maybeFindEntity();\n const changed = setField(this.entity, this.fieldName, id);\n if (!changed) {\n return;\n }\n\n const loaded = newId ? (this.entity.em.getEntity(newId) as U) : undefined;\n this.#state = loaded ? new M2OLoadedState<U, N>(this, loaded) : new M2OUnloadedState<U, N>(this);\n this.#loadPromise = undefined;\n this.#percolateRemove(previousId, previous);\n this.#percolateAdd();\n }\n\n get idIfSet(): IdOf<U> | N | undefined {\n return this.idMaybe || failIfNewEntity(this.entity, this.fieldName, this.current());\n }\n\n get idUntagged(): string {\n return this.idUntaggedMaybe || failNoId(this.entity, this.fieldName, this.current());\n }\n\n get idUntaggedIfSet(): string | undefined {\n return this.idUntaggedMaybe || failIfNewEntity(this.entity, this.fieldName, this.current());\n }\n\n get idMaybe(): IdOf<U> | N | undefined {\n ensureNotDeleted(this.entity, \"pending\");\n return toIdOf(this.otherMeta, this.idTaggedMaybe);\n }\n\n /** Returns the tagged id of the current value, or undefined if unset or a new entity. */\n get idTaggedMaybe(): TaggedId | undefined {\n // Skip the deleted check so that `isPreloaded` doesn't blow up during em.refreshes/populates\n // ensureNotDeleted(this.entity, \"pending\");\n return maybeResolveReferenceToId(this.current());\n }\n\n get fieldName(): string {\n return this.#field.fieldName;\n }\n\n get otherFieldName(): keyof U & string {\n return this.#field.otherFieldName as keyof U & string;\n }\n\n private get idUntaggedMaybe(): string | undefined {\n return deTagId(this.otherMeta, this.idMaybe);\n }\n\n #setImpl(_other: U | IdOf<U> | N): void {\n ensureNotDeleted(this.entity, \"pending\");\n this.#hasBeenSet = true;\n\n // If the project is not using tagged ids, we still want it tagged internally\n const other = ensureTagged(this.otherMeta, _other);\n if (sameEntity(other, this.current({ withDeleted: true }))) {\n return;\n }\n\n const previousId = this.idTaggedMaybe;\n const previous = this.maybeFindEntity();\n // Prefer to keep the id in our data hash, but if this is a new entity w/o an id, use the entity itself\n setField(this.entity, this.fieldName, isEntity(other) ? (other?.idTaggedMaybe ?? other) : other);\n\n // Setting to an entity | undefined => loaded, setting to an id might reverse to unloaded\n const maybeEntity = isEntity(other) ? (other as U) : other ? this.entity.em.getEntity(other) : undefined;\n if (maybeEntity || other === undefined) {\n this.#state = new M2OLoadedState<U, N>(this, (maybeEntity ?? other) as U | N);\n } else {\n this.#state = new M2OUnloadedState<U, N>(this);\n }\n this.#loadPromise = undefined;\n this.#percolateRemove(previousId, previous);\n this.#percolateAdd();\n }\n\n // private impl\n\n setFromOpts(other: U | IdOf<U> | N): void {\n this.#setImpl(other);\n }\n\n maybeCascadeDelete(): void {\n if (this.isCascadeDelete) {\n const current = this.current({ withDeleted: true });\n if (current !== undefined) {\n if (typeof current === \"string\") {\n // We're invoked twice, once sync on em.delete, and again in em.flush when we're loaded;\n // but even when loaded, current might still be the tagged id, but we can find the entity in the EM\n const other = this.entity.em.getEntity(current);\n if (other) this.entity.em.delete(other as U);\n } else {\n this.entity.em.delete(current as U);\n }\n }\n }\n }\n\n async cleanupOnEntityDeleted(): Promise<void> {\n // if we are going to delete this relation as well, then we don't need to clean it up\n if (this.isCascadeDelete) return;\n const current = await this.load({ withDeleted: true });\n if (current !== undefined) {\n const o2m = this.getOtherRelation(current);\n if (o2m instanceof OneToManyCollection) {\n o2m.remove(this.entity, { requireLoaded: false });\n } else if (o2m instanceof OneToManyLargeCollection) {\n o2m.remove(this.entity);\n } else if (o2m instanceof OneToOneReferenceImpl) {\n o2m.set(undefined as any);\n } else {\n throw new Error(`Unhandled ${o2m}`);\n }\n }\n setField(this.entity, this.fieldName, undefined);\n this.#state = new M2OLoadedState<U, N>(this, undefined as any);\n }\n\n // Called on `book.author.set(...)` with our previous value, to percolate a `author.books.remove`. */\n #percolateRemove(prevId: string | undefined, prevEntity: U | undefined) {\n if (prevEntity) {\n const prevRelation = this.getOtherRelation(prevEntity);\n if (prevRelation instanceof OneToManyCollection) {\n prevRelation.removeIfLoaded(this.entity);\n } else if (prevRelation instanceof OneToManyLargeCollection) {\n prevRelation.remove(this.entity);\n } else {\n prevRelation.set(undefined as any, { percolating: true });\n }\n } else if (prevId) {\n // prevEntity is not loaded in memory, but cache it in case our other side is later loaded\n const { em } = this.entity;\n let map = getEmInternalApi(em).pendingPercolate.get(prevId);\n if (!map) {\n map = new Map();\n getEmInternalApi(em).pendingPercolate.set(prevId, map);\n }\n let pending = map.get(this.otherFieldName);\n if (!pending) {\n pending = { adds: [], removes: [] };\n map.set(this.otherFieldName, pending);\n }\n maybeAdd(pending.removes, this.entity);\n maybeRemove(pending.adds, this.entity);\n }\n }\n\n #percolateAdd() {\n const id = this.current();\n const other = this.maybeFindEntity();\n if (other) {\n // Other is already loaded in memory, immediately hook it up\n const newRelation = this.getOtherRelation(other);\n if (newRelation instanceof OneToManyCollection) {\n newRelation.add(this.entity);\n } else if (newRelation instanceof OneToManyLargeCollection) {\n newRelation.add(this.entity);\n } else if (newRelation) {\n newRelation.set(this.entity, { percolating: true });\n } else {\n // Something is wrong, we should always have a relation, but instead\n // of blowing up here, let this finish b/c it will probably turn into\n // a validation error.\n }\n } else if (typeof id === \"string\") {\n // Other is not loaded in memory, but cache it in case our other side is later loaded\n const { em } = this.entity;\n let map = getEmInternalApi(em).pendingPercolate.get(id);\n if (!map) {\n map = new Map();\n getEmInternalApi(em).pendingPercolate.set(id, map);\n }\n let pending = map.get(this.otherFieldName);\n if (!pending) {\n pending = { adds: [], removes: [] };\n map.set(this.otherFieldName, pending);\n }\n maybeAdd(pending.adds, this.entity);\n maybeRemove(pending.removes, this.entity);\n }\n }\n\n // We need to keep U in data[fieldName] to handle entities without an id assigned yet.\n current(opts?: { withDeleted?: boolean }): U | TaggedId | N {\n const current = getField(this.entity, this.fieldName);\n if (current !== undefined && isEntity(current)) {\n return this.filterDeleted(current as U, opts);\n }\n return current;\n }\n\n get otherMeta(): EntityMetadata<U> {\n return this.#field.otherMetadata();\n }\n\n get hasBeenSet(): boolean {\n return this.#hasBeenSet;\n }\n\n public toString(): string {\n return `ManyToOneReference(entity: ${this.entity}, fieldName: ${this.fieldName}, otherType: ${this.otherMeta.type}, otherFieldName: ${this.otherFieldName}, id: ${this.idMaybe})`;\n }\n\n /**\n * Removes pending-hard-delete (but not soft-deleted), unless explicitly asked for.\n *\n * Note that we leave soft-deleted entities b/c the call signature of a `book.author.get`\n * very likely does not expect a soft-deleted entity to result in `undefined`.\n *\n * (Contrasted with `author.books.get` which is more intuitive to have soft-deleted\n * entities filtered out, i.e. it doesn't fundamentally change the return type.)\n */\n private filterDeleted(entity: U | N, opts?: { withDeleted?: boolean }): U | N {\n if (entity && (!opts || !opts.withDeleted) && entity.isDeletedEntity) {\n return undefined!;\n }\n return entity;\n }\n\n /** Returns the other relation that points back at us, i.e. we're `book.author_id` and this is `Author.books`. */\n private getOtherRelation(\n other: U,\n ): OneToManyCollection<U, T> | OneToOneReferenceImpl<U, T> | OneToManyLargeCollection<U, T> {\n return (other as U)[this.otherFieldName] as any;\n }\n\n private get isCascadeDelete(): boolean {\n return isCascadeDelete(this, this.fieldName);\n }\n\n /**\n * Looks for an entity in `EntityManager`, b/c we may have it in memory even if\n * our reference is not specifically loaded.\n */\n maybeFindEntity(): U | undefined {\n // Check current() first b/c a new entity won't have an id yet\n const current = this.current();\n if (isEntity(current)) return current as U;\n const { idTaggedMaybe } = this;\n return idTaggedMaybe !== undefined ? (this.entity.em.getEntity(idTaggedMaybe) as U) : undefined;\n }\n\n [RelationT]: T = null!;\n [RelationU]: U = null!;\n [ReferenceN]: N = null!;\n}\n\n/**\n * Fails when we can't return an `.id` for a reference, i.e. it's unset or a new entity.\n *\n * We strictly type `book.author.id` as `AuthorId`, even if `book.author` is nullable, to\n * avoid users calling `.id` and getting back `undefined`, but not because \"there is no\n * author\", but only because \"the assigned author doesn't have an id assigned yet\".\n *\n * To handle nullable references, set `.idIfSet`.\n *\n * Assumes being called like `return idMaybe ?? failNoId()`, i.e. if we're called we\n * know the id is not available.\n */\nexport function failNoId(entity: Entity, fieldName: string, current: string | Entity | undefined): never {\n if (!current) fail(`Reference ${entity}.${fieldName} is unset`);\n // Throw NoIdError, which lets ReactionsManager will handle reactive fields gracefully\n if (current instanceof BaseEntity)\n throw new NoIdError(`Reference ${entity}.${fieldName} is assigned to a new entity`);\n fail(`Unreachable`);\n}\n\n/**\n * Fails when we can't return an `.idIfSet` for a reference, i.e. it's a new entity.\n *\n * Assumes being called like `return idMaybe ?? failIfNewEntity()`, i.e. if we're called we\n * know the id is not available.\n */\nexport function failIfNewEntity(entity: Entity, fieldName: string, current: string | Entity | undefined): undefined {\n if (current instanceof BaseEntity) {\n throw new NoIdError(`Reference ${entity}.${fieldName} is assigned to a new entity`);\n }\n // Since this is a `.idIfSet`, having no `current` is fine, return undefined.\n return undefined;\n}\n\ninterface M2OState<U extends Entity, N extends never | undefined> {\n readonly isLoaded: boolean;\n maybeInstantLoad(): M2OState<U, N>;\n applyLoad(loaded: U | N | undefined): M2OLoadedState<U, N>;\n import(target: ManyToOneReferenceImpl<any, any, any>, findEntity: (e: U) => U): M2OState<U, N>;\n doGet(): U | N | undefined;\n}\n\nclass M2OUnloadedState<U extends Entity, N extends never | undefined> implements M2OState<U, N> {\n readonly isLoaded = false;\n #m2o: ManyToOneReferenceImpl<any, any, any>;\n constructor(m2o: ManyToOneReferenceImpl<any, any, any>) {\n this.#m2o = m2o;\n }\n applyLoad(loaded: U | N | undefined): M2OLoadedState<U, N> {\n return new M2OLoadedState<U, N>(this.#m2o, loaded);\n }\n import(target: ManyToOneReferenceImpl<any, any, any>): M2OState<U, N> {\n return new M2OUnloadedState<U, N>(target);\n }\n maybeInstantLoad(): M2OState<U, N> {\n const current = this.#m2o.current();\n if (current === undefined || isEntity(current)) {\n return new M2OLoadedState<U, N>(this.#m2o, current as U | undefined);\n } else {\n const maybeFound = this.#m2o.entity.em.getEntity(current);\n if (maybeFound) {\n return new M2OLoadedState<U, N>(this.#m2o, maybeFound as U);\n }\n }\n return this;\n }\n doGet(): U | N | undefined {\n throw new Error(`${this.#m2o.entity}.${this.#m2o.fieldName} was not loaded`);\n }\n}\n\n/** The when loaded, with either the loaded entity or undefined. */\nclass M2OLoadedState<U extends Entity, N extends never | undefined> implements M2OState<U, N> {\n readonly isLoaded = true;\n #m2o: ManyToOneReferenceImpl<any, any, any>;\n #loaded: U | N | undefined;\n constructor(m2o: ManyToOneReferenceImpl<any, any, any>, loaded: U | N | undefined) {\n this.#loaded = loaded;\n this.#m2o = m2o;\n }\n applyLoad(loaded: U | N | undefined): M2OLoadedState<U, N> {\n return new M2OLoadedState<U, N>(this.#m2o, loaded);\n }\n import(target: ManyToOneReferenceImpl<any, any, any>, findEntity: (e: U) => U): M2OState<U, N> {\n return new M2OLoadedState<U, N>(target, isEntity(this.#loaded) ? findEntity(this.#loaded) : undefined);\n }\n maybeInstantLoad(): M2OState<U, N> {\n return this;\n }\n doGet(): U | N | undefined {\n return this.#loaded;\n }\n}\n"],"mappings":";;;;;;;;;;;;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,kBAAkB;;;;;;;iBAc9D,gBAAgB,QAAQ,QAAQ,mBAAmB,kBAAkB"}
@@ -158,7 +158,12 @@ var ManyToOneReferenceImpl = class extends AbstractRelationImpl {
158
158
  maybeCascadeDelete() {
159
159
  if (this.isCascadeDelete) {
160
160
  const current = this.current({ withDeleted: true });
161
- if (current !== void 0 && typeof current !== "string") this.entity.em.delete(current);
161
+ if (current !== void 0) {
162
+ if (typeof current === "string") {
163
+ const other = this.entity.em.getEntity(current);
164
+ if (other) this.entity.em.delete(other);
165
+ } else this.entity.em.delete(current);
166
+ }
162
167
  }
163
168
  }
164
169
  async cleanupOnEntityDeleted() {
@@ -1 +1 @@
1
- {"version":3,"file":"ManyToOneReference.js","names":["#field","#state","#loadPromise","#setImpl","#percolateRemove","#percolateAdd","#hasBeenSet","#m2o","#loaded"],"sources":["../../src/relations/ManyToOneReference.ts"],"sourcesContent":["import { type Entity, isEntity } from \"../Entity.ts\";\nimport { type IdOf, type TaggedId, getEmInternalApi, sameEntity } from \"../EntityManager.ts\";\nimport { type EntityMetadata, type ManyToOneField, getMetadata } from \"../EntityMetadata.ts\";\nimport { getField, setField } from \"../fields.ts\";\nimport {\n BaseEntity,\n NoIdError,\n OneToManyLargeCollection,\n OneToOneReferenceImpl,\n type Reference,\n deTagId,\n ensureNotDeleted,\n ensureTagged,\n fail,\n getInstanceData,\n maybeResolveReferenceToId,\n toIdOf,\n toTaggedId,\n} from \"../index.ts\";\nimport { lazyField } from \"../newEntity.ts\";\nimport { maybeAdd, maybeRemove } from \"../utils.ts\";\nimport { AbstractRelationImpl, isCascadeDelete } from \"./AbstractRelationImpl.ts\";\nimport { OneToManyCollection } from \"./OneToManyCollection.ts\";\nimport { ReferenceN } from \"./ReferenceSymbols.ts\";\nimport { RelationT, RelationU } from \"./RelationSymbols.ts\";\n\n/** An alias for creating `ManyToOneReference`s. */\nexport function hasOne<T extends Entity, U extends Entity, N extends never | undefined>(): ManyToOneReference<T, U, N> {\n return lazyField((entity: T, fieldName) => {\n const m2o = getMetadata(entity).allFields[fieldName] as ManyToOneField;\n return new ManyToOneReferenceImpl<T, U, N>(entity, m2o);\n });\n}\n\n/** Type guard utility for determining if an entity field is a ManyToOneReference. */\nexport function isManyToOneReference(maybeReference: any): maybeReference is ManyToOneReference<any, any, any> {\n return maybeReference instanceof ManyToOneReferenceImpl;\n}\n\nexport interface ManyToOneReference<T extends Entity, U extends Entity, N extends never | undefined> extends Reference<\n T,\n U,\n N\n> {\n /** Returns the id of the current assigned entity, or a runtime error if either 1) unset or 2) set to a new entity that doesn't have an `id` yet. */\n id: IdOf<U>;\n\n /** Returns the id of the current assigned entity, undefined if unset, or a runtime error if set to a new entity. */\n idIfSet: IdOf<U> | undefined;\n\n /** Returns the id of the current assigned entity, undefined if unset, or undefined if set to a new entity. */\n idMaybe: IdOf<U> | undefined;\n\n idUntagged: string;\n\n idUntaggedIfSet: string | undefined;\n\n /** Returns `true` if this relation is currently set (i.e. regardless of whether it's loaded, or if it is set but the assigned entity doesn't have an id saved. */\n readonly isSet: boolean;\n}\n\n/**\n * Manages a foreign key from one entity to another, i.e. `Book.author --> Author`.\n *\n * We keep the current `author` / `author_id` value in the `__orm.data` hash, where the\n * current value could be either the (string) author id from the database, or an entity\n * `Author` that the user has set.\n *\n * Note that if our `images.author_id` column is unique, this `ManyToOneReference` will essentially\n * be half of a one-to-one relationship, but we'll keep using this `ManyToOneReference` on the \"many\"\n * side, and the other side, i.e. `Author.image` will use a `OneToOneReference` to point back to us.\n */\nexport class ManyToOneReferenceImpl<T extends Entity, U extends Entity, N extends never | undefined>\n extends AbstractRelationImpl<T, U>\n implements ManyToOneReference<T, U, N>\n{\n readonly #field: ManyToOneField;\n #state: M2OState<U, N>;\n #loadPromise: Promise<void> | undefined;\n #hasBeenSet = false;\n\n constructor(entity: T, field: ManyToOneField) {\n super(entity);\n this.#field = field;\n this.#state = getInstanceData(entity).isOrWasNew\n ? new M2OLoadedState<U, N>(this, undefined)\n : new M2OUnloadedState<U, N>(this);\n }\n\n async load(opts: { withDeleted?: boolean; forceReload?: boolean } = {}): Promise<U | N> {\n ensureNotDeleted(this.entity, \"pending\");\n this.#state = this.#state.maybeInstantLoad();\n if (!this.#state.isLoaded || opts.forceReload) {\n const current = this.current();\n if (isEntity(current) || current === undefined) {\n this.#state = new M2OLoadedState<U, N>(this, current as U | N | undefined);\n } else {\n await (this.#loadPromise ??= this.entity.em.load(this.otherMeta.cstr, current).then((entity) => {\n this.#loadPromise = undefined;\n // In extremely rare cases, someone might have called `set` while this promise was in-flight,\n // so make sure our current value is still the same as the fetched entity.\n if (sameEntity(entity, this.current())) {\n this.#state = this.#state.applyLoad(entity);\n }\n }));\n }\n }\n return this.filterDeleted(this.#state.doGet() as U | N, opts);\n }\n\n set(other: U | N): void {\n this.#setImpl(other);\n }\n\n get isSet(): boolean {\n return this.current() !== undefined;\n }\n\n get isLoaded(): boolean {\n // Even if `.load()` has not been called, `.get` will detect\n this.#state = this.#state.maybeInstantLoad();\n return this.#state.isLoaded;\n }\n\n get isPreloaded(): boolean {\n return !!this.maybeFindEntity();\n }\n\n preload(): void {\n this.#state = new M2OLoadedState<U, N>(this, this.maybeFindEntity());\n }\n\n private unload(): void {\n this.#state = new M2OUnloadedState<U, N>(this);\n this.#loadPromise = undefined;\n }\n\n /** Copy another em's `source` relation into our state. */\n import(source: ManyToOneReferenceImpl<T, U, N>, findEntity: (e: U) => U): void {\n this.#state = source.#state.import(this, findEntity);\n }\n\n private doGet(opts?: { withDeleted?: boolean }): U | N {\n ensureNotDeleted(this.entity, \"pending\");\n this.#state = this.#state.maybeInstantLoad();\n return this.filterDeleted(this.#state.doGet() as U | N, opts);\n }\n\n get getWithDeleted(): U | N {\n return this.doGet({ withDeleted: true });\n }\n\n get get(): U | N {\n return this.doGet({ withDeleted: false });\n }\n\n /** Returns the id of the current value. */\n get id(): IdOf<U> {\n return this.idMaybe || failNoId(this.entity, this.fieldName, this.current());\n }\n\n /** Sets the m2o to `id`, and allows accepting `undefined` (`N`) if this is a nullable relation. */\n set id(id: IdOf<U> | N) {\n ensureNotDeleted(this.entity, \"pending\");\n const newId = toTaggedId(this.otherMeta, id);\n\n const previousId = this.idTaggedMaybe;\n const previous = this.maybeFindEntity();\n const changed = setField(this.entity, this.fieldName, id);\n if (!changed) {\n return;\n }\n\n const loaded = newId ? (this.entity.em.getEntity(newId) as U) : undefined;\n this.#state = loaded ? new M2OLoadedState<U, N>(this, loaded) : new M2OUnloadedState<U, N>(this);\n this.#loadPromise = undefined;\n this.#percolateRemove(previousId, previous);\n this.#percolateAdd();\n }\n\n get idIfSet(): IdOf<U> | N | undefined {\n return this.idMaybe || failIfNewEntity(this.entity, this.fieldName, this.current());\n }\n\n get idUntagged(): string {\n return this.idUntaggedMaybe || failNoId(this.entity, this.fieldName, this.current());\n }\n\n get idUntaggedIfSet(): string | undefined {\n return this.idUntaggedMaybe || failIfNewEntity(this.entity, this.fieldName, this.current());\n }\n\n get idMaybe(): IdOf<U> | N | undefined {\n ensureNotDeleted(this.entity, \"pending\");\n return toIdOf(this.otherMeta, this.idTaggedMaybe);\n }\n\n /** Returns the tagged id of the current value, or undefined if unset or a new entity. */\n get idTaggedMaybe(): TaggedId | undefined {\n // Skip the deleted check so that `isPreloaded` doesn't blow up during em.refreshes/populates\n // ensureNotDeleted(this.entity, \"pending\");\n return maybeResolveReferenceToId(this.current());\n }\n\n get fieldName(): string {\n return this.#field.fieldName;\n }\n\n get otherFieldName(): keyof U & string {\n return this.#field.otherFieldName as keyof U & string;\n }\n\n private get idUntaggedMaybe(): string | undefined {\n return deTagId(this.otherMeta, this.idMaybe);\n }\n\n #setImpl(_other: U | IdOf<U> | N): void {\n ensureNotDeleted(this.entity, \"pending\");\n this.#hasBeenSet = true;\n\n // If the project is not using tagged ids, we still want it tagged internally\n const other = ensureTagged(this.otherMeta, _other);\n if (sameEntity(other, this.current({ withDeleted: true }))) {\n return;\n }\n\n const previousId = this.idTaggedMaybe;\n const previous = this.maybeFindEntity();\n // Prefer to keep the id in our data hash, but if this is a new entity w/o an id, use the entity itself\n setField(this.entity, this.fieldName, isEntity(other) ? (other?.idTaggedMaybe ?? other) : other);\n\n // Setting to an entity | undefined => loaded, setting to an id might reverse to unloaded\n const maybeEntity = isEntity(other) ? (other as U) : other ? this.entity.em.getEntity(other) : undefined;\n if (maybeEntity || other === undefined) {\n this.#state = new M2OLoadedState<U, N>(this, (maybeEntity ?? other) as U | N);\n } else {\n this.#state = new M2OUnloadedState<U, N>(this);\n }\n this.#loadPromise = undefined;\n this.#percolateRemove(previousId, previous);\n this.#percolateAdd();\n }\n\n // private impl\n\n setFromOpts(other: U | IdOf<U> | N): void {\n this.#setImpl(other);\n }\n\n maybeCascadeDelete(): void {\n if (this.isCascadeDelete) {\n const current = this.current({ withDeleted: true });\n if (current !== undefined && typeof current !== \"string\") {\n this.entity.em.delete(current as U);\n }\n }\n }\n\n async cleanupOnEntityDeleted(): Promise<void> {\n // if we are going to delete this relation as well, then we don't need to clean it up\n if (this.isCascadeDelete) return;\n const current = await this.load({ withDeleted: true });\n if (current !== undefined) {\n const o2m = this.getOtherRelation(current);\n if (o2m instanceof OneToManyCollection) {\n o2m.remove(this.entity, { requireLoaded: false });\n } else if (o2m instanceof OneToManyLargeCollection) {\n o2m.remove(this.entity);\n } else if (o2m instanceof OneToOneReferenceImpl) {\n o2m.set(undefined as any);\n } else {\n throw new Error(`Unhandled ${o2m}`);\n }\n }\n setField(this.entity, this.fieldName, undefined);\n this.#state = new M2OLoadedState<U, N>(this, undefined as any);\n }\n\n // Called on `book.author.set(...)` with our previous value, to percolate a `author.books.remove`. */\n #percolateRemove(prevId: string | undefined, prevEntity: U | undefined) {\n if (prevEntity) {\n const prevRelation = this.getOtherRelation(prevEntity);\n if (prevRelation instanceof OneToManyCollection) {\n prevRelation.removeIfLoaded(this.entity);\n } else if (prevRelation instanceof OneToManyLargeCollection) {\n prevRelation.remove(this.entity);\n } else {\n prevRelation.set(undefined as any, { percolating: true });\n }\n } else if (prevId) {\n // prevEntity is not loaded in memory, but cache it in case our other side is later loaded\n const { em } = this.entity;\n let map = getEmInternalApi(em).pendingPercolate.get(prevId);\n if (!map) {\n map = new Map();\n getEmInternalApi(em).pendingPercolate.set(prevId, map);\n }\n let pending = map.get(this.otherFieldName);\n if (!pending) {\n pending = { adds: [], removes: [] };\n map.set(this.otherFieldName, pending);\n }\n maybeAdd(pending.removes, this.entity);\n maybeRemove(pending.adds, this.entity);\n }\n }\n\n #percolateAdd() {\n const id = this.current();\n const other = this.maybeFindEntity();\n if (other) {\n // Other is already loaded in memory, immediately hook it up\n const newRelation = this.getOtherRelation(other);\n if (newRelation instanceof OneToManyCollection) {\n newRelation.add(this.entity);\n } else if (newRelation instanceof OneToManyLargeCollection) {\n newRelation.add(this.entity);\n } else if (newRelation) {\n newRelation.set(this.entity, { percolating: true });\n } else {\n // Something is wrong, we should always have a relation, but instead\n // of blowing up here, let this finish b/c it will probably turn into\n // a validation error.\n }\n } else if (typeof id === \"string\") {\n // Other is not loaded in memory, but cache it in case our other side is later loaded\n const { em } = this.entity;\n let map = getEmInternalApi(em).pendingPercolate.get(id);\n if (!map) {\n map = new Map();\n getEmInternalApi(em).pendingPercolate.set(id, map);\n }\n let pending = map.get(this.otherFieldName);\n if (!pending) {\n pending = { adds: [], removes: [] };\n map.set(this.otherFieldName, pending);\n }\n maybeAdd(pending.adds, this.entity);\n maybeRemove(pending.removes, this.entity);\n }\n }\n\n // We need to keep U in data[fieldName] to handle entities without an id assigned yet.\n current(opts?: { withDeleted?: boolean }): U | TaggedId | N {\n const current = getField(this.entity, this.fieldName);\n if (current !== undefined && isEntity(current)) {\n return this.filterDeleted(current as U, opts);\n }\n return current;\n }\n\n get otherMeta(): EntityMetadata<U> {\n return this.#field.otherMetadata();\n }\n\n get hasBeenSet(): boolean {\n return this.#hasBeenSet;\n }\n\n public toString(): string {\n return `ManyToOneReference(entity: ${this.entity}, fieldName: ${this.fieldName}, otherType: ${this.otherMeta.type}, otherFieldName: ${this.otherFieldName}, id: ${this.idMaybe})`;\n }\n\n /**\n * Removes pending-hard-delete (but not soft-deleted), unless explicitly asked for.\n *\n * Note that we leave soft-deleted entities b/c the call signature of a `book.author.get`\n * very likely does not expect a soft-deleted entity to result in `undefined`.\n *\n * (Contrasted with `author.books.get` which is more intuitive to have soft-deleted\n * entities filtered out, i.e. it doesn't fundamentally change the return type.)\n */\n private filterDeleted(entity: U | N, opts?: { withDeleted?: boolean }): U | N {\n if (entity && (!opts || !opts.withDeleted) && entity.isDeletedEntity) {\n return undefined!;\n }\n return entity;\n }\n\n /** Returns the other relation that points back at us, i.e. we're `book.author_id` and this is `Author.books`. */\n private getOtherRelation(\n other: U,\n ): OneToManyCollection<U, T> | OneToOneReferenceImpl<U, T> | OneToManyLargeCollection<U, T> {\n return (other as U)[this.otherFieldName] as any;\n }\n\n private get isCascadeDelete(): boolean {\n return isCascadeDelete(this, this.fieldName);\n }\n\n /**\n * Looks for an entity in `EntityManager`, b/c we may have it in memory even if\n * our reference is not specifically loaded.\n */\n maybeFindEntity(): U | undefined {\n // Check current() first b/c a new entity won't have an id yet\n const current = this.current();\n if (isEntity(current)) return current as U;\n const { idTaggedMaybe } = this;\n return idTaggedMaybe !== undefined ? (this.entity.em.getEntity(idTaggedMaybe) as U) : undefined;\n }\n\n [RelationT]: T = null!;\n [RelationU]: U = null!;\n [ReferenceN]: N = null!;\n}\n\n/**\n * Fails when we can't return an `.id` for a reference, i.e. it's unset or a new entity.\n *\n * We strictly type `book.author.id` as `AuthorId`, even if `book.author` is nullable, to\n * avoid users calling `.id` and getting back `undefined`, but not because \"there is no\n * author\", but only because \"the assigned author doesn't have an id assigned yet\".\n *\n * To handle nullable references, set `.idIfSet`.\n *\n * Assumes being called like `return idMaybe ?? failNoId()`, i.e. if we're called we\n * know the id is not available.\n */\nexport function failNoId(entity: Entity, fieldName: string, current: string | Entity | undefined): never {\n if (!current) fail(`Reference ${entity}.${fieldName} is unset`);\n // Throw NoIdError, which lets ReactionsManager will handle reactive fields gracefully\n if (current instanceof BaseEntity)\n throw new NoIdError(`Reference ${entity}.${fieldName} is assigned to a new entity`);\n fail(`Unreachable`);\n}\n\n/**\n * Fails when we can't return an `.idIfSet` for a reference, i.e. it's a new entity.\n *\n * Assumes being called like `return idMaybe ?? failIfNewEntity()`, i.e. if we're called we\n * know the id is not available.\n */\nexport function failIfNewEntity(entity: Entity, fieldName: string, current: string | Entity | undefined): undefined {\n if (current instanceof BaseEntity) {\n throw new NoIdError(`Reference ${entity}.${fieldName} is assigned to a new entity`);\n }\n // Since this is a `.idIfSet`, having no `current` is fine, return undefined.\n return undefined;\n}\n\ninterface M2OState<U extends Entity, N extends never | undefined> {\n readonly isLoaded: boolean;\n maybeInstantLoad(): M2OState<U, N>;\n applyLoad(loaded: U | N | undefined): M2OLoadedState<U, N>;\n import(target: ManyToOneReferenceImpl<any, any, any>, findEntity: (e: U) => U): M2OState<U, N>;\n doGet(): U | N | undefined;\n}\n\nclass M2OUnloadedState<U extends Entity, N extends never | undefined> implements M2OState<U, N> {\n readonly isLoaded = false;\n #m2o: ManyToOneReferenceImpl<any, any, any>;\n constructor(m2o: ManyToOneReferenceImpl<any, any, any>) {\n this.#m2o = m2o;\n }\n applyLoad(loaded: U | N | undefined): M2OLoadedState<U, N> {\n return new M2OLoadedState<U, N>(this.#m2o, loaded);\n }\n import(target: ManyToOneReferenceImpl<any, any, any>): M2OState<U, N> {\n return new M2OUnloadedState<U, N>(target);\n }\n maybeInstantLoad(): M2OState<U, N> {\n const current = this.#m2o.current();\n if (current === undefined || isEntity(current)) {\n return new M2OLoadedState<U, N>(this.#m2o, current as U | undefined);\n } else {\n const maybeFound = this.#m2o.entity.em.getEntity(current);\n if (maybeFound) {\n return new M2OLoadedState<U, N>(this.#m2o, maybeFound as U);\n }\n }\n return this;\n }\n doGet(): U | N | undefined {\n throw new Error(`${this.#m2o.entity}.${this.#m2o.fieldName} was not loaded`);\n }\n}\n\n/** The when loaded, with either the loaded entity or undefined. */\nclass M2OLoadedState<U extends Entity, N extends never | undefined> implements M2OState<U, N> {\n readonly isLoaded = true;\n #m2o: ManyToOneReferenceImpl<any, any, any>;\n #loaded: U | N | undefined;\n constructor(m2o: ManyToOneReferenceImpl<any, any, any>, loaded: U | N | undefined) {\n this.#loaded = loaded;\n this.#m2o = m2o;\n }\n applyLoad(loaded: U | N | undefined): M2OLoadedState<U, N> {\n return new M2OLoadedState<U, N>(this.#m2o, loaded);\n }\n import(target: ManyToOneReferenceImpl<any, any, any>, findEntity: (e: U) => U): M2OState<U, N> {\n return new M2OLoadedState<U, N>(target, isEntity(this.#loaded) ? findEntity(this.#loaded) : undefined);\n }\n maybeInstantLoad(): M2OState<U, N> {\n return this;\n }\n doGet(): U | N | undefined {\n return this.#loaded;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA2BA,SAAgB,SAAuG;CACrH,OAAO,WAAW,QAAW,cAAc;EACzC,MAAM,MAAM,YAAY,MAAM,CAAC,CAAC,UAAU;EAC1C,OAAO,IAAI,uBAAgC,QAAQ,GAAG;CACxD,CAAC;AACH;;AAGA,SAAgB,qBAAqB,gBAA0E;CAC7G,OAAO,0BAA0B;AACnC;;;;;;;;;;;;AAmCA,IAAa,yBAAb,cACU,qBAEV;CACE;CACA;CACA;CACA,cAAc;CAEd,YAAY,QAAW,OAAuB;EAC5C,MAAM,MAAM;EACZ,KAAKA,SAAS;EACd,KAAKC,SAAS,gBAAgB,MAAM,CAAC,CAAC,aAClC,IAAI,eAAqB,MAAM,KAAA,CAAS,IACxC,IAAI,iBAAuB,IAAI;CACrC;CAEA,MAAM,KAAK,OAAyD,CAAC,GAAmB;EACtF,iBAAiB,KAAK,QAAQ,SAAS;EACvC,KAAKA,SAAS,KAAKA,OAAO,iBAAiB;EAC3C,IAAI,CAAC,KAAKA,OAAO,YAAY,KAAK,aAAa;GAC7C,MAAM,UAAU,KAAK,QAAQ;GAC7B,IAAI,SAAS,OAAO,KAAK,YAAY,KAAA,GACnC,KAAKA,SAAS,IAAI,eAAq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AO,IAAI,WAAW,KAAKA,OAAO,IAAI,KAAA,CAAS;CACvG;CACA,mBAAmC;EACjC,OAAO;CACT;CACA,QAA2B;EACzB,OAAO,KAAKA;CACd;AACF"}
1
+ {"version":3,"file":"ManyToOneReference.js","names":["#field","#state","#loadPromise","#setImpl","#percolateRemove","#percolateAdd","#hasBeenSet","#m2o","#loaded"],"sources":["../../src/relations/ManyToOneReference.ts"],"sourcesContent":["import { type Entity, isEntity } from \"../Entity.ts\";\nimport { type IdOf, type TaggedId, getEmInternalApi, sameEntity } from \"../EntityManager.ts\";\nimport { type EntityMetadata, type ManyToOneField, getMetadata } from \"../EntityMetadata.ts\";\nimport { getField, setField } from \"../fields.ts\";\nimport {\n BaseEntity,\n NoIdError,\n OneToManyLargeCollection,\n OneToOneReferenceImpl,\n type Reference,\n deTagId,\n ensureNotDeleted,\n ensureTagged,\n fail,\n getInstanceData,\n maybeResolveReferenceToId,\n toIdOf,\n toTaggedId,\n} from \"../index.ts\";\nimport { lazyField } from \"../newEntity.ts\";\nimport { maybeAdd, maybeRemove } from \"../utils.ts\";\nimport { AbstractRelationImpl, isCascadeDelete } from \"./AbstractRelationImpl.ts\";\nimport { OneToManyCollection } from \"./OneToManyCollection.ts\";\nimport { ReferenceN } from \"./ReferenceSymbols.ts\";\nimport { RelationT, RelationU } from \"./RelationSymbols.ts\";\n\n/** An alias for creating `ManyToOneReference`s. */\nexport function hasOne<T extends Entity, U extends Entity, N extends never | undefined>(): ManyToOneReference<T, U, N> {\n return lazyField((entity: T, fieldName) => {\n const m2o = getMetadata(entity).allFields[fieldName] as ManyToOneField;\n return new ManyToOneReferenceImpl<T, U, N>(entity, m2o);\n });\n}\n\n/** Type guard utility for determining if an entity field is a ManyToOneReference. */\nexport function isManyToOneReference(maybeReference: any): maybeReference is ManyToOneReference<any, any, any> {\n return maybeReference instanceof ManyToOneReferenceImpl;\n}\n\nexport interface ManyToOneReference<T extends Entity, U extends Entity, N extends never | undefined> extends Reference<\n T,\n U,\n N\n> {\n /** Returns the id of the current assigned entity, or a runtime error if either 1) unset or 2) set to a new entity that doesn't have an `id` yet. */\n id: IdOf<U>;\n\n /** Returns the id of the current assigned entity, undefined if unset, or a runtime error if set to a new entity. */\n idIfSet: IdOf<U> | undefined;\n\n /** Returns the id of the current assigned entity, undefined if unset, or undefined if set to a new entity. */\n idMaybe: IdOf<U> | undefined;\n\n idUntagged: string;\n\n idUntaggedIfSet: string | undefined;\n\n /** Returns `true` if this relation is currently set (i.e. regardless of whether it's loaded, or if it is set but the assigned entity doesn't have an id saved. */\n readonly isSet: boolean;\n}\n\n/**\n * Manages a foreign key from one entity to another, i.e. `Book.author --> Author`.\n *\n * We keep the current `author` / `author_id` value in the `__orm.data` hash, where the\n * current value could be either the (string) author id from the database, or an entity\n * `Author` that the user has set.\n *\n * Note that if our `images.author_id` column is unique, this `ManyToOneReference` will essentially\n * be half of a one-to-one relationship, but we'll keep using this `ManyToOneReference` on the \"many\"\n * side, and the other side, i.e. `Author.image` will use a `OneToOneReference` to point back to us.\n */\nexport class ManyToOneReferenceImpl<T extends Entity, U extends Entity, N extends never | undefined>\n extends AbstractRelationImpl<T, U>\n implements ManyToOneReference<T, U, N>\n{\n readonly #field: ManyToOneField;\n #state: M2OState<U, N>;\n #loadPromise: Promise<void> | undefined;\n #hasBeenSet = false;\n\n constructor(entity: T, field: ManyToOneField) {\n super(entity);\n this.#field = field;\n this.#state = getInstanceData(entity).isOrWasNew\n ? new M2OLoadedState<U, N>(this, undefined)\n : new M2OUnloadedState<U, N>(this);\n }\n\n async load(opts: { withDeleted?: boolean; forceReload?: boolean } = {}): Promise<U | N> {\n ensureNotDeleted(this.entity, \"pending\");\n this.#state = this.#state.maybeInstantLoad();\n if (!this.#state.isLoaded || opts.forceReload) {\n const current = this.current();\n if (isEntity(current) || current === undefined) {\n this.#state = new M2OLoadedState<U, N>(this, current as U | N | undefined);\n } else {\n await (this.#loadPromise ??= this.entity.em.load(this.otherMeta.cstr, current).then((entity) => {\n this.#loadPromise = undefined;\n // In extremely rare cases, someone might have called `set` while this promise was in-flight,\n // so make sure our current value is still the same as the fetched entity.\n if (sameEntity(entity, this.current())) {\n this.#state = this.#state.applyLoad(entity);\n }\n }));\n }\n }\n return this.filterDeleted(this.#state.doGet() as U | N, opts);\n }\n\n set(other: U | N): void {\n this.#setImpl(other);\n }\n\n get isSet(): boolean {\n return this.current() !== undefined;\n }\n\n get isLoaded(): boolean {\n // Even if `.load()` has not been called, `.get` will detect\n this.#state = this.#state.maybeInstantLoad();\n return this.#state.isLoaded;\n }\n\n get isPreloaded(): boolean {\n return !!this.maybeFindEntity();\n }\n\n preload(): void {\n this.#state = new M2OLoadedState<U, N>(this, this.maybeFindEntity());\n }\n\n private unload(): void {\n this.#state = new M2OUnloadedState<U, N>(this);\n this.#loadPromise = undefined;\n }\n\n /** Copy another em's `source` relation into our state. */\n import(source: ManyToOneReferenceImpl<T, U, N>, findEntity: (e: U) => U): void {\n this.#state = source.#state.import(this, findEntity);\n }\n\n private doGet(opts?: { withDeleted?: boolean }): U | N {\n ensureNotDeleted(this.entity, \"pending\");\n this.#state = this.#state.maybeInstantLoad();\n return this.filterDeleted(this.#state.doGet() as U | N, opts);\n }\n\n get getWithDeleted(): U | N {\n return this.doGet({ withDeleted: true });\n }\n\n get get(): U | N {\n return this.doGet({ withDeleted: false });\n }\n\n /** Returns the id of the current value. */\n get id(): IdOf<U> {\n return this.idMaybe || failNoId(this.entity, this.fieldName, this.current());\n }\n\n /** Sets the m2o to `id`, and allows accepting `undefined` (`N`) if this is a nullable relation. */\n set id(id: IdOf<U> | N) {\n ensureNotDeleted(this.entity, \"pending\");\n const newId = toTaggedId(this.otherMeta, id);\n\n const previousId = this.idTaggedMaybe;\n const previous = this.maybeFindEntity();\n const changed = setField(this.entity, this.fieldName, id);\n if (!changed) {\n return;\n }\n\n const loaded = newId ? (this.entity.em.getEntity(newId) as U) : undefined;\n this.#state = loaded ? new M2OLoadedState<U, N>(this, loaded) : new M2OUnloadedState<U, N>(this);\n this.#loadPromise = undefined;\n this.#percolateRemove(previousId, previous);\n this.#percolateAdd();\n }\n\n get idIfSet(): IdOf<U> | N | undefined {\n return this.idMaybe || failIfNewEntity(this.entity, this.fieldName, this.current());\n }\n\n get idUntagged(): string {\n return this.idUntaggedMaybe || failNoId(this.entity, this.fieldName, this.current());\n }\n\n get idUntaggedIfSet(): string | undefined {\n return this.idUntaggedMaybe || failIfNewEntity(this.entity, this.fieldName, this.current());\n }\n\n get idMaybe(): IdOf<U> | N | undefined {\n ensureNotDeleted(this.entity, \"pending\");\n return toIdOf(this.otherMeta, this.idTaggedMaybe);\n }\n\n /** Returns the tagged id of the current value, or undefined if unset or a new entity. */\n get idTaggedMaybe(): TaggedId | undefined {\n // Skip the deleted check so that `isPreloaded` doesn't blow up during em.refreshes/populates\n // ensureNotDeleted(this.entity, \"pending\");\n return maybeResolveReferenceToId(this.current());\n }\n\n get fieldName(): string {\n return this.#field.fieldName;\n }\n\n get otherFieldName(): keyof U & string {\n return this.#field.otherFieldName as keyof U & string;\n }\n\n private get idUntaggedMaybe(): string | undefined {\n return deTagId(this.otherMeta, this.idMaybe);\n }\n\n #setImpl(_other: U | IdOf<U> | N): void {\n ensureNotDeleted(this.entity, \"pending\");\n this.#hasBeenSet = true;\n\n // If the project is not using tagged ids, we still want it tagged internally\n const other = ensureTagged(this.otherMeta, _other);\n if (sameEntity(other, this.current({ withDeleted: true }))) {\n return;\n }\n\n const previousId = this.idTaggedMaybe;\n const previous = this.maybeFindEntity();\n // Prefer to keep the id in our data hash, but if this is a new entity w/o an id, use the entity itself\n setField(this.entity, this.fieldName, isEntity(other) ? (other?.idTaggedMaybe ?? other) : other);\n\n // Setting to an entity | undefined => loaded, setting to an id might reverse to unloaded\n const maybeEntity = isEntity(other) ? (other as U) : other ? this.entity.em.getEntity(other) : undefined;\n if (maybeEntity || other === undefined) {\n this.#state = new M2OLoadedState<U, N>(this, (maybeEntity ?? other) as U | N);\n } else {\n this.#state = new M2OUnloadedState<U, N>(this);\n }\n this.#loadPromise = undefined;\n this.#percolateRemove(previousId, previous);\n this.#percolateAdd();\n }\n\n // private impl\n\n setFromOpts(other: U | IdOf<U> | N): void {\n this.#setImpl(other);\n }\n\n maybeCascadeDelete(): void {\n if (this.isCascadeDelete) {\n const current = this.current({ withDeleted: true });\n if (current !== undefined) {\n if (typeof current === \"string\") {\n // We're invoked twice, once sync on em.delete, and again in em.flush when we're loaded;\n // but even when loaded, current might still be the tagged id, but we can find the entity in the EM\n const other = this.entity.em.getEntity(current);\n if (other) this.entity.em.delete(other as U);\n } else {\n this.entity.em.delete(current as U);\n }\n }\n }\n }\n\n async cleanupOnEntityDeleted(): Promise<void> {\n // if we are going to delete this relation as well, then we don't need to clean it up\n if (this.isCascadeDelete) return;\n const current = await this.load({ withDeleted: true });\n if (current !== undefined) {\n const o2m = this.getOtherRelation(current);\n if (o2m instanceof OneToManyCollection) {\n o2m.remove(this.entity, { requireLoaded: false });\n } else if (o2m instanceof OneToManyLargeCollection) {\n o2m.remove(this.entity);\n } else if (o2m instanceof OneToOneReferenceImpl) {\n o2m.set(undefined as any);\n } else {\n throw new Error(`Unhandled ${o2m}`);\n }\n }\n setField(this.entity, this.fieldName, undefined);\n this.#state = new M2OLoadedState<U, N>(this, undefined as any);\n }\n\n // Called on `book.author.set(...)` with our previous value, to percolate a `author.books.remove`. */\n #percolateRemove(prevId: string | undefined, prevEntity: U | undefined) {\n if (prevEntity) {\n const prevRelation = this.getOtherRelation(prevEntity);\n if (prevRelation instanceof OneToManyCollection) {\n prevRelation.removeIfLoaded(this.entity);\n } else if (prevRelation instanceof OneToManyLargeCollection) {\n prevRelation.remove(this.entity);\n } else {\n prevRelation.set(undefined as any, { percolating: true });\n }\n } else if (prevId) {\n // prevEntity is not loaded in memory, but cache it in case our other side is later loaded\n const { em } = this.entity;\n let map = getEmInternalApi(em).pendingPercolate.get(prevId);\n if (!map) {\n map = new Map();\n getEmInternalApi(em).pendingPercolate.set(prevId, map);\n }\n let pending = map.get(this.otherFieldName);\n if (!pending) {\n pending = { adds: [], removes: [] };\n map.set(this.otherFieldName, pending);\n }\n maybeAdd(pending.removes, this.entity);\n maybeRemove(pending.adds, this.entity);\n }\n }\n\n #percolateAdd() {\n const id = this.current();\n const other = this.maybeFindEntity();\n if (other) {\n // Other is already loaded in memory, immediately hook it up\n const newRelation = this.getOtherRelation(other);\n if (newRelation instanceof OneToManyCollection) {\n newRelation.add(this.entity);\n } else if (newRelation instanceof OneToManyLargeCollection) {\n newRelation.add(this.entity);\n } else if (newRelation) {\n newRelation.set(this.entity, { percolating: true });\n } else {\n // Something is wrong, we should always have a relation, but instead\n // of blowing up here, let this finish b/c it will probably turn into\n // a validation error.\n }\n } else if (typeof id === \"string\") {\n // Other is not loaded in memory, but cache it in case our other side is later loaded\n const { em } = this.entity;\n let map = getEmInternalApi(em).pendingPercolate.get(id);\n if (!map) {\n map = new Map();\n getEmInternalApi(em).pendingPercolate.set(id, map);\n }\n let pending = map.get(this.otherFieldName);\n if (!pending) {\n pending = { adds: [], removes: [] };\n map.set(this.otherFieldName, pending);\n }\n maybeAdd(pending.adds, this.entity);\n maybeRemove(pending.removes, this.entity);\n }\n }\n\n // We need to keep U in data[fieldName] to handle entities without an id assigned yet.\n current(opts?: { withDeleted?: boolean }): U | TaggedId | N {\n const current = getField(this.entity, this.fieldName);\n if (current !== undefined && isEntity(current)) {\n return this.filterDeleted(current as U, opts);\n }\n return current;\n }\n\n get otherMeta(): EntityMetadata<U> {\n return this.#field.otherMetadata();\n }\n\n get hasBeenSet(): boolean {\n return this.#hasBeenSet;\n }\n\n public toString(): string {\n return `ManyToOneReference(entity: ${this.entity}, fieldName: ${this.fieldName}, otherType: ${this.otherMeta.type}, otherFieldName: ${this.otherFieldName}, id: ${this.idMaybe})`;\n }\n\n /**\n * Removes pending-hard-delete (but not soft-deleted), unless explicitly asked for.\n *\n * Note that we leave soft-deleted entities b/c the call signature of a `book.author.get`\n * very likely does not expect a soft-deleted entity to result in `undefined`.\n *\n * (Contrasted with `author.books.get` which is more intuitive to have soft-deleted\n * entities filtered out, i.e. it doesn't fundamentally change the return type.)\n */\n private filterDeleted(entity: U | N, opts?: { withDeleted?: boolean }): U | N {\n if (entity && (!opts || !opts.withDeleted) && entity.isDeletedEntity) {\n return undefined!;\n }\n return entity;\n }\n\n /** Returns the other relation that points back at us, i.e. we're `book.author_id` and this is `Author.books`. */\n private getOtherRelation(\n other: U,\n ): OneToManyCollection<U, T> | OneToOneReferenceImpl<U, T> | OneToManyLargeCollection<U, T> {\n return (other as U)[this.otherFieldName] as any;\n }\n\n private get isCascadeDelete(): boolean {\n return isCascadeDelete(this, this.fieldName);\n }\n\n /**\n * Looks for an entity in `EntityManager`, b/c we may have it in memory even if\n * our reference is not specifically loaded.\n */\n maybeFindEntity(): U | undefined {\n // Check current() first b/c a new entity won't have an id yet\n const current = this.current();\n if (isEntity(current)) return current as U;\n const { idTaggedMaybe } = this;\n return idTaggedMaybe !== undefined ? (this.entity.em.getEntity(idTaggedMaybe) as U) : undefined;\n }\n\n [RelationT]: T = null!;\n [RelationU]: U = null!;\n [ReferenceN]: N = null!;\n}\n\n/**\n * Fails when we can't return an `.id` for a reference, i.e. it's unset or a new entity.\n *\n * We strictly type `book.author.id` as `AuthorId`, even if `book.author` is nullable, to\n * avoid users calling `.id` and getting back `undefined`, but not because \"there is no\n * author\", but only because \"the assigned author doesn't have an id assigned yet\".\n *\n * To handle nullable references, set `.idIfSet`.\n *\n * Assumes being called like `return idMaybe ?? failNoId()`, i.e. if we're called we\n * know the id is not available.\n */\nexport function failNoId(entity: Entity, fieldName: string, current: string | Entity | undefined): never {\n if (!current) fail(`Reference ${entity}.${fieldName} is unset`);\n // Throw NoIdError, which lets ReactionsManager will handle reactive fields gracefully\n if (current instanceof BaseEntity)\n throw new NoIdError(`Reference ${entity}.${fieldName} is assigned to a new entity`);\n fail(`Unreachable`);\n}\n\n/**\n * Fails when we can't return an `.idIfSet` for a reference, i.e. it's a new entity.\n *\n * Assumes being called like `return idMaybe ?? failIfNewEntity()`, i.e. if we're called we\n * know the id is not available.\n */\nexport function failIfNewEntity(entity: Entity, fieldName: string, current: string | Entity | undefined): undefined {\n if (current instanceof BaseEntity) {\n throw new NoIdError(`Reference ${entity}.${fieldName} is assigned to a new entity`);\n }\n // Since this is a `.idIfSet`, having no `current` is fine, return undefined.\n return undefined;\n}\n\ninterface M2OState<U extends Entity, N extends never | undefined> {\n readonly isLoaded: boolean;\n maybeInstantLoad(): M2OState<U, N>;\n applyLoad(loaded: U | N | undefined): M2OLoadedState<U, N>;\n import(target: ManyToOneReferenceImpl<any, any, any>, findEntity: (e: U) => U): M2OState<U, N>;\n doGet(): U | N | undefined;\n}\n\nclass M2OUnloadedState<U extends Entity, N extends never | undefined> implements M2OState<U, N> {\n readonly isLoaded = false;\n #m2o: ManyToOneReferenceImpl<any, any, any>;\n constructor(m2o: ManyToOneReferenceImpl<any, any, any>) {\n this.#m2o = m2o;\n }\n applyLoad(loaded: U | N | undefined): M2OLoadedState<U, N> {\n return new M2OLoadedState<U, N>(this.#m2o, loaded);\n }\n import(target: ManyToOneReferenceImpl<any, any, any>): M2OState<U, N> {\n return new M2OUnloadedState<U, N>(target);\n }\n maybeInstantLoad(): M2OState<U, N> {\n const current = this.#m2o.current();\n if (current === undefined || isEntity(current)) {\n return new M2OLoadedState<U, N>(this.#m2o, current as U | undefined);\n } else {\n const maybeFound = this.#m2o.entity.em.getEntity(current);\n if (maybeFound) {\n return new M2OLoadedState<U, N>(this.#m2o, maybeFound as U);\n }\n }\n return this;\n }\n doGet(): U | N | undefined {\n throw new Error(`${this.#m2o.entity}.${this.#m2o.fieldName} was not loaded`);\n }\n}\n\n/** The when loaded, with either the loaded entity or undefined. */\nclass M2OLoadedState<U extends Entity, N extends never | undefined> implements M2OState<U, N> {\n readonly isLoaded = true;\n #m2o: ManyToOneReferenceImpl<any, any, any>;\n #loaded: U | N | undefined;\n constructor(m2o: ManyToOneReferenceImpl<any, any, any>, loaded: U | N | undefined) {\n this.#loaded = loaded;\n this.#m2o = m2o;\n }\n applyLoad(loaded: U | N | undefined): M2OLoadedState<U, N> {\n return new M2OLoadedState<U, N>(this.#m2o, loaded);\n }\n import(target: ManyToOneReferenceImpl<any, any, any>, findEntity: (e: U) => U): M2OState<U, N> {\n return new M2OLoadedState<U, N>(target, isEntity(this.#loaded) ? findEntity(this.#loaded) : undefined);\n }\n maybeInstantLoad(): M2OState<U, N> {\n return this;\n }\n doGet(): U | N | undefined {\n return this.#loaded;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA2BA,SAAgB,SAAuG;CACrH,OAAO,WAAW,QAAW,cAAc;EACzC,MAAM,MAAM,YAAY,MAAM,CAAC,CAAC,UAAU;EAC1C,OAAO,IAAI,uBAAgC,QAAQ,GAAG;CACxD,CAAC;AACH;;AAGA,SAAgB,qBAAqB,gBAA0E;CAC7G,OAAO,0BAA0B;AACnC;;;;;;;;;;;;AAmCA,IAAa,yBAAb,cACU,qBAEV;CACE;CACA;CACA;CACA,cAAc;CAEd,YAAY,QAAW,OAAuB;EAC5C,MAAM,MAAM;EACZ,KAAKA,SAAS;EACd,KAAKC,SAAS,gBAAgB,MAAM,CAAC,CAAC,aAClC,IAAI,eAAqB,MAAM,KAAA,CAAS,IACxC,IAAI,iBAAuB,IAAI;CACrC;CAEA,MAAM,KAAK,OAAyD,CAAC,GAAmB;EACtF,iBAAiB,KAAK,QAAQ,SAAS;EACvC,KAAKA,SAAS,KAAKA,OAAO,iBAAiB;EAC3C,IAAI,CAAC,KAAKA,OAAO,YAAY,KAAK,aAAa;GAC7C,MAAM,UAAU,KAAK,QAAQ;GAC7B,IAAI,SAAS,OAAO,KAAK,YAAY,KAAA,GACnC,KAAKA,SAAS,IAAI,eAAqB,MAAM,OAA4B;QAEzE,OAAO,KAAKC,iBAAiB,KAAK,OAAO,GAAG,KAAK,KAAK,UAAU,MAAM,OAAO,CAAC,CAAC,MAAM,WAAW;IAC9F,KAAKA,eAAe,KAAA;IAG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KA,OAAO,IAAI,KAAA,CAAS;CACvG;CACA,mBAAmC;EACjC,OAAO;CACT;CACA,QAA2B;EACzB,OAAO,KAAKA;CACd;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "joist-core",
3
- "version": "2.3.0-next.45",
3
+ "version": "2.3.0-next.47",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {
@@ -42,20 +42,20 @@
42
42
  "build"
43
43
  ],
44
44
  "peerDependencies": {
45
- "joist-utils": "2.3.0-next.45"
45
+ "joist-utils": "2.3.0-next.47"
46
46
  },
47
47
  "dependencies": {
48
48
  "ansis": "^4.3.1",
49
49
  "dataloader": "^2.2.3",
50
- "temporal-spec": "^0.3.1"
50
+ "temporal-spec": "^1.0.1"
51
51
  },
52
52
  "devDependencies": {
53
- "@swc/core": "^1.15.47",
53
+ "@swc/core": "^1.16.0",
54
54
  "@swc/jest": "^0.2.39",
55
55
  "@types/jest": "^30.0.0",
56
56
  "@types/node": "^26.2.0",
57
57
  "jest": "30.4.2",
58
- "temporal-polyfill": "^0.3.2",
58
+ "temporal-polyfill": "^1.0.4",
59
59
  "tsconfig-paths": "^4.2.0",
60
60
  "typescript": "7.0.2"
61
61
  }