uql-orm 0.102.2 → 0.102.3
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/dist/cockroachdb/cockroachDialect.d.ts +1 -1
- package/dist/cockroachdb/cockroachDialect.js +1 -1
- package/dist/dialect/aliases.d.ts +2 -2
- package/dist/dialect/aliases.js +4 -2
- package/dist/mongodb/mongoDialect.d.ts +20 -19
- package/dist/mongodb/mongoDialect.js +111 -103
- package/dist/type/queryAggregate.d.ts +3 -6
- package/package.json +3 -3
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { PgLikeSqlDialect } from '../dialect/pgLikeSqlDialect.js';
|
|
2
2
|
import type { IndexType } from '../schema/types.js';
|
|
3
3
|
import type { QueryContext, SqlDialectFeatures, Type } from '../type/index.js';
|
|
4
|
-
/** CockroachDB: the Postgres wire and SQL,
|
|
4
|
+
/** CockroachDB: the Postgres wire and SQL, with native vectors. */
|
|
5
5
|
export declare class CockroachDialect extends PgLikeSqlDialect {
|
|
6
6
|
readonly dialectName = "cockroachdb";
|
|
7
7
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AGGREGATE_VALUE_ALIAS } from '../dialect/aliases.js';
|
|
2
2
|
import { PG_FEATURES, PG_VECTOR_METRICS, PgLikeSqlDialect } from '../dialect/pgLikeSqlDialect.js';
|
|
3
3
|
import { getMeta } from '../entity/index.js';
|
|
4
|
-
/** CockroachDB: the Postgres wire and SQL,
|
|
4
|
+
/** CockroachDB: the Postgres wire and SQL, with native vectors. */
|
|
5
5
|
export class CockroachDialect extends PgLikeSqlDialect {
|
|
6
6
|
dialectName = 'cockroachdb';
|
|
7
7
|
/**
|
|
@@ -43,8 +43,8 @@ export declare const TEXT_SCORE_ALIAS = "_uql_text_score";
|
|
|
43
43
|
* A dot becomes `_o` and an underscore `__`, so two paths such as `a.b` and `a_b` never share a flag.
|
|
44
44
|
*/
|
|
45
45
|
export declare function nullsSortField(path: string): string;
|
|
46
|
-
/**
|
|
47
|
-
export declare
|
|
46
|
+
/** The field a MongoDB lookup parks its result on, named by what it reads so two lookups alive at once cannot share one. */
|
|
47
|
+
export declare function relTempField(kind: 'cond' | 'field' | 'sort' | 'count' | 'rel', name: string | number): string;
|
|
48
48
|
/** Prefix for the field a MongoDB lookup puts a relation's rows on where a stored field has the relation's name. */
|
|
49
49
|
export declare const REL_FIELD_PREFIX = "_uql_joined_";
|
|
50
50
|
/** The field a ManyToMany lookup nests its target match under, inside the junction's own pipeline. */
|
package/dist/dialect/aliases.js
CHANGED
|
@@ -46,8 +46,10 @@ export const TEXT_SCORE_ALIAS = `${OWNED_PREFIX}_text_score`;
|
|
|
46
46
|
export function nullsSortField(path) {
|
|
47
47
|
return `${OWNED_PREFIX}_nulls_${path.replace(/[._]/g, (char) => (char === '.' ? '_o' : '__'))}`;
|
|
48
48
|
}
|
|
49
|
-
/**
|
|
50
|
-
export
|
|
49
|
+
/** The field a MongoDB lookup parks its result on, named by what it reads so two lookups alive at once cannot share one. */
|
|
50
|
+
export function relTempField(kind, name) {
|
|
51
|
+
return `${OWNED_PREFIX}_rel_${kind}_${name}`;
|
|
52
|
+
}
|
|
51
53
|
/** Prefix for the field a MongoDB lookup puts a relation's rows on where a stored field has the relation's name. */
|
|
52
54
|
export const REL_FIELD_PREFIX = `${OWNED_PREFIX}_joined_`;
|
|
53
55
|
/** The field a ManyToMany lookup nests its target match under, inside the junction's own pipeline. */
|
|
@@ -24,10 +24,10 @@ type MongoReadStages = {
|
|
|
24
24
|
readonly temporary?: boolean;
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
|
-
/**
|
|
28
|
-
type
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
/** Lookup stages, and the temp fields they fill, which the pipeline takes back out once read. */
|
|
28
|
+
type Lookup = {
|
|
29
|
+
stages: MongoAggregationPipelineEntry<Document>[];
|
|
30
|
+
temps: string[];
|
|
31
31
|
};
|
|
32
32
|
/** Default {@link DialectFeatures} for MongoDB. */
|
|
33
33
|
export declare const mongoDialectFeatures: DialectFeatures;
|
|
@@ -45,27 +45,28 @@ export declare class MongoDialect extends AbstractDialect {
|
|
|
45
45
|
/**
|
|
46
46
|
* The stages every pipeline starts with: the `$lookup` each relation condition needs, into a temporary
|
|
47
47
|
* field so `$or` keeps its meaning, the `$match` reading them, and the temporaries taken back out. Each
|
|
48
|
-
* relation aggregate the `$where` or `
|
|
48
|
+
* relation aggregate the `$where` or `fields` reads is left on the document, once, under its column.
|
|
49
|
+
* `lookup` keeps the temporaries used, so a later lookup neither repeats nor reuses one.
|
|
49
50
|
*/
|
|
50
|
-
matchStages<E extends Document>(entity: Type<E>, where?: QueryWhere<E>, opts?: QueryOptions,
|
|
51
|
+
matchStages<E extends Document>(entity: Type<E>, where?: QueryWhere<E>, opts?: QueryOptions, fields?: readonly string[], lookup?: Lookup): MongoAggregationPipelineEntry<Document>[];
|
|
51
52
|
/**
|
|
52
53
|
* Renders a `$where` tree without applying entity filters (used for same-scope group-operator
|
|
53
54
|
* recursion). A relation, or a relation aggregate, needs `$lookup` stages, so it is only accepted
|
|
54
|
-
* when `
|
|
55
|
+
* when `lookup` is given - a plain `find`/`updateMany` filter has nowhere to put them.
|
|
55
56
|
*/
|
|
56
|
-
protected renderFilter<E extends Document>(entity: Type<E>, where?: QueryWhere<E>,
|
|
57
|
+
protected renderFilter<E extends Document>(entity: Type<E>, where?: QueryWhere<E>, lookup?: Lookup): Filter<E>;
|
|
57
58
|
/**
|
|
58
59
|
* Renders `$and`/`$or`/`$not`/`$nor` into `filter`, both negations as MongoDB's `$nor` (a `$not`'s clauses
|
|
59
60
|
* wrapped in one `$and`), dropping empty clauses, since MongoDB refuses an empty operator.
|
|
60
61
|
*/
|
|
61
62
|
private appendLogicalOperator;
|
|
62
63
|
/**
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
64
|
+
* One relation condition as an expression over the correlated `$lookup` it adds to `lookup`: a row exists
|
|
65
|
+
* for a plain filter, the row count compared for a `$size`. The target's (and, for ManyToMany, the
|
|
66
|
+
* junction's) own filters scope the lookup, so a relation subquery can no more read out-of-scope rows
|
|
67
|
+
* than a direct query on the target can.
|
|
67
68
|
*/
|
|
68
|
-
private
|
|
69
|
+
private relationExpression;
|
|
69
70
|
/**
|
|
70
71
|
* The correlated `$lookup` for the target rows of one relation `where` narrows, as `temp`: straight at
|
|
71
72
|
* the target, or for a many-to-many from inside its junction's rows. The caller's filter bypass is not
|
|
@@ -123,8 +124,7 @@ export declare class MongoDialect extends AbstractDialect {
|
|
|
123
124
|
* correlated `$lookup` reading it per parent, and the `$set` that lifts it onto the document as the
|
|
124
125
|
* field the `$sort` then orders by. A parent with no related row reads a zero tally and no distance.
|
|
125
126
|
*/
|
|
126
|
-
sortAggregateStages<E extends Document>(entity: Type<E>, sort: QuerySortMap<E> | undefined): {
|
|
127
|
-
readonly stages: MongoAggregationPipelineEntry<Document>[];
|
|
127
|
+
sortAggregateStages<E extends Document>(entity: Type<E>, sort: QuerySortMap<E> | undefined): Lookup & {
|
|
128
128
|
readonly fields: string[];
|
|
129
129
|
};
|
|
130
130
|
/** Whether a read answers with a relation aggregate, which only the pipeline can build. */
|
|
@@ -245,16 +245,17 @@ export declare class MongoDialect extends AbstractDialect {
|
|
|
245
245
|
/**
|
|
246
246
|
* Resolve parsed group entries into the `_id` keys and accumulators of a `$group` stage, and the
|
|
247
247
|
* `columns` its `$project` reads each result from: a group key out of `_id`, a DISTINCT set by its
|
|
248
|
-
* size, and a `$sum` as null where it read no value, as SQL answers it. `
|
|
249
|
-
*
|
|
248
|
+
* size, and a `$sum` as null where it read no value, as SQL answers it. `lookups` puts on each matched
|
|
249
|
+
* row what the group reads through one: a relation aggregate, and an aggregate's relation condition,
|
|
250
|
+
* past the `matched` temporaries, whose relation aggregates are on the row already.
|
|
250
251
|
*/
|
|
251
252
|
private buildGroupSpec;
|
|
252
253
|
/** The field a grouped path reads, on the document or on the joined one its lookup unwound. */
|
|
253
254
|
private groupedPath;
|
|
254
255
|
/**
|
|
255
256
|
* An aggregate's own `$where` as the expression a `$cond` tests, which a query filter is not: the
|
|
256
|
-
* comparisons
|
|
257
|
-
* the
|
|
257
|
+
* comparisons, the logical operators and relations translate, anything else is refused by name. `lookup`
|
|
258
|
+
* gathers the lookups it tests: a relation condition's, and a relation aggregate's it compares.
|
|
258
259
|
*/
|
|
259
260
|
private whereExpression;
|
|
260
261
|
/** One field's condition as an expression: a value it equals, a list it is in, or a map of comparisons. */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ObjectId } from 'mongodb';
|
|
2
2
|
import { AbstractDialect } from '../dialect/abstractDialect.js';
|
|
3
|
-
import { AGGREGATE_VALUE_ALIAS, REL_NESTED_KEY, REL_FIELD_PREFIX,
|
|
3
|
+
import { AGGREGATE_VALUE_ALIAS, REL_NESTED_KEY, REL_FIELD_PREFIX, relTempField, SUM_COUNT_ALIAS, nullsSortField, sortAggregateField, TEXT_SCORE_ALIAS, } from '../dialect/aliases.js';
|
|
4
4
|
import { betweenBounds, GROUP_OPS, groupClauses, isGroupOp, LIKE_OPS, likeRegex, whereOperators, } from '../dialect/operators.js';
|
|
5
5
|
import { aggregateColumnField, groupPathField, resolveGroupJoins, relationSortTerms, resolveQueryJoins, resolveSortableJoin, } from '../dialect/queryJoins.js';
|
|
6
6
|
import { assertSoleId, fieldOf, getMeta, relationOf, soleIdOf } from '../entity/index.js';
|
|
@@ -19,6 +19,11 @@ function relationField(relKey) {
|
|
|
19
19
|
function joinedPath(join) {
|
|
20
20
|
return join.parent ? `${joinedPath(join.parent)}.${relationField(join.key)}` : relationField(join.key);
|
|
21
21
|
}
|
|
22
|
+
const mergeLookups = (lookups) => ({
|
|
23
|
+
stages: lookups.flatMap((lookup) => lookup.stages),
|
|
24
|
+
temps: lookups.flatMap((lookup) => lookup.temps),
|
|
25
|
+
});
|
|
26
|
+
const unsetOf = (temps) => temps.length ? [{ $unset: temps }] : [];
|
|
22
27
|
/** A scalar field's operator as the aggregation operator computing it. */
|
|
23
28
|
const MONGO_ARITHMETIC = { $inc: '$add', $mul: '$multiply' };
|
|
24
29
|
/**
|
|
@@ -203,34 +208,30 @@ export class MongoDialect extends AbstractDialect {
|
|
|
203
208
|
/**
|
|
204
209
|
* The stages every pipeline starts with: the `$lookup` each relation condition needs, into a temporary
|
|
205
210
|
* field so `$or` keeps its meaning, the `$match` reading them, and the temporaries taken back out. Each
|
|
206
|
-
* relation aggregate the `$where` or `
|
|
211
|
+
* relation aggregate the `$where` or `fields` reads is left on the document, once, under its column.
|
|
212
|
+
* `lookup` keeps the temporaries used, so a later lookup neither repeats nor reuses one.
|
|
207
213
|
*/
|
|
208
|
-
matchStages(entity, where = {}, opts = {},
|
|
214
|
+
matchStages(entity, where = {}, opts = {}, fields = [], lookup = { stages: [], temps: [] }) {
|
|
209
215
|
const meta = getMeta(entity);
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
this.appendAggregateField(meta, key, lookups);
|
|
216
|
+
for (const key of fields) {
|
|
217
|
+
this.appendAggregateField(meta, key, lookup);
|
|
213
218
|
}
|
|
214
|
-
const filter = this.renderFilter(entity, this.scopedWhere(meta, where, opts),
|
|
215
|
-
return [
|
|
216
|
-
...lookups.stages,
|
|
217
|
-
...(hasKeys(filter) ? [{ $match: filter }] : []),
|
|
218
|
-
...(lookups.temps.length ? [{ $unset: lookups.temps }] : []),
|
|
219
|
-
];
|
|
219
|
+
const filter = this.renderFilter(entity, this.scopedWhere(meta, where, opts), lookup);
|
|
220
|
+
return [...lookup.stages, ...(hasKeys(filter) ? [{ $match: filter }] : []), ...unsetOf(lookup.temps)];
|
|
220
221
|
}
|
|
221
222
|
/**
|
|
222
223
|
* Renders a `$where` tree without applying entity filters (used for same-scope group-operator
|
|
223
224
|
* recursion). A relation, or a relation aggregate, needs `$lookup` stages, so it is only accepted
|
|
224
|
-
* when `
|
|
225
|
+
* when `lookup` is given - a plain `find`/`updateMany` filter has nowhere to put them.
|
|
225
226
|
*/
|
|
226
|
-
renderFilter(entity, where = {},
|
|
227
|
+
renderFilter(entity, where = {}, lookup) {
|
|
227
228
|
const meta = getMeta(entity);
|
|
228
229
|
const filter = {};
|
|
229
230
|
for (const [rawKey, rawVal] of Object.entries(where)) {
|
|
230
231
|
let key = rawKey;
|
|
231
232
|
let val = rawVal;
|
|
232
233
|
if (isGroupOp(key)) {
|
|
233
|
-
this.appendLogicalOperator(filter, entity, key, val,
|
|
234
|
+
this.appendLogicalOperator(filter, entity, key, val, lookup);
|
|
234
235
|
}
|
|
235
236
|
else if (key === '$text') {
|
|
236
237
|
// MongoDB's text index declares which fields it covers, so `$fields` cannot narrow the search
|
|
@@ -240,19 +241,19 @@ export class MongoDialect extends AbstractDialect {
|
|
|
240
241
|
}
|
|
241
242
|
else if (meta.relations[key]) {
|
|
242
243
|
assertNoRaw(val, '$where');
|
|
243
|
-
if (!
|
|
244
|
+
if (!lookup) {
|
|
244
245
|
throw new UqlUsageError(`filtering by relation '${key}' is not supported here on MongoDB`);
|
|
245
246
|
}
|
|
246
|
-
|
|
247
|
+
andExpr(filter, this.relationExpression(meta, key, val, lookup));
|
|
247
248
|
}
|
|
248
249
|
else {
|
|
249
250
|
assertNoRaw(val, '$where');
|
|
250
251
|
this.assertKnownPathRoot(meta, key);
|
|
251
252
|
if (aggregateOf(meta.fields[key])) {
|
|
252
|
-
if (!
|
|
253
|
+
if (!lookup) {
|
|
253
254
|
throw new UqlUsageError(`filtering by relation aggregate '${key}' is not supported here on MongoDB`);
|
|
254
255
|
}
|
|
255
|
-
this.appendAggregateField(meta, key,
|
|
256
|
+
this.appendAggregateField(meta, key, lookup);
|
|
256
257
|
}
|
|
257
258
|
const keyed = this.holdsKeys(meta, key);
|
|
258
259
|
key = this.pathOf(meta, key);
|
|
@@ -284,12 +285,12 @@ export class MongoDialect extends AbstractDialect {
|
|
|
284
285
|
* Renders `$and`/`$or`/`$not`/`$nor` into `filter`, both negations as MongoDB's `$nor` (a `$not`'s clauses
|
|
285
286
|
* wrapped in one `$and`), dropping empty clauses, since MongoDB refuses an empty operator.
|
|
286
287
|
*/
|
|
287
|
-
appendLogicalOperator(filter, entity, key, val,
|
|
288
|
+
appendLogicalOperator(filter, entity, key, val, lookup) {
|
|
288
289
|
const { join, negate } = GROUP_OPS[key];
|
|
289
290
|
const parts = groupClauses(key, val)
|
|
290
291
|
.map((filterIt) => {
|
|
291
292
|
assertNoRaw(filterIt, '$where');
|
|
292
|
-
return this.renderFilter(entity, filterIt,
|
|
293
|
+
return this.renderFilter(entity, filterIt, lookup);
|
|
293
294
|
})
|
|
294
295
|
.filter((part) => Object.keys(part).length > 0);
|
|
295
296
|
if (!parts.length) {
|
|
@@ -303,24 +304,19 @@ export class MongoDialect extends AbstractDialect {
|
|
|
303
304
|
filter['$nor'] = [...(filter['$nor'] ?? []), ...negated];
|
|
304
305
|
}
|
|
305
306
|
/**
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
307
|
+
* One relation condition as an expression over the correlated `$lookup` it adds to `lookup`: a row exists
|
|
308
|
+
* for a plain filter, the row count compared for a `$size`. The target's (and, for ManyToMany, the
|
|
309
|
+
* junction's) own filters scope the lookup, so a relation subquery can no more read out-of-scope rows
|
|
310
|
+
* than a direct query on the target can.
|
|
310
311
|
*/
|
|
311
|
-
|
|
312
|
-
const temp = `${REL_TEMP_PREFIX}${lookups.temps.length}`;
|
|
312
|
+
relationExpression(meta, relKey, val, lookup) {
|
|
313
313
|
const sizeVal = parseRelationSize(val);
|
|
314
|
+
const temp = relTempField('cond', lookup.temps.length);
|
|
314
315
|
const tail = sizeVal === undefined ? [{ $limit: 1 }] : [{ $count: AGGREGATE_VALUE_ALIAS }];
|
|
315
316
|
const where = (sizeVal === undefined ? val : {});
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
filter[`${temp}.0`] = { $exists: true };
|
|
320
|
-
}
|
|
321
|
-
else {
|
|
322
|
-
andExpr(filter, compareCount(this.tally(temp), sizeVal));
|
|
323
|
-
}
|
|
317
|
+
lookup.temps.push(temp);
|
|
318
|
+
lookup.stages.push(this.relationLookup(meta, meta.relations[relKey], where, temp, tail));
|
|
319
|
+
return sizeVal === undefined ? { $gt: [{ $size: `$${temp}` }, 0] } : compareCount(this.tally(temp), sizeVal);
|
|
324
320
|
}
|
|
325
321
|
/**
|
|
326
322
|
* The correlated `$lookup` for the target rows of one relation `where` narrows, as `temp`: straight at
|
|
@@ -614,8 +610,8 @@ export class MongoDialect extends AbstractDialect {
|
|
|
614
610
|
const meta = getMeta(entity);
|
|
615
611
|
const specs = Object.entries(sort ?? {}).flatMap(([key, value]) => meta.relations[key] ? relationSortTerms(key, key, value).aggregates.map(({ spec }) => spec) : []);
|
|
616
612
|
const fields = specs.map(sortAggregateField);
|
|
617
|
-
const
|
|
618
|
-
return {
|
|
613
|
+
const lookups = specs.map((spec, index) => this.aggregateStages(meta, spec, relTempField('sort', fields[index]), fields[index]));
|
|
614
|
+
return { ...mergeLookups(lookups), fields };
|
|
619
615
|
}
|
|
620
616
|
/** Whether a read answers with a relation aggregate, which only the pipeline can build. */
|
|
621
617
|
readsAggregates(entity, q) {
|
|
@@ -632,14 +628,15 @@ export class MongoDialect extends AbstractDialect {
|
|
|
632
628
|
* {@link aggregateStages} builds from the same spec SQL renders as a subquery. Once however many
|
|
633
629
|
* clauses read it; a key computing none adds nothing.
|
|
634
630
|
*/
|
|
635
|
-
appendAggregateField(meta, key,
|
|
631
|
+
appendAggregateField(meta, key, lookup) {
|
|
636
632
|
const spec = aggregateOf(meta.fields[key]);
|
|
637
|
-
const temp =
|
|
638
|
-
if (!spec ||
|
|
633
|
+
const temp = relTempField('field', key);
|
|
634
|
+
if (!spec || lookup.temps.includes(temp)) {
|
|
639
635
|
return;
|
|
640
636
|
}
|
|
641
|
-
|
|
642
|
-
|
|
637
|
+
const { stages, temps } = this.aggregateStages(meta, spec, temp, this.columnOf(meta, key));
|
|
638
|
+
lookup.stages.push(...stages);
|
|
639
|
+
lookup.temps.push(...temps);
|
|
643
640
|
}
|
|
644
641
|
/**
|
|
645
642
|
* One relation aggregate on the document under `field`: the correlated lookup that reads the related
|
|
@@ -673,10 +670,13 @@ export class MongoDialect extends AbstractDialect {
|
|
|
673
670
|
}
|
|
674
671
|
: { $count: AGGREGATE_VALUE_ALIAS },
|
|
675
672
|
];
|
|
676
|
-
return
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
673
|
+
return {
|
|
674
|
+
stages: [
|
|
675
|
+
this.relationLookup(meta, relOpts, spec.where ?? {}, temp, tail),
|
|
676
|
+
{ $addFields: { [field]: this.tally(temp, spec.op) } },
|
|
677
|
+
],
|
|
678
|
+
temps: [temp],
|
|
679
|
+
};
|
|
680
680
|
}
|
|
681
681
|
/**
|
|
682
682
|
* The value a lookup left in `temp`, which holds no row at all where nothing matched: `0` for the
|
|
@@ -693,25 +693,21 @@ export class MongoDialect extends AbstractDialect {
|
|
|
693
693
|
*/
|
|
694
694
|
relationReadStages(entity, q) {
|
|
695
695
|
const meta = getMeta(entity);
|
|
696
|
-
const
|
|
697
|
-
const
|
|
698
|
-
|
|
699
|
-
stages.push(...this.toManyLookup(meta, relKey, parseRelationAtKey(relKey, q.$populate).query, temps));
|
|
700
|
-
}
|
|
701
|
-
for (const { relKey, where } of countedRelations(meta, q.$count)) {
|
|
702
|
-
const temp = `${REL_TEMP_PREFIX}count_${relKey}`;
|
|
703
|
-
temps.push(temp);
|
|
696
|
+
const toMany = getRelationRequestSummary(meta, q.$populate).toManyKeys.map((relKey) => this.toManyLookup(meta, relKey, parseRelationAtKey(relKey, q.$populate).query));
|
|
697
|
+
const counts = countedRelations(meta, q.$count).map(({ relKey, where }) => {
|
|
698
|
+
const temp = relTempField('count', relKey);
|
|
704
699
|
const spec = { relation: relKey, op: '$count', where };
|
|
705
|
-
|
|
706
|
-
}
|
|
707
|
-
|
|
700
|
+
return this.aggregateStages(meta, spec, temp, `${COUNT_RESULT_KEY}.${relKey}`);
|
|
701
|
+
});
|
|
702
|
+
const { stages, temps } = mergeLookups([...toMany, ...counts]);
|
|
703
|
+
return [...stages, ...unsetOf(temps)];
|
|
708
704
|
}
|
|
709
705
|
/**
|
|
710
706
|
* A to-many's rows as a lookup running their own read, whose filters, ordering and page apply per
|
|
711
707
|
* parent inside it. A many-to-many reads its targets, each once, by the ids its junction pairs the
|
|
712
708
|
* parent with, parked in a temporary field of the parent's.
|
|
713
709
|
*/
|
|
714
|
-
toManyLookup(meta, relKey, query
|
|
710
|
+
toManyLookup(meta, relKey, query) {
|
|
715
711
|
const relOpts = relationOf(meta, relKey);
|
|
716
712
|
const relEntity = relOpts.entity();
|
|
717
713
|
const relMeta = getMeta(relEntity);
|
|
@@ -719,38 +715,36 @@ export class MongoDialect extends AbstractDialect {
|
|
|
719
715
|
const pipeline = read.length ? { pipeline: read } : {};
|
|
720
716
|
const from = this.resolveTableName(relMeta);
|
|
721
717
|
if (!relOpts.through) {
|
|
722
|
-
return
|
|
718
|
+
return {
|
|
719
|
+
stages: [
|
|
720
|
+
{ $lookup: { from, ...this.joinKeys(meta, relMeta, relOpts), ...pipeline, as: relationField(relKey) } },
|
|
721
|
+
],
|
|
722
|
+
temps: [],
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
const junction = this.junctionOf(meta, relOpts, relMeta, relOpts.through());
|
|
726
|
+
const temp = relTempField('rel', relKey);
|
|
727
|
+
return {
|
|
728
|
+
temps: [temp],
|
|
729
|
+
stages: [
|
|
730
|
+
{
|
|
731
|
+
$lookup: {
|
|
732
|
+
...junction.lookup,
|
|
733
|
+
pipeline: [...junction.scope, { $project: { [junction.target]: 1 } }],
|
|
734
|
+
as: temp,
|
|
735
|
+
},
|
|
736
|
+
},
|
|
723
737
|
{
|
|
724
738
|
$lookup: {
|
|
725
739
|
from,
|
|
726
|
-
|
|
740
|
+
localField: `${temp}.${junction.target}`,
|
|
741
|
+
foreignField: ID_KEY,
|
|
727
742
|
...pipeline,
|
|
728
743
|
as: relationField(relKey),
|
|
729
744
|
},
|
|
730
745
|
},
|
|
731
|
-
]
|
|
732
|
-
}
|
|
733
|
-
const junction = this.junctionOf(meta, relOpts, relMeta, relOpts.through());
|
|
734
|
-
const temp = `${REL_TEMP_PREFIX}${relKey}`;
|
|
735
|
-
temps.push(temp);
|
|
736
|
-
return [
|
|
737
|
-
{
|
|
738
|
-
$lookup: {
|
|
739
|
-
...junction.lookup,
|
|
740
|
-
pipeline: [...junction.scope, { $project: { [junction.target]: 1 } }],
|
|
741
|
-
as: temp,
|
|
742
|
-
},
|
|
743
|
-
},
|
|
744
|
-
{
|
|
745
|
-
$lookup: {
|
|
746
|
-
from,
|
|
747
|
-
localField: `${temp}.${junction.target}`,
|
|
748
|
-
foreignField: ID_KEY,
|
|
749
|
-
...pipeline,
|
|
750
|
-
as: relationField(relKey),
|
|
751
|
-
},
|
|
752
|
-
},
|
|
753
|
-
];
|
|
746
|
+
],
|
|
747
|
+
};
|
|
754
748
|
}
|
|
755
749
|
/** Whether a `$sort` reads a relation, which is what forces the lookups to run before it. */
|
|
756
750
|
sortsRelations(entity, sort) {
|
|
@@ -835,8 +829,13 @@ export class MongoDialect extends AbstractDialect {
|
|
|
835
829
|
// which is the one way this differs from a SQL join. Taken back out once the `$sort` that needed
|
|
836
830
|
// it has run, so ordering by an unpopulated relation costs the same nothing it does there.
|
|
837
831
|
const sortOnly = [...joins.values()].filter((join) => !join.projected);
|
|
838
|
-
const dropped = [
|
|
839
|
-
|
|
832
|
+
const dropped = [
|
|
833
|
+
...sortOnly.map((join) => joinedPath(join)),
|
|
834
|
+
...aggregated.temps,
|
|
835
|
+
...aggregated.fields,
|
|
836
|
+
...ordering.fields,
|
|
837
|
+
];
|
|
838
|
+
const unset = unsetOf(dropped);
|
|
840
839
|
// The grouping collapses rows onto the columns it projects, which leaves nothing for an ordering
|
|
841
840
|
// that reads a lookup those columns do not carry. Refused rather than answered all-equal, and in
|
|
842
841
|
// the same terms the SQL dialects refuse `SELECT DISTINCT` ordered by an unselected column.
|
|
@@ -1114,7 +1113,7 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1114
1113
|
assignments[key] = { $literal: value };
|
|
1115
1114
|
}
|
|
1116
1115
|
}
|
|
1117
|
-
return [...(hasKeys(assignments) ? [{ $set: assignments }] : []), ...(unset
|
|
1116
|
+
return [...(hasKeys(assignments) ? [{ $set: assignments }] : []), ...unsetOf(unset)];
|
|
1118
1117
|
}
|
|
1119
1118
|
/** Refuses a key left to MongoDB that it cannot mint: only an `ObjectId`, read back as a string, so not a `Number`. */
|
|
1120
1119
|
assertMintableKey(meta, field) {
|
|
@@ -1159,9 +1158,13 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1159
1158
|
buildAggregateStages(entity, q, opts) {
|
|
1160
1159
|
const meta = getMeta(entity);
|
|
1161
1160
|
const { joins, where } = resolveGroupJoins(meta, q);
|
|
1162
|
-
const {
|
|
1161
|
+
const matched = { stages: [], temps: [] };
|
|
1162
|
+
const match = this.matchStages(entity, where, opts, [], matched);
|
|
1163
|
+
const { groupId, accumulators, columns, lookups } = this.buildGroupSpec(meta, parseGroupMap(q.$group, q.$select), joins, matched.temps);
|
|
1163
1164
|
const pipeline = [
|
|
1164
|
-
...
|
|
1165
|
+
...match,
|
|
1166
|
+
// What the `$group` reads off each matched row through a lookup, the temporaries it then drops.
|
|
1167
|
+
...lookups,
|
|
1165
1168
|
...this.lookupStages(meta, joins),
|
|
1166
1169
|
{ $group: { _id: hasKeys(groupId) ? groupId : null, ...accumulators } },
|
|
1167
1170
|
// `$group` answers with `_id` even when grouping by nothing, and with what `columns` read to the end.
|
|
@@ -1192,29 +1195,30 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1192
1195
|
/**
|
|
1193
1196
|
* Resolve parsed group entries into the `_id` keys and accumulators of a `$group` stage, and the
|
|
1194
1197
|
* `columns` its `$project` reads each result from: a group key out of `_id`, a DISTINCT set by its
|
|
1195
|
-
* size, and a `$sum` as null where it read no value, as SQL answers it. `
|
|
1196
|
-
*
|
|
1198
|
+
* size, and a `$sum` as null where it read no value, as SQL answers it. `lookups` puts on each matched
|
|
1199
|
+
* row what the group reads through one: a relation aggregate, and an aggregate's relation condition,
|
|
1200
|
+
* past the `matched` temporaries, whose relation aggregates are on the row already.
|
|
1197
1201
|
*/
|
|
1198
|
-
buildGroupSpec(meta, groupEntries, joins) {
|
|
1202
|
+
buildGroupSpec(meta, groupEntries, joins, matched) {
|
|
1199
1203
|
if (!groupEntries.length) {
|
|
1200
1204
|
throw new UqlUsageError('aggregate requires at least one $group column or $select function');
|
|
1201
1205
|
}
|
|
1202
1206
|
const groupId = {};
|
|
1203
1207
|
const accumulators = {};
|
|
1204
1208
|
const columns = {};
|
|
1205
|
-
const
|
|
1209
|
+
const lookup = { stages: [], temps: [...matched] };
|
|
1206
1210
|
for (const entry of groupEntries) {
|
|
1207
1211
|
// Aliases stay as the caller wrote them ($project maps them back); the *refs* address columns.
|
|
1208
1212
|
if (entry.kind === 'key') {
|
|
1209
|
-
groupId[entry.alias] = `$${this.groupedPath(meta, joins, entry.path,
|
|
1213
|
+
groupId[entry.alias] = `$${this.groupedPath(meta, joins, entry.path, lookup)}`;
|
|
1210
1214
|
columns[entry.alias] = `$_id.${entry.alias}`;
|
|
1211
1215
|
continue;
|
|
1212
1216
|
}
|
|
1213
1217
|
const { field } = entry;
|
|
1214
1218
|
if (field !== undefined) {
|
|
1215
|
-
|
|
1219
|
+
this.appendAggregateField(meta, field, lookup);
|
|
1216
1220
|
}
|
|
1217
|
-
const test = entry.where && this.whereExpression(meta, entry.where,
|
|
1221
|
+
const test = entry.where && this.whereExpression(meta, entry.where, lookup);
|
|
1218
1222
|
// What the accumulator reads from a row its own `$where` passes, and from one it does not.
|
|
1219
1223
|
const read = (passed, failed) => (test ? { $cond: [test, passed, failed] } : passed);
|
|
1220
1224
|
columns[entry.alias] = 1;
|
|
@@ -1242,13 +1246,13 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1242
1246
|
columns[entry.alias] = { $cond: [{ $eq: [`$${counted}`, 0] }, null, `$${entry.alias}`] };
|
|
1243
1247
|
}
|
|
1244
1248
|
}
|
|
1245
|
-
return { groupId, accumulators, columns,
|
|
1249
|
+
return { groupId, accumulators, columns, lookups: lookup.stages };
|
|
1246
1250
|
}
|
|
1247
1251
|
/** The field a grouped path reads, on the document or on the joined one its lookup unwound. */
|
|
1248
|
-
groupedPath(meta, joins, path,
|
|
1252
|
+
groupedPath(meta, joins, path, lookup) {
|
|
1249
1253
|
const { key, join } = groupPathField(joins, path);
|
|
1250
1254
|
if (!join) {
|
|
1251
|
-
|
|
1255
|
+
this.appendAggregateField(meta, key, lookup);
|
|
1252
1256
|
return this.columnOf(meta, key);
|
|
1253
1257
|
}
|
|
1254
1258
|
if (aggregateOf(join.meta.fields[key])) {
|
|
@@ -1258,24 +1262,28 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1258
1262
|
}
|
|
1259
1263
|
/**
|
|
1260
1264
|
* An aggregate's own `$where` as the expression a `$cond` tests, which a query filter is not: the
|
|
1261
|
-
* comparisons
|
|
1262
|
-
* the
|
|
1265
|
+
* comparisons, the logical operators and relations translate, anything else is refused by name. `lookup`
|
|
1266
|
+
* gathers the lookups it tests: a relation condition's, and a relation aggregate's it compares.
|
|
1263
1267
|
*/
|
|
1264
|
-
whereExpression(meta, where,
|
|
1268
|
+
whereExpression(meta, where, lookup) {
|
|
1265
1269
|
const terms = getKeys(where).map((key) => {
|
|
1266
1270
|
if (isGroupOp(key)) {
|
|
1267
1271
|
const { join, negate } = GROUP_OPS[key];
|
|
1268
1272
|
const clauses = groupClauses(key, where[key]).map((clause) => {
|
|
1269
1273
|
assertNoRaw(clause, 'an aggregate $where');
|
|
1270
|
-
return this.whereExpression(meta, clause,
|
|
1274
|
+
return this.whereExpression(meta, clause, lookup);
|
|
1271
1275
|
});
|
|
1272
1276
|
return negate ? { $not: [{ [join]: clauses }] } : { [join]: clauses };
|
|
1273
1277
|
}
|
|
1278
|
+
if (meta.relations[key]) {
|
|
1279
|
+
assertNoRaw(where[key], 'an aggregate $where');
|
|
1280
|
+
return this.relationExpression(meta, key, where[key], lookup);
|
|
1281
|
+
}
|
|
1274
1282
|
if (key.startsWith('$')) {
|
|
1275
1283
|
throw new UqlUsageError(`aggregate $where operator '${key}' is not supported on MongoDB`);
|
|
1276
1284
|
}
|
|
1277
1285
|
const val = where[key];
|
|
1278
|
-
|
|
1286
|
+
this.appendAggregateField(meta, key, lookup);
|
|
1279
1287
|
const wire = this.holdsKeys(meta, key) ? (value) => this.toWireId(value) : (value) => value;
|
|
1280
1288
|
return this.fieldExpression(`$${this.pathOf(meta, key)}`, val, wire);
|
|
1281
1289
|
});
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { FieldKey, FieldKeyOf, RelationKey, RelationTarget } from './entity.js';
|
|
2
2
|
import type { QueryPager, QuerySelect, QuerySortDirection } from './query.js';
|
|
3
|
-
import type { QuerySql } from './querySql.js';
|
|
4
3
|
import type { QueryWhere, QueryWhereFieldValue } from './queryWhere.js';
|
|
5
4
|
import type { ExactlyOne, IsMany, RejectKeys } from './utility.js';
|
|
6
5
|
/** The columns `$group` names by a literal `true`, so an uninferred `$group`, its own constraint, names none. */
|
|
@@ -67,18 +66,16 @@ type QueryAggregateArgMap<E> = Record<'$count', QueryAggregateArg<E>> & Record<T
|
|
|
67
66
|
/**
|
|
68
67
|
* An aggregate over one field, exactly one op per entry: `{ $sum: { amount: true } }` is `SUM("amount")`,
|
|
69
68
|
* `{ $countDistinct: { id: true } }` is `COUNT(DISTINCT "id")`, and only `$count` takes `'*'`. Its own
|
|
70
|
-
* `$where` narrows the rows it reads to those matching,
|
|
69
|
+
* `$where` narrows the rows it reads to those matching, as a query's `$where` does.
|
|
71
70
|
*/
|
|
72
71
|
export type QueryAggregateFn<E> = ExactlyOne<QueryAggregateArgMap<E>> & {
|
|
73
|
-
readonly $where?:
|
|
72
|
+
readonly $where?: QueryWhere<E>;
|
|
74
73
|
};
|
|
75
|
-
/** What an aggregate's own `$where` reads: the entity's fields, since a relation there is a subquery inside it. */
|
|
76
|
-
type QueryAggregateWhere<E> = QueryWhere<E, QuerySql, FieldKey<E>>;
|
|
77
74
|
/** An aggregate's `$where` naming a key it does not have, refused: a captured `$select` skips the excess-property check. */
|
|
78
75
|
type AggregateWhereKeys<E, Fn> = Fn extends {
|
|
79
76
|
readonly $where: infer W;
|
|
80
77
|
} ? {
|
|
81
|
-
readonly $where: RejectKeys<Exclude<keyof W, keyof
|
|
78
|
+
readonly $where: RejectKeys<Exclude<keyof W, keyof QueryWhere<E>>>;
|
|
82
79
|
} : unknown;
|
|
83
80
|
/** A single-key `{ [op]: Arg }` shape for each op in `Ops`, matched to infer that op's result or its argument. */
|
|
84
81
|
type FnWithOp<Ops extends string, Arg = unknown> = {
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"homepage": "https://uql-orm.dev",
|
|
4
4
|
"description": "JSON-native TypeScript ORM for Bun, Browsers, Edge, Node, Workers. Supports PostgreSQL, PGlite, MySQL, MariaDB, SQLite, CockroachDB, SQL Server, Turso, Neon, Cloudflare D1 and MongoDB. Queries are plain JSON, typed to the leaf.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "0.102.
|
|
6
|
+
"version": "0.102.3",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"engines": {
|
|
9
9
|
"node": ">=22.18"
|
|
@@ -144,8 +144,8 @@
|
|
|
144
144
|
"express": "^5.3.0",
|
|
145
145
|
"mariadb": "^3.5.4",
|
|
146
146
|
"mongodb": "^7.7.0",
|
|
147
|
-
"mssql": "^12.7.
|
|
148
|
-
"mysql2": "^3.24.
|
|
147
|
+
"mssql": "^12.7.5",
|
|
148
|
+
"mysql2": "^3.24.6",
|
|
149
149
|
"pg": "^8.23.1",
|
|
150
150
|
"pg-query-stream": "^4.17.1",
|
|
151
151
|
"rxjs": "^7.8.2",
|