joist-core 2.3.0-next.74 → 2.3.0-next.76
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 +1 -0
- package/build/Aliases.cjs.map +1 -1
- package/build/Aliases.d.cts +2 -1
- package/build/Aliases.d.cts.map +1 -1
- package/build/Aliases.d.mts +2 -1
- package/build/Aliases.d.mts.map +1 -1
- package/build/Aliases.js +1 -0
- package/build/Aliases.js.map +1 -1
- package/build/ConditionBuilder.cjs +1 -1
- package/build/ConditionBuilder.cjs.map +1 -1
- package/build/ConditionBuilder.d.cts +2 -2
- package/build/ConditionBuilder.d.cts.map +1 -1
- package/build/ConditionBuilder.d.mts +2 -2
- package/build/ConditionBuilder.d.mts.map +1 -1
- package/build/ConditionBuilder.js +1 -1
- package/build/ConditionBuilder.js.map +1 -1
- package/build/DeferredAlias.cjs +2 -1
- package/build/DeferredAlias.cjs.map +1 -1
- package/build/DeferredAlias.d.cts +2 -1
- package/build/DeferredAlias.d.cts.map +1 -1
- package/build/DeferredAlias.d.mts +2 -1
- package/build/DeferredAlias.d.mts.map +1 -1
- package/build/DeferredAlias.js +2 -1
- package/build/DeferredAlias.js.map +1 -1
- package/build/EntityFilter.cjs +1 -1
- package/build/EntityFilter.cjs.map +1 -1
- package/build/EntityFilter.d.cts +4 -12
- package/build/EntityFilter.d.cts.map +1 -1
- package/build/EntityFilter.d.mts +4 -12
- package/build/EntityFilter.d.mts.map +1 -1
- package/build/EntityFilter.js +1 -1
- package/build/EntityFilter.js.map +1 -1
- package/build/Expr.cjs +3 -2
- package/build/Expr.cjs.map +1 -1
- package/build/Expr.d.cts +28 -28
- package/build/Expr.d.cts.map +1 -1
- package/build/Expr.d.mts +28 -28
- package/build/Expr.d.mts.map +1 -1
- package/build/Expr.js +3 -2
- package/build/Expr.js.map +1 -1
- package/build/QueryParser.cjs +15 -1
- package/build/QueryParser.cjs.map +1 -1
- package/build/QueryParser.d.cts.map +1 -1
- package/build/QueryParser.d.mts.map +1 -1
- package/build/QueryParser.js +15 -1
- package/build/QueryParser.js.map +1 -1
- package/build/Tables.cjs +1 -0
- package/build/Tables.cjs.map +1 -1
- package/build/Tables.d.cts +34 -35
- package/build/Tables.d.cts.map +1 -1
- package/build/Tables.d.mts +34 -35
- package/build/Tables.d.mts.map +1 -1
- package/build/Tables.js +1 -0
- package/build/Tables.js.map +1 -1
- package/build/conditions.cjs +16 -0
- package/build/conditions.cjs.map +1 -0
- package/build/conditions.d.cts +38 -0
- package/build/conditions.d.cts.map +1 -0
- package/build/conditions.d.mts +38 -0
- package/build/conditions.d.mts.map +1 -0
- package/build/conditions.js +14 -0
- package/build/conditions.js.map +1 -0
- package/build/execute.cjs +1 -1
- package/build/execute.cjs.map +1 -1
- package/build/execute.d.cts +2 -2
- package/build/execute.d.cts.map +1 -1
- package/build/execute.d.mts +2 -2
- package/build/execute.d.mts.map +1 -1
- package/build/execute.js +1 -1
- package/build/execute.js.map +1 -1
- package/build/index.cjs.map +1 -1
- package/build/index.d.cts +2 -1
- package/build/index.d.cts.map +1 -1
- package/build/index.d.mts +2 -1
- package/build/index.d.mts.map +1 -1
- package/build/index.js.map +1 -1
- package/build/query.cjs +15 -8
- package/build/query.cjs.map +1 -1
- package/build/query.d.cts +199 -199
- package/build/query.d.cts.map +1 -1
- package/build/query.d.mts +199 -199
- package/build/query.d.mts.map +1 -1
- package/build/query.js +15 -8
- package/build/query.js.map +1 -1
- package/package.json +2 -2
package/build/Aliases.cjs
CHANGED
package/build/Aliases.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,WAAW;;AAqG3C,SAAgB,aAAa,OAAuD;CAClF,OAAO,MAAM;AACf;;AAGA,SAAgB,i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AAQ,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"}
|
|
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\";\nimport { type PredicateBrand } from \"./conditions.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 & PredicateBrand<\"domain\">;\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>): ExpressionCondition {\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(\n left: AliasColumnImpl,\n right: AliasColumnImpl,\n op: string,\n negate = false,\n): RawCondition & PredicateBrand<\"domain\"> {\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":";;;;;;;;;;;;;;;;AAyBA,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,S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A,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,qBACP,MACA,OACA,IACA,SAAS,OACgC;CAEzC,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"}
|
package/build/Aliases.d.cts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PredicateBrand } from "./conditions.cjs";
|
|
1
2
|
import { FieldsOf } from "./typeMap.cjs";
|
|
2
3
|
import { EntityMetadata, Field } from "./EntityMetadata.cjs";
|
|
3
4
|
import { IdOf, MaybeAbstractEntityConstructor, TaggedId } from "./EntityManager.cjs";
|
|
@@ -19,7 +20,7 @@ interface AliasMgmt {
|
|
|
19
20
|
* Builds a raw condition using each query's bound metadata and SQL alias, before join pruning.
|
|
20
21
|
* Return a fresh condition instead of mutating shared state; the callback runs only when used.
|
|
21
22
|
*/
|
|
22
|
-
condition(build: (meta: EntityMetadata, alias: string) => RawCondition): RawCondition
|
|
23
|
+
condition(build: (meta: EntityMetadata, alias: string) => RawCondition): RawCondition & PredicateBrand<"domain">;
|
|
23
24
|
}
|
|
24
25
|
/** Keeps domain aliases covariant in their entity type. */
|
|
25
26
|
interface AliasBrand<T> extends AliasMgmt {
|
package/build/Aliases.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Aliases.d.cts","names":[],"sources":["../src/Aliases.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"Aliases.d.cts","names":[],"sources":["../src/Aliases.ts"],"mappings":";;;;;;;;;iBAyBgB,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;;;;;EAKN,UAAU,QAAQ,MAAM,gBAAgB,kBAAkB,eAAe,eAAe;;;UAIzE,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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AAQ,iBAAiB,SAAS,MAAM;;iBAiMxC,iBACd,MAAM,gBACN,OAAO;EAAU;GACjB,SAAS,gBACT"}
|
package/build/Aliases.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PredicateBrand } from "./conditions.mjs";
|
|
1
2
|
import { FieldsOf } from "./typeMap.mjs";
|
|
2
3
|
import { EntityMetadata, Field } from "./EntityMetadata.mjs";
|
|
3
4
|
import { IdOf, MaybeAbstractEntityConstructor, TaggedId } from "./EntityManager.mjs";
|
|
@@ -19,7 +20,7 @@ interface AliasMgmt {
|
|
|
19
20
|
* Builds a raw condition using each query's bound metadata and SQL alias, before join pruning.
|
|
20
21
|
* Return a fresh condition instead of mutating shared state; the callback runs only when used.
|
|
21
22
|
*/
|
|
22
|
-
condition(build: (meta: EntityMetadata, alias: string) => RawCondition): RawCondition
|
|
23
|
+
condition(build: (meta: EntityMetadata, alias: string) => RawCondition): RawCondition & PredicateBrand<"domain">;
|
|
23
24
|
}
|
|
24
25
|
/** Keeps domain aliases covariant in their entity type. */
|
|
25
26
|
interface AliasBrand<T> extends AliasMgmt {
|
package/build/Aliases.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Aliases.d.mts","names":[],"sources":["../src/Aliases.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"Aliases.d.mts","names":[],"sources":["../src/Aliases.ts"],"mappings":";;;;;;;;;iBAyBgB,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;;;;;EAKN,UAAU,QAAQ,MAAM,gBAAgB,kBAAkB,eAAe,eAAe;;;UAIzE,WAAW,WAAW;WAC5B,UAAU;;;;;;;;;;;;;;;;;;;;;KAsBT,MAAM,UAAU;YAAsB,YAAY,WAAW;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;EACjE,GAAG,OAAO,KAAK,KAAK,YAAY,KAAK,gCAAgC;EACrE,IAAI,OAAO,KAAK,KAAK,YAAY,KAAK,gCAAgC;EACtE,GAAG,OAAO,KAAK,KAAK,YAAY,KAAK,gCAAgC;EACrE,IAAI,OAAO,KAAK,KAAK,YAAY,KAAK,gCAAgC;EACtE,IAAI,aAAa,2CAA2C;;UAG7C,UAAU,UAAU;EACnC,GAAG,OAAO,IAAI,WAAW,YAAY,KAAK,gCAAgC;EAC1E,GAAG,OAAO,IAAI,WAAW,YAAY,KAAK,gCAAgC;EAC1E,GAAG,kBAAkB,IAAI,0BAA0B;;;iBAIrC,aAAa;YAAmB,YAAY;IAAc;;iBAK1D,iBAAiB,UAAU,QAAQ,OAAO,MAAM,KAAK,eAAe;;iBAKpE,cAAc,UAAU,QAAQ,MAAM,+BAA+B,KAAK,MAAM;;iBA+BhF,QAAQ,iBAAiB,SAAS,MAAM;;iBAiMxC,iBACd,MAAM,gBACN,OAAO;EAAU;GACjB,SAAS,gBACT"}
|
package/build/Aliases.js
CHANGED
package/build/Aliases.js.map
CHANGED
|
@@ -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 * 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;GACp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AM,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\";\nimport { type PredicateBrand } from \"./conditions.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 & PredicateBrand<\"domain\">;\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>): ExpressionCondition {\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(\n left: AliasColumnImpl,\n right: AliasColumnImpl,\n op: string,\n negate = false,\n): RawCondition & PredicateBrand<\"domain\"> {\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":";;;;;;;;;;;;;;;AAyBA,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,KAAK;GACV,IAAI,QAAQ,WAAW,OAAO;GAC9B,IAAI,OAAO,QAAQ,UAAU,OAAO,KAAA;GACpC,MAAM,QAAQ,KAAK,UAAU,QAAQ,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,OAAO,KAAK,gCAAgC,M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AAE,eAAe,KAAK,UAAU,KAAK,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,OACpC;MAAA,oBAAoB,OACN,CAAC,CAAC,SAAS,OACrB,GAAG,OAAO,GAAG,SAAS;CAAA;CAE1C,OAAO,GAAG,WAAW,MAAM;AAC7B;;AAGA,SAAS,qBACP,MACA,OACA,IACA,SAAS,OACgC;CAEzC,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,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
require("./
|
|
2
|
+
require("./conditions.cjs");
|
|
3
3
|
const require_utils = require("./utils.cjs");
|
|
4
4
|
const require_skipCondition = require("./skipCondition.cjs");
|
|
5
5
|
const require_EntityManager = require("./EntityManager.cjs");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConditionBuilder.cjs","names":["mapToDb","fail","partition","skipCondition","isDefined"],"sources":["../src/ConditionBuilder.ts"],"sourcesContent":["import { type Column } from \"./columns.ts\";\nimport { type
|
|
1
|
+
{"version":3,"file":"ConditionBuilder.cjs","names":["mapToDb","fail","partition","skipCondition","isDefined"],"sources":["../src/ConditionBuilder.ts"],"sourcesContent":["import { type Column } from \"./columns.ts\";\nimport { type ConditionGroup, type ConditionInput } from \"./conditions.ts\";\nimport { isDefined } from \"./EntityManager.ts\";\nimport {\n type ColumnCondition,\n type ParsedExpressionCondition,\n type ParsedExpressionFilter,\n type ParsedValueFilter,\n type RawCondition,\n mapToDb,\n} from \"./QueryParser.ts\";\nimport { skipCondition } from \"./skipCondition.ts\";\nimport { fail, partition } from \"./utils.ts\";\n\ntype PartialSome<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;\n\n/** Converts domain-level values like string ids/enums into their db equivalent. */\nexport class ConditionBuilder {\n /** Simple, single-column conditions, which will be AND-d together. */\n private conditions: (ColumnCondition | RawCondition)[] = [];\n /** Complex expressions, which will also be AND-d together with `conditions`. */\n private expressions: ParsedExpressionFilter[] = [];\n\n /** Accepts a raw user-facing DSL filter, and parses it into a `ParsedExpressionFilter`. */\n maybeAddExpression(expression: ConditionGroup<ConditionInput>): void {\n const parsed = parseExpression(expression);\n if (parsed) this.expressions.push(parsed);\n }\n\n /** Adds an already-db-level condition to the simple conditions list. */\n addSimpleCondition(condition: ColumnCondition): void {\n this.conditions.push(condition);\n }\n\n /** Adds an already-db-level condition to the simple conditions list. */\n addRawCondition(condition: PartialSome<RawCondition, \"kind\" | \"bindings\">): void {\n this.conditions.push({\n kind: \"raw\",\n bindings: [],\n ...condition,\n });\n }\n\n /** Adds an already-db-level expression to the expressions list. */\n addParsedExpression(parsed: ParsedExpressionFilter): void {\n this.expressions.push(parsed);\n }\n\n /**\n * Adds a user-facing `ParsedValueFilter` to the inline conditions.\n *\n * Unless it's something like `in: [a1, null]`, in which case we split it into two `is-null` and `in` conditions.\n */\n addValueFilter(alias: string, column: Column, filter: ParsedValueFilter<any>): void {\n if (filter.kind === \"in\" && filter.value.includes(null)) {\n // If the filter contains a null, we need to split it into an `is-null` and `in` condition\n const isNull = {\n kind: \"column\",\n alias,\n column: column.columnName,\n dbType: column.dbType,\n cond: { kind: \"is-null\" },\n } satisfies ColumnCondition;\n const inValues = {\n kind: \"column\",\n alias,\n column: column.columnName,\n dbType: column.dbType,\n cond: {\n kind: \"in\",\n // Filter out the nulls from the in condition\n value: filter.value.filter((v) => v !== null).map((v) => column.mapToDb(v)),\n },\n } satisfies ColumnCondition;\n // Now OR them back together\n this.expressions.push({ kind: \"exp\", op: \"or\", conditions: [isNull, inValues] });\n } else {\n const cond = {\n kind: \"column\",\n alias,\n column: column.columnName,\n dbType: column.dbType,\n // Rewrite the user-facing domain values to db values\n cond: mapToDb(column, filter),\n } satisfies ColumnCondition;\n this.conditions.push(cond);\n }\n }\n\n /** Combines our collected `conditions` and `expressions` into a single `ParsedExpressionFilter`. */\n toExpressionFilter(): ParsedExpressionFilter | undefined {\n const { expressions, conditions } = this;\n const allSimple: ParsedExpressionCondition[] = [...conditions];\n if (allSimple.length === 0 && expressions.length === 1) {\n // If no inline conditions, and just 1 opt expression, just use that\n return expressions[0];\n } else if (expressions.length === 1 && expressions[0].op === \"and\") {\n // Merge the 1 `AND` expression with the other simple conditions\n return { kind: \"exp\", op: \"and\", conditions: [...allSimple, ...expressions[0].conditions] };\n } else if (allSimple.length > 0 || expressions.length > 0) {\n // Combine the conditions within the `em.find` join literal & the `conditions` as ANDs\n return { kind: \"exp\", op: \"and\", conditions: [...allSimple, ...expressions] };\n }\n return undefined;\n }\n}\n\n/** Parses user-facing `{ and: ... }` or `{ or: ... }` into a `ParsedExpressionFilter`. */\nfunction parseExpression(expression: ConditionGroup<ConditionInput>): ParsedExpressionFilter | undefined {\n // Look for `{ and: [...] }` or `{ or: [...] }`\n const [op, expressions] =\n \"and\" in expression && expression.and\n ? [\"and\" as const, expression.and]\n : \"or\" in expression && expression.or\n ? [\"or\" as const, expression.or]\n : fail(`Invalid expression ${expression}`);\n // Potentially recurse into nested expressions\n const conditions = expressions.map((exp) => (exp && (\"and\" in exp || \"or\" in exp) ? parseExpression(exp) : exp));\n const [skip, valid] = partition(conditions, (cond) => cond === undefined || cond === skipCondition);\n if ((skip.length > 0 && expression.pruneIfUndefined === \"any\") || valid.length === 0) {\n return undefined;\n }\n return { kind: \"exp\", op, conditions: valid.filter(isDefined) };\n}\n"],"mappings":";;;;;;;;AAiBA,IAAa,mBAAb,MAA8B;;CAE5B,aAAyD,CAAC;;CAE1D,cAAgD,CAAC;;CAGjD,mBAAmB,YAAkD;EACnE,MAAM,SAAS,gBAAgB,UAAU;EACzC,IAAI,QAAQ,KAAK,YAAY,KAAK,MAAM;CAC1C;;CAGA,mBAAmB,WAAkC;EACnD,KAAK,WAAW,KAAK,SAAS;CAChC;;CAGA,gBAAgB,WAAiE;EAC/E,KAAK,WAAW,KAAK;GACnB,MAAM;GACN,UAAU,CAAC;GACX,GAAG;EACL,CAAC;CACH;;CAGA,oBAAoB,QAAsC;EACxD,KAAK,YAAY,KAAK,MAAM;CAC9B;;;;;;CAOA,eAAe,OAAe,QAAgB,QAAsC;EAClF,IAAI,OAAO,SAAS,QAAQ,OAAO,MAAM,SAAS,IAAI,GAAG;GAEvD,MAAM,SAAS;IACb,MAAM;IACN;IACA,QAAQ,OAAO;IACf,QAAQ,OAAO;IACf,MAAM,EAAE,MAAM,UAAU;GAC1B;GACA,MAAM,WAAW;IACf,MAAM;IACN;IACA,QAAQ,OAAO;IACf,QAAQ,OAAO;IACf,MAAM;KACJ,MAAM;KAEN,OAAO,OAAO,MAAM,QAAQ,MAAM,MAAM,IAAI,CAAC,CAAC,KAAK,MAAM,OAAO,QAAQ,CAAC,CAAC;IAC5E;GACF;GAEA,KAAK,YAAY,KAAK;IAAE,MAAM;IAAO,IAAI;IAAM,YAAY,CAAC,QAAQ,QAAQ;GAAE,CAAC;EACjF,OAAO;GACL,MAAM,OAAO;IACX,MAAM;IAC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-
import { ExpressionFilter } from "./EntityFilter.cjs";
|
|
3
3
|
import { ColumnCondition, ParsedExpressionFilter, ParsedValueFilter, RawCondition } from "./QueryParser.cjs";
|
|
4
4
|
//#region src/ConditionBuilder.d.ts
|
|
5
5
|
type PartialSome<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
@@ -10,7 +10,7 @@ declare class ConditionBuilder {
|
|
|
10
10
|
/** Complex expressions, which will also be AND-d together with `conditions`. */
|
|
11
11
|
private expressions;
|
|
12
12
|
/** Accepts a raw user-facing DSL filter, and parses it into a `ParsedExpressionFilter`. */
|
|
13
|
-
maybeAddExpression(expression:
|
|
13
|
+
maybeAddExpression(expression: ConditionGroup<ConditionInput>): void;
|
|
14
14
|
/** Adds an already-db-level condition to the simple conditions list. */
|
|
15
15
|
addSimpleCondition(condition: ColumnCondition): void;
|
|
16
16
|
/** Adds an already-db-level condition to the simple conditions list. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConditionBuilder.d.cts","names":[],"sources":["../src/ConditionBuilder.ts"],"mappings":";;;;KAcK,YAAY,GAAG,gBAAgB,KAAK,KAAK,GAAG,KAAK,QAAQ,KAAK,GAAG;;cAGzD;;UAEH;;UAEA;;EAGR,mBAAmB,YAAY;;
|
|
1
|
+
{"version":3,"file":"ConditionBuilder.d.cts","names":[],"sources":["../src/ConditionBuilder.ts"],"mappings":";;;;KAcK,YAAY,GAAG,gBAAgB,KAAK,KAAK,GAAG,KAAK,QAAQ,KAAK,GAAG;;cAGzD;;UAEH;;UAEA;;EAGR,mBAAmB,YAAY,eAAe;;EAM9C,mBAAmB,WAAW;;EAK9B,gBAAgB,WAAW,YAAY;;EASvC,oBAAoB,QAAQ;;;;;;EAS5B,eAAe,eAAe,QAAQ,QAAQ,QAAQ;;EAqCtD,sBAAsB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { ConditionGroup, ConditionInput } from "./conditions.mjs";
|
|
1
2
|
import { Column } from "./columns.mjs";
|
|
2
|
-
import { ExpressionFilter } from "./EntityFilter.mjs";
|
|
3
3
|
import { ColumnCondition, ParsedExpressionFilter, ParsedValueFilter, RawCondition } from "./QueryParser.mjs";
|
|
4
4
|
//#region src/ConditionBuilder.d.ts
|
|
5
5
|
type PartialSome<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
@@ -10,7 +10,7 @@ declare class ConditionBuilder {
|
|
|
10
10
|
/** Complex expressions, which will also be AND-d together with `conditions`. */
|
|
11
11
|
private expressions;
|
|
12
12
|
/** Accepts a raw user-facing DSL filter, and parses it into a `ParsedExpressionFilter`. */
|
|
13
|
-
maybeAddExpression(expression:
|
|
13
|
+
maybeAddExpression(expression: ConditionGroup<ConditionInput>): void;
|
|
14
14
|
/** Adds an already-db-level condition to the simple conditions list. */
|
|
15
15
|
addSimpleCondition(condition: ColumnCondition): void;
|
|
16
16
|
/** Adds an already-db-level condition to the simple conditions list. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConditionBuilder.d.mts","names":[],"sources":["../src/ConditionBuilder.ts"],"mappings":";;;;KAcK,YAAY,GAAG,gBAAgB,KAAK,KAAK,GAAG,KAAK,QAAQ,KAAK,GAAG;;cAGzD;;UAEH;;UAEA;;EAGR,mBAAmB,YAAY;;
|
|
1
|
+
{"version":3,"file":"ConditionBuilder.d.mts","names":[],"sources":["../src/ConditionBuilder.ts"],"mappings":";;;;KAcK,YAAY,GAAG,gBAAgB,KAAK,KAAK,GAAG,KAAK,QAAQ,KAAK,GAAG;;cAGzD;;UAEH;;UAEA;;EAGR,mBAAmB,YAAY,eAAe;;EAM9C,mBAAmB,WAAW;;EAK9B,gBAAgB,WAAW,YAAY;;EASvC,oBAAoB,QAAQ;;;;;;EAS5B,eAAe,eAAe,QAAQ,QAAQ,QAAQ;;EAqCtD,sBAAsB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConditionBuilder.js","names":[],"sources":["../src/ConditionBuilder.ts"],"sourcesContent":["import { type Column } from \"./columns.ts\";\nimport { type
|
|
1
|
+
{"version":3,"file":"ConditionBuilder.js","names":[],"sources":["../src/ConditionBuilder.ts"],"sourcesContent":["import { type Column } from \"./columns.ts\";\nimport { type ConditionGroup, type ConditionInput } from \"./conditions.ts\";\nimport { isDefined } from \"./EntityManager.ts\";\nimport {\n type ColumnCondition,\n type ParsedExpressionCondition,\n type ParsedExpressionFilter,\n type ParsedValueFilter,\n type RawCondition,\n mapToDb,\n} from \"./QueryParser.ts\";\nimport { skipCondition } from \"./skipCondition.ts\";\nimport { fail, partition } from \"./utils.ts\";\n\ntype PartialSome<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;\n\n/** Converts domain-level values like string ids/enums into their db equivalent. */\nexport class ConditionBuilder {\n /** Simple, single-column conditions, which will be AND-d together. */\n private conditions: (ColumnCondition | RawCondition)[] = [];\n /** Complex expressions, which will also be AND-d together with `conditions`. */\n private expressions: ParsedExpressionFilter[] = [];\n\n /** Accepts a raw user-facing DSL filter, and parses it into a `ParsedExpressionFilter`. */\n maybeAddExpression(expression: ConditionGroup<ConditionInput>): void {\n const parsed = parseExpression(expression);\n if (parsed) this.expressions.push(parsed);\n }\n\n /** Adds an already-db-level condition to the simple conditions list. */\n addSimpleCondition(condition: ColumnCondition): void {\n this.conditions.push(condition);\n }\n\n /** Adds an already-db-level condition to the simple conditions list. */\n addRawCondition(condition: PartialSome<RawCondition, \"kind\" | \"bindings\">): void {\n this.conditions.push({\n kind: \"raw\",\n bindings: [],\n ...condition,\n });\n }\n\n /** Adds an already-db-level expression to the expressions list. */\n addParsedExpression(parsed: ParsedExpressionFilter): void {\n this.expressions.push(parsed);\n }\n\n /**\n * Adds a user-facing `ParsedValueFilter` to the inline conditions.\n *\n * Unless it's something like `in: [a1, null]`, in which case we split it into two `is-null` and `in` conditions.\n */\n addValueFilter(alias: string, column: Column, filter: ParsedValueFilter<any>): void {\n if (filter.kind === \"in\" && filter.value.includes(null)) {\n // If the filter contains a null, we need to split it into an `is-null` and `in` condition\n const isNull = {\n kind: \"column\",\n alias,\n column: column.columnName,\n dbType: column.dbType,\n cond: { kind: \"is-null\" },\n } satisfies ColumnCondition;\n const inValues = {\n kind: \"column\",\n alias,\n column: column.columnName,\n dbType: column.dbType,\n cond: {\n kind: \"in\",\n // Filter out the nulls from the in condition\n value: filter.value.filter((v) => v !== null).map((v) => column.mapToDb(v)),\n },\n } satisfies ColumnCondition;\n // Now OR them back together\n this.expressions.push({ kind: \"exp\", op: \"or\", conditions: [isNull, inValues] });\n } else {\n const cond = {\n kind: \"column\",\n alias,\n column: column.columnName,\n dbType: column.dbType,\n // Rewrite the user-facing domain values to db values\n cond: mapToDb(column, filter),\n } satisfies ColumnCondition;\n this.conditions.push(cond);\n }\n }\n\n /** Combines our collected `conditions` and `expressions` into a single `ParsedExpressionFilter`. */\n toExpressionFilter(): ParsedExpressionFilter | undefined {\n const { expressions, conditions } = this;\n const allSimple: ParsedExpressionCondition[] = [...conditions];\n if (allSimple.length === 0 && expressions.length === 1) {\n // If no inline conditions, and just 1 opt expression, just use that\n return expressions[0];\n } else if (expressions.length === 1 && expressions[0].op === \"and\") {\n // Merge the 1 `AND` expression with the other simple conditions\n return { kind: \"exp\", op: \"and\", conditions: [...allSimple, ...expressions[0].conditions] };\n } else if (allSimple.length > 0 || expressions.length > 0) {\n // Combine the conditions within the `em.find` join literal & the `conditions` as ANDs\n return { kind: \"exp\", op: \"and\", conditions: [...allSimple, ...expressions] };\n }\n return undefined;\n }\n}\n\n/** Parses user-facing `{ and: ... }` or `{ or: ... }` into a `ParsedExpressionFilter`. */\nfunction parseExpression(expression: ConditionGroup<ConditionInput>): ParsedExpressionFilter | undefined {\n // Look for `{ and: [...] }` or `{ or: [...] }`\n const [op, expressions] =\n \"and\" in expression && expression.and\n ? [\"and\" as const, expression.and]\n : \"or\" in expression && expression.or\n ? [\"or\" as const, expression.or]\n : fail(`Invalid expression ${expression}`);\n // Potentially recurse into nested expressions\n const conditions = expressions.map((exp) => (exp && (\"and\" in exp || \"or\" in exp) ? parseExpression(exp) : exp));\n const [skip, valid] = partition(conditions, (cond) => cond === undefined || cond === skipCondition);\n if ((skip.length > 0 && expression.pruneIfUndefined === \"any\") || valid.length === 0) {\n return undefined;\n }\n return { kind: \"exp\", op, conditions: valid.filter(isDefined) };\n}\n"],"mappings":";;;;;;;AAiBA,IAAa,mBAAb,MAA8B;;CAE5B,aAAyD,CAAC;;CAE1D,cAAgD,CAAC;;CAGjD,mBAAmB,YAAkD;EACnE,MAAM,SAAS,gBAAgB,UAAU;EACzC,IAAI,QAAQ,KAAK,YAAY,KAAK,MAAM;CAC1C;;CAGA,mBAAmB,WAAkC;EACnD,KAAK,WAAW,KAAK,SAAS;CAChC;;CAGA,gBAAgB,WAAiE;EAC/E,KAAK,WAAW,KAAK;GACnB,MAAM;GACN,UAAU,CAAC;GACX,GAAG;EACL,CAAC;CACH;;CAGA,oBAAoB,QAAsC;EACxD,KAAK,YAAY,KAAK,MAAM;CAC9B;;;;;;CAOA,eAAe,OAAe,QAAgB,QAAsC;EAClF,IAAI,OAAO,SAAS,QAAQ,OAAO,MAAM,SAAS,IAAI,GAAG;GAEvD,MAAM,SAAS;IACb,MAAM;IACN;IACA,QAAQ,OAAO;IACf,QAAQ,OAAO;IACf,MAAM,EAAE,MAAM,UAAU;GAC1B;GACA,MAAM,WAAW;IACf,MAAM;IACN;IACA,QAAQ,OAAO;IACf,QAAQ,OAAO;IACf,MAAM;KACJ,MAAM;KAEN,OAAO,OAAO,MAAM,QAAQ,MAAM,MAAM,IAAI,CAAC,CAAC,KAAK,MAAM,OAAO,QAAQ,CAAC,CAAC;IAC5E;GACF;GAEA,KAAK,YAAY,KAAK;IAAE,MAAM;IAAO,IAAI;IAAM,YAAY,CAAC,QAAQ,QAAQ;GAAE,CAAC;EACjF,OAAO;GACL,MAAM,OAAO;IACX,MAAM;IACN;IACA,QAAQ,OAAO;IACf,QAAQ,OAAO;IAEf,MAAM,QAAQ,QAAQ,MAAM;GAC9B;GACA,KAAK,WAAW,KAAK,IAAI;EAC3B;CACF;;CAGA,qBAAyD;EACvD,MAAM,EAAE,aAAa,eAAe;EACpC,MAAM,YAAyC,CAAC,GAAG,UAAU;EAC7D,IAAI,UAAU,WAAW,KAAK,YAAY,WAAW,GAEnD,OAAO,YAAY;OACd,IAAI,YAAY,WAAW,KAAK,YAAY,EAAE,CAAC,OAAO,OAE3D,OAAO;GAAE,MAAM;GAAO,IAAI;GAAO,YAAY,CAAC,GAAG,WAAW,GAAG,YAAY,EAAE,CAAC,UAAU;EAAE;OACrF,IAAI,UAAU,SAAS,KAAK,YAAY,SAAS,GAEtD,OAAO;GAAE,MAAM;GAAO,IAAI;GAAO,YAAY,CAAC,GAAG,WAAW,GAAG,WAAW;EAAE;CAGhF;AACF;;AAGA,SAAS,gBAAgB,YAAgF;CAEvG,MAAM,CAAC,IAAI,eACT,SAAS,cAAc,WAAW,MAC9B,CAAC,OAAgB,WAAW,GAAG,IAC/B,QAAQ,cAAc,WAAW,KAC/B,CAAC,MAAe,WAAW,EAAE,IAC7B,KAAK,sBAAsB,YAAY;CAE/C,MAAM,aAAa,YAAY,KAAK,QAAS,QAAQ,SAAS,OAAO,QAAQ,OAAO,gBAAgB,GAAG,IAAI,GAAI;CAC/G,MAAM,CAAC,MAAM,SAAS,UAAU,aAAa,SAAS,SAAS,KAAA,KAAa,SAAS,aAAa;CAClG,IAAK,KAAK,SAAS,KAAK,WAAW,qBAAqB,SAAU,MAAM,WAAW,GACjF;CAEF,OAAO;EAAE,MAAM;EAAO;EAAI,YAAY,MAAM,OAAO,SAAS;CAAE;AAChE"}
|
package/build/DeferredAlias.cjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_conditions = require("./conditions.cjs");
|
|
2
3
|
require("./EntityMetadata.cjs");
|
|
3
4
|
require("./Aliases.cjs");
|
|
4
5
|
require("./QueryParser.cjs");
|
|
@@ -10,7 +11,7 @@ function isDeferredAliasCondition(cond) {
|
|
|
10
11
|
}
|
|
11
12
|
/** Tags a condition without changing its enumerable shape; resolution only mutates a fresh copy. */
|
|
12
13
|
function withDeferredAlias(cond, resolve) {
|
|
13
|
-
return Object.defineProperty(cond, deferredAliasSym, {
|
|
14
|
+
return Object.defineProperty(require_conditions.brandPredicate(cond, "domain"), deferredAliasSym, {
|
|
14
15
|
value: (r) => {
|
|
15
16
|
const copy = { ...cond };
|
|
16
17
|
resolve(r, copy);
|
|
@@ -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 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":"
|
|
1
|
+
{"version":3,"file":"DeferredAlias.cjs","names":["brandPredicate"],"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 PredicateBrand, brandPredicate } from \"./conditions.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> extends PredicateBrand<\"domain\"> {\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(brandPredicate(cond, \"domain\"), 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":";;;;;;AAMA,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,eAAeA,mBAAAA,eAAe,MAAM,QAAQ,GAAG,kBAAkB;EAC7E,QAAQ,MAAqB;GAC3B,MAAM,OAAO,EAAE,GAAG,KAAK;GACvB,QAAQ,GAAG,IAAI;GACf,OAAO;EACT;EACA,YAAY;CACd,CAAC;AACH"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PredicateBrand } from "./conditions.cjs";
|
|
1
2
|
import { EntityMetadata } from "./EntityMetadata.cjs";
|
|
2
3
|
import { AliasMgmt } from "./Aliases.cjs";
|
|
3
4
|
import { ColumnCondition, RawCondition } from "./QueryParser.cjs";
|
|
@@ -9,7 +10,7 @@ type AliasResolver = (handle: AliasMgmt) => {
|
|
|
9
10
|
alias: string;
|
|
10
11
|
};
|
|
11
12
|
/** A domain condition whose SQL aliases are resolved afresh for each occurrence in em.find. */
|
|
12
|
-
interface DeferredAliasCondition<C = ColumnCondition | RawCondition> {
|
|
13
|
+
interface DeferredAliasCondition<C = ColumnCondition | RawCondition> extends PredicateBrand<"domain"> {
|
|
13
14
|
[deferredAliasSym]: (resolve: AliasResolver) => C;
|
|
14
15
|
}
|
|
15
16
|
/** Detects conditions that carry a per-parse alias resolver. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DeferredAlias.d.cts","names":[],"sources":["../src/DeferredAlias.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"DeferredAlias.d.cts","names":[],"sources":["../src/DeferredAlias.ts"],"mappings":";;;;;cAMa;;KAGD,iBAAiB,QAAQ;EACnC,MAAM;EACN;;;UAIe,uBAAuB,IAAI,kBAAkB,sBAAsB;GACjF,oBAAoB,SAAS,kBAAkB;;;iBAIlC,yBAAyB,GAAG,MAAM,IAAI,QAAQ,IAAI,uBAAuB;;iBAKzE,kBAAkB,kBAChC,MAAM,GACN,UAAU,GAAG,eAAe,MAAM,aACjC,IAAI,uBAAuB"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PredicateBrand } from "./conditions.mjs";
|
|
1
2
|
import { EntityMetadata } from "./EntityMetadata.mjs";
|
|
2
3
|
import { AliasMgmt } from "./Aliases.mjs";
|
|
3
4
|
import { ColumnCondition, RawCondition } from "./QueryParser.mjs";
|
|
@@ -9,7 +10,7 @@ type AliasResolver = (handle: AliasMgmt) => {
|
|
|
9
10
|
alias: string;
|
|
10
11
|
};
|
|
11
12
|
/** A domain condition whose SQL aliases are resolved afresh for each occurrence in em.find. */
|
|
12
|
-
interface DeferredAliasCondition<C = ColumnCondition | RawCondition> {
|
|
13
|
+
interface DeferredAliasCondition<C = ColumnCondition | RawCondition> extends PredicateBrand<"domain"> {
|
|
13
14
|
[deferredAliasSym]: (resolve: AliasResolver) => C;
|
|
14
15
|
}
|
|
15
16
|
/** Detects conditions that carry a per-parse alias resolver. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DeferredAlias.d.mts","names":[],"sources":["../src/DeferredAlias.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"DeferredAlias.d.mts","names":[],"sources":["../src/DeferredAlias.ts"],"mappings":";;;;;cAMa;;KAGD,iBAAiB,QAAQ;EACnC,MAAM;EACN;;;UAIe,uBAAuB,IAAI,kBAAkB,sBAAsB;GACjF,oBAAoB,SAAS,kBAAkB;;;iBAIlC,yBAAyB,GAAG,MAAM,IAAI,QAAQ,IAAI,uBAAuB;;iBAKzE,kBAAkB,kBAChC,MAAM,GACN,UAAU,GAAG,eAAe,MAAM,aACjC,IAAI,uBAAuB"}
|
package/build/DeferredAlias.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { brandPredicate } from "./conditions.js";
|
|
1
2
|
import "./EntityMetadata.js";
|
|
2
3
|
import "./Aliases.js";
|
|
3
4
|
import "./QueryParser.js";
|
|
@@ -9,7 +10,7 @@ function isDeferredAliasCondition(cond) {
|
|
|
9
10
|
}
|
|
10
11
|
/** Tags a condition without changing its enumerable shape; resolution only mutates a fresh copy. */
|
|
11
12
|
function withDeferredAlias(cond, resolve) {
|
|
12
|
-
return Object.defineProperty(cond, deferredAliasSym, {
|
|
13
|
+
return Object.defineProperty(brandPredicate(cond, "domain"), deferredAliasSym, {
|
|
13
14
|
value: (r) => {
|
|
14
15
|
const copy = { ...cond };
|
|
15
16
|
resolve(r, copy);
|
|
@@ -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 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":"
|
|
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 PredicateBrand, brandPredicate } from \"./conditions.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> extends PredicateBrand<\"domain\"> {\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(brandPredicate(cond, \"domain\"), 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":";;;;;AAMA,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,eAAe,MAAM,QAAQ,GAAG,kBAAkB;EAC7E,QAAQ,MAAqB;GAC3B,MAAM,OAAO,EAAE,GAAG,KAAK;GACvB,QAAQ,GAAG,IAAI;GACf,OAAO;EACT;EACA,YAAY;CACd,CAAC;AACH"}
|
package/build/EntityFilter.cjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
require("./conditions.cjs");
|
|
2
3
|
const require_scopes = require("./scopes.cjs");
|
|
3
4
|
require("./typeMap.cjs");
|
|
4
5
|
require("./EntityManager.cjs");
|
|
5
6
|
require("./Entity.cjs");
|
|
6
7
|
require("./Aliases.cjs");
|
|
7
|
-
require("./QueryParser.cjs");
|
|
8
8
|
//#region src/EntityFilter.ts
|
|
9
9
|
/** Merges root-scope find settings with caller options, letting caller options win. */
|
|
10
10
|
function mergeFindOptions(where, options) {
|