joist-core 2.3.0-next.76 → 2.3.0-next.77
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/index.cjs.map +1 -1
- package/build/index.d.cts +2 -2
- package/build/index.d.cts.map +1 -1
- package/build/index.d.mts +2 -2
- package/build/index.d.mts.map +1 -1
- package/build/index.js.map +1 -1
- package/build/query.cjs +149 -9
- package/build/query.cjs.map +1 -1
- package/build/query.d.cts +65 -4
- package/build/query.d.cts.map +1 -1
- package/build/query.d.mts +65 -4
- package/build/query.d.mts.map +1 -1
- package/build/query.js +149 -9
- package/build/query.js.map +1 -1
- package/package.json +2 -2
package/build/query.d.cts
CHANGED
|
@@ -96,6 +96,15 @@ type QuerySource = {
|
|
|
96
96
|
*/
|
|
97
97
|
type QueryJoin = InnerJoin<QuerySource, QueryCondition> | LeftJoin<QuerySource, QueryCondition>;
|
|
98
98
|
type QueryJoins = readonly (QueryJoin | undefined)[];
|
|
99
|
+
/**
|
|
100
|
+
* A CTE declaration: a `query(...)` value with named columns, i.e. a POJO or set-operation select.
|
|
101
|
+
*
|
|
102
|
+
* Entity-mode and scalar `query(...)` shapes are excluded: a CTE must be a table shape, so it needs
|
|
103
|
+
* columns.
|
|
104
|
+
*/
|
|
105
|
+
type WithSource = Subquery<unknown, string>;
|
|
106
|
+
/** One CTE or an array of them; an `undefined` entry prunes, like any other clause. */
|
|
107
|
+
type WithInput = WithSource | readonly (WithSource | undefined)[];
|
|
99
108
|
/**
|
|
100
109
|
* An expression order-by entry: the direction is the key and the expression is the value, unlike
|
|
101
110
|
* the keyed form's field name and `"ASC" | "DESC"` value. `never` on the other key keeps an
|
|
@@ -142,6 +151,15 @@ type QuerySelect = QuerySource | ExprLike<any> | Record<string, ExprLike<any>>;
|
|
|
142
151
|
* a standalone object use `satisfies Query` (or `satisfies Clauses` for a source-less fragment).
|
|
143
152
|
*/
|
|
144
153
|
interface Clauses<S extends QuerySelect = QuerySelect, J extends QueryJoins = QueryJoins> {
|
|
154
|
+
/**
|
|
155
|
+
* CTEs to include in a `WITH` clause, declared as `query(...)` values; either a single value or an array.
|
|
156
|
+
*
|
|
157
|
+
* I.e. `{ with: bookStats, from: a, join: [{ left: bookStats, on: ... }], select: { n: bookStats.n } }`.
|
|
158
|
+
*
|
|
159
|
+
* An `undefined` entry is ignored, and a CTE that nothing else in the query references will be pruned,
|
|
160
|
+
* the same pruning `join` gets (see "Pruning"); `pruneJoins: false` keeps every CTE.
|
|
161
|
+
*/
|
|
162
|
+
with?: WithInput;
|
|
145
163
|
join?: J;
|
|
146
164
|
/** A boolean group, an exists/notExists query, or a bare condition such as `a.age.gte(18)`. */
|
|
147
165
|
where?: QueryCondition;
|
|
@@ -163,7 +181,7 @@ interface Clauses<S extends QuerySelect = QuerySelect, J extends QueryJoins = Qu
|
|
|
163
181
|
softDeletes?: "include" | "exclude";
|
|
164
182
|
}
|
|
165
183
|
/** A whole query: `Clauses` plus its source. `query(q)` turns it into a value; `em.query(q)` runs it. */
|
|
166
|
-
interface Query<S extends QuerySelect = QuerySelect, J extends QueryJoins = QueryJoins> extends Clauses<S, J>, Partial<Record<SetOperation | MutationKey | "
|
|
184
|
+
interface Query<S extends QuerySelect = QuerySelect, J extends QueryJoins = QueryJoins> extends Clauses<S, J>, Partial<Record<SetOperation | MutationKey | "ctes", never>> {
|
|
167
185
|
from: QuerySource;
|
|
168
186
|
}
|
|
169
187
|
/**
|
|
@@ -202,11 +220,13 @@ type SetOperand = Query<Record<string, ExprLike<unknown>> | Subquery<unknown, st
|
|
|
202
220
|
* This input shape checks direction values; `CheckSetQuery` checks the inferred output keys in orderBy.
|
|
203
221
|
*/
|
|
204
222
|
type SetQuery<Operands extends readonly SetOperand[] = readonly [SetOperand, SetOperand, ...SetOperand[]]> = { [K in SetOperation]: { readonly [P in K]: Operands; } & { readonly [P in Exclude<SetOperation, K>]?: never; }; }[SetOperation] & {
|
|
223
|
+
/** CTEs for the combined query, declared once and in scope for every operand; see `Clauses.with`. */
|
|
224
|
+
readonly with?: WithInput;
|
|
205
225
|
readonly orderBy?: Readonly<Record<string, OrderByDirection | undefined>> | readonly (Readonly<Record<string, OrderByDirection | undefined>> | undefined)[];
|
|
206
226
|
readonly limit?: number;
|
|
207
227
|
readonly offset?: number;
|
|
208
228
|
readonly as?: string;
|
|
209
|
-
} & { readonly [K in "from" | "select" | "join" | "where" | "groupBy" | "having" | "distinct" | "softDeletes" | "pruneJoins" | MutationKey | "
|
|
229
|
+
} & { readonly [K in "from" | "select" | "join" | "where" | "groupBy" | "having" | "distinct" | "softDeletes" | "pruneJoins" | MutationKey | "ctes"]?: never; };
|
|
210
230
|
/**
|
|
211
231
|
* Extracts the operand collection from each possible compound root, ignoring pagination and ordering.
|
|
212
232
|
*
|
|
@@ -323,7 +343,7 @@ type CheckReadQuery<Q> = SetOperand extends Q ? unknown : Q extends readonly unk
|
|
|
323
343
|
} ? { readonly [K in keyof Q]: K extends typeof entityQueryBrand ? unknown : never; } : Q extends {
|
|
324
344
|
readonly from: unknown;
|
|
325
345
|
readonly select: unknown;
|
|
326
|
-
} ? { readonly [K in keyof Q]: K extends keyof Clauses | "from" | "as" ? unknown : never; } : { readonly [K in keyof Q]: K extends SetOperation ? Q[K] extends readonly unknown[] ? CheckReadQuery<Q[K]> : unknown : K extends "orderBy" | "limit" | "offset" | "as" ? unknown : never; };
|
|
346
|
+
} ? { readonly [K in keyof Q]: K extends keyof Clauses | "from" | "as" ? unknown : never; } : { readonly [K in keyof Q]: K extends SetOperation ? Q[K] extends readonly unknown[] ? CheckReadQuery<Q[K]> : unknown : K extends "with" | "orderBy" | "limit" | "offset" | "as" ? unknown : never; };
|
|
327
347
|
/** The type-level name of a table or subquery, i.e. `"Author"` or `"book_stats"`. */
|
|
328
348
|
type NameOf<A> = A extends {
|
|
329
349
|
readonly [tableMgmt]: {
|
|
@@ -3873,9 +3893,50 @@ declare class Ctx implements ExprContext {
|
|
|
3873
3893
|
readonly assigner: AliasAssigner;
|
|
3874
3894
|
private parent;
|
|
3875
3895
|
private aliases;
|
|
3896
|
+
/**
|
|
3897
|
+
* The subset of `aliases` that are CTE names. A `from`/`join` on one of these emits just the name,
|
|
3898
|
+
* i.e. `FROM book_stats`, where an ordinary `query(...)` value emits its whole body inline, i.e.
|
|
3899
|
+
* `FROM (SELECT ...) AS sq`.
|
|
3900
|
+
*/
|
|
3901
|
+
private ctes;
|
|
3902
|
+
/**
|
|
3903
|
+
* `with` entries named but not yet turned into SQL. `registerCtes` names every entry first, then
|
|
3904
|
+
* generates the bodies one at a time, so while one body is being generated the entries after it sit
|
|
3905
|
+
* here. Reading one of those is a forward reference.
|
|
3906
|
+
*/
|
|
3907
|
+
private pendingCtes;
|
|
3908
|
+
/** CTEs already read by this query's `from`/`join`, so a second read fails instead of colliding. */
|
|
3909
|
+
private usedCtes;
|
|
3876
3910
|
readonly outerRefs: Set<string>;
|
|
3877
3911
|
constructor(assigner: AliasAssigner, parent: Ctx | undefined);
|
|
3912
|
+
/**
|
|
3913
|
+
* One handle can hold only one alias per query, so a value used twice must be told apart, not
|
|
3914
|
+
* silently collapsed into the second registration's alias.
|
|
3915
|
+
*/
|
|
3878
3916
|
register(handle: object, alias: string): void;
|
|
3917
|
+
/** Names every `with` entry up front, so a CTE reading a later sibling is reported, not inlined. */
|
|
3918
|
+
declareCte(handle: object, alias: string): void;
|
|
3919
|
+
/** Brings a pending CTE into scope, for the sources and columns that may now read it. */
|
|
3920
|
+
promoteCte(handle: object): void;
|
|
3921
|
+
/**
|
|
3922
|
+
* The CTE name for `handle`, looking in the enclosing queries too, because a CTE is in scope for the
|
|
3923
|
+
* whole statement.
|
|
3924
|
+
*
|
|
3925
|
+
* Unlike `aliasFor`, a hit in an enclosing query is not added to `outerRefs`: reading a CTE by name
|
|
3926
|
+
* is not a correlated reference, so it must not keep an enclosing join alive.
|
|
3927
|
+
*/
|
|
3928
|
+
cteAliasFor(handle: object): string | undefined;
|
|
3929
|
+
/** Whether `handle` is a CTE with no SQL yet, i.e. itself or a later `with` entry. */
|
|
3930
|
+
isPendingCte(handle: object): boolean;
|
|
3931
|
+
/**
|
|
3932
|
+
* Fails if this query already reads `handle`. A `Ctx` maps each handle to a single alias, so two
|
|
3933
|
+
* reads of one CTE value would render as the same name, i.e. `FROM tree JOIN tree`, and neither the
|
|
3934
|
+
* SQL nor a column expression could say which one it meant. Each read needs its own `query(...)`.
|
|
3935
|
+
*
|
|
3936
|
+
* Only this query is checked, not the enclosing ones: a nested subquery has its own FROM, so reading
|
|
3937
|
+
* the same CTE in there is fine.
|
|
3938
|
+
*/
|
|
3939
|
+
useCte(handle: object): void;
|
|
3879
3940
|
aliasFor(handle: object): string;
|
|
3880
3941
|
conditionToSql(cond: SqlCondition): SqlFragment | undefined;
|
|
3881
3942
|
}
|
|
@@ -3899,5 +3960,5 @@ declare function injectedConditions(source: {
|
|
|
3899
3960
|
*/
|
|
3900
3961
|
declare function conditionToSql(cond: QueryCondition | undefined, ctx: Ctx, topLevel: boolean): SqlFragment | undefined;
|
|
3901
3962
|
//#endregion
|
|
3902
|
-
export { CheckReadQuery, CheckScope, CheckSetQuery, Clauses, Ctx, EntityHydrator, EntityQuery, ExistsQuery, MaybeNull, NameOf, NotWidened, OrderByDirection, OrderByKeys, Plan, Query, QueryArg, QueryCondition, QueryJoin, QueryJoins, QueryOrderBy, QueryOutput, QueryRow, QuerySelect, QuerySource, QueryValue, ReadQueryRow, ScalarQuery, SetOperand, SetOperation, SetQuery, SetQueryRow, Subquery, SubqueryBrand, SubqueryHandle, conditionToSql, entityQueryBrand, injectedConditions, isReadQueryValue, parseUserQuery, projectionToSql, query, scalarQueryBrand, sql, subqueryBrand };
|
|
3963
|
+
export { CheckReadQuery, CheckScope, CheckSetQuery, Clauses, Ctx, EntityHydrator, EntityQuery, ExistsQuery, MaybeNull, NameOf, NotWidened, OrderByDirection, OrderByKeys, Plan, Query, QueryArg, QueryCondition, QueryJoin, QueryJoins, QueryOrderBy, QueryOutput, QueryRow, QuerySelect, QuerySource, QueryValue, ReadQueryRow, ScalarQuery, SetOperand, SetOperation, SetQuery, SetQueryRow, Subquery, SubqueryBrand, SubqueryHandle, WithInput, WithSource, conditionToSql, entityQueryBrand, injectedConditions, isReadQueryValue, parseUserQuery, projectionToSql, query, scalarQueryBrand, sql, subqueryBrand };
|
|
3903
3964
|
//# sourceMappingURL=query.d.cts.map
|
package/build/query.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.d.cts","names":[],"sources":["../src/query.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAgGa;cACA;cACA;;KAGD,cAAc,4BAA4B,YAAY;YAAsB;;;;;;;;;KAS5E,iBACR,eACA;EAEM,KAAK,MAAM;EAA6B;EAAY;EAAgB;;EACpE,IAAI,MAAM;EAA6B;EAAa;EAAgB;;EACpE;;EACJ,QAAQ;EAAyB;EAAmB;EAAa;;EACjE,WAAW;EAAyB;EAAgB;EAAa;;;UAGtD,cAAc,GAAG;WACvB,OAAO;WACP,QAAQ;;;KAIP;YACI,YAAY;;YACZ,gBAAgB;;;;;;;KAOpB,YAAY,UAAU,aAAa,kBAAkB,SAAS,aAAa;KAC3E,uBAAuB;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"query.d.cts","names":[],"sources":["../src/query.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAgGa;cACA;cACA;;KAGD,cAAc,4BAA4B,YAAY;YAAsB;;;;;;;;;KAS5E,iBACR,eACA;EAEM,KAAK,MAAM;EAA6B;EAAY;EAAgB;;EACpE,IAAI,MAAM;EAA6B;EAAa;EAAgB;;EACpE;;EACJ,QAAQ;EAAyB;EAAmB;EAAa;;EACjE,WAAW;EAAyB;EAAgB;EAAa;;;UAGtD,cAAc,GAAG;WACvB,OAAO;WACP,QAAQ;;;KAIP;YACI,YAAY;;YACZ,gBAAgB;;;;;;;KAOpB,YAAY,UA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MAAA,aAAC,QAAoB,aAAW,mBAAmB,KAAK;;;;;;;;iBAU/D,eAAe,eAAe;;iBAM9B,iBAAiB;;;;;iBAQjB,gBACd,iBACA,KAAK;EACF,SAAS;EAAe,YAAY;EAAoB,QAAQ;;;;;;;;;;UAmCpD;EACf,QAAQ,WAAW;;UAGJ;EACf;EACA;;EAEA;;EAEA,QAAQ;EACR,WAAW,IAAI,gBAAgB;;KAO5B,WAAW;EAAoB;;KAC/B,eAAe,WAAW,kBAAkB;;UAGhC;EACf;;EAEA,qCAAqC;;;cAI1B;WACU,GAAG;EAAxB,YAAqB,GAAG;MAEpB;;EAKJ;;EAOA,WAAW,cAAc;;EAQzB,UAAU;EAIV,OAAO,cAAc;EAIrB;;;cAMI,2BAA2B;UAErB;UACA;UACA;EAHV,YACU,QAAQ,gBACR,aACA,OAAO;EAKjB,MAAM,KAAK,cAAc;EAMzB,OAAO;EAIP,OAAO;MAIH,cAAc;MAId;MAIA;;;;;;;;;cAwaO,eAAe;WAmBf,UAAU;UACX;UAnBF;;;;;;UAMA;;;;;;UAMA;;UAEA;WACC,WAAS;EAElB,YACW,UAAU,eACX,QAAQ;;;;;EAOlB,SAAS,gBAAgB;;EAUzB,WAAW,gBAAgB;;EAK3B,WAAW;;;;;;;;EAcX,YAAY;;EAKZ,aAAa;;;;;;;;;EAYb,OAAO;EASP,SAAS;EAWT,eAAe,MAAM,eAAe;;;;;;;;;;iBAmOtB,mBACd;EAAU,MAAM;EAA4B;GAC5C,qCACC;;;;;;;iBAiKa,eAAe,MAAM,4BAA4B,KAAK,KAAK,oBAAoB"}
|
package/build/query.d.mts
CHANGED
|
@@ -96,6 +96,15 @@ type QuerySource = {
|
|
|
96
96
|
*/
|
|
97
97
|
type QueryJoin = InnerJoin<QuerySource, QueryCondition> | LeftJoin<QuerySource, QueryCondition>;
|
|
98
98
|
type QueryJoins = readonly (QueryJoin | undefined)[];
|
|
99
|
+
/**
|
|
100
|
+
* A CTE declaration: a `query(...)` value with named columns, i.e. a POJO or set-operation select.
|
|
101
|
+
*
|
|
102
|
+
* Entity-mode and scalar `query(...)` shapes are excluded: a CTE must be a table shape, so it needs
|
|
103
|
+
* columns.
|
|
104
|
+
*/
|
|
105
|
+
type WithSource = Subquery<unknown, string>;
|
|
106
|
+
/** One CTE or an array of them; an `undefined` entry prunes, like any other clause. */
|
|
107
|
+
type WithInput = WithSource | readonly (WithSource | undefined)[];
|
|
99
108
|
/**
|
|
100
109
|
* An expression order-by entry: the direction is the key and the expression is the value, unlike
|
|
101
110
|
* the keyed form's field name and `"ASC" | "DESC"` value. `never` on the other key keeps an
|
|
@@ -142,6 +151,15 @@ type QuerySelect = QuerySource | ExprLike<any> | Record<string, ExprLike<any>>;
|
|
|
142
151
|
* a standalone object use `satisfies Query` (or `satisfies Clauses` for a source-less fragment).
|
|
143
152
|
*/
|
|
144
153
|
interface Clauses<S extends QuerySelect = QuerySelect, J extends QueryJoins = QueryJoins> {
|
|
154
|
+
/**
|
|
155
|
+
* CTEs to include in a `WITH` clause, declared as `query(...)` values; either a single value or an array.
|
|
156
|
+
*
|
|
157
|
+
* I.e. `{ with: bookStats, from: a, join: [{ left: bookStats, on: ... }], select: { n: bookStats.n } }`.
|
|
158
|
+
*
|
|
159
|
+
* An `undefined` entry is ignored, and a CTE that nothing else in the query references will be pruned,
|
|
160
|
+
* the same pruning `join` gets (see "Pruning"); `pruneJoins: false` keeps every CTE.
|
|
161
|
+
*/
|
|
162
|
+
with?: WithInput;
|
|
145
163
|
join?: J;
|
|
146
164
|
/** A boolean group, an exists/notExists query, or a bare condition such as `a.age.gte(18)`. */
|
|
147
165
|
where?: QueryCondition;
|
|
@@ -163,7 +181,7 @@ interface Clauses<S extends QuerySelect = QuerySelect, J extends QueryJoins = Qu
|
|
|
163
181
|
softDeletes?: "include" | "exclude";
|
|
164
182
|
}
|
|
165
183
|
/** A whole query: `Clauses` plus its source. `query(q)` turns it into a value; `em.query(q)` runs it. */
|
|
166
|
-
interface Query<S extends QuerySelect = QuerySelect, J extends QueryJoins = QueryJoins> extends Clauses<S, J>, Partial<Record<SetOperation | MutationKey | "
|
|
184
|
+
interface Query<S extends QuerySelect = QuerySelect, J extends QueryJoins = QueryJoins> extends Clauses<S, J>, Partial<Record<SetOperation | MutationKey | "ctes", never>> {
|
|
167
185
|
from: QuerySource;
|
|
168
186
|
}
|
|
169
187
|
/**
|
|
@@ -202,11 +220,13 @@ type SetOperand = Query<Record<string, ExprLike<unknown>> | Subquery<unknown, st
|
|
|
202
220
|
* This input shape checks direction values; `CheckSetQuery` checks the inferred output keys in orderBy.
|
|
203
221
|
*/
|
|
204
222
|
type SetQuery<Operands extends readonly SetOperand[] = readonly [SetOperand, SetOperand, ...SetOperand[]]> = { [K in SetOperation]: { readonly [P in K]: Operands; } & { readonly [P in Exclude<SetOperation, K>]?: never; }; }[SetOperation] & {
|
|
223
|
+
/** CTEs for the combined query, declared once and in scope for every operand; see `Clauses.with`. */
|
|
224
|
+
readonly with?: WithInput;
|
|
205
225
|
readonly orderBy?: Readonly<Record<string, OrderByDirection | undefined>> | readonly (Readonly<Record<string, OrderByDirection | undefined>> | undefined)[];
|
|
206
226
|
readonly limit?: number;
|
|
207
227
|
readonly offset?: number;
|
|
208
228
|
readonly as?: string;
|
|
209
|
-
} & { readonly [K in "from" | "select" | "join" | "where" | "groupBy" | "having" | "distinct" | "softDeletes" | "pruneJoins" | MutationKey | "
|
|
229
|
+
} & { readonly [K in "from" | "select" | "join" | "where" | "groupBy" | "having" | "distinct" | "softDeletes" | "pruneJoins" | MutationKey | "ctes"]?: never; };
|
|
210
230
|
/**
|
|
211
231
|
* Extracts the operand collection from each possible compound root, ignoring pagination and ordering.
|
|
212
232
|
*
|
|
@@ -323,7 +343,7 @@ type CheckReadQuery<Q> = SetOperand extends Q ? unknown : Q extends readonly unk
|
|
|
323
343
|
} ? { readonly [K in keyof Q]: K extends typeof entityQueryBrand ? unknown : never; } : Q extends {
|
|
324
344
|
readonly from: unknown;
|
|
325
345
|
readonly select: unknown;
|
|
326
|
-
} ? { readonly [K in keyof Q]: K extends keyof Clauses | "from" | "as" ? unknown : never; } : { readonly [K in keyof Q]: K extends SetOperation ? Q[K] extends readonly unknown[] ? CheckReadQuery<Q[K]> : unknown : K extends "orderBy" | "limit" | "offset" | "as" ? unknown : never; };
|
|
346
|
+
} ? { readonly [K in keyof Q]: K extends keyof Clauses | "from" | "as" ? unknown : never; } : { readonly [K in keyof Q]: K extends SetOperation ? Q[K] extends readonly unknown[] ? CheckReadQuery<Q[K]> : unknown : K extends "with" | "orderBy" | "limit" | "offset" | "as" ? unknown : never; };
|
|
327
347
|
/** The type-level name of a table or subquery, i.e. `"Author"` or `"book_stats"`. */
|
|
328
348
|
type NameOf<A> = A extends {
|
|
329
349
|
readonly [tableMgmt]: {
|
|
@@ -3873,9 +3893,50 @@ declare class Ctx implements ExprContext {
|
|
|
3873
3893
|
readonly assigner: AliasAssigner;
|
|
3874
3894
|
private parent;
|
|
3875
3895
|
private aliases;
|
|
3896
|
+
/**
|
|
3897
|
+
* The subset of `aliases` that are CTE names. A `from`/`join` on one of these emits just the name,
|
|
3898
|
+
* i.e. `FROM book_stats`, where an ordinary `query(...)` value emits its whole body inline, i.e.
|
|
3899
|
+
* `FROM (SELECT ...) AS sq`.
|
|
3900
|
+
*/
|
|
3901
|
+
private ctes;
|
|
3902
|
+
/**
|
|
3903
|
+
* `with` entries named but not yet turned into SQL. `registerCtes` names every entry first, then
|
|
3904
|
+
* generates the bodies one at a time, so while one body is being generated the entries after it sit
|
|
3905
|
+
* here. Reading one of those is a forward reference.
|
|
3906
|
+
*/
|
|
3907
|
+
private pendingCtes;
|
|
3908
|
+
/** CTEs already read by this query's `from`/`join`, so a second read fails instead of colliding. */
|
|
3909
|
+
private usedCtes;
|
|
3876
3910
|
readonly outerRefs: Set<string>;
|
|
3877
3911
|
constructor(assigner: AliasAssigner, parent: Ctx | undefined);
|
|
3912
|
+
/**
|
|
3913
|
+
* One handle can hold only one alias per query, so a value used twice must be told apart, not
|
|
3914
|
+
* silently collapsed into the second registration's alias.
|
|
3915
|
+
*/
|
|
3878
3916
|
register(handle: object, alias: string): void;
|
|
3917
|
+
/** Names every `with` entry up front, so a CTE reading a later sibling is reported, not inlined. */
|
|
3918
|
+
declareCte(handle: object, alias: string): void;
|
|
3919
|
+
/** Brings a pending CTE into scope, for the sources and columns that may now read it. */
|
|
3920
|
+
promoteCte(handle: object): void;
|
|
3921
|
+
/**
|
|
3922
|
+
* The CTE name for `handle`, looking in the enclosing queries too, because a CTE is in scope for the
|
|
3923
|
+
* whole statement.
|
|
3924
|
+
*
|
|
3925
|
+
* Unlike `aliasFor`, a hit in an enclosing query is not added to `outerRefs`: reading a CTE by name
|
|
3926
|
+
* is not a correlated reference, so it must not keep an enclosing join alive.
|
|
3927
|
+
*/
|
|
3928
|
+
cteAliasFor(handle: object): string | undefined;
|
|
3929
|
+
/** Whether `handle` is a CTE with no SQL yet, i.e. itself or a later `with` entry. */
|
|
3930
|
+
isPendingCte(handle: object): boolean;
|
|
3931
|
+
/**
|
|
3932
|
+
* Fails if this query already reads `handle`. A `Ctx` maps each handle to a single alias, so two
|
|
3933
|
+
* reads of one CTE value would render as the same name, i.e. `FROM tree JOIN tree`, and neither the
|
|
3934
|
+
* SQL nor a column expression could say which one it meant. Each read needs its own `query(...)`.
|
|
3935
|
+
*
|
|
3936
|
+
* Only this query is checked, not the enclosing ones: a nested subquery has its own FROM, so reading
|
|
3937
|
+
* the same CTE in there is fine.
|
|
3938
|
+
*/
|
|
3939
|
+
useCte(handle: object): void;
|
|
3879
3940
|
aliasFor(handle: object): string;
|
|
3880
3941
|
conditionToSql(cond: SqlCondition): SqlFragment | undefined;
|
|
3881
3942
|
}
|
|
@@ -3899,5 +3960,5 @@ declare function injectedConditions(source: {
|
|
|
3899
3960
|
*/
|
|
3900
3961
|
declare function conditionToSql(cond: QueryCondition | undefined, ctx: Ctx, topLevel: boolean): SqlFragment | undefined;
|
|
3901
3962
|
//#endregion
|
|
3902
|
-
export { CheckReadQuery, CheckScope, CheckSetQuery, Clauses, Ctx, EntityHydrator, EntityQuery, ExistsQuery, MaybeNull, NameOf, NotWidened, OrderByDirection, OrderByKeys, Plan, Query, QueryArg, QueryCondition, QueryJoin, QueryJoins, QueryOrderBy, QueryOutput, QueryRow, QuerySelect, QuerySource, QueryValue, ReadQueryRow, ScalarQuery, SetOperand, SetOperation, SetQuery, SetQueryRow, Subquery, SubqueryBrand, SubqueryHandle, conditionToSql, entityQueryBrand, injectedConditions, isReadQueryValue, parseUserQuery, projectionToSql, query, scalarQueryBrand, sql, subqueryBrand };
|
|
3963
|
+
export { CheckReadQuery, CheckScope, CheckSetQuery, Clauses, Ctx, EntityHydrator, EntityQuery, ExistsQuery, MaybeNull, NameOf, NotWidened, OrderByDirection, OrderByKeys, Plan, Query, QueryArg, QueryCondition, QueryJoin, QueryJoins, QueryOrderBy, QueryOutput, QueryRow, QuerySelect, QuerySource, QueryValue, ReadQueryRow, ScalarQuery, SetOperand, SetOperation, SetQuery, SetQueryRow, Subquery, SubqueryBrand, SubqueryHandle, WithInput, WithSource, conditionToSql, entityQueryBrand, injectedConditions, isReadQueryValue, parseUserQuery, projectionToSql, query, scalarQueryBrand, sql, subqueryBrand };
|
|
3903
3964
|
//# sourceMappingURL=query.d.mts.map
|
package/build/query.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.d.mts","names":[],"sources":["../src/query.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAgGa;cACA;cACA;;KAGD,cAAc,4BAA4B,YAAY;YAAsB;;;;;;;;;KAS5E,iBACR,eACA;EAEM,KAAK,MAAM;EAA6B;EAAY;EAAgB;;EACpE,IAAI,MAAM;EAA6B;EAAa;EAAgB;;EACpE;;EACJ,QAAQ;EAAyB;EAAmB;EAAa;;EACjE,WAAW;EAAyB;EAAgB;EAAa;;;UAGtD,cAAc,GAAG;WACvB,OAAO;WACP,QAAQ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;UAEA;WACC,WAAS;EAElB,YACW,UAAU,eACX,QAAQ;;;;;EAOlB,SAAS,gBAAgB;;EAUzB,WAAW,gBAAgB;;EAK3B,WAAW;;;;;;;;EAcX,YAAY;;EAKZ,aAAa;;;;;;;;;EAYb,OAAO;EASP,SAAS;EAWT,eAAe,MAAM,eAAe;;;;;;;;;;iBAmOtB,mBACd;EAAU,MAAM;EAA4B;GAC5C,qCACC;;;;;;;iBAiKa,eAAe,MAAM,4BAA4B,KAAK,KAAK,oBAAoB"}
|
package/build/query.js
CHANGED
|
@@ -312,6 +312,7 @@ const MUTATION_KEYS = [
|
|
|
312
312
|
"allowAll"
|
|
313
313
|
];
|
|
314
314
|
const READ_KEYS = [
|
|
315
|
+
"with",
|
|
315
316
|
"from",
|
|
316
317
|
"join",
|
|
317
318
|
"where",
|
|
@@ -368,6 +369,7 @@ function setOperands(q) {
|
|
|
368
369
|
if (keys.length !== 1) fail("A set query requires exactly one operation key");
|
|
369
370
|
validateQueryKeys(q, [
|
|
370
371
|
keys[0],
|
|
372
|
+
"with",
|
|
371
373
|
"orderBy",
|
|
372
374
|
"limit",
|
|
373
375
|
"offset",
|
|
@@ -494,7 +496,9 @@ var OutputExpr = class extends BaseExpr {
|
|
|
494
496
|
function parseSetQuery(q, parent, assigner) {
|
|
495
497
|
const [operation, operands] = setOperands(q);
|
|
496
498
|
const output = queryOutput(q);
|
|
497
|
-
const
|
|
499
|
+
const ctx = new Ctx(assigner, parent);
|
|
500
|
+
const ctes = registerCtes(q, ctx, assigner);
|
|
501
|
+
const plans = operands.map((operand) => parseQuery(toQuery(operand), ctx, assigner));
|
|
498
502
|
let sql = "";
|
|
499
503
|
for (const plan of plans) {
|
|
500
504
|
let branch = plan.sql;
|
|
@@ -515,10 +519,25 @@ function parseSetQuery(q, parent, assigner) {
|
|
|
515
519
|
sql += " OFFSET ?";
|
|
516
520
|
bindings.push(q.offset);
|
|
517
521
|
}
|
|
522
|
+
const referenced = new Set(plans.flatMap((plan) => plan.outerRefs));
|
|
523
|
+
const keptCtes = [];
|
|
524
|
+
for (let i = ctes.length - 1; i >= 0; i--) {
|
|
525
|
+
const cte = ctes[i];
|
|
526
|
+
if (!referenced.has(cte.alias)) continue;
|
|
527
|
+
for (const ref of cte.plan.outerRefs) referenced.add(ref);
|
|
528
|
+
keptCtes.unshift(cte);
|
|
529
|
+
}
|
|
530
|
+
if (keptCtes.length > 0) {
|
|
531
|
+
const clause = withFragment(keptCtes);
|
|
532
|
+
sql = clause.sql + sql;
|
|
533
|
+
bindings.unshift(...clause.bindings);
|
|
534
|
+
}
|
|
535
|
+
const cteAliases = new Set(ctes.map((cte) => cte.alias));
|
|
536
|
+
const outerRefs = [...ctx.outerRefs, ...plans.flatMap((plan) => plan.outerRefs)];
|
|
518
537
|
return {
|
|
519
538
|
sql,
|
|
520
539
|
bindings,
|
|
521
|
-
outerRefs: [...new Set(
|
|
540
|
+
outerRefs: [...new Set(outerRefs.filter((ref) => !cteAliases.has(ref)))],
|
|
522
541
|
output,
|
|
523
542
|
decodeRows: plans[0].decodeRows
|
|
524
543
|
};
|
|
@@ -549,14 +568,70 @@ var Ctx = class {
|
|
|
549
568
|
assigner;
|
|
550
569
|
parent;
|
|
551
570
|
aliases = /* @__PURE__ */ new Map();
|
|
571
|
+
/**
|
|
572
|
+
* The subset of `aliases` that are CTE names. A `from`/`join` on one of these emits just the name,
|
|
573
|
+
* i.e. `FROM book_stats`, where an ordinary `query(...)` value emits its whole body inline, i.e.
|
|
574
|
+
* `FROM (SELECT ...) AS sq`.
|
|
575
|
+
*/
|
|
576
|
+
ctes = /* @__PURE__ */ new Map();
|
|
577
|
+
/**
|
|
578
|
+
* `with` entries named but not yet turned into SQL. `registerCtes` names every entry first, then
|
|
579
|
+
* generates the bodies one at a time, so while one body is being generated the entries after it sit
|
|
580
|
+
* here. Reading one of those is a forward reference.
|
|
581
|
+
*/
|
|
582
|
+
pendingCtes = /* @__PURE__ */ new Map();
|
|
583
|
+
/** CTEs already read by this query's `from`/`join`, so a second read fails instead of colliding. */
|
|
584
|
+
usedCtes = /* @__PURE__ */ new Set();
|
|
552
585
|
outerRefs = /* @__PURE__ */ new Set();
|
|
553
586
|
constructor(assigner, parent) {
|
|
554
587
|
this.assigner = assigner;
|
|
555
588
|
this.parent = parent;
|
|
556
589
|
}
|
|
590
|
+
/**
|
|
591
|
+
* One handle can hold only one alias per query, so a value used twice must be told apart, not
|
|
592
|
+
* silently collapsed into the second registration's alias.
|
|
593
|
+
*/
|
|
557
594
|
register(handle, alias) {
|
|
595
|
+
if (this.aliases.has(handle)) fail(`${describeHandle(handle)} is already in this query's \`with\`/\`from\`/\`join\`; use a separate table(...)/query(...) value for each use`);
|
|
558
596
|
this.aliases.set(handle, alias);
|
|
559
597
|
}
|
|
598
|
+
/** Names every `with` entry up front, so a CTE reading a later sibling is reported, not inlined. */
|
|
599
|
+
declareCte(handle, alias) {
|
|
600
|
+
this.pendingCtes.set(handle, alias);
|
|
601
|
+
}
|
|
602
|
+
/** Brings a pending CTE into scope, for the sources and columns that may now read it. */
|
|
603
|
+
promoteCte(handle) {
|
|
604
|
+
const alias = this.pendingCtes.get(handle) ?? fail("CTE was not declared");
|
|
605
|
+
this.pendingCtes.delete(handle);
|
|
606
|
+
this.ctes.set(handle, alias);
|
|
607
|
+
this.register(handle, alias);
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* The CTE name for `handle`, looking in the enclosing queries too, because a CTE is in scope for the
|
|
611
|
+
* whole statement.
|
|
612
|
+
*
|
|
613
|
+
* Unlike `aliasFor`, a hit in an enclosing query is not added to `outerRefs`: reading a CTE by name
|
|
614
|
+
* is not a correlated reference, so it must not keep an enclosing join alive.
|
|
615
|
+
*/
|
|
616
|
+
cteAliasFor(handle) {
|
|
617
|
+
return this.ctes.get(handle) ?? this.parent?.cteAliasFor(handle);
|
|
618
|
+
}
|
|
619
|
+
/** Whether `handle` is a CTE with no SQL yet, i.e. itself or a later `with` entry. */
|
|
620
|
+
isPendingCte(handle) {
|
|
621
|
+
return this.pendingCtes.has(handle) || (this.parent?.isPendingCte(handle) ?? false);
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Fails if this query already reads `handle`. A `Ctx` maps each handle to a single alias, so two
|
|
625
|
+
* reads of one CTE value would render as the same name, i.e. `FROM tree JOIN tree`, and neither the
|
|
626
|
+
* SQL nor a column expression could say which one it meant. Each read needs its own `query(...)`.
|
|
627
|
+
*
|
|
628
|
+
* Only this query is checked, not the enclosing ones: a nested subquery has its own FROM, so reading
|
|
629
|
+
* the same CTE in there is fine.
|
|
630
|
+
*/
|
|
631
|
+
useCte(handle) {
|
|
632
|
+
if (this.usedCtes.has(handle)) fail(`${describeHandle(handle)} is already in this query's \`from\`/\`join\`; use a separate query(...) value for each use`);
|
|
633
|
+
this.usedCtes.add(handle);
|
|
634
|
+
}
|
|
560
635
|
aliasFor(handle) {
|
|
561
636
|
const local = this.aliases.get(handle);
|
|
562
637
|
if (local) return local;
|
|
@@ -591,6 +666,7 @@ function parseQuery(q, parent, assigner) {
|
|
|
591
666
|
if (isSetQuery(q)) return parseSetQuery(q, parent, assigner);
|
|
592
667
|
const ctx = new Ctx(assigner, parent);
|
|
593
668
|
const joinEntries = [...q.join ?? []].filter(isDefined);
|
|
669
|
+
const ctes = registerCtes(q, ctx, assigner);
|
|
594
670
|
const parseFrom = registerSource(q.from, ctx, assigner);
|
|
595
671
|
const pendingJoins = joinEntries.flatMap((j) => {
|
|
596
672
|
const kind = "inner" in j && j.inner ? "inner" : "left";
|
|
@@ -635,7 +711,7 @@ function parseQuery(q, parent, assigner) {
|
|
|
635
711
|
const having = conditionToSql(q.having, ctx, true);
|
|
636
712
|
const groupBys = (q.groupBy ?? []).map((g) => asExpr(g, "groupBy").toSql(ctx));
|
|
637
713
|
const orderBys = orderBysToSql(q, ctx);
|
|
638
|
-
const kept = pruneJoins(q, from, joins, [
|
|
714
|
+
const { joins: kept, ctes: keptCtes } = pruneJoins(q, from, joins, ctes, [
|
|
639
715
|
...selects,
|
|
640
716
|
...groupBys,
|
|
641
717
|
...orderBys,
|
|
@@ -650,6 +726,7 @@ function parseQuery(q, parent, assigner) {
|
|
|
650
726
|
if (forward) fail(`Join ${describeHandle(j.source.handle)} references '${forward}', which is joined later; move that join earlier in the join array`);
|
|
651
727
|
}
|
|
652
728
|
const out = [];
|
|
729
|
+
if (keptCtes.length > 0) out.push(withFragment(keptCtes));
|
|
653
730
|
out.push({
|
|
654
731
|
sql: `SELECT ${q.distinct ? "DISTINCT " : ""}`,
|
|
655
732
|
bindings: [],
|
|
@@ -713,6 +790,20 @@ function parseQuery(q, parent, assigner) {
|
|
|
713
790
|
*/
|
|
714
791
|
function registerSource(source, ctx, assigner) {
|
|
715
792
|
const handle = handleOf(source);
|
|
793
|
+
const cteAlias = ctx.cteAliasFor(handle);
|
|
794
|
+
if (cteAlias) {
|
|
795
|
+
ctx.useCte(handle);
|
|
796
|
+
return () => ({
|
|
797
|
+
handle,
|
|
798
|
+
alias: cteAlias,
|
|
799
|
+
sql: safeKq(cteAlias),
|
|
800
|
+
bindings: [],
|
|
801
|
+
refs: [],
|
|
802
|
+
entitySelects: [],
|
|
803
|
+
meta: void 0
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
if (ctx.isPendingCte(handle)) fail(`${describeHandle(handle)} is declared later in this query's \`with\`; a CTE can only read earlier ones, and cannot read itself`);
|
|
716
807
|
if (handle instanceof SubqueryHandle) {
|
|
717
808
|
const alias = handle.name ? assigner.getLiteralAlias(handle.name) : assigner.getLiteralAlias("sq");
|
|
718
809
|
ctx.register(handle, alias);
|
|
@@ -1057,14 +1148,25 @@ function refsOf(parsed) {
|
|
|
1057
1148
|
* explicit `{ inner: b, on }` here does filter rows, so pruning it when unreferenced drops that filter;
|
|
1058
1149
|
* that matches `{ books: { title: undefined } }` in em.find and is deliberate. `keep: true` pins it, and
|
|
1059
1150
|
* a pure existence filter is better written as `a.id.in(query({ ... }))`, which is never `undefined`.
|
|
1151
|
+
*
|
|
1152
|
+
* CTEs prune on the same rule and through the same dependency map: a `with` entry nothing reads
|
|
1153
|
+
* anymore drops with the join that read it, and a CTE read only by another CTE survives with it.
|
|
1154
|
+
* A CTE joined into the query shares its alias with that join, so their dependencies are merged.
|
|
1060
1155
|
*/
|
|
1061
|
-
function pruneJoins(q, from, joins, used) {
|
|
1062
|
-
if (q.pruneJoins === false) return
|
|
1156
|
+
function pruneJoins(q, from, joins, ctes, used) {
|
|
1157
|
+
if (q.pruneJoins === false) return {
|
|
1158
|
+
joins,
|
|
1159
|
+
ctes
|
|
1160
|
+
};
|
|
1063
1161
|
const deps = /* @__PURE__ */ new Map();
|
|
1064
|
-
|
|
1065
|
-
const
|
|
1066
|
-
deps.
|
|
1162
|
+
function addDeps(alias, refs) {
|
|
1163
|
+
const own = refs.filter((r) => r !== alias);
|
|
1164
|
+
const existing = deps.get(alias);
|
|
1165
|
+
if (existing) existing.push(...own);
|
|
1166
|
+
else deps.set(alias, own);
|
|
1067
1167
|
}
|
|
1168
|
+
for (const j of joins) addDeps(j.source.alias, [...j.userOn?.refs ?? [], ...j.source.refs]);
|
|
1169
|
+
for (const c of ctes) addDeps(c.alias, c.plan.outerRefs);
|
|
1068
1170
|
const required = /* @__PURE__ */ new Set();
|
|
1069
1171
|
function markRequired(alias) {
|
|
1070
1172
|
if (required.has(alias)) return;
|
|
@@ -1074,7 +1176,45 @@ function pruneJoins(q, from, joins, used) {
|
|
|
1074
1176
|
markRequired(from.alias);
|
|
1075
1177
|
for (const r of used.flatMap((u) => u.refs)) markRequired(r);
|
|
1076
1178
|
for (const j of joins) if (j.keep) markRequired(j.source.alias);
|
|
1077
|
-
return
|
|
1179
|
+
return {
|
|
1180
|
+
joins: joins.filter((j) => required.has(j.source.alias)),
|
|
1181
|
+
ctes: ctes.filter((c) => required.has(c.alias))
|
|
1182
|
+
};
|
|
1183
|
+
}
|
|
1184
|
+
/**
|
|
1185
|
+
* Names and parses each `with` entry, in declaration order.
|
|
1186
|
+
*
|
|
1187
|
+
* Each entry is in scope before the next is parsed, so a CTE can read an *earlier* sibling by name,
|
|
1188
|
+
* PostgreSQL's rule for a non-recursive WITH. A forward reference, or a CTE reading the query's own
|
|
1189
|
+
* `from`, fails as "not in this query's from/join", the same error any out-of-scope source gets.
|
|
1190
|
+
*
|
|
1191
|
+
* I.e. `with: [totals, ranked]` parses `totals` first, so `ranked` can join it, but not the reverse.
|
|
1192
|
+
*/
|
|
1193
|
+
function registerCtes(q, ctx, assigner) {
|
|
1194
|
+
const handles = (q.with === void 0 ? [] : Array.isArray(q.with) ? q.with : [q.with]).filter(isDefined).map(withEntryHandle);
|
|
1195
|
+
const aliases = handles.map((handle) => assigner.getLiteralAlias(handle.name ?? "cte"));
|
|
1196
|
+
handles.forEach((handle, i) => ctx.declareCte(handle, aliases[i]));
|
|
1197
|
+
return handles.map((handle, i) => {
|
|
1198
|
+
const plan = parseQuery(handle.q, ctx, assigner);
|
|
1199
|
+
ctx.promoteCte(handle);
|
|
1200
|
+
return {
|
|
1201
|
+
alias: aliases[i],
|
|
1202
|
+
plan
|
|
1203
|
+
};
|
|
1204
|
+
});
|
|
1205
|
+
}
|
|
1206
|
+
/** A CTE must be a table shape, so entity-mode and scalar values, which have no columns, are out. */
|
|
1207
|
+
function withEntryHandle(entry) {
|
|
1208
|
+
if (!isSubqueryValue(entry)) fail(entry instanceof SubqueryExpr || isEntityQueryValue(entry) ? "A `with` entry needs named columns; entity and scalar query(...) values have none" : "A `with` entry must be a query(...) value");
|
|
1209
|
+
return readValueHandle(entry);
|
|
1210
|
+
}
|
|
1211
|
+
/** Renders `WITH a AS (...), b AS (...) `, whose bindings lead the statement, as their SQL does. */
|
|
1212
|
+
function withFragment(ctes) {
|
|
1213
|
+
return {
|
|
1214
|
+
sql: `WITH ${ctes.map((c) => `${safeKq(c.alias)} AS (${c.plan.sql})`).join(", ")} `,
|
|
1215
|
+
bindings: ctes.flatMap((c) => c.plan.bindings),
|
|
1216
|
+
refs: []
|
|
1217
|
+
};
|
|
1078
1218
|
}
|
|
1079
1219
|
function asExpr(value, where) {
|
|
1080
1220
|
if (value instanceof BaseExpr) return value;
|