joist-core 2.3.0-next.72 → 2.3.0-next.73

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/Aliases.cjs CHANGED
@@ -34,7 +34,19 @@ function newAliasProxy(cstr) {
34
34
  const meta = require_EntityMetadata.getMetadata(cstr);
35
35
  const mgmt = {
36
36
  tableName: meta.tableName,
37
- meta
37
+ meta,
38
+ condition(build) {
39
+ return require_DeferredAlias.withDeferredAlias({
40
+ kind: "raw",
41
+ aliases: [],
42
+ condition: "unset",
43
+ bindings: [],
44
+ pruneable: false
45
+ }, (resolve, copy) => {
46
+ const bound = resolve(mgmt);
47
+ Object.assign(copy, build(bound.meta, bound.alias));
48
+ });
49
+ }
38
50
  };
39
51
  return new Proxy(cstr, {
40
52
  get(_, key) {
@@ -1 +1 @@
1
- {"version":3,"file":"Aliases.cjs","names":["getMetadata","fail","skipCondition","makeLike","withDeferredAlias","mapToDb","groupBy","getConstructorFromTaggedId","maybeResolveReferenceToId","getBaseAndSelfMetas"],"sources":["../src/Aliases.ts"],"sourcesContent":["import { groupBy } from \"joist-utils\";\n\nimport { type Column } from \"./columns.ts\";\n// Load configure first: relations must not evaluate before their base classes exist.\nimport { getConstructorFromTaggedId } from \"./configure.ts\";\nimport { withDeferredAlias } from \"./DeferredAlias.ts\";\nimport { type Entity } from \"./Entity.ts\";\nimport { type ExpressionCondition } from \"./EntityFilter.ts\";\nimport { type IdOf, type MaybeAbstractEntityConstructor, type TaggedId } from \"./EntityManager.ts\";\nimport {\n type EntityMetadata,\n type Field,\n type PolymorphicField,\n type PolymorphicFieldComponent,\n getBaseAndSelfMetas,\n getMetadata,\n} from \"./EntityMetadata.ts\";\nimport { maybeResolveReferenceToId } from \"./keys.ts\";\nimport { type ColumnCondition, type ParsedValueFilter, type RawCondition, makeLike, mapToDb } from \"./QueryParser.ts\";\nimport { skipCondition } from \"./skipCondition.ts\";\nimport { type FieldsOf } from \"./typeMap.ts\";\nimport { fail } from \"./utils.ts\";\n\n/** Creates an alias for complex domain filtering in `em.find`. */\nexport function alias<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Alias<T> {\n return newAliasProxy(cstr);\n}\n\n/** Creates multiple aliases for complex filtering. */\nexport function aliases<T extends readonly MaybeAbstractEntityConstructor<any>[]>(\n ...types: T\n): { [P in keyof T]: T[P] extends MaybeAbstractEntityConstructor<infer E extends Entity> ? Alias<E> : never } {\n return types.map((t) => newAliasProxy(t)) as any;\n}\n\nexport const aliasMgmt = Symbol(\"aliasMgmt\");\nconst aliasColumn = Symbol(\"aliasColumn\");\n\n/** The identity and original entity metadata of an `em.find` alias. */\nexport interface AliasMgmt {\n tableName: string;\n meta: EntityMetadata;\n}\n\n/** Keeps domain aliases covariant in their entity type. */\nexport interface AliasBrand<T> extends AliasMgmt {\n readonly __entity: T;\n}\n\n/**\n * Domain field predicates, deliberately separate from SQL table expressions.\n *\n * An Alias names a place in an em.find relationship tree, not a fixed SQL table occurrence.\n * I.e. with `a = alias(Author)`, `{ author: { as: a } }` binds `a` to the Book's Author;\n * binding it under `{ author: { mentor: { as: a } } }` instead makes it the Author's mentor.\n * The find parser builds those joins and knows which entity metadata and SQL name apply there,\n * including any inheritance joins. The Alias cannot know that when `a.firstName.eq(name)` is created.\n *\n * DeferredAlias therefore resolves each predicate to a fresh column/raw condition using that parse's\n * binding. The resolved condition records the SQL aliases it reads, including both sides of a\n * cross-alias comparison. Join pruning uses those references to retain the joins the filter needs.\n * An undefined predicate is skipped, so it does not keep an otherwise-unused join alive; explicit\n * keepAliases and other query dependencies can still retain that join.\n *\n * Table expressions solve a different problem: their sources are supplied directly in from/join,\n * so they render through ExprContext rather than asking the find parser to bind a relationship path.\n * Neither path stores a resolved SQL name on the reusable handle or predicate.\n */\nexport type Alias<T extends Entity> = { readonly [aliasMgmt]: AliasBrand<T> } & {\n [P in keyof FieldsOf<T>]: P extends \"id\"\n ? EntityAlias<T>\n : FieldsOf<T>[P] extends { kind: \"primitive\" | \"enum\"; type: infer V; nullable: infer N }\n ? PrimitiveAlias<V, N extends undefined ? null : never>\n : FieldsOf<T>[P] extends { kind: \"m2o\"; type: infer U extends Entity; nullable: infer N }\n ? EntityAlias<U, N extends undefined ? null : never>\n : FieldsOf<T>[P] extends { kind: \"poly\"; type: infer U extends Entity }\n ? PolyAlias<U>\n : never;\n};\n\n/** A domain column accepted on the other side of a cross-alias comparison. */\nexport interface AliasColumn<V> {\n readonly [aliasColumn]: V;\n}\n\nexport interface PrimitiveAlias<V, N extends null | never = never> extends AliasColumn<V | N> {\n eq(value: V | N | AliasColumn<V | null> | undefined): ExpressionCondition;\n ne(value: V | N | AliasColumn<V | null> | undefined): ExpressionCondition;\n in(values: readonly (V | null)[] | undefined): ExpressionCondition;\n nin(values: readonly (V | null)[] | undefined): ExpressionCondition;\n gt(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n gte(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n lt(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n lte(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n like(value: V | undefined): ExpressionCondition;\n ilike(value: V | undefined): ExpressionCondition;\n search(value: V | undefined): ExpressionCondition;\n between(v1: V | undefined, v2: V | undefined): ExpressionCondition;\n // V is already an array; strings also support jsonb contains.\n contains(value: string | V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n ncontains(value: string | V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n overlaps(value: V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n noverlaps(value: V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n /** Adds a parameterized JSON path existence condition using `@?`. */\n pathExists(jsonPath: string | undefined): ExpressionCondition;\n /** Adds a parameterized JSON path predicate condition using `@@`. */\n pathIsTrue(jsonPath: string | undefined): ExpressionCondition;\n /** Adds an operator and expression with knex-style placeholders, without the column name. */\n raw(exp: string, bindings: readonly unknown[] | undefined): ExpressionCondition;\n}\n\nexport interface EntityAlias<T, N extends null | never = never> extends AliasColumn<IdOf<T> | N> {\n eq(value: T | IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n ne(value: T | IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n // Adding `| null` for GraphQL support\n in(values: readonly (T | IdOf<T> | null)[] | null | undefined): ExpressionCondition;\n nin(values: readonly (T | IdOf<T> | null)[] | null | undefined): ExpressionCondition;\n gt(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n gte(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n lt(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n lte(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n raw(exp: string, bindings: readonly unknown[] | undefined): ExpressionCondition;\n}\n\nexport interface PolyAlias<T extends Entity> {\n eq(value: T | TaggedId | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n ne(value: T | TaggedId | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n in(values: readonly (T | TaggedId)[] | undefined): ExpressionCondition;\n}\n\n/** Returns the runtime identity bound by the find parser. */\nexport function getAliasMgmt(alias: { readonly [aliasMgmt]: AliasMgmt }): AliasMgmt {\n return alias[aliasMgmt];\n}\n\n/** Returns the original metadata, preserving STI subtype identity. */\nexport function getAliasMetadata<T extends Entity>(alias: Alias<T>): EntityMetadata<T> {\n return alias[aliasMgmt].meta as EntityMetadata<T>;\n}\n\n/** Creates domain predicates lazily from entity field metadata. */\nexport function newAliasProxy<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Alias<T> {\n const meta = getMetadata(cstr);\n const mgmt: AliasMgmt = { tableName: meta.tableName, meta };\n return new Proxy(cstr, {\n get(_, key) {\n if (key === aliasMgmt) return mgmt;\n if (typeof key !== \"string\") return undefined;\n const field = meta.allFields[key] ?? fail(`No field ${key} on ${cstr.name}`);\n if (field.kind === \"poly\") return new PolyReferenceAlias(meta, mgmt, field);\n if ([\"primaryKey\", \"primitive\", \"enum\", \"m2o\"].includes(field.kind)) {\n return new AliasColumnImpl(meta, field, field.serde!.columns[0], mgmt);\n }\n return fail(`Unsupported alias field kind ${field.kind}`);\n },\n has(_, key) {\n return key === aliasMgmt || key in meta.allFields;\n },\n }) as unknown as Alias<T>;\n}\n\n/** Recognizes only domain aliases, not physical tables. */\nexport function isAlias(value: unknown): value is Alias<Entity> {\n return typeof value === \"function\" && aliasMgmt in value;\n}\n\n/** Builds field conditions without implementing the SQL expression protocol. */\nclass AliasColumnImpl {\n declare readonly [aliasColumn]: unknown;\n\n constructor(\n readonly meta: EntityMetadata,\n readonly field: Field & { aliasSuffix: string },\n readonly column: Column,\n readonly mgmt: AliasMgmt,\n ) {}\n\n eq(value: unknown): ExpressionCondition {\n return this.compare(\"eq\", \"=\", value);\n }\n ne(value: unknown): ExpressionCondition {\n return this.compare(\"ne\", \"!=\", value);\n }\n gt(value: unknown): ExpressionCondition {\n return this.compare(\"gt\", \">\", value);\n }\n gte(value: unknown): ExpressionCondition {\n return this.compare(\"gte\", \">=\", value);\n }\n lt(value: unknown): ExpressionCondition {\n return this.compare(\"lt\", \"<\", value);\n }\n lte(value: unknown): ExpressionCondition {\n return this.compare(\"lte\", \"<=\", value);\n }\n contains(value: unknown): ExpressionCondition {\n return this.compare(\"contains\", \"@>\", value);\n }\n ncontains(value: unknown): ExpressionCondition {\n if (value instanceof AliasColumnImpl) return crossColumnCondition(this, value, \"@>\", true);\n return this.addCondition({ kind: \"ncontains\", value: value as readonly unknown[] });\n }\n overlaps(value: unknown): ExpressionCondition {\n return this.compare(\"overlaps\", \"&&\", value);\n }\n noverlaps(value: unknown): ExpressionCondition {\n if (value instanceof AliasColumnImpl) return crossColumnCondition(this, value, \"&&\", true);\n return this.addCondition({ kind: \"noverlaps\", value: value as readonly unknown[] });\n }\n\n in(values: readonly unknown[] | null | undefined): ExpressionCondition {\n if (values === undefined) return skipCondition;\n if (values === null) return fail(\"Unsupported\");\n if (values.includes(null)) {\n return {\n or: [\n this.addCondition({ kind: \"is-null\" }),\n this.addCondition({ kind: \"in\", value: values.filter((v) => v !== null) }),\n ],\n };\n }\n return this.addCondition({ kind: \"in\", value: [...values] });\n }\n\n nin(values: readonly unknown[] | null | undefined): ExpressionCondition {\n if (values === undefined) return skipCondition;\n if (values === null) return fail(\"Unsupported\");\n return this.addCondition({ kind: \"nin\", value: values.filter((v) => v !== null) });\n }\n\n between(v1: unknown, v2: unknown): ExpressionCondition {\n if (v1 === undefined || v2 === undefined) return skipCondition;\n return this.addCondition({ kind: \"between\", value: [v1, v2] });\n }\n\n like(value: unknown): ExpressionCondition {\n return this.addCondition({ kind: \"like\", value });\n }\n ilike(value: unknown): ExpressionCondition {\n return this.addCondition({ kind: \"ilike\", value });\n }\n search(value: unknown): ExpressionCondition {\n // Check !value so that empty strings are pruned\n return !value ? skipCondition : this.addCondition({ kind: \"ilike\", value: makeLike(value) });\n }\n pathExists(value: string | undefined): ExpressionCondition {\n return this.addCondition({ kind: \"jsonPathExists\", value: value! });\n }\n pathIsTrue(value: string | undefined): ExpressionCondition {\n return this.addCondition({ kind: \"jsonPathPredicate\", value: value! });\n }\n\n raw(exp: string, bindings: readonly unknown[] | undefined): ExpressionCondition {\n if (bindings === undefined) return skipCondition;\n const cond: RawCondition = { kind: \"raw\", aliases: [], condition: \"unset\", pruneable: false, bindings };\n return withDeferredAlias(cond, (resolve, copy) => {\n const r = resolve(this.mgmt);\n const alias = getMaybeCtiAlias(this.meta, this.field, r.meta, r.alias);\n copy.aliases = [alias];\n copy.condition = `${alias}.${this.column.columnName} ${exp}`;\n });\n }\n\n /** Maps literal values through the field serde and binds aliases only when parsing. */\n addCondition(value: ParsedValueFilter<unknown>): ColumnCondition {\n if (\"value\" in value && value.value === undefined) return skipCondition;\n const cond: ColumnCondition = {\n kind: \"column\",\n alias: \"unset\",\n column: this.column.columnName,\n dbType: this.column.dbType,\n cond: mapToDb(this.column, value),\n };\n return withDeferredAlias(cond, (resolve, copy) => {\n const r = resolve(this.mgmt);\n copy.alias = getMaybeCtiAlias(this.meta, this.field, r.meta, r.alias);\n });\n }\n\n /** Handles literal values, NULL, and domain cross-alias comparisons. */\n private compare(\n kind: \"eq\" | \"ne\" | \"gt\" | \"gte\" | \"lt\" | \"lte\" | \"contains\" | \"ncontains\" | \"overlaps\",\n op: string,\n value: unknown,\n ): ExpressionCondition {\n if (value === undefined) return skipCondition;\n if (value instanceof AliasColumnImpl) return crossColumnCondition(this, value, op);\n if (value === null && (kind === \"eq\" || kind === \"ne\"))\n return this.addCondition({ kind: kind === \"eq\" ? \"is-null\" : \"not-null\" });\n if (value === null && [\"gt\", \"gte\", \"lt\", \"lte\"].includes(kind)) return fail(\"Unsupported\");\n return this.addCondition({ kind, value } as ParsedValueFilter<unknown>);\n }\n}\n\n/** Resolves polymorphic predicates to their physical component columns. */\nclass PolyReferenceAlias {\n constructor(\n private meta: EntityMetadata,\n private mgmt: AliasMgmt,\n private field: PolymorphicField & { aliasSuffix: string },\n ) {}\n\n eq(value: unknown): ExpressionCondition {\n return this.addEqOrNe(\"eq\", value);\n }\n ne(value: unknown): ExpressionCondition {\n return this.addEqOrNe(\"ne\", value);\n }\n\n in(values: readonly (Entity | TaggedId)[] | undefined): ExpressionCondition {\n if (values === undefined) return skipCondition;\n // Split up the ids by constructor\n const groups = groupBy(values, (id) => getConstructorFromTaggedId(maybeResolveReferenceToId(id)!).name);\n // Or together `parent_book_id in (1,2,3) OR parent_author_id IN (4,5,6)`\n return {\n or: Object.entries(groups).map(([name, ids]) => {\n const comp =\n this.field.components.find((c) => c.otherMetadata().cstr.name === name) ?? fail(`No component for ${name}`);\n return this.component(comp).addCondition({ kind: \"in\", value: ids });\n }),\n };\n }\n\n /** Picks the component from an entity, tagged id, or another domain alias column. */\n private addEqOrNe(kind: \"eq\" | \"ne\", value: unknown): ExpressionCondition {\n if (value === undefined) return skipCondition;\n if (value === null) {\n // We can AND each of the components as many conditions\n return {\n and: this.field.components.map((c) =>\n this.component(c).addCondition({ kind: kind === \"eq\" ? \"is-null\" : \"not-null\" }),\n ),\n };\n }\n const otherMeta =\n value instanceof AliasColumnImpl\n ? value.field.kind === \"m2o\"\n ? value.field.otherMetadata()\n : value.meta\n : getMetadata(getConstructorFromTaggedId(maybeResolveReferenceToId(value as Entity | TaggedId)!));\n const comp =\n this.field.components.find((c) => getBaseAndSelfMetas(otherMeta).includes(c.otherMetadata())) ??\n fail(`${this.field.fieldName} has no component for ${otherMeta.type}`);\n const column = this.component(comp);\n return kind === \"eq\" ? column.eq(value) : column.ne(value);\n }\n\n /** Uses the component serde, not the first component's codec. */\n private component(comp: PolymorphicFieldComponent): AliasColumnImpl {\n const column = this.field.serde.columns.find((c) => c.columnName === comp.columnName) ?? fail(\"Missing column\");\n return new AliasColumnImpl(this.meta, this.field, column, this.mgmt);\n }\n}\n\n/** Adjusts a field's SQL alias when a base alias is bound to a CTI subtype in the join tree. */\nexport function getMaybeCtiAlias(\n meta: EntityMetadata,\n field: Field & { aliasSuffix: string },\n newMeta: EntityMetadata,\n newAlias: string,\n): string {\n // Do we have mismatched `em.find(ChildMeta)` with a `alias(BaseMeta)`? If so, the\n // usual `${field.aliasSuffix}` won't know it should have a suffix, so we need to calc it.\n if (newMeta !== meta && newMeta.inheritanceType === \"cti\") {\n const bases = getBaseAndSelfMetas(newMeta);\n const fieldIsFromBase = bases.includes(newMeta);\n if (fieldIsFromBase) return `${newAlias}_b0`;\n }\n return `${newAlias}${field.aliasSuffix}`;\n}\n\n/** Compares domain columns with per-parse aliases, preserving condition reuse across find calls. */\nfunction crossColumnCondition(left: AliasColumnImpl, right: AliasColumnImpl, op: string, negate = false): RawCondition {\n const cond: RawCondition = { kind: \"raw\", aliases: [], condition: \"unset\", bindings: [], pruneable: false };\n return withDeferredAlias(cond, (resolve, copy) => {\n const l = resolve(left.mgmt);\n const r = resolve(right.mgmt);\n const a1 = getMaybeCtiAlias(left.meta, left.field, l.meta, l.alias);\n const a2 = getMaybeCtiAlias(right.meta, right.field, r.meta, r.alias);\n copy.aliases = [a1, a2];\n const sql = `${a1}.${left.column.columnName} ${op} ${a2}.${right.column.columnName}`;\n copy.condition = negate ? `NOT (${sql})` : sql;\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;AAwBA,SAAgB,MAAwB,MAAmD;CACzF,OAAO,cAAc,IAAI;AAC3B;;AAGA,SAAgB,QACd,GAAG,OACyG;CAC5G,OAAO,MAAM,KAAK,MAAM,cAAc,CAAC,CAAC;AAC1C;AAEA,MAAa,YAAY,OAAO,WAAW;;AAgG3C,SAAgB,aAAa,OAAuD;CAClF,OAAO,MAAM;AACf;;AAGA,SAAgB,iBAAmC,OAAoC;CACrF,OAAO,MAAM,UAAU,CAAC;AAC1B;;AAGA,SAAgB,cAAgC,MAAmD;CACjG,MAAM,OAAOA,uBAAAA,YAAY,IAAI;CAC7B,MAAM,OAAkB;EAAE,WAAW,KAAK;EAAW;CAAK;CAC1D,OAAO,IAAI,MAAM,MAAM;EACrB,IAAI,GAAG,KAAK;GACV,IAAI,QAAQ,WAAW,OAAO;GAC9B,IAAI,OAAO,QAAQ,UAAU,OAAO,KAAA;GACpC,MAAM,QAAQ,KAAK,UAAU,QAAQC,cAAAA,KAAK,YAAY,IAAI,MAAM,KAAK,MAAM;GAC3E,IAAI,MAAM,SAAS,QAAQ,OAAO,IAAI,mBAAmB,MAAM,MAAM,KAAK;GAC1E,IAAI;IAAC;IAAc;IAAa;IAAQ;GAAK,CAAC,CAAC,SAAS,MAAM,IAAI,GAChE,OAAO,IAAI,gBAAgB,MAAM,OAAO,MAAM,MAAO,QAAQ,IAAI,IAAI;GAEvE,OAAOA,cAAAA,KAAK,gCAAgC,MAAM,MAAM;EAC1D;EACA,IAAI,GAAG,KAAK;GACV,OAAO,QAAQ,aAAa,OAAO,KAAK;EAC1C;CACF,CAAC;AACH;;AAGA,SAAgB,QAAQ,OAAw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,KAAK,IAAI,OAAO,GAAG,KAAK;CAC3D;;CAGA,UAAkB,MAAkD;EAClE,MAAM,SAAS,KAAK,MAAM,MAAM,QAAQ,MAAM,MAAM,EAAE,eAAe,KAAK,UAAU,KAAKA,cAAAA,KAAK,gBAAgB;EAC9G,OAAO,IAAI,gBAAgB,KAAK,MAAM,KAAK,OAAO,QAAQ,KAAK,IAAI;CACrE;AACF;;AAGA,SAAgB,iBACd,MACA,OACA,SACA,UACQ;CAGR,IAAI,YAAY,QAAQ,QAAQ,oBAAoB,OACpCQ;MAAAA,uBAAAA,oBAAoB,OACN,CAAC,CAAC,SAAS,OACrB,GAAG,OAAO,GAAG,SAAS;CAAA;CAE1C,OAAO,GAAG,WAAW,MAAM;AAC7B;;AAGA,SAAS,qBAAqB,MAAuB,OAAwB,IAAY,SAAS,OAAqB;CAErH,OAAOL,sBAAAA,kBAAkB;EADI,MAAM;EAAO,SAAS,CAAC;EAAG,WAAW;EAAS,UAAU,CAAC;EAAG,WAAW;CACxE,IAAI,SAAS,SAAS;EAChD,MAAM,IAAI,QAAQ,KAAK,IAAI;EAC3B,MAAM,IAAI,QAAQ,MAAM,IAAI;EAC5B,MAAM,KAAK,iBAAiB,KAAK,MAAM,KAAK,OAAO,EAAE,MAAM,EAAE,KAAK;EAClE,MAAM,KAAK,iBAAiB,MAAM,MAAM,MAAM,OAAO,EAAE,MAAM,EAAE,KAAK;EACpE,KAAK,UAAU,CAAC,IAAI,EAAE;EACtB,MAAM,MAAM,GAAG,GAAG,GAAG,KAAK,OAAO,WAAW,GAAG,GAAG,GAAG,GAAG,GAAG,MAAM,OAAO;EACxE,KAAK,YAAY,SAAS,QAAQ,IAAI,KAAK;CAC7C,CAAC;AACH"}
1
+ {"version":3,"file":"Aliases.cjs","names":["getMetadata","withDeferredAlias","fail","skipCondition","makeLike","mapToDb","groupBy","getConstructorFromTaggedId","maybeResolveReferenceToId","getBaseAndSelfMetas"],"sources":["../src/Aliases.ts"],"sourcesContent":["import { groupBy } from \"joist-utils\";\n\nimport { type Column } from \"./columns.ts\";\n// Load configure first: relations must not evaluate before their base classes exist.\nimport { getConstructorFromTaggedId } from \"./configure.ts\";\nimport { withDeferredAlias } from \"./DeferredAlias.ts\";\nimport { type Entity } from \"./Entity.ts\";\nimport { type ExpressionCondition } from \"./EntityFilter.ts\";\nimport { type IdOf, type MaybeAbstractEntityConstructor, type TaggedId } from \"./EntityManager.ts\";\nimport {\n type EntityMetadata,\n type Field,\n type PolymorphicField,\n type PolymorphicFieldComponent,\n getBaseAndSelfMetas,\n getMetadata,\n} from \"./EntityMetadata.ts\";\nimport { maybeResolveReferenceToId } from \"./keys.ts\";\nimport { type ColumnCondition, type ParsedValueFilter, type RawCondition, makeLike, mapToDb } from \"./QueryParser.ts\";\nimport { skipCondition } from \"./skipCondition.ts\";\nimport { type FieldsOf } from \"./typeMap.ts\";\nimport { fail } from \"./utils.ts\";\n\n/** Creates an alias for complex domain filtering in `em.find`. */\nexport function alias<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Alias<T> {\n return newAliasProxy(cstr);\n}\n\n/** Creates multiple aliases for complex filtering. */\nexport function aliases<T extends readonly MaybeAbstractEntityConstructor<any>[]>(\n ...types: T\n): { [P in keyof T]: T[P] extends MaybeAbstractEntityConstructor<infer E extends Entity> ? Alias<E> : never } {\n return types.map((t) => newAliasProxy(t)) as any;\n}\n\nexport const aliasMgmt = Symbol(\"aliasMgmt\");\nconst aliasColumn = Symbol(\"aliasColumn\");\n\n/** The identity and original entity metadata of an `em.find` alias. */\nexport interface AliasMgmt {\n tableName: string;\n meta: EntityMetadata;\n /**\n * Builds a raw condition using each query's bound metadata and SQL alias, before join pruning.\n * Return a fresh condition instead of mutating shared state; the callback runs only when used.\n */\n condition(build: (meta: EntityMetadata, alias: string) => RawCondition): RawCondition;\n}\n\n/** Keeps domain aliases covariant in their entity type. */\nexport interface AliasBrand<T> extends AliasMgmt {\n readonly __entity: T;\n}\n\n/**\n * Domain field predicates, deliberately separate from SQL table expressions.\n *\n * An Alias names a place in an em.find relationship tree, not a fixed SQL table occurrence.\n * I.e. with `a = alias(Author)`, `{ author: { as: a } }` binds `a` to the Book's Author;\n * binding it under `{ author: { mentor: { as: a } } }` instead makes it the Author's mentor.\n * The find parser builds those joins and knows which entity metadata and SQL name apply there,\n * including any inheritance joins. The Alias cannot know that when `a.firstName.eq(name)` is created.\n *\n * DeferredAlias therefore resolves each predicate to a fresh column/raw condition using that parse's\n * binding. The resolved condition records the SQL aliases it reads, including both sides of a\n * cross-alias comparison. Join pruning uses those references to retain the joins the filter needs.\n * An undefined predicate is skipped, so it does not keep an otherwise-unused join alive; explicit\n * keepAliases and other query dependencies can still retain that join.\n *\n * Table expressions solve a different problem: their sources are supplied directly in from/join,\n * so they render through ExprContext rather than asking the find parser to bind a relationship path.\n * Neither path stores a resolved SQL name on the reusable handle or predicate.\n */\nexport type Alias<T extends Entity> = { readonly [aliasMgmt]: AliasBrand<T> } & {\n [P in keyof FieldsOf<T>]: P extends \"id\"\n ? EntityAlias<T>\n : FieldsOf<T>[P] extends { kind: \"primitive\" | \"enum\"; type: infer V; nullable: infer N }\n ? PrimitiveAlias<V, N extends undefined ? null : never>\n : FieldsOf<T>[P] extends { kind: \"m2o\"; type: infer U extends Entity; nullable: infer N }\n ? EntityAlias<U, N extends undefined ? null : never>\n : FieldsOf<T>[P] extends { kind: \"poly\"; type: infer U extends Entity }\n ? PolyAlias<U>\n : never;\n};\n\n/** A domain column accepted on the other side of a cross-alias comparison. */\nexport interface AliasColumn<V> {\n readonly [aliasColumn]: V;\n}\n\nexport interface PrimitiveAlias<V, N extends null | never = never> extends AliasColumn<V | N> {\n eq(value: V | N | AliasColumn<V | null> | undefined): ExpressionCondition;\n ne(value: V | N | AliasColumn<V | null> | undefined): ExpressionCondition;\n in(values: readonly (V | null)[] | undefined): ExpressionCondition;\n nin(values: readonly (V | null)[] | undefined): ExpressionCondition;\n gt(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n gte(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n lt(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n lte(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n like(value: V | undefined): ExpressionCondition;\n ilike(value: V | undefined): ExpressionCondition;\n search(value: V | undefined): ExpressionCondition;\n between(v1: V | undefined, v2: V | undefined): ExpressionCondition;\n // V is already an array; strings also support jsonb contains.\n contains(value: string | V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n ncontains(value: string | V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n overlaps(value: V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n noverlaps(value: V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n /** Adds a parameterized JSON path existence condition using `@?`. */\n pathExists(jsonPath: string | undefined): ExpressionCondition;\n /** Adds a parameterized JSON path predicate condition using `@@`. */\n pathIsTrue(jsonPath: string | undefined): ExpressionCondition;\n /** Adds an operator and expression with knex-style placeholders, without the column name. */\n raw(exp: string, bindings: readonly unknown[] | undefined): ExpressionCondition;\n}\n\nexport interface EntityAlias<T, N extends null | never = never> extends AliasColumn<IdOf<T> | N> {\n eq(value: T | IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n ne(value: T | IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n // Adding `| null` for GraphQL support\n in(values: readonly (T | IdOf<T> | null)[] | null | undefined): ExpressionCondition;\n nin(values: readonly (T | IdOf<T> | null)[] | null | undefined): ExpressionCondition;\n gt(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n gte(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n lt(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n lte(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n raw(exp: string, bindings: readonly unknown[] | undefined): ExpressionCondition;\n}\n\nexport interface PolyAlias<T extends Entity> {\n eq(value: T | TaggedId | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n ne(value: T | TaggedId | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n in(values: readonly (T | TaggedId)[] | undefined): ExpressionCondition;\n}\n\n/** Returns the runtime identity bound by the find parser. */\nexport function getAliasMgmt(alias: { readonly [aliasMgmt]: AliasMgmt }): AliasMgmt {\n return alias[aliasMgmt];\n}\n\n/** Returns the original metadata, preserving STI subtype identity. */\nexport function getAliasMetadata<T extends Entity>(alias: Alias<T>): EntityMetadata<T> {\n return alias[aliasMgmt].meta as EntityMetadata<T>;\n}\n\n/** Creates domain predicates lazily from entity field metadata. */\nexport function newAliasProxy<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Alias<T> {\n const meta = getMetadata(cstr);\n const mgmt: AliasMgmt = {\n tableName: meta.tableName,\n meta,\n condition(build) {\n const cond: RawCondition = { kind: \"raw\", aliases: [], condition: \"unset\", bindings: [], pruneable: false };\n return withDeferredAlias(cond, (resolve, copy) => {\n const bound = resolve(mgmt);\n Object.assign(copy, build(bound.meta, bound.alias));\n });\n },\n };\n return new Proxy(cstr, {\n get(_, key) {\n if (key === aliasMgmt) return mgmt;\n if (typeof key !== \"string\") return undefined;\n const field = meta.allFields[key] ?? fail(`No field ${key} on ${cstr.name}`);\n if (field.kind === \"poly\") return new PolyReferenceAlias(meta, mgmt, field);\n if ([\"primaryKey\", \"primitive\", \"enum\", \"m2o\"].includes(field.kind)) {\n return new AliasColumnImpl(meta, field, field.serde!.columns[0], mgmt);\n }\n return fail(`Unsupported alias field kind ${field.kind}`);\n },\n has(_, key) {\n return key === aliasMgmt || key in meta.allFields;\n },\n }) as unknown as Alias<T>;\n}\n\n/** Recognizes only domain aliases, not physical tables. */\nexport function isAlias(value: unknown): value is Alias<Entity> {\n return typeof value === \"function\" && aliasMgmt in value;\n}\n\n/** Builds field conditions without implementing the SQL expression protocol. */\nclass AliasColumnImpl {\n declare readonly [aliasColumn]: unknown;\n\n constructor(\n readonly meta: EntityMetadata,\n readonly field: Field & { aliasSuffix: string },\n readonly column: Column,\n readonly mgmt: AliasMgmt,\n ) {}\n\n eq(value: unknown): ExpressionCondition {\n return this.compare(\"eq\", \"=\", value);\n }\n ne(value: unknown): ExpressionCondition {\n return this.compare(\"ne\", \"!=\", value);\n }\n gt(value: unknown): ExpressionCondition {\n return this.compare(\"gt\", \">\", value);\n }\n gte(value: unknown): ExpressionCondition {\n return this.compare(\"gte\", \">=\", value);\n }\n lt(value: unknown): ExpressionCondition {\n return this.compare(\"lt\", \"<\", value);\n }\n lte(value: unknown): ExpressionCondition {\n return this.compare(\"lte\", \"<=\", value);\n }\n contains(value: unknown): ExpressionCondition {\n return this.compare(\"contains\", \"@>\", value);\n }\n ncontains(value: unknown): ExpressionCondition {\n if (value instanceof AliasColumnImpl) return crossColumnCondition(this, value, \"@>\", true);\n return this.addCondition({ kind: \"ncontains\", value: value as readonly unknown[] });\n }\n overlaps(value: unknown): ExpressionCondition {\n return this.compare(\"overlaps\", \"&&\", value);\n }\n noverlaps(value: unknown): ExpressionCondition {\n if (value instanceof AliasColumnImpl) return crossColumnCondition(this, value, \"&&\", true);\n return this.addCondition({ kind: \"noverlaps\", value: value as readonly unknown[] });\n }\n\n in(values: readonly unknown[] | null | undefined): ExpressionCondition {\n if (values === undefined) return skipCondition;\n if (values === null) return fail(\"Unsupported\");\n if (values.includes(null)) {\n return {\n or: [\n this.addCondition({ kind: \"is-null\" }),\n this.addCondition({ kind: \"in\", value: values.filter((v) => v !== null) }),\n ],\n };\n }\n return this.addCondition({ kind: \"in\", value: [...values] });\n }\n\n nin(values: readonly unknown[] | null | undefined): ExpressionCondition {\n if (values === undefined) return skipCondition;\n if (values === null) return fail(\"Unsupported\");\n return this.addCondition({ kind: \"nin\", value: values.filter((v) => v !== null) });\n }\n\n between(v1: unknown, v2: unknown): ExpressionCondition {\n if (v1 === undefined || v2 === undefined) return skipCondition;\n return this.addCondition({ kind: \"between\", value: [v1, v2] });\n }\n\n like(value: unknown): ExpressionCondition {\n return this.addCondition({ kind: \"like\", value });\n }\n ilike(value: unknown): ExpressionCondition {\n return this.addCondition({ kind: \"ilike\", value });\n }\n search(value: unknown): ExpressionCondition {\n // Check !value so that empty strings are pruned\n return !value ? skipCondition : this.addCondition({ kind: \"ilike\", value: makeLike(value) });\n }\n pathExists(value: string | undefined): ExpressionCondition {\n return this.addCondition({ kind: \"jsonPathExists\", value: value! });\n }\n pathIsTrue(value: string | undefined): ExpressionCondition {\n return this.addCondition({ kind: \"jsonPathPredicate\", value: value! });\n }\n\n raw(exp: string, bindings: readonly unknown[] | undefined): ExpressionCondition {\n if (bindings === undefined) return skipCondition;\n const cond: RawCondition = { kind: \"raw\", aliases: [], condition: \"unset\", pruneable: false, bindings };\n return withDeferredAlias(cond, (resolve, copy) => {\n const r = resolve(this.mgmt);\n const alias = getMaybeCtiAlias(this.meta, this.field, r.meta, r.alias);\n copy.aliases = [alias];\n copy.condition = `${alias}.${this.column.columnName} ${exp}`;\n });\n }\n\n /** Maps literal values through the field serde and binds aliases only when parsing. */\n addCondition(value: ParsedValueFilter<unknown>): ColumnCondition {\n if (\"value\" in value && value.value === undefined) return skipCondition;\n const cond: ColumnCondition = {\n kind: \"column\",\n alias: \"unset\",\n column: this.column.columnName,\n dbType: this.column.dbType,\n cond: mapToDb(this.column, value),\n };\n return withDeferredAlias(cond, (resolve, copy) => {\n const r = resolve(this.mgmt);\n copy.alias = getMaybeCtiAlias(this.meta, this.field, r.meta, r.alias);\n });\n }\n\n /** Handles literal values, NULL, and domain cross-alias comparisons. */\n private compare(\n kind: \"eq\" | \"ne\" | \"gt\" | \"gte\" | \"lt\" | \"lte\" | \"contains\" | \"ncontains\" | \"overlaps\",\n op: string,\n value: unknown,\n ): ExpressionCondition {\n if (value === undefined) return skipCondition;\n if (value instanceof AliasColumnImpl) return crossColumnCondition(this, value, op);\n if (value === null && (kind === \"eq\" || kind === \"ne\"))\n return this.addCondition({ kind: kind === \"eq\" ? \"is-null\" : \"not-null\" });\n if (value === null && [\"gt\", \"gte\", \"lt\", \"lte\"].includes(kind)) return fail(\"Unsupported\");\n return this.addCondition({ kind, value } as ParsedValueFilter<unknown>);\n }\n}\n\n/** Resolves polymorphic predicates to their physical component columns. */\nclass PolyReferenceAlias {\n constructor(\n private meta: EntityMetadata,\n private mgmt: AliasMgmt,\n private field: PolymorphicField & { aliasSuffix: string },\n ) {}\n\n eq(value: unknown): ExpressionCondition {\n return this.addEqOrNe(\"eq\", value);\n }\n ne(value: unknown): ExpressionCondition {\n return this.addEqOrNe(\"ne\", value);\n }\n\n in(values: readonly (Entity | TaggedId)[] | undefined): ExpressionCondition {\n if (values === undefined) return skipCondition;\n // Split up the ids by constructor\n const groups = groupBy(values, (id) => getConstructorFromTaggedId(maybeResolveReferenceToId(id)!).name);\n // Or together `parent_book_id in (1,2,3) OR parent_author_id IN (4,5,6)`\n return {\n or: Object.entries(groups).map(([name, ids]) => {\n const comp =\n this.field.components.find((c) => c.otherMetadata().cstr.name === name) ?? fail(`No component for ${name}`);\n return this.component(comp).addCondition({ kind: \"in\", value: ids });\n }),\n };\n }\n\n /** Picks the component from an entity, tagged id, or another domain alias column. */\n private addEqOrNe(kind: \"eq\" | \"ne\", value: unknown): ExpressionCondition {\n if (value === undefined) return skipCondition;\n if (value === null) {\n // We can AND each of the components as many conditions\n return {\n and: this.field.components.map((c) =>\n this.component(c).addCondition({ kind: kind === \"eq\" ? \"is-null\" : \"not-null\" }),\n ),\n };\n }\n const otherMeta =\n value instanceof AliasColumnImpl\n ? value.field.kind === \"m2o\"\n ? value.field.otherMetadata()\n : value.meta\n : getMetadata(getConstructorFromTaggedId(maybeResolveReferenceToId(value as Entity | TaggedId)!));\n const comp =\n this.field.components.find((c) => getBaseAndSelfMetas(otherMeta).includes(c.otherMetadata())) ??\n fail(`${this.field.fieldName} has no component for ${otherMeta.type}`);\n const column = this.component(comp);\n return kind === \"eq\" ? column.eq(value) : column.ne(value);\n }\n\n /** Uses the component serde, not the first component's codec. */\n private component(comp: PolymorphicFieldComponent): AliasColumnImpl {\n const column = this.field.serde.columns.find((c) => c.columnName === comp.columnName) ?? fail(\"Missing column\");\n return new AliasColumnImpl(this.meta, this.field, column, this.mgmt);\n }\n}\n\n/** Adjusts a field's SQL alias when a base alias is bound to a CTI subtype in the join tree. */\nexport function getMaybeCtiAlias(\n meta: EntityMetadata,\n field: Field & { aliasSuffix: string },\n newMeta: EntityMetadata,\n newAlias: string,\n): string {\n // Do we have mismatched `em.find(ChildMeta)` with a `alias(BaseMeta)`? If so, the\n // usual `${field.aliasSuffix}` won't know it should have a suffix, so we need to calc it.\n if (newMeta !== meta && newMeta.inheritanceType === \"cti\") {\n const bases = getBaseAndSelfMetas(newMeta);\n const fieldIsFromBase = bases.includes(newMeta);\n if (fieldIsFromBase) return `${newAlias}_b0`;\n }\n return `${newAlias}${field.aliasSuffix}`;\n}\n\n/** Compares domain columns with per-parse aliases, preserving condition reuse across find calls. */\nfunction crossColumnCondition(left: AliasColumnImpl, right: AliasColumnImpl, op: string, negate = false): RawCondition {\n const cond: RawCondition = { kind: \"raw\", aliases: [], condition: \"unset\", bindings: [], pruneable: false };\n return withDeferredAlias(cond, (resolve, copy) => {\n const l = resolve(left.mgmt);\n const r = resolve(right.mgmt);\n const a1 = getMaybeCtiAlias(left.meta, left.field, l.meta, l.alias);\n const a2 = getMaybeCtiAlias(right.meta, right.field, r.meta, r.alias);\n copy.aliases = [a1, a2];\n const sql = `${a1}.${left.column.columnName} ${op} ${a2}.${right.column.columnName}`;\n copy.condition = negate ? `NOT (${sql})` : sql;\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;AAwBA,SAAgB,MAAwB,MAAmD;CACzF,OAAO,cAAc,IAAI;AAC3B;;AAGA,SAAgB,QACd,GAAG,OACyG;CAC5G,OAAO,MAAM,KAAK,MAAM,cAAc,CAAC,CAAC;AAC1C;AAEA,MAAa,YAAY,OAAO,WAA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AA,KAAK,gBAAgB;EAC9G,OAAO,IAAI,gBAAgB,KAAK,MAAM,KAAK,OAAO,QAAQ,KAAK,IAAI;CACrE;AACF;;AAGA,SAAgB,iBACd,MACA,OACA,SACA,UACQ;CAGR,IAAI,YAAY,QAAQ,QAAQ,oBAAoB,OACpCO;MAAAA,uBAAAA,oBAAoB,OACN,CAAC,CAAC,SAAS,OACrB,GAAG,OAAO,GAAG,SAAS;CAAA;CAE1C,OAAO,GAAG,WAAW,MAAM;AAC7B;;AAGA,SAAS,qBAAqB,MAAuB,OAAwB,IAAY,SAAS,OAAqB;CAErH,OAAOR,sBAAAA,kBAAkB;EADI,MAAM;EAAO,SAAS,CAAC;EAAG,WAAW;EAAS,UAAU,CAAC;EAAG,WAAW;CACxE,IAAI,SAAS,SAAS;EAChD,MAAM,IAAI,QAAQ,KAAK,IAAI;EAC3B,MAAM,IAAI,QAAQ,MAAM,IAAI;EAC5B,MAAM,KAAK,iBAAiB,KAAK,MAAM,KAAK,OAAO,EAAE,MAAM,EAAE,KAAK;EAClE,MAAM,KAAK,iBAAiB,MAAM,MAAM,MAAM,OAAO,EAAE,MAAM,EAAE,KAAK;EACpE,KAAK,UAAU,CAAC,IAAI,EAAE;EACtB,MAAM,MAAM,GAAG,GAAG,GAAG,KAAK,OAAO,WAAW,GAAG,GAAG,GAAG,GAAG,GAAG,MAAM,OAAO;EACxE,KAAK,YAAY,SAAS,QAAQ,IAAI,KAAK;CAC7C,CAAC;AACH"}
@@ -3,6 +3,7 @@ import { EntityMetadata, Field } from "./EntityMetadata.cjs";
3
3
  import { IdOf, MaybeAbstractEntityConstructor, TaggedId } from "./EntityManager.cjs";
4
4
  import { Entity } from "./Entity.cjs";
5
5
  import { ExpressionCondition } from "./EntityFilter.cjs";
6
+ import { RawCondition } from "./QueryParser.cjs";
6
7
  //#region src/Aliases.d.ts
7
8
  /** Creates an alias for complex domain filtering in `em.find`. */
8
9
  declare function alias<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Alias<T>;
@@ -14,6 +15,11 @@ declare const aliasColumn: unique symbol;
14
15
  interface AliasMgmt {
15
16
  tableName: string;
16
17
  meta: EntityMetadata;
18
+ /**
19
+ * Builds a raw condition using each query's bound metadata and SQL alias, before join pruning.
20
+ * Return a fresh condition instead of mutating shared state; the callback runs only when used.
21
+ */
22
+ condition(build: (meta: EntityMetadata, alias: string) => RawCondition): RawCondition;
17
23
  }
18
24
  /** Keeps domain aliases covariant in their entity type. */
19
25
  interface AliasBrand<T> extends AliasMgmt {
@@ -1 +1 @@
1
- {"version":3,"file":"Aliases.d.cts","names":[],"sources":["../src/Aliases.ts"],"mappings":";;;;;;;iBAwBgB,MAAM,UAAU,QAAQ,MAAM,+BAA+B,KAAK,MAAM;;iBAKxE,QAAQ,mBAAmB,0CACtC,OAAO,OACN,WAAW,IAAI,EAAE,WAAW,qCAAqC,UAAU,UAAU,MAAM;cAIpF;cACP;;UAGW;EACf;EACA,MAAM;;;UAIS,WAAW,WAAW;WAC5B,UAAU;;;;;;;;;;;;;;;;;;;;;KAsBT,MAAM,UAAU;YAAsB,YAAY,WAAW;OACtE,WAAW,SAAS,KAAK,iBACtB,YAAY,KACZ,SAAS,GAAG;EAAa;EAA4B,YAAY;EAAG,gBAAgB;IAClF,eAAe,GAAG,sCAClB,SAAS,GAAG;EAAa;EAAa,YAAY,UAAU;EAAQ,gBAAgB;IAClF,YAAY,GAAG,sCACf,SAAS,GAAG;EAAa;EAAc,YAAY,UAAU;IAC3D,UAAU;;UAKL,YAAY;YACjB,cAAc;;UAGT,eAAe,GAAG,wCAAwC,YAAY,IAAI;EACzF,GAAG,OAAO,IAAI,IAAI,YAAY,wBAAwB;EACtD,GAAG,OAAO,IAAI,IAAI,YAAY,wBAAwB;EACtD,GAAG,kBAAkB,0BAA0B;EAC/C,IAAI,kBAAkB,0BAA0B;EAChD,GAAG,OAAO,IAAI,YAAY,wBAAwB;EAClD,IAAI,OAAO,IAAI,YAAY,wBAAwB;EACnD,GAAG,OAAO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ager.mjs";
4
4
  import { Entity } from "./Entity.mjs";
5
5
  import { ExpressionCondition } from "./EntityFilter.mjs";
6
+ import { RawCondition } from "./QueryParser.mjs";
6
7
  //#region src/Aliases.d.ts
7
8
  /** Creates an alias for complex domain filtering in `em.find`. */
8
9
  declare function alias<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Alias<T>;
@@ -14,6 +15,11 @@ declare const aliasColumn: unique symbol;
14
15
  interface AliasMgmt {
15
16
  tableName: string;
16
17
  meta: EntityMetadata;
18
+ /**
19
+ * Builds a raw condition using each query's bound metadata and SQL alias, before join pruning.
20
+ * Return a fresh condition instead of mutating shared state; the callback runs only when used.
21
+ */
22
+ condition(build: (meta: EntityMetadata, alias: string) => RawCondition): RawCondition;
17
23
  }
18
24
  /** Keeps domain aliases covariant in their entity type. */
19
25
  interface AliasBrand<T> extends AliasMgmt {
@@ -1 +1 @@
1
- {"version":3,"file":"Aliases.d.mts","names":[],"sources":["../src/Aliases.ts"],"mappings":";;;;;;;iBAwBgB,MAAM,UAAU,QAAQ,MAAM,+BAA+B,KAAK,MAAM;;iBAKxE,QAAQ,mBAAmB,0CACtC,OAAO,OACN,WAAW,IAAI,EAAE,WAAW,qCAAqC,UAAU,UAAU,MAAM;cAIpF;cACP;;UAGW;EACf;EACA,MAAM;;;UAIS,WAAW,WAAW;WAC5B,UAAU;;;;;;;;;;;;;;;;;;;;;KAsBT,MAAM,UAAU;YAAsB,YAAY,WAAW;OACtE,WAAW,SAAS,KAAK,iBACtB,YAAY,KACZ,SAAS,GAAG;EAAa;EAA4B,YAAY;EAAG,gBAAgB;IAClF,eAAe,GAAG,sCAClB,SAAS,GAAG;EAAa;EAAa,YAAY,UAAU;EAAQ,gBAAgB;IAClF,YAAY,GAAG,sCACf,SAAS,GAAG;EAAa;EAAc,YAAY,UAAU;IAC3D,UAAU;;UAKL,YAAY;YACjB,cAAc;;UAGT,eAAe,GAAG,wCAAwC,YAAY,IAAI;EACzF,GAAG,OAAO,IAAI,IA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KAAK,MAAM;;iBA+BhF,QAAQ,iBAAiB,SAAS,MAAM;;iBAiMxC,iBACd,MAAM,gBACN,OAAO;EAAU;GACjB,SAAS,gBACT"}
package/build/Aliases.js CHANGED
@@ -33,7 +33,19 @@ function newAliasProxy(cstr) {
33
33
  const meta = getMetadata(cstr);
34
34
  const mgmt = {
35
35
  tableName: meta.tableName,
36
- meta
36
+ meta,
37
+ condition(build) {
38
+ return withDeferredAlias({
39
+ kind: "raw",
40
+ aliases: [],
41
+ condition: "unset",
42
+ bindings: [],
43
+ pruneable: false
44
+ }, (resolve, copy) => {
45
+ const bound = resolve(mgmt);
46
+ Object.assign(copy, build(bound.meta, bound.alias));
47
+ });
48
+ }
37
49
  };
38
50
  return new Proxy(cstr, {
39
51
  get(_, key) {
@@ -1 +1 @@
1
- {"version":3,"file":"Aliases.js","names":[],"sources":["../src/Aliases.ts"],"sourcesContent":["import { groupBy } from \"joist-utils\";\n\nimport { type Column } from \"./columns.ts\";\n// Load configure first: relations must not evaluate before their base classes exist.\nimport { getConstructorFromTaggedId } from \"./configure.ts\";\nimport { withDeferredAlias } from \"./DeferredAlias.ts\";\nimport { type Entity } from \"./Entity.ts\";\nimport { type ExpressionCondition } from \"./EntityFilter.ts\";\nimport { type IdOf, type MaybeAbstractEntityConstructor, type TaggedId } from \"./EntityManager.ts\";\nimport {\n type EntityMetadata,\n type Field,\n type PolymorphicField,\n type PolymorphicFieldComponent,\n getBaseAndSelfMetas,\n getMetadata,\n} from \"./EntityMetadata.ts\";\nimport { maybeResolveReferenceToId } from \"./keys.ts\";\nimport { type ColumnCondition, type ParsedValueFilter, type RawCondition, makeLike, mapToDb } from \"./QueryParser.ts\";\nimport { skipCondition } from \"./skipCondition.ts\";\nimport { type FieldsOf } from \"./typeMap.ts\";\nimport { fail } from \"./utils.ts\";\n\n/** Creates an alias for complex domain filtering in `em.find`. */\nexport function alias<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Alias<T> {\n return newAliasProxy(cstr);\n}\n\n/** Creates multiple aliases for complex filtering. */\nexport function aliases<T extends readonly MaybeAbstractEntityConstructor<any>[]>(\n ...types: T\n): { [P in keyof T]: T[P] extends MaybeAbstractEntityConstructor<infer E extends Entity> ? Alias<E> : never } {\n return types.map((t) => newAliasProxy(t)) as any;\n}\n\nexport const aliasMgmt = Symbol(\"aliasMgmt\");\nconst aliasColumn = Symbol(\"aliasColumn\");\n\n/** The identity and original entity metadata of an `em.find` alias. */\nexport interface AliasMgmt {\n tableName: string;\n meta: EntityMetadata;\n}\n\n/** Keeps domain aliases covariant in their entity type. */\nexport interface AliasBrand<T> extends AliasMgmt {\n readonly __entity: T;\n}\n\n/**\n * Domain field predicates, deliberately separate from SQL table expressions.\n *\n * An Alias names a place in an em.find relationship tree, not a fixed SQL table occurrence.\n * I.e. with `a = alias(Author)`, `{ author: { as: a } }` binds `a` to the Book's Author;\n * binding it under `{ author: { mentor: { as: a } } }` instead makes it the Author's mentor.\n * The find parser builds those joins and knows which entity metadata and SQL name apply there,\n * including any inheritance joins. The Alias cannot know that when `a.firstName.eq(name)` is created.\n *\n * DeferredAlias therefore resolves each predicate to a fresh column/raw condition using that parse's\n * binding. The resolved condition records the SQL aliases it reads, including both sides of a\n * cross-alias comparison. Join pruning uses those references to retain the joins the filter needs.\n * An undefined predicate is skipped, so it does not keep an otherwise-unused join alive; explicit\n * keepAliases and other query dependencies can still retain that join.\n *\n * Table expressions solve a different problem: their sources are supplied directly in from/join,\n * so they render through ExprContext rather than asking the find parser to bind a relationship path.\n * Neither path stores a resolved SQL name on the reusable handle or predicate.\n */\nexport type Alias<T extends Entity> = { readonly [aliasMgmt]: AliasBrand<T> } & {\n [P in keyof FieldsOf<T>]: P extends \"id\"\n ? EntityAlias<T>\n : FieldsOf<T>[P] extends { kind: \"primitive\" | \"enum\"; type: infer V; nullable: infer N }\n ? PrimitiveAlias<V, N extends undefined ? null : never>\n : FieldsOf<T>[P] extends { kind: \"m2o\"; type: infer U extends Entity; nullable: infer N }\n ? EntityAlias<U, N extends undefined ? null : never>\n : FieldsOf<T>[P] extends { kind: \"poly\"; type: infer U extends Entity }\n ? PolyAlias<U>\n : never;\n};\n\n/** A domain column accepted on the other side of a cross-alias comparison. */\nexport interface AliasColumn<V> {\n readonly [aliasColumn]: V;\n}\n\nexport interface PrimitiveAlias<V, N extends null | never = never> extends AliasColumn<V | N> {\n eq(value: V | N | AliasColumn<V | null> | undefined): ExpressionCondition;\n ne(value: V | N | AliasColumn<V | null> | undefined): ExpressionCondition;\n in(values: readonly (V | null)[] | undefined): ExpressionCondition;\n nin(values: readonly (V | null)[] | undefined): ExpressionCondition;\n gt(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n gte(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n lt(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n lte(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n like(value: V | undefined): ExpressionCondition;\n ilike(value: V | undefined): ExpressionCondition;\n search(value: V | undefined): ExpressionCondition;\n between(v1: V | undefined, v2: V | undefined): ExpressionCondition;\n // V is already an array; strings also support jsonb contains.\n contains(value: string | V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n ncontains(value: string | V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n overlaps(value: V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n noverlaps(value: V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n /** Adds a parameterized JSON path existence condition using `@?`. */\n pathExists(jsonPath: string | undefined): ExpressionCondition;\n /** Adds a parameterized JSON path predicate condition using `@@`. */\n pathIsTrue(jsonPath: string | undefined): ExpressionCondition;\n /** Adds an operator and expression with knex-style placeholders, without the column name. */\n raw(exp: string, bindings: readonly unknown[] | undefined): ExpressionCondition;\n}\n\nexport interface EntityAlias<T, N extends null | never = never> extends AliasColumn<IdOf<T> | N> {\n eq(value: T | IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n ne(value: T | IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n // Adding `| null` for GraphQL support\n in(values: readonly (T | IdOf<T> | null)[] | null | undefined): ExpressionCondition;\n nin(values: readonly (T | IdOf<T> | null)[] | null | undefined): ExpressionCondition;\n gt(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n gte(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n lt(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n lte(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n raw(exp: string, bindings: readonly unknown[] | undefined): ExpressionCondition;\n}\n\nexport interface PolyAlias<T extends Entity> {\n eq(value: T | TaggedId | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n ne(value: T | TaggedId | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n in(values: readonly (T | TaggedId)[] | undefined): ExpressionCondition;\n}\n\n/** Returns the runtime identity bound by the find parser. */\nexport function getAliasMgmt(alias: { readonly [aliasMgmt]: AliasMgmt }): AliasMgmt {\n return alias[aliasMgmt];\n}\n\n/** Returns the original metadata, preserving STI subtype identity. */\nexport function getAliasMetadata<T extends Entity>(alias: Alias<T>): EntityMetadata<T> {\n return alias[aliasMgmt].meta as EntityMetadata<T>;\n}\n\n/** Creates domain predicates lazily from entity field metadata. */\nexport function newAliasProxy<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Alias<T> {\n const meta = getMetadata(cstr);\n const mgmt: AliasMgmt = { tableName: meta.tableName, meta };\n return new Proxy(cstr, {\n get(_, key) {\n if (key === aliasMgmt) return mgmt;\n if (typeof key !== \"string\") return undefined;\n const field = meta.allFields[key] ?? fail(`No field ${key} on ${cstr.name}`);\n if (field.kind === \"poly\") return new PolyReferenceAlias(meta, mgmt, field);\n if ([\"primaryKey\", \"primitive\", \"enum\", \"m2o\"].includes(field.kind)) {\n return new AliasColumnImpl(meta, field, field.serde!.columns[0], mgmt);\n }\n return fail(`Unsupported alias field kind ${field.kind}`);\n },\n has(_, key) {\n return key === aliasMgmt || key in meta.allFields;\n },\n }) as unknown as Alias<T>;\n}\n\n/** Recognizes only domain aliases, not physical tables. */\nexport function isAlias(value: unknown): value is Alias<Entity> {\n return typeof value === \"function\" && aliasMgmt in value;\n}\n\n/** Builds field conditions without implementing the SQL expression protocol. */\nclass AliasColumnImpl {\n declare readonly [aliasColumn]: unknown;\n\n constructor(\n readonly meta: EntityMetadata,\n readonly field: Field & { aliasSuffix: string },\n readonly column: Column,\n readonly mgmt: AliasMgmt,\n ) {}\n\n eq(value: unknown): ExpressionCondition {\n return this.compare(\"eq\", \"=\", value);\n }\n ne(value: unknown): ExpressionCondition {\n return this.compare(\"ne\", \"!=\", value);\n }\n gt(value: unknown): ExpressionCondition {\n return this.compare(\"gt\", \">\", value);\n }\n gte(value: unknown): ExpressionCondition {\n return this.compare(\"gte\", \">=\", value);\n }\n lt(value: unknown): ExpressionCondition {\n return this.compare(\"lt\", \"<\", value);\n }\n lte(value: unknown): ExpressionCondition {\n return this.compare(\"lte\", \"<=\", value);\n }\n contains(value: unknown): ExpressionCondition {\n return this.compare(\"contains\", \"@>\", value);\n }\n ncontains(value: unknown): ExpressionCondition {\n if (value instanceof AliasColumnImpl) return crossColumnCondition(this, value, \"@>\", true);\n return this.addCondition({ kind: \"ncontains\", value: value as readonly unknown[] });\n }\n overlaps(value: unknown): ExpressionCondition {\n return this.compare(\"overlaps\", \"&&\", value);\n }\n noverlaps(value: unknown): ExpressionCondition {\n if (value instanceof AliasColumnImpl) return crossColumnCondition(this, value, \"&&\", true);\n return this.addCondition({ kind: \"noverlaps\", value: value as readonly unknown[] });\n }\n\n in(values: readonly unknown[] | null | undefined): ExpressionCondition {\n if (values === undefined) return skipCondition;\n if (values === null) return fail(\"Unsupported\");\n if (values.includes(null)) {\n return {\n or: [\n this.addCondition({ kind: \"is-null\" }),\n this.addCondition({ kind: \"in\", value: values.filter((v) => v !== null) }),\n ],\n };\n }\n return this.addCondition({ kind: \"in\", value: [...values] });\n }\n\n nin(values: readonly unknown[] | null | undefined): ExpressionCondition {\n if (values === undefined) return skipCondition;\n if (values === null) return fail(\"Unsupported\");\n return this.addCondition({ kind: \"nin\", value: values.filter((v) => v !== null) });\n }\n\n between(v1: unknown, v2: unknown): ExpressionCondition {\n if (v1 === undefined || v2 === undefined) return skipCondition;\n return this.addCondition({ kind: \"between\", value: [v1, v2] });\n }\n\n like(value: unknown): ExpressionCondition {\n return this.addCondition({ kind: \"like\", value });\n }\n ilike(value: unknown): ExpressionCondition {\n return this.addCondition({ kind: \"ilike\", value });\n }\n search(value: unknown): ExpressionCondition {\n // Check !value so that empty strings are pruned\n return !value ? skipCondition : this.addCondition({ kind: \"ilike\", value: makeLike(value) });\n }\n pathExists(value: string | undefined): ExpressionCondition {\n return this.addCondition({ kind: \"jsonPathExists\", value: value! });\n }\n pathIsTrue(value: string | undefined): ExpressionCondition {\n return this.addCondition({ kind: \"jsonPathPredicate\", value: value! });\n }\n\n raw(exp: string, bindings: readonly unknown[] | undefined): ExpressionCondition {\n if (bindings === undefined) return skipCondition;\n const cond: RawCondition = { kind: \"raw\", aliases: [], condition: \"unset\", pruneable: false, bindings };\n return withDeferredAlias(cond, (resolve, copy) => {\n const r = resolve(this.mgmt);\n const alias = getMaybeCtiAlias(this.meta, this.field, r.meta, r.alias);\n copy.aliases = [alias];\n copy.condition = `${alias}.${this.column.columnName} ${exp}`;\n });\n }\n\n /** Maps literal values through the field serde and binds aliases only when parsing. */\n addCondition(value: ParsedValueFilter<unknown>): ColumnCondition {\n if (\"value\" in value && value.value === undefined) return skipCondition;\n const cond: ColumnCondition = {\n kind: \"column\",\n alias: \"unset\",\n column: this.column.columnName,\n dbType: this.column.dbType,\n cond: mapToDb(this.column, value),\n };\n return withDeferredAlias(cond, (resolve, copy) => {\n const r = resolve(this.mgmt);\n copy.alias = getMaybeCtiAlias(this.meta, this.field, r.meta, r.alias);\n });\n }\n\n /** Handles literal values, NULL, and domain cross-alias comparisons. */\n private compare(\n kind: \"eq\" | \"ne\" | \"gt\" | \"gte\" | \"lt\" | \"lte\" | \"contains\" | \"ncontains\" | \"overlaps\",\n op: string,\n value: unknown,\n ): ExpressionCondition {\n if (value === undefined) return skipCondition;\n if (value instanceof AliasColumnImpl) return crossColumnCondition(this, value, op);\n if (value === null && (kind === \"eq\" || kind === \"ne\"))\n return this.addCondition({ kind: kind === \"eq\" ? \"is-null\" : \"not-null\" });\n if (value === null && [\"gt\", \"gte\", \"lt\", \"lte\"].includes(kind)) return fail(\"Unsupported\");\n return this.addCondition({ kind, value } as ParsedValueFilter<unknown>);\n }\n}\n\n/** Resolves polymorphic predicates to their physical component columns. */\nclass PolyReferenceAlias {\n constructor(\n private meta: EntityMetadata,\n private mgmt: AliasMgmt,\n private field: PolymorphicField & { aliasSuffix: string },\n ) {}\n\n eq(value: unknown): ExpressionCondition {\n return this.addEqOrNe(\"eq\", value);\n }\n ne(value: unknown): ExpressionCondition {\n return this.addEqOrNe(\"ne\", value);\n }\n\n in(values: readonly (Entity | TaggedId)[] | undefined): ExpressionCondition {\n if (values === undefined) return skipCondition;\n // Split up the ids by constructor\n const groups = groupBy(values, (id) => getConstructorFromTaggedId(maybeResolveReferenceToId(id)!).name);\n // Or together `parent_book_id in (1,2,3) OR parent_author_id IN (4,5,6)`\n return {\n or: Object.entries(groups).map(([name, ids]) => {\n const comp =\n this.field.components.find((c) => c.otherMetadata().cstr.name === name) ?? fail(`No component for ${name}`);\n return this.component(comp).addCondition({ kind: \"in\", value: ids });\n }),\n };\n }\n\n /** Picks the component from an entity, tagged id, or another domain alias column. */\n private addEqOrNe(kind: \"eq\" | \"ne\", value: unknown): ExpressionCondition {\n if (value === undefined) return skipCondition;\n if (value === null) {\n // We can AND each of the components as many conditions\n return {\n and: this.field.components.map((c) =>\n this.component(c).addCondition({ kind: kind === \"eq\" ? \"is-null\" : \"not-null\" }),\n ),\n };\n }\n const otherMeta =\n value instanceof AliasColumnImpl\n ? value.field.kind === \"m2o\"\n ? value.field.otherMetadata()\n : value.meta\n : getMetadata(getConstructorFromTaggedId(maybeResolveReferenceToId(value as Entity | TaggedId)!));\n const comp =\n this.field.components.find((c) => getBaseAndSelfMetas(otherMeta).includes(c.otherMetadata())) ??\n fail(`${this.field.fieldName} has no component for ${otherMeta.type}`);\n const column = this.component(comp);\n return kind === \"eq\" ? column.eq(value) : column.ne(value);\n }\n\n /** Uses the component serde, not the first component's codec. */\n private component(comp: PolymorphicFieldComponent): AliasColumnImpl {\n const column = this.field.serde.columns.find((c) => c.columnName === comp.columnName) ?? fail(\"Missing column\");\n return new AliasColumnImpl(this.meta, this.field, column, this.mgmt);\n }\n}\n\n/** Adjusts a field's SQL alias when a base alias is bound to a CTI subtype in the join tree. */\nexport function getMaybeCtiAlias(\n meta: EntityMetadata,\n field: Field & { aliasSuffix: string },\n newMeta: EntityMetadata,\n newAlias: string,\n): string {\n // Do we have mismatched `em.find(ChildMeta)` with a `alias(BaseMeta)`? If so, the\n // usual `${field.aliasSuffix}` won't know it should have a suffix, so we need to calc it.\n if (newMeta !== meta && newMeta.inheritanceType === \"cti\") {\n const bases = getBaseAndSelfMetas(newMeta);\n const fieldIsFromBase = bases.includes(newMeta);\n if (fieldIsFromBase) return `${newAlias}_b0`;\n }\n return `${newAlias}${field.aliasSuffix}`;\n}\n\n/** Compares domain columns with per-parse aliases, preserving condition reuse across find calls. */\nfunction crossColumnCondition(left: AliasColumnImpl, right: AliasColumnImpl, op: string, negate = false): RawCondition {\n const cond: RawCondition = { kind: \"raw\", aliases: [], condition: \"unset\", bindings: [], pruneable: false };\n return withDeferredAlias(cond, (resolve, copy) => {\n const l = resolve(left.mgmt);\n const r = resolve(right.mgmt);\n const a1 = getMaybeCtiAlias(left.meta, left.field, l.meta, l.alias);\n const a2 = getMaybeCtiAlias(right.meta, right.field, r.meta, r.alias);\n copy.aliases = [a1, a2];\n const sql = `${a1}.${left.column.colum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;CAAA;CAE1C,OAAO,GAAG,WAAW,MAAM;AAC7B;;AAGA,SAAS,qBAAqB,MAAuB,OAAwB,IAAY,SAAS,OAAqB;CAErH,OAAO,kBAAkB;EADI,MAAM;EAAO,SAAS,CAAC;EAAG,WAAW;EAAS,UAAU,CAAC;EAAG,WAAW;CACxE,IAAI,SAAS,SAAS;EAChD,MAAM,IAAI,QAAQ,KAAK,IAAI;EAC3B,MAAM,IAAI,QAAQ,MAAM,IAAI;EAC5B,MAAM,KAAK,iBAAiB,KAAK,MAAM,KAAK,OAAO,EAAE,MAAM,EAAE,KAAK;EAClE,MAAM,KAAK,iBAAiB,MAAM,MAAM,MAAM,OAAO,EAAE,MAAM,EAAE,KAAK;EACpE,KAAK,UAAU,CAAC,IAAI,EAAE;EACtB,MAAM,MAAM,GAAG,GAAG,GAAG,KAAK,OAAO,WAAW,GAAG,GAAG,GAAG,GAAG,GAAG,MAAM,OAAO;EACxE,KAAK,YAAY,SAAS,QAAQ,IAAI,KAAK;CAC7C,CAAC;AACH"}
1
+ {"version":3,"file":"Aliases.js","names":[],"sources":["../src/Aliases.ts"],"sourcesContent":["import { groupBy } from \"joist-utils\";\n\nimport { type Column } from \"./columns.ts\";\n// Load configure first: relations must not evaluate before their base classes exist.\nimport { getConstructorFromTaggedId } from \"./configure.ts\";\nimport { withDeferredAlias } from \"./DeferredAlias.ts\";\nimport { type Entity } from \"./Entity.ts\";\nimport { type ExpressionCondition } from \"./EntityFilter.ts\";\nimport { type IdOf, type MaybeAbstractEntityConstructor, type TaggedId } from \"./EntityManager.ts\";\nimport {\n type EntityMetadata,\n type Field,\n type PolymorphicField,\n type PolymorphicFieldComponent,\n getBaseAndSelfMetas,\n getMetadata,\n} from \"./EntityMetadata.ts\";\nimport { maybeResolveReferenceToId } from \"./keys.ts\";\nimport { type ColumnCondition, type ParsedValueFilter, type RawCondition, makeLike, mapToDb } from \"./QueryParser.ts\";\nimport { skipCondition } from \"./skipCondition.ts\";\nimport { type FieldsOf } from \"./typeMap.ts\";\nimport { fail } from \"./utils.ts\";\n\n/** Creates an alias for complex domain filtering in `em.find`. */\nexport function alias<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Alias<T> {\n return newAliasProxy(cstr);\n}\n\n/** Creates multiple aliases for complex filtering. */\nexport function aliases<T extends readonly MaybeAbstractEntityConstructor<any>[]>(\n ...types: T\n): { [P in keyof T]: T[P] extends MaybeAbstractEntityConstructor<infer E extends Entity> ? Alias<E> : never } {\n return types.map((t) => newAliasProxy(t)) as any;\n}\n\nexport const aliasMgmt = Symbol(\"aliasMgmt\");\nconst aliasColumn = Symbol(\"aliasColumn\");\n\n/** The identity and original entity metadata of an `em.find` alias. */\nexport interface AliasMgmt {\n tableName: string;\n meta: EntityMetadata;\n /**\n * Builds a raw condition using each query's bound metadata and SQL alias, before join pruning.\n * Return a fresh condition instead of mutating shared state; the callback runs only when used.\n */\n condition(build: (meta: EntityMetadata, alias: string) => RawCondition): RawCondition;\n}\n\n/** Keeps domain aliases covariant in their entity type. */\nexport interface AliasBrand<T> extends AliasMgmt {\n readonly __entity: T;\n}\n\n/**\n * Domain field predicates, deliberately separate from SQL table expressions.\n *\n * An Alias names a place in an em.find relationship tree, not a fixed SQL table occurrence.\n * I.e. with `a = alias(Author)`, `{ author: { as: a } }` binds `a` to the Book's Author;\n * binding it under `{ author: { mentor: { as: a } } }` instead makes it the Author's mentor.\n * The find parser builds those joins and knows which entity metadata and SQL name apply there,\n * including any inheritance joins. The Alias cannot know that when `a.firstName.eq(name)` is created.\n *\n * DeferredAlias therefore resolves each predicate to a fresh column/raw condition using that parse's\n * binding. The resolved condition records the SQL aliases it reads, including both sides of a\n * cross-alias comparison. Join pruning uses those references to retain the joins the filter needs.\n * An undefined predicate is skipped, so it does not keep an otherwise-unused join alive; explicit\n * keepAliases and other query dependencies can still retain that join.\n *\n * Table expressions solve a different problem: their sources are supplied directly in from/join,\n * so they render through ExprContext rather than asking the find parser to bind a relationship path.\n * Neither path stores a resolved SQL name on the reusable handle or predicate.\n */\nexport type Alias<T extends Entity> = { readonly [aliasMgmt]: AliasBrand<T> } & {\n [P in keyof FieldsOf<T>]: P extends \"id\"\n ? EntityAlias<T>\n : FieldsOf<T>[P] extends { kind: \"primitive\" | \"enum\"; type: infer V; nullable: infer N }\n ? PrimitiveAlias<V, N extends undefined ? null : never>\n : FieldsOf<T>[P] extends { kind: \"m2o\"; type: infer U extends Entity; nullable: infer N }\n ? EntityAlias<U, N extends undefined ? null : never>\n : FieldsOf<T>[P] extends { kind: \"poly\"; type: infer U extends Entity }\n ? PolyAlias<U>\n : never;\n};\n\n/** A domain column accepted on the other side of a cross-alias comparison. */\nexport interface AliasColumn<V> {\n readonly [aliasColumn]: V;\n}\n\nexport interface PrimitiveAlias<V, N extends null | never = never> extends AliasColumn<V | N> {\n eq(value: V | N | AliasColumn<V | null> | undefined): ExpressionCondition;\n ne(value: V | N | AliasColumn<V | null> | undefined): ExpressionCondition;\n in(values: readonly (V | null)[] | undefined): ExpressionCondition;\n nin(values: readonly (V | null)[] | undefined): ExpressionCondition;\n gt(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n gte(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n lt(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n lte(value: V | AliasColumn<V | null> | undefined): ExpressionCondition;\n like(value: V | undefined): ExpressionCondition;\n ilike(value: V | undefined): ExpressionCondition;\n search(value: V | undefined): ExpressionCondition;\n between(v1: V | undefined, v2: V | undefined): ExpressionCondition;\n // V is already an array; strings also support jsonb contains.\n contains(value: string | V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n ncontains(value: string | V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n overlaps(value: V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n noverlaps(value: V | AliasColumn<V | null> | N | undefined): ExpressionCondition;\n /** Adds a parameterized JSON path existence condition using `@?`. */\n pathExists(jsonPath: string | undefined): ExpressionCondition;\n /** Adds a parameterized JSON path predicate condition using `@@`. */\n pathIsTrue(jsonPath: string | undefined): ExpressionCondition;\n /** Adds an operator and expression with knex-style placeholders, without the column name. */\n raw(exp: string, bindings: readonly unknown[] | undefined): ExpressionCondition;\n}\n\nexport interface EntityAlias<T, N extends null | never = never> extends AliasColumn<IdOf<T> | N> {\n eq(value: T | IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n ne(value: T | IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n // Adding `| null` for GraphQL support\n in(values: readonly (T | IdOf<T> | null)[] | null | undefined): ExpressionCondition;\n nin(values: readonly (T | IdOf<T> | null)[] | null | undefined): ExpressionCondition;\n gt(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n gte(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n lt(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n lte(value: IdOf<T> | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n raw(exp: string, bindings: readonly unknown[] | undefined): ExpressionCondition;\n}\n\nexport interface PolyAlias<T extends Entity> {\n eq(value: T | TaggedId | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n ne(value: T | TaggedId | AliasColumn<IdOf<T> | null> | null | undefined): ExpressionCondition;\n in(values: readonly (T | TaggedId)[] | undefined): ExpressionCondition;\n}\n\n/** Returns the runtime identity bound by the find parser. */\nexport function getAliasMgmt(alias: { readonly [aliasMgmt]: AliasMgmt }): AliasMgmt {\n return alias[aliasMgmt];\n}\n\n/** Returns the original metadata, preserving STI subtype identity. */\nexport function getAliasMetadata<T extends Entity>(alias: Alias<T>): EntityMetadata<T> {\n return alias[aliasMgmt].meta as EntityMetadata<T>;\n}\n\n/** Creates domain predicates lazily from entity field metadata. */\nexport function newAliasProxy<T extends Entity>(cstr: MaybeAbstractEntityConstructor<T>): Alias<T> {\n const meta = getMetadata(cstr);\n const mgmt: AliasMgmt = {\n tableName: meta.tableName,\n meta,\n condition(build) {\n const cond: RawCondition = { kind: \"raw\", aliases: [], condition: \"unset\", bindings: [], pruneable: false };\n return withDeferredAlias(cond, (resolve, copy) => {\n const bound = resolve(mgmt);\n Object.assign(copy, build(bound.meta, bound.alias));\n });\n },\n };\n return new Proxy(cstr, {\n get(_, key) {\n if (key === aliasMgmt) return mgmt;\n if (typeof key !== \"string\") return undefined;\n const field = meta.allFields[key] ?? fail(`No field ${key} on ${cstr.name}`);\n if (field.kind === \"poly\") return new PolyReferenceAlias(meta, mgmt, field);\n if ([\"primaryKey\", \"primitive\", \"enum\", \"m2o\"].includes(field.kind)) {\n return new AliasColumnImpl(meta, field, field.serde!.columns[0], mgmt);\n }\n return fail(`Unsupported alias field kind ${field.kind}`);\n },\n has(_, key) {\n return key === aliasMgmt || key in meta.allFields;\n },\n }) as unknown as Alias<T>;\n}\n\n/** Recognizes only domain aliases, not physical tables. */\nexport function isAlias(value: unknown): value is Alias<Entity> {\n return typeof value === \"function\" && aliasMgmt in value;\n}\n\n/** Builds field conditions without implementing the SQL expression protocol. */\nclass AliasColumnImpl {\n declare readonly [aliasColumn]: unknown;\n\n constructor(\n readonly meta: EntityMetadata,\n readonly field: Field & { aliasSuffix: string },\n readonly column: Column,\n readonly mgmt: AliasMgmt,\n ) {}\n\n eq(value: unknown): ExpressionCondition {\n return this.compare(\"eq\", \"=\", value);\n }\n ne(value: unknown): ExpressionCondition {\n return this.compare(\"ne\", \"!=\", value);\n }\n gt(value: unknown): ExpressionCondition {\n return this.compare(\"gt\", \">\", value);\n }\n gte(value: unknown): ExpressionCondition {\n return this.compare(\"gte\", \">=\", value);\n }\n lt(value: unknown): ExpressionCondition {\n return this.compare(\"lt\", \"<\", value);\n }\n lte(value: unknown): ExpressionCondition {\n return this.compare(\"lte\", \"<=\", value);\n }\n contains(value: unknown): ExpressionCondition {\n return this.compare(\"contains\", \"@>\", value);\n }\n ncontains(value: unknown): ExpressionCondition {\n if (value instanceof AliasColumnImpl) return crossColumnCondition(this, value, \"@>\", true);\n return this.addCondition({ kind: \"ncontains\", value: value as readonly unknown[] });\n }\n overlaps(value: unknown): ExpressionCondition {\n return this.compare(\"overlaps\", \"&&\", value);\n }\n noverlaps(value: unknown): ExpressionCondition {\n if (value instanceof AliasColumnImpl) return crossColumnCondition(this, value, \"&&\", true);\n return this.addCondition({ kind: \"noverlaps\", value: value as readonly unknown[] });\n }\n\n in(values: readonly unknown[] | null | undefined): ExpressionCondition {\n if (values === undefined) return skipCondition;\n if (values === null) return fail(\"Unsupported\");\n if (values.includes(null)) {\n return {\n or: [\n this.addCondition({ kind: \"is-null\" }),\n this.addCondition({ kind: \"in\", value: values.filter((v) => v !== null) }),\n ],\n };\n }\n return this.addCondition({ kind: \"in\", value: [...values] });\n }\n\n nin(values: readonly unknown[] | null | undefined): ExpressionCondition {\n if (values === undefined) return skipCondition;\n if (values === null) return fail(\"Unsupported\");\n return this.addCondition({ kind: \"nin\", value: values.filter((v) => v !== null) });\n }\n\n between(v1: unknown, v2: unknown): ExpressionCondition {\n if (v1 === undefined || v2 === undefined) return skipCondition;\n return this.addCondition({ kind: \"between\", value: [v1, v2] });\n }\n\n like(value: unknown): ExpressionCondition {\n return this.addCondition({ kind: \"like\", value });\n }\n ilike(value: unknown): ExpressionCondition {\n return this.addCondition({ kind: \"ilike\", value });\n }\n search(value: unknown): ExpressionCondition {\n // Check !value so that empty strings are pruned\n return !value ? skipCondition : this.addCondition({ kind: \"ilike\", value: makeLike(value) });\n }\n pathExists(value: string | undefined): ExpressionCondition {\n return this.addCondition({ kind: \"jsonPathExists\", value: value! });\n }\n pathIsTrue(value: string | undefined): ExpressionCondition {\n return this.addCondition({ kind: \"jsonPathPredicate\", value: value! });\n }\n\n raw(exp: string, bindings: readonly unknown[] | undefined): ExpressionCondition {\n if (bindings === undefined) return skipCondition;\n const cond: RawCondition = { kind: \"raw\", aliases: [], condition: \"unset\", pruneable: false, bindings };\n return withDeferredAlias(cond, (resolve, copy) => {\n const r = resolve(this.mgmt);\n const alias = getMaybeCtiAlias(this.meta, this.field, r.meta, r.alias);\n copy.aliases = [alias];\n copy.condition = `${alias}.${this.column.columnName} ${exp}`;\n });\n }\n\n /** Maps literal values through the field serde and binds aliases only when parsing. */\n addCondition(value: ParsedValueFilter<unknown>): ColumnCondition {\n if (\"value\" in value && value.value === undefined) return skipCondition;\n const cond: ColumnCondition = {\n kind: \"column\",\n alias: \"unset\",\n column: this.column.columnName,\n dbType: this.column.dbType,\n cond: mapToDb(this.column, value),\n };\n return withDeferredAlias(cond, (resolve, copy) => {\n const r = resolve(this.mgmt);\n copy.alias = getMaybeCtiAlias(this.meta, this.field, r.meta, r.alias);\n });\n }\n\n /** Handles literal values, NULL, and domain cross-alias comparisons. */\n private compare(\n kind: \"eq\" | \"ne\" | \"gt\" | \"gte\" | \"lt\" | \"lte\" | \"contains\" | \"ncontains\" | \"overlaps\",\n op: string,\n value: unknown,\n ): ExpressionCondition {\n if (value === undefined) return skipCondition;\n if (value instanceof AliasColumnImpl) return crossColumnCondition(this, value, op);\n if (value === null && (kind === \"eq\" || kind === \"ne\"))\n return this.addCondition({ kind: kind === \"eq\" ? \"is-null\" : \"not-null\" });\n if (value === null && [\"gt\", \"gte\", \"lt\", \"lte\"].includes(kind)) return fail(\"Unsupported\");\n return this.addCondition({ kind, value } as ParsedValueFilter<unknown>);\n }\n}\n\n/** Resolves polymorphic predicates to their physical component columns. */\nclass PolyReferenceAlias {\n constructor(\n private meta: EntityMetadata,\n private mgmt: AliasMgmt,\n private field: PolymorphicField & { aliasSuffix: string },\n ) {}\n\n eq(value: unknown): ExpressionCondition {\n return this.addEqOrNe(\"eq\", value);\n }\n ne(value: unknown): ExpressionCondition {\n return this.addEqOrNe(\"ne\", value);\n }\n\n in(values: readonly (Entity | TaggedId)[] | undefined): ExpressionCondition {\n if (values === undefined) return skipCondition;\n // Split up the ids by constructor\n const groups = groupBy(values, (id) => getConstructorFromTaggedId(maybeResolveReferenceToId(id)!).name);\n // Or together `parent_book_id in (1,2,3) OR parent_author_id IN (4,5,6)`\n return {\n or: Object.entries(groups).map(([name, ids]) => {\n const comp =\n this.field.components.find((c) => c.otherMetadata().cstr.name === name) ?? fail(`No component for ${name}`);\n return this.component(comp).addCondition({ kind: \"in\", value: ids });\n }),\n };\n }\n\n /** Picks the component from an entity, tagged id, or another domain alias column. */\n private addEqOrNe(kind: \"eq\" | \"ne\", value: unknown): ExpressionCondition {\n if (value === undefined) return skipCondition;\n if (value === null) {\n // We can AND each of the components as many conditions\n return {\n and: this.field.components.map((c) =>\n this.component(c).addCondition({ kind: kind === \"eq\" ? \"is-null\" : \"not-null\" }),\n ),\n };\n }\n const otherMeta =\n value instanceof AliasColumnImpl\n ? value.field.kind === \"m2o\"\n ? value.field.otherMetadata()\n : value.meta\n : getMetadata(getConstructorFromTaggedId(maybeResolveReferenceToId(value as Entity | TaggedId)!));\n const comp =\n this.field.components.find((c) => getBaseAndSelfMetas(otherMeta).includes(c.otherMetadata())) ??\n fail(`${this.field.fieldName} has no component for ${otherMeta.type}`);\n const column = this.component(comp);\n return kind === \"eq\" ? column.eq(value) : column.ne(value);\n }\n\n /** Uses the component serde, not the first component's codec. */\n private component(comp: PolymorphicFieldComponent): AliasColumnImpl {\n const column = this.field.serde.columns.find((c) => c.columnName === comp.columnName) ?? fail(\"Missing column\");\n return new AliasColumnImpl(this.meta, this.field, column, this.mgmt);\n }\n}\n\n/** Adjusts a field's SQL alias when a base alias is bound to a CTI subtype in the join tree. */\nexport function getMaybeCtiAlias(\n meta: EntityMetadata,\n field: Field & { aliasSuffix: string },\n newMeta: EntityMetadata,\n newAlias: string,\n): string {\n // Do we have mismatched `em.find(ChildMeta)` with a `alias(BaseMeta)`? If so, the\n // usual `${field.aliasSuffix}` won't know it should have a suffix, so we need to calc it.\n if (newMeta !== meta && newMeta.inheritanceType === \"cti\") {\n const bases = getBaseAndSelfMetas(newMeta);\n const fieldIsFromBase = bases.includes(newMeta);\n if (fieldIsFromBase) return `${newAlias}_b0`;\n }\n return `${newAlias}${field.aliasSuffix}`;\n}\n\n/** Compares domain columns with per-parse aliases, preserving condition reuse across find calls. */\nfunction crossColumnCondition(left: AliasColumnImpl, right: AliasColumnImpl, op: string, negate = false): RawCondition {\n const cond: RawCondition = { kind: \"raw\", aliases: [], condition: \"unset\", bindings: [], pruneable: false };\n return withDeferredAlias(cond, (resolve, copy) => {\n const l = resolve(left.mgmt);\n const r = resolve(right.mgmt);\n const a1 = getMaybeCtiAlias(left.meta, left.field, l.meta, l.alias);\n const a2 = getMaybeCtiAlias(right.meta, right.field, r.meta, r.alias);\n copy.aliases = [a1, a2];\n const sql = `${a1}.${left.column.columnName} ${op} ${a2}.${right.column.columnName}`;\n copy.condition = negate ? `NOT (${sql})` : sql;\n });\n}\n"],"mappings":";;;;;;;;;;;;;;AAwBA,SAAgB,MAAwB,MAAmD;CACzF,OAAO,cAAc,IAAI;AAC3B;;AAGA,SAAgB,QACd,GAAG,OACyG;CAC5G,OAAO,MAAM,KAAK,MAAM,cAAc,CAAC,CAAC;AAC1C;AAEA,MAAa,YAAY,OAAO,WAAW;;AAqG3C,SAAgB,aAAa,OAAuD;CAClF,OAAO,MAAM;AACf;;AAGA,SAAgB,iBAAmC,OAAoC;CACrF,OAAO,MAAM,UAAU,CAAC;AAC1B;;AAGA,SAAgB,cAAgC,MAAmD;CACjG,MAAM,OAAO,YAAY,IAAI;CAC7B,MAAM,OAAkB;EACtB,WAAW,KAAK;EAChB;EACA,UAAU,OAAO;GAEf,OAAO,kBAAkB;IADI,MAAM;IAAO,SAAS,CAAC;IAAG,WAAW;IAAS,UAAU,CAAC;IAAG,WAAW;GACxE,IAAI,SAAS,SAAS;IAChD,MAAM,QAAQ,QAAQ,IAAI;IAC1B,OAAO,OAAO,MAAM,MAAM,MAAM,MAAM,MAAM,KAAK,CAAC;GACpD,CAAC;EACH;CACF;CACA,OAAO,IAAI,MAAM,MAAM;EACrB,IAAI,GAAG,KAA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,GAAG,MAAM,OAAO;EACxE,KAAK,YAAY,SAAS,QAAQ,IAAI,KAAK;CAC7C,CAAC;AACH"}
@@ -1,5 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  require("./EntityMetadata.cjs");
3
+ require("./Aliases.cjs");
3
4
  require("./QueryParser.cjs");
4
5
  //#region src/DeferredAlias.ts
5
6
  const deferredAliasSym = Symbol("joist.deferredAliasCondition");
@@ -1 +1 @@
1
- {"version":3,"file":"DeferredAlias.cjs","names":[],"sources":["../src/DeferredAlias.ts"],"sourcesContent":["// Erased imports keep domain predicates independent of the query parser at module load time.\nimport { type EntityMetadata } from \"./EntityMetadata.ts\";\nimport { type ColumnCondition, type RawCondition } from \"./QueryParser.ts\";\n\nexport const deferredAliasSym: unique symbol = Symbol(\"joist.deferredAliasCondition\");\n\n/** Resolves a domain alias to this em.find parse's binding. */\nexport type AliasResolver = (handle: { meta: EntityMetadata; tableName: string }) => {\n meta: EntityMetadata;\n alias: string;\n};\n\n/** A domain condition whose SQL aliases are resolved afresh for each occurrence in em.find. */\nexport interface DeferredAliasCondition<C = ColumnCondition | RawCondition> {\n [deferredAliasSym]: (resolve: AliasResolver) => C;\n}\n\n/** Detects conditions that carry a per-parse alias resolver. */\nexport function isDeferredAliasCondition<C>(cond: C): cond is C & DeferredAliasCondition<C> {\n return typeof cond === \"object\" && cond !== null && deferredAliasSym in cond;\n}\n\n/** Tags a condition without changing its enumerable shape; resolution only mutates a fresh copy. */\nexport function withDeferredAlias<C extends object>(\n cond: C,\n resolve: (r: AliasResolver, copy: C) => void,\n): C & DeferredAliasCondition<C> {\n return Object.defineProperty(cond, deferredAliasSym, {\n value: (r: AliasResolver) => {\n const copy = { ...cond };\n resolve(r, copy);\n return copy;\n },\n enumerable: false,\n }) as C & DeferredAliasCondition<C>;\n}\n"],"mappings":";;;;AAIA,MAAa,mBAAkC,OAAO,8BAA8B;;AAcpF,SAAgB,yBAA4B,MAAgD;CAC1F,OAAO,OAAO,SAAS,YAAY,SAAS,QAAQ,oBAAoB;AAC1E;;AAGA,SAAgB,kBACd,MACA,SAC+B;CAC/B,OAAO,OAAO,eAAe,MAAM,kBAAkB;EACnD,QAAQ,MAAqB;GAC3B,MAAM,OAAO,EAAE,GAAG,KAAK;GACvB,QAAQ,GAAG,IAAI;GACf,OAAO;EACT;EACA,YAAY;CACd,CAAC;AACH"}
1
+ {"version":3,"file":"DeferredAlias.cjs","names":[],"sources":["../src/DeferredAlias.ts"],"sourcesContent":["// Erased imports keep domain predicates independent of the query parser at module load time.\nimport { type AliasMgmt } from \"./Aliases.ts\";\nimport { type EntityMetadata } from \"./EntityMetadata.ts\";\nimport { type ColumnCondition, type RawCondition } from \"./QueryParser.ts\";\n\nexport const deferredAliasSym: unique symbol = Symbol(\"joist.deferredAliasCondition\");\n\n/** Resolves a domain alias to this em.find parse's binding. */\nexport type AliasResolver = (handle: AliasMgmt) => {\n meta: EntityMetadata;\n alias: string;\n};\n\n/** A domain condition whose SQL aliases are resolved afresh for each occurrence in em.find. */\nexport interface DeferredAliasCondition<C = ColumnCondition | RawCondition> {\n [deferredAliasSym]: (resolve: AliasResolver) => C;\n}\n\n/** Detects conditions that carry a per-parse alias resolver. */\nexport function isDeferredAliasCondition<C>(cond: C): cond is C & DeferredAliasCondition<C> {\n return typeof cond === \"object\" && cond !== null && deferredAliasSym in cond;\n}\n\n/** Tags a condition without changing its enumerable shape; resolution only mutates a fresh copy. */\nexport function withDeferredAlias<C extends object>(\n cond: C,\n resolve: (r: AliasResolver, copy: C) => void,\n): C & DeferredAliasCondition<C> {\n return Object.defineProperty(cond, deferredAliasSym, {\n value: (r: AliasResolver) => {\n const copy = { ...cond };\n resolve(r, copy);\n return copy;\n },\n enumerable: false,\n }) as C & DeferredAliasCondition<C>;\n}\n"],"mappings":";;;;;AAKA,MAAa,mBAAkC,OAAO,8BAA8B;;AAcpF,SAAgB,yBAA4B,MAAgD;CAC1F,OAAO,OAAO,SAAS,YAAY,SAAS,QAAQ,oBAAoB;AAC1E;;AAGA,SAAgB,kBACd,MACA,SAC+B;CAC/B,OAAO,OAAO,eAAe,MAAM,kBAAkB;EACnD,QAAQ,MAAqB;GAC3B,MAAM,OAAO,EAAE,GAAG,KAAK;GACvB,QAAQ,GAAG,IAAI;GACf,OAAO;EACT;EACA,YAAY;CACd,CAAC;AACH"}
@@ -1,12 +1,10 @@
1
1
  import { EntityMetadata } from "./EntityMetadata.cjs";
2
+ import { AliasMgmt } from "./Aliases.cjs";
2
3
  import { ColumnCondition, RawCondition } from "./QueryParser.cjs";
3
4
  //#region src/DeferredAlias.d.ts
4
5
  declare const deferredAliasSym: unique symbol;
5
6
  /** Resolves a domain alias to this em.find parse's binding. */
6
- type AliasResolver = (handle: {
7
- meta: EntityMetadata;
8
- tableName: string;
9
- }) => {
7
+ type AliasResolver = (handle: AliasMgmt) => {
10
8
  meta: EntityMetadata;
11
9
  alias: string;
12
10
  };
@@ -1 +1 @@
1
- {"version":3,"file":"DeferredAlias.d.cts","names":[],"sources":["../src/DeferredAlias.ts"],"mappings":";;;cAIa;;KAGD,iBAAiB;EAAU,MAAM;EAAgB;;EAC3D,MAAM;EACN;;;UAIe,uBAAuB,IAAI,kBAAkB;GAC3D,oBAAoB,SAAS,kBAAkB;;;iBAIlC,yBAAyB,GAAG,MAAM,IAAI,QAAQ,IAAI,uBAAuB;;iBAKzE,kBAAkB,kBAChC,MAAM,GACN,UAAU,GAAG,eAAe,MAAM,aACjC,IAAI,uBAAuB"}
1
+ {"version":3,"file":"DeferredAlias.d.cts","names":[],"sources":["../src/DeferredAlias.ts"],"mappings":";;;;cAKa;;KAGD,iBAAiB,QAAQ;EACnC,MAAM;EACN;;;UAIe,uBAAuB,IAAI,kBAAkB;GAC3D,oBAAoB,SAAS,kBAAkB;;;iBAIlC,yBAAyB,GAAG,MAAM,IAAI,QAAQ,IAAI,uBAAuB;;iBAKzE,kBAAkB,kBAChC,MAAM,GACN,UAAU,GAAG,eAAe,MAAM,aACjC,IAAI,uBAAuB"}
@@ -1,12 +1,10 @@
1
1
  import { EntityMetadata } from "./EntityMetadata.mjs";
2
+ import { AliasMgmt } from "./Aliases.mjs";
2
3
  import { ColumnCondition, RawCondition } from "./QueryParser.mjs";
3
4
  //#region src/DeferredAlias.d.ts
4
5
  declare const deferredAliasSym: unique symbol;
5
6
  /** Resolves a domain alias to this em.find parse's binding. */
6
- type AliasResolver = (handle: {
7
- meta: EntityMetadata;
8
- tableName: string;
9
- }) => {
7
+ type AliasResolver = (handle: AliasMgmt) => {
10
8
  meta: EntityMetadata;
11
9
  alias: string;
12
10
  };
@@ -1 +1 @@
1
- {"version":3,"file":"DeferredAlias.d.mts","names":[],"sources":["../src/DeferredAlias.ts"],"mappings":";;;cAIa;;KAGD,iBAAiB;EAAU,MAAM;EAAgB;;EAC3D,MAAM;EACN;;;UAIe,uBAAuB,IAAI,kBAAkB;GAC3D,oBAAoB,SAAS,kBAAkB;;;iBAIlC,yBAAyB,GAAG,MAAM,IAAI,QAAQ,IAAI,uBAAuB;;iBAKzE,kBAAkB,kBAChC,MAAM,GACN,UAAU,GAAG,eAAe,MAAM,aACjC,IAAI,uBAAuB"}
1
+ {"version":3,"file":"DeferredAlias.d.mts","names":[],"sources":["../src/DeferredAlias.ts"],"mappings":";;;;cAKa;;KAGD,iBAAiB,QAAQ;EACnC,MAAM;EACN;;;UAIe,uBAAuB,IAAI,kBAAkB;GAC3D,oBAAoB,SAAS,kBAAkB;;;iBAIlC,yBAAyB,GAAG,MAAM,IAAI,QAAQ,IAAI,uBAAuB;;iBAKzE,kBAAkB,kBAChC,MAAM,GACN,UAAU,GAAG,eAAe,MAAM,aACjC,IAAI,uBAAuB"}
@@ -1,4 +1,5 @@
1
1
  import "./EntityMetadata.js";
2
+ import "./Aliases.js";
2
3
  import "./QueryParser.js";
3
4
  //#region src/DeferredAlias.ts
4
5
  const deferredAliasSym = Symbol("joist.deferredAliasCondition");
@@ -1 +1 @@
1
- {"version":3,"file":"DeferredAlias.js","names":[],"sources":["../src/DeferredAlias.ts"],"sourcesContent":["// Erased imports keep domain predicates independent of the query parser at module load time.\nimport { type EntityMetadata } from \"./EntityMetadata.ts\";\nimport { type ColumnCondition, type RawCondition } from \"./QueryParser.ts\";\n\nexport const deferredAliasSym: unique symbol = Symbol(\"joist.deferredAliasCondition\");\n\n/** Resolves a domain alias to this em.find parse's binding. */\nexport type AliasResolver = (handle: { meta: EntityMetadata; tableName: string }) => {\n meta: EntityMetadata;\n alias: string;\n};\n\n/** A domain condition whose SQL aliases are resolved afresh for each occurrence in em.find. */\nexport interface DeferredAliasCondition<C = ColumnCondition | RawCondition> {\n [deferredAliasSym]: (resolve: AliasResolver) => C;\n}\n\n/** Detects conditions that carry a per-parse alias resolver. */\nexport function isDeferredAliasCondition<C>(cond: C): cond is C & DeferredAliasCondition<C> {\n return typeof cond === \"object\" && cond !== null && deferredAliasSym in cond;\n}\n\n/** Tags a condition without changing its enumerable shape; resolution only mutates a fresh copy. */\nexport function withDeferredAlias<C extends object>(\n cond: C,\n resolve: (r: AliasResolver, copy: C) => void,\n): C & DeferredAliasCondition<C> {\n return Object.defineProperty(cond, deferredAliasSym, {\n value: (r: AliasResolver) => {\n const copy = { ...cond };\n resolve(r, copy);\n return copy;\n },\n enumerable: false,\n }) as C & DeferredAliasCondition<C>;\n}\n"],"mappings":";;;AAIA,MAAa,mBAAkC,OAAO,8BAA8B;;AAcpF,SAAgB,yBAA4B,MAAgD;CAC1F,OAAO,OAAO,SAAS,YAAY,SAAS,QAAQ,oBAAoB;AAC1E;;AAGA,SAAgB,kBACd,MACA,SAC+B;CAC/B,OAAO,OAAO,eAAe,MAAM,kBAAkB;EACnD,QAAQ,MAAqB;GAC3B,MAAM,OAAO,EAAE,GAAG,KAAK;GACvB,QAAQ,GAAG,IAAI;GACf,OAAO;EACT;EACA,YAAY;CACd,CAAC;AACH"}
1
+ {"version":3,"file":"DeferredAlias.js","names":[],"sources":["../src/DeferredAlias.ts"],"sourcesContent":["// Erased imports keep domain predicates independent of the query parser at module load time.\nimport { type AliasMgmt } from \"./Aliases.ts\";\nimport { type EntityMetadata } from \"./EntityMetadata.ts\";\nimport { type ColumnCondition, type RawCondition } from \"./QueryParser.ts\";\n\nexport const deferredAliasSym: unique symbol = Symbol(\"joist.deferredAliasCondition\");\n\n/** Resolves a domain alias to this em.find parse's binding. */\nexport type AliasResolver = (handle: AliasMgmt) => {\n meta: EntityMetadata;\n alias: string;\n};\n\n/** A domain condition whose SQL aliases are resolved afresh for each occurrence in em.find. */\nexport interface DeferredAliasCondition<C = ColumnCondition | RawCondition> {\n [deferredAliasSym]: (resolve: AliasResolver) => C;\n}\n\n/** Detects conditions that carry a per-parse alias resolver. */\nexport function isDeferredAliasCondition<C>(cond: C): cond is C & DeferredAliasCondition<C> {\n return typeof cond === \"object\" && cond !== null && deferredAliasSym in cond;\n}\n\n/** Tags a condition without changing its enumerable shape; resolution only mutates a fresh copy. */\nexport function withDeferredAlias<C extends object>(\n cond: C,\n resolve: (r: AliasResolver, copy: C) => void,\n): C & DeferredAliasCondition<C> {\n return Object.defineProperty(cond, deferredAliasSym, {\n value: (r: AliasResolver) => {\n const copy = { ...cond };\n resolve(r, copy);\n return copy;\n },\n enumerable: false,\n }) as C & DeferredAliasCondition<C>;\n}\n"],"mappings":";;;;AAKA,MAAa,mBAAkC,OAAO,8BAA8B;;AAcpF,SAAgB,yBAA4B,MAAgD;CAC1F,OAAO,OAAO,SAAS,YAAY,SAAS,QAAQ,oBAAoB;AAC1E;;AAGA,SAAgB,kBACd,MACA,SAC+B;CAC/B,OAAO,OAAO,eAAe,MAAM,kBAAkB;EACnD,QAAQ,MAAqB;GAC3B,MAAM,OAAO,EAAE,GAAG,KAAK;GACvB,QAAQ,GAAG,IAAI;GACf,OAAO;EACT;EACA,YAAY;CACd,CAAC;AACH"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "joist-core",
3
- "version": "2.3.0-next.72",
3
+ "version": "2.3.0-next.73",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {
@@ -42,7 +42,7 @@
42
42
  "build"
43
43
  ],
44
44
  "peerDependencies": {
45
- "joist-utils": "2.3.0-next.72"
45
+ "joist-utils": "2.3.0-next.73"
46
46
  },
47
47
  "dependencies": {
48
48
  "ansis": "^4.3.1",