uql-orm 0.102.2 → 0.102.4
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 +35 -21
- package/dist/mongodb/mongoDialect.js +237 -146
- 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,20 +245,34 @@ 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
|
-
* An aggregate's own `$where` as the expression a `$cond` tests,
|
|
256
|
-
*
|
|
257
|
-
* the
|
|
256
|
+
* An aggregate's own `$where` as the expression a `$cond` tests, matching the rows {@link renderFilter}
|
|
257
|
+
* would: a `$match` cannot sit inside a `$group`, and `$text`, bound to the pipeline's first stage, is
|
|
258
|
+
* refused by name. `lookup` gathers the lookups it tests: a relation condition's, and a relation aggregate's.
|
|
258
259
|
*/
|
|
259
260
|
private whereExpression;
|
|
260
|
-
/**
|
|
261
|
+
/**
|
|
262
|
+
* One field's condition as an expression, read as {@link transformOperators} reads it in a query: a value
|
|
263
|
+
* it equals, a list it is in, or operators. `wire` converts what a key field is compared with.
|
|
264
|
+
*/
|
|
261
265
|
private fieldExpression;
|
|
266
|
+
/**
|
|
267
|
+
* What `ref` satisfies to hold `value`, as {@link held} reads it in a query: an array each of its
|
|
268
|
+
* elements, an object each of its keys as a path, anything else as a field condition.
|
|
269
|
+
*/
|
|
270
|
+
private heldExpression;
|
|
271
|
+
/**
|
|
272
|
+
* `$all`, as {@link allHolding} reads it: the array at `ref` has an element holding each value, a scalar
|
|
273
|
+
* by `$in`, which stops at the first match where testing each element would map them all.
|
|
274
|
+
*/
|
|
275
|
+
private allHeld;
|
|
262
276
|
private buildHavingFilter;
|
|
263
277
|
/**
|
|
264
278
|
* Separate vector sort entries from regular sort entries.
|
|
@@ -1,13 +1,13 @@
|
|
|
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,
|
|
4
|
-
import { betweenBounds, GROUP_OPS, groupClauses, isGroupOp, LIKE_OPS, likeRegex, whereOperators, } from '../dialect/operators.js';
|
|
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
|
+
import { betweenBounds, inOperands, 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';
|
|
7
7
|
import { COUNT_RESULT_KEY } from '../type/query.js';
|
|
8
8
|
import { QuerySql } from '../type/querySql.js';
|
|
9
9
|
import { aggregateOf, isSelectList, assertAggregateColumns, assertNoUndefined, assertNonNegativeInteger, columnFamily, countedRelations, entityName, fieldUpdateOf, fillOnFields, filterFieldKeys, findVectorIndex, findVectorSort, definedEntries, getKeys, holdsForeignKey, jsonPathKeys, getRelationRequestSummary, hasKeys, isFieldUpdateOp, isJsonObject, foldJson, writesKeys, isJsonUpdateOp, isOperatorMap, isOperatorObject, isRecord, isVectorSearch, normalizeScalarFieldSelection, parentJoins, parseGroupMap, parseRelationAtKey, parseRelationSize, parseSortDirection, rankedTextSearch, someKey, targetJoins, textSortOf, vectorDistanceOf, } from '../util/index.js';
|
|
10
|
-
import { UqlUsageError } from '../util/uqlError.js';
|
|
10
|
+
import { kindOf, UqlUsageError } from '../util/uqlError.js';
|
|
11
11
|
import { decodeBigIntsExcept } from '../util/wideNumber.js';
|
|
12
12
|
import { textLanguage } from './textLanguage.js';
|
|
13
13
|
import { vectorDistanceExpr } from './vectorDistance.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
|
/**
|
|
@@ -128,27 +133,67 @@ function andExpr(filter, expr) {
|
|
|
128
133
|
filter['$expr'] = filter['$expr'] ? { $and: [filter['$expr'], expr] } : expr;
|
|
129
134
|
}
|
|
130
135
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
* empty array in place of any other value.
|
|
136
|
+
* `value` as an expression reads it, verbatim: bare, a string could name a field (`'$name'`), an object
|
|
137
|
+
* an operator, and an array its elements' expressions. A number reads as itself.
|
|
134
138
|
*/
|
|
135
|
-
function
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
+
function literal(value) {
|
|
140
|
+
return typeof value === 'number' ? value : { $literal: value };
|
|
141
|
+
}
|
|
142
|
+
/** Every term true: the term itself when it is the only one. */
|
|
143
|
+
function allOf(terms) {
|
|
144
|
+
return terms.length === 1 ? terms[0] : { $and: terms };
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* The array at `ref` has `size` elements, or a count in its bounds: an expression, since MongoDB's own
|
|
148
|
+
* `$size` takes only a number. No other value has a size, and `$and` may evaluate every operand, so the
|
|
149
|
+
* count reads an empty array in place of one.
|
|
150
|
+
*/
|
|
151
|
+
function arraySize(ref, size) {
|
|
152
|
+
const count = { $size: { $cond: [{ $isArray: ref }, ref, []] } };
|
|
153
|
+
return { $and: [{ $isArray: ref }, compareCount(count, size)] };
|
|
139
154
|
}
|
|
140
155
|
/** `count` compared with `size`, a number or its bounds, as an aggregation expression. */
|
|
141
156
|
function compareCount(count, size) {
|
|
142
157
|
if (typeof size === 'number') {
|
|
143
158
|
return { $eq: [count, size] };
|
|
144
159
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
160
|
+
if (!isRecord(size)) {
|
|
161
|
+
throw new UqlUsageError(`$size expects a number or bounds, got ${kindOf(size)}`);
|
|
162
|
+
}
|
|
163
|
+
const comparisons = Object.entries(size).flatMap(([op, bound]) => {
|
|
164
|
+
if (op !== '$between') {
|
|
165
|
+
return [{ [op]: [count, literal(bound)] }];
|
|
166
|
+
}
|
|
167
|
+
const [min, max] = betweenBounds(bound);
|
|
168
|
+
return [{ $gte: [count, literal(min)] }, { $lte: [count, literal(max)] }];
|
|
169
|
+
});
|
|
148
170
|
if (!comparisons.length) {
|
|
149
171
|
throw new UqlUsageError('$size needs at least one comparison');
|
|
150
172
|
}
|
|
151
|
-
return comparisons
|
|
173
|
+
return allOf(comparisons);
|
|
174
|
+
}
|
|
175
|
+
/** Whether the array at `ref` has an element `test` passes, reading it as `$$this`. No other value has one. */
|
|
176
|
+
function anyElement(ref, test) {
|
|
177
|
+
return { $cond: [{ $isArray: ref }, { $anyElementTrue: [{ $map: { input: ref, in: test } }] }, false] };
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Whether `ref` holds a value of `operand`'s type, the only one a query's ordering compares it with: an
|
|
181
|
+
* expression orders across types, every string above a number and a null below it.
|
|
182
|
+
*/
|
|
183
|
+
function sameType(ref, operand) {
|
|
184
|
+
return typeof operand === 'number' || typeof operand === 'bigint'
|
|
185
|
+
? { $isNumber: ref }
|
|
186
|
+
: { $eq: [{ $type: ref }, { $type: literal(operand) }] };
|
|
187
|
+
}
|
|
188
|
+
/** Whether the string at `ref` matches `regex`. Any other value matches none, where `$regexMatch` would fail. */
|
|
189
|
+
function regexMatch(ref, regex, insensitive = false) {
|
|
190
|
+
const match = { $regexMatch: { input: ref, regex: literal(regex), ...(insensitive && { options: 'i' }) } };
|
|
191
|
+
return { $cond: [{ $eq: [{ $type: ref }, 'string'] }, match, false] };
|
|
192
|
+
}
|
|
193
|
+
/** Atlas offers only a similarity threshold, on the index's own scale: converting a distance would mean guessing the metric. */
|
|
194
|
+
function nearRefusal() {
|
|
195
|
+
return new UqlUsageError('$near is not supported on MongoDB: Atlas scores by index-defined similarity, not distance. ' +
|
|
196
|
+
"Project the score with $sort's $project and filter on it instead.");
|
|
152
197
|
}
|
|
153
198
|
/** MongoDB native operators - pass through as-is. */
|
|
154
199
|
const NATIVE_OPS = new Set([
|
|
@@ -203,34 +248,30 @@ export class MongoDialect extends AbstractDialect {
|
|
|
203
248
|
/**
|
|
204
249
|
* The stages every pipeline starts with: the `$lookup` each relation condition needs, into a temporary
|
|
205
250
|
* field so `$or` keeps its meaning, the `$match` reading them, and the temporaries taken back out. Each
|
|
206
|
-
* relation aggregate the `$where` or `
|
|
251
|
+
* relation aggregate the `$where` or `fields` reads is left on the document, once, under its column.
|
|
252
|
+
* `lookup` keeps the temporaries used, so a later lookup neither repeats nor reuses one.
|
|
207
253
|
*/
|
|
208
|
-
matchStages(entity, where = {}, opts = {},
|
|
254
|
+
matchStages(entity, where = {}, opts = {}, fields = [], lookup = { stages: [], temps: [] }) {
|
|
209
255
|
const meta = getMeta(entity);
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
this.appendAggregateField(meta, key, lookups);
|
|
256
|
+
for (const key of fields) {
|
|
257
|
+
this.appendAggregateField(meta, key, lookup);
|
|
213
258
|
}
|
|
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
|
-
];
|
|
259
|
+
const filter = this.renderFilter(entity, this.scopedWhere(meta, where, opts), lookup);
|
|
260
|
+
return [...lookup.stages, ...(hasKeys(filter) ? [{ $match: filter }] : []), ...unsetOf(lookup.temps)];
|
|
220
261
|
}
|
|
221
262
|
/**
|
|
222
263
|
* Renders a `$where` tree without applying entity filters (used for same-scope group-operator
|
|
223
264
|
* recursion). A relation, or a relation aggregate, needs `$lookup` stages, so it is only accepted
|
|
224
|
-
* when `
|
|
265
|
+
* when `lookup` is given - a plain `find`/`updateMany` filter has nowhere to put them.
|
|
225
266
|
*/
|
|
226
|
-
renderFilter(entity, where = {},
|
|
267
|
+
renderFilter(entity, where = {}, lookup) {
|
|
227
268
|
const meta = getMeta(entity);
|
|
228
269
|
const filter = {};
|
|
229
270
|
for (const [rawKey, rawVal] of Object.entries(where)) {
|
|
230
271
|
let key = rawKey;
|
|
231
272
|
let val = rawVal;
|
|
232
273
|
if (isGroupOp(key)) {
|
|
233
|
-
this.appendLogicalOperator(filter, entity, key, val,
|
|
274
|
+
this.appendLogicalOperator(filter, entity, key, val, lookup);
|
|
234
275
|
}
|
|
235
276
|
else if (key === '$text') {
|
|
236
277
|
// MongoDB's text index declares which fields it covers, so `$fields` cannot narrow the search
|
|
@@ -240,19 +281,19 @@ export class MongoDialect extends AbstractDialect {
|
|
|
240
281
|
}
|
|
241
282
|
else if (meta.relations[key]) {
|
|
242
283
|
assertNoRaw(val, '$where');
|
|
243
|
-
if (!
|
|
284
|
+
if (!lookup) {
|
|
244
285
|
throw new UqlUsageError(`filtering by relation '${key}' is not supported here on MongoDB`);
|
|
245
286
|
}
|
|
246
|
-
|
|
287
|
+
andExpr(filter, this.relationExpression(meta, key, val, lookup));
|
|
247
288
|
}
|
|
248
289
|
else {
|
|
249
290
|
assertNoRaw(val, '$where');
|
|
250
291
|
this.assertKnownPathRoot(meta, key);
|
|
251
292
|
if (aggregateOf(meta.fields[key])) {
|
|
252
|
-
if (!
|
|
293
|
+
if (!lookup) {
|
|
253
294
|
throw new UqlUsageError(`filtering by relation aggregate '${key}' is not supported here on MongoDB`);
|
|
254
295
|
}
|
|
255
|
-
this.appendAggregateField(meta, key,
|
|
296
|
+
this.appendAggregateField(meta, key, lookup);
|
|
256
297
|
}
|
|
257
298
|
const keyed = this.holdsKeys(meta, key);
|
|
258
299
|
key = this.pathOf(meta, key);
|
|
@@ -271,7 +312,7 @@ export class MongoDialect extends AbstractDialect {
|
|
|
271
312
|
filter[key] = operators(val);
|
|
272
313
|
}
|
|
273
314
|
else {
|
|
274
|
-
andExpr(filter, arraySize(key
|
|
315
|
+
andExpr(filter, arraySize(`$${key}`, size));
|
|
275
316
|
if (hasKeys(ops)) {
|
|
276
317
|
filter[key] = operators(ops);
|
|
277
318
|
}
|
|
@@ -284,12 +325,12 @@ export class MongoDialect extends AbstractDialect {
|
|
|
284
325
|
* Renders `$and`/`$or`/`$not`/`$nor` into `filter`, both negations as MongoDB's `$nor` (a `$not`'s clauses
|
|
285
326
|
* wrapped in one `$and`), dropping empty clauses, since MongoDB refuses an empty operator.
|
|
286
327
|
*/
|
|
287
|
-
appendLogicalOperator(filter, entity, key, val,
|
|
328
|
+
appendLogicalOperator(filter, entity, key, val, lookup) {
|
|
288
329
|
const { join, negate } = GROUP_OPS[key];
|
|
289
330
|
const parts = groupClauses(key, val)
|
|
290
331
|
.map((filterIt) => {
|
|
291
332
|
assertNoRaw(filterIt, '$where');
|
|
292
|
-
return this.renderFilter(entity, filterIt,
|
|
333
|
+
return this.renderFilter(entity, filterIt, lookup);
|
|
293
334
|
})
|
|
294
335
|
.filter((part) => Object.keys(part).length > 0);
|
|
295
336
|
if (!parts.length) {
|
|
@@ -303,24 +344,19 @@ export class MongoDialect extends AbstractDialect {
|
|
|
303
344
|
filter['$nor'] = [...(filter['$nor'] ?? []), ...negated];
|
|
304
345
|
}
|
|
305
346
|
/**
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
347
|
+
* One relation condition as an expression over the correlated `$lookup` it adds to `lookup`: a row exists
|
|
348
|
+
* for a plain filter, the row count compared for a `$size`. The target's (and, for ManyToMany, the
|
|
349
|
+
* junction's) own filters scope the lookup, so a relation subquery can no more read out-of-scope rows
|
|
350
|
+
* than a direct query on the target can.
|
|
310
351
|
*/
|
|
311
|
-
|
|
312
|
-
const temp = `${REL_TEMP_PREFIX}${lookups.temps.length}`;
|
|
352
|
+
relationExpression(meta, relKey, val, lookup) {
|
|
313
353
|
const sizeVal = parseRelationSize(val);
|
|
354
|
+
const temp = relTempField('cond', lookup.temps.length);
|
|
314
355
|
const tail = sizeVal === undefined ? [{ $limit: 1 }] : [{ $count: AGGREGATE_VALUE_ALIAS }];
|
|
315
356
|
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
|
-
}
|
|
357
|
+
lookup.temps.push(temp);
|
|
358
|
+
lookup.stages.push(this.relationLookup(meta, meta.relations[relKey], where, temp, tail));
|
|
359
|
+
return sizeVal === undefined ? { $gt: [{ $size: `$${temp}` }, 0] } : compareCount(this.tally(temp), sizeVal);
|
|
324
360
|
}
|
|
325
361
|
/**
|
|
326
362
|
* The correlated `$lookup` for the target rows of one relation `where` narrows, as `temp`: straight at
|
|
@@ -402,8 +438,9 @@ export class MongoDialect extends AbstractDialect {
|
|
|
402
438
|
result[op] = this.held(val);
|
|
403
439
|
continue;
|
|
404
440
|
}
|
|
405
|
-
// `$not` wraps a condition too
|
|
406
|
-
|
|
441
|
+
// `$not` wraps a condition too: a value or a list as the operator it means, which native `$not` refuses,
|
|
442
|
+
// and a uql-only operator inside it (`$isNull`, `$startsWith`) mapped.
|
|
443
|
+
if (op === '$not') {
|
|
407
444
|
result[op] = this.transformOperators(val, refusal);
|
|
408
445
|
continue;
|
|
409
446
|
}
|
|
@@ -441,10 +478,7 @@ export class MongoDialect extends AbstractDialect {
|
|
|
441
478
|
result[val ? '$ne' : '$eq'] = null;
|
|
442
479
|
break;
|
|
443
480
|
case '$near':
|
|
444
|
-
|
|
445
|
-
// reads: converting a distance would mean guessing the metric, so this refuses.
|
|
446
|
-
throw new UqlUsageError('$near is not supported on MongoDB: Atlas scores by index-defined similarity, not distance. ' +
|
|
447
|
-
"Project the score with $sort's $project and filter on it instead.");
|
|
481
|
+
throw nearRefusal();
|
|
448
482
|
}
|
|
449
483
|
}
|
|
450
484
|
return result;
|
|
@@ -614,8 +648,8 @@ export class MongoDialect extends AbstractDialect {
|
|
|
614
648
|
const meta = getMeta(entity);
|
|
615
649
|
const specs = Object.entries(sort ?? {}).flatMap(([key, value]) => meta.relations[key] ? relationSortTerms(key, key, value).aggregates.map(({ spec }) => spec) : []);
|
|
616
650
|
const fields = specs.map(sortAggregateField);
|
|
617
|
-
const
|
|
618
|
-
return {
|
|
651
|
+
const lookups = specs.map((spec, index) => this.aggregateStages(meta, spec, relTempField('sort', fields[index]), fields[index]));
|
|
652
|
+
return { ...mergeLookups(lookups), fields };
|
|
619
653
|
}
|
|
620
654
|
/** Whether a read answers with a relation aggregate, which only the pipeline can build. */
|
|
621
655
|
readsAggregates(entity, q) {
|
|
@@ -632,14 +666,15 @@ export class MongoDialect extends AbstractDialect {
|
|
|
632
666
|
* {@link aggregateStages} builds from the same spec SQL renders as a subquery. Once however many
|
|
633
667
|
* clauses read it; a key computing none adds nothing.
|
|
634
668
|
*/
|
|
635
|
-
appendAggregateField(meta, key,
|
|
669
|
+
appendAggregateField(meta, key, lookup) {
|
|
636
670
|
const spec = aggregateOf(meta.fields[key]);
|
|
637
|
-
const temp =
|
|
638
|
-
if (!spec ||
|
|
671
|
+
const temp = relTempField('field', key);
|
|
672
|
+
if (!spec || lookup.temps.includes(temp)) {
|
|
639
673
|
return;
|
|
640
674
|
}
|
|
641
|
-
|
|
642
|
-
|
|
675
|
+
const { stages, temps } = this.aggregateStages(meta, spec, temp, this.columnOf(meta, key));
|
|
676
|
+
lookup.stages.push(...stages);
|
|
677
|
+
lookup.temps.push(...temps);
|
|
643
678
|
}
|
|
644
679
|
/**
|
|
645
680
|
* One relation aggregate on the document under `field`: the correlated lookup that reads the related
|
|
@@ -673,10 +708,13 @@ export class MongoDialect extends AbstractDialect {
|
|
|
673
708
|
}
|
|
674
709
|
: { $count: AGGREGATE_VALUE_ALIAS },
|
|
675
710
|
];
|
|
676
|
-
return
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
711
|
+
return {
|
|
712
|
+
stages: [
|
|
713
|
+
this.relationLookup(meta, relOpts, spec.where ?? {}, temp, tail),
|
|
714
|
+
{ $addFields: { [field]: this.tally(temp, spec.op) } },
|
|
715
|
+
],
|
|
716
|
+
temps: [temp],
|
|
717
|
+
};
|
|
680
718
|
}
|
|
681
719
|
/**
|
|
682
720
|
* The value a lookup left in `temp`, which holds no row at all where nothing matched: `0` for the
|
|
@@ -693,25 +731,21 @@ export class MongoDialect extends AbstractDialect {
|
|
|
693
731
|
*/
|
|
694
732
|
relationReadStages(entity, q) {
|
|
695
733
|
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);
|
|
734
|
+
const toMany = getRelationRequestSummary(meta, q.$populate).toManyKeys.map((relKey) => this.toManyLookup(meta, relKey, parseRelationAtKey(relKey, q.$populate).query));
|
|
735
|
+
const counts = countedRelations(meta, q.$count).map(({ relKey, where }) => {
|
|
736
|
+
const temp = relTempField('count', relKey);
|
|
704
737
|
const spec = { relation: relKey, op: '$count', where };
|
|
705
|
-
|
|
706
|
-
}
|
|
707
|
-
|
|
738
|
+
return this.aggregateStages(meta, spec, temp, `${COUNT_RESULT_KEY}.${relKey}`);
|
|
739
|
+
});
|
|
740
|
+
const { stages, temps } = mergeLookups([...toMany, ...counts]);
|
|
741
|
+
return [...stages, ...unsetOf(temps)];
|
|
708
742
|
}
|
|
709
743
|
/**
|
|
710
744
|
* A to-many's rows as a lookup running their own read, whose filters, ordering and page apply per
|
|
711
745
|
* parent inside it. A many-to-many reads its targets, each once, by the ids its junction pairs the
|
|
712
746
|
* parent with, parked in a temporary field of the parent's.
|
|
713
747
|
*/
|
|
714
|
-
toManyLookup(meta, relKey, query
|
|
748
|
+
toManyLookup(meta, relKey, query) {
|
|
715
749
|
const relOpts = relationOf(meta, relKey);
|
|
716
750
|
const relEntity = relOpts.entity();
|
|
717
751
|
const relMeta = getMeta(relEntity);
|
|
@@ -719,38 +753,36 @@ export class MongoDialect extends AbstractDialect {
|
|
|
719
753
|
const pipeline = read.length ? { pipeline: read } : {};
|
|
720
754
|
const from = this.resolveTableName(relMeta);
|
|
721
755
|
if (!relOpts.through) {
|
|
722
|
-
return
|
|
756
|
+
return {
|
|
757
|
+
stages: [
|
|
758
|
+
{ $lookup: { from, ...this.joinKeys(meta, relMeta, relOpts), ...pipeline, as: relationField(relKey) } },
|
|
759
|
+
],
|
|
760
|
+
temps: [],
|
|
761
|
+
};
|
|
762
|
+
}
|
|
763
|
+
const junction = this.junctionOf(meta, relOpts, relMeta, relOpts.through());
|
|
764
|
+
const temp = relTempField('rel', relKey);
|
|
765
|
+
return {
|
|
766
|
+
temps: [temp],
|
|
767
|
+
stages: [
|
|
768
|
+
{
|
|
769
|
+
$lookup: {
|
|
770
|
+
...junction.lookup,
|
|
771
|
+
pipeline: [...junction.scope, { $project: { [junction.target]: 1 } }],
|
|
772
|
+
as: temp,
|
|
773
|
+
},
|
|
774
|
+
},
|
|
723
775
|
{
|
|
724
776
|
$lookup: {
|
|
725
777
|
from,
|
|
726
|
-
|
|
778
|
+
localField: `${temp}.${junction.target}`,
|
|
779
|
+
foreignField: ID_KEY,
|
|
727
780
|
...pipeline,
|
|
728
781
|
as: relationField(relKey),
|
|
729
782
|
},
|
|
730
783
|
},
|
|
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
|
-
];
|
|
784
|
+
],
|
|
785
|
+
};
|
|
754
786
|
}
|
|
755
787
|
/** Whether a `$sort` reads a relation, which is what forces the lookups to run before it. */
|
|
756
788
|
sortsRelations(entity, sort) {
|
|
@@ -835,8 +867,13 @@ export class MongoDialect extends AbstractDialect {
|
|
|
835
867
|
// which is the one way this differs from a SQL join. Taken back out once the `$sort` that needed
|
|
836
868
|
// it has run, so ordering by an unpopulated relation costs the same nothing it does there.
|
|
837
869
|
const sortOnly = [...joins.values()].filter((join) => !join.projected);
|
|
838
|
-
const dropped = [
|
|
839
|
-
|
|
870
|
+
const dropped = [
|
|
871
|
+
...sortOnly.map((join) => joinedPath(join)),
|
|
872
|
+
...aggregated.temps,
|
|
873
|
+
...aggregated.fields,
|
|
874
|
+
...ordering.fields,
|
|
875
|
+
];
|
|
876
|
+
const unset = unsetOf(dropped);
|
|
840
877
|
// The grouping collapses rows onto the columns it projects, which leaves nothing for an ordering
|
|
841
878
|
// that reads a lookup those columns do not carry. Refused rather than answered all-equal, and in
|
|
842
879
|
// the same terms the SQL dialects refuse `SELECT DISTINCT` ordered by an unselected column.
|
|
@@ -1114,7 +1151,7 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1114
1151
|
assignments[key] = { $literal: value };
|
|
1115
1152
|
}
|
|
1116
1153
|
}
|
|
1117
|
-
return [...(hasKeys(assignments) ? [{ $set: assignments }] : []), ...(unset
|
|
1154
|
+
return [...(hasKeys(assignments) ? [{ $set: assignments }] : []), ...unsetOf(unset)];
|
|
1118
1155
|
}
|
|
1119
1156
|
/** Refuses a key left to MongoDB that it cannot mint: only an `ObjectId`, read back as a string, so not a `Number`. */
|
|
1120
1157
|
assertMintableKey(meta, field) {
|
|
@@ -1159,9 +1196,13 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1159
1196
|
buildAggregateStages(entity, q, opts) {
|
|
1160
1197
|
const meta = getMeta(entity);
|
|
1161
1198
|
const { joins, where } = resolveGroupJoins(meta, q);
|
|
1162
|
-
const {
|
|
1199
|
+
const matched = { stages: [], temps: [] };
|
|
1200
|
+
const match = this.matchStages(entity, where, opts, [], matched);
|
|
1201
|
+
const { groupId, accumulators, columns, lookups } = this.buildGroupSpec(meta, parseGroupMap(q.$group, q.$select), joins, matched.temps);
|
|
1163
1202
|
const pipeline = [
|
|
1164
|
-
...
|
|
1203
|
+
...match,
|
|
1204
|
+
// What the `$group` reads off each matched row through a lookup, the temporaries it then drops.
|
|
1205
|
+
...lookups,
|
|
1165
1206
|
...this.lookupStages(meta, joins),
|
|
1166
1207
|
{ $group: { _id: hasKeys(groupId) ? groupId : null, ...accumulators } },
|
|
1167
1208
|
// `$group` answers with `_id` even when grouping by nothing, and with what `columns` read to the end.
|
|
@@ -1192,29 +1233,30 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1192
1233
|
/**
|
|
1193
1234
|
* Resolve parsed group entries into the `_id` keys and accumulators of a `$group` stage, and the
|
|
1194
1235
|
* `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
|
-
*
|
|
1236
|
+
* size, and a `$sum` as null where it read no value, as SQL answers it. `lookups` puts on each matched
|
|
1237
|
+
* row what the group reads through one: a relation aggregate, and an aggregate's relation condition,
|
|
1238
|
+
* past the `matched` temporaries, whose relation aggregates are on the row already.
|
|
1197
1239
|
*/
|
|
1198
|
-
buildGroupSpec(meta, groupEntries, joins) {
|
|
1240
|
+
buildGroupSpec(meta, groupEntries, joins, matched) {
|
|
1199
1241
|
if (!groupEntries.length) {
|
|
1200
1242
|
throw new UqlUsageError('aggregate requires at least one $group column or $select function');
|
|
1201
1243
|
}
|
|
1202
1244
|
const groupId = {};
|
|
1203
1245
|
const accumulators = {};
|
|
1204
1246
|
const columns = {};
|
|
1205
|
-
const
|
|
1247
|
+
const lookup = { stages: [], temps: [...matched] };
|
|
1206
1248
|
for (const entry of groupEntries) {
|
|
1207
1249
|
// Aliases stay as the caller wrote them ($project maps them back); the *refs* address columns.
|
|
1208
1250
|
if (entry.kind === 'key') {
|
|
1209
|
-
groupId[entry.alias] = `$${this.groupedPath(meta, joins, entry.path,
|
|
1251
|
+
groupId[entry.alias] = `$${this.groupedPath(meta, joins, entry.path, lookup)}`;
|
|
1210
1252
|
columns[entry.alias] = `$_id.${entry.alias}`;
|
|
1211
1253
|
continue;
|
|
1212
1254
|
}
|
|
1213
1255
|
const { field } = entry;
|
|
1214
1256
|
if (field !== undefined) {
|
|
1215
|
-
|
|
1257
|
+
this.appendAggregateField(meta, field, lookup);
|
|
1216
1258
|
}
|
|
1217
|
-
const test = entry.where && this.whereExpression(meta, entry.where,
|
|
1259
|
+
const test = entry.where && this.whereExpression(meta, entry.where, lookup);
|
|
1218
1260
|
// What the accumulator reads from a row its own `$where` passes, and from one it does not.
|
|
1219
1261
|
const read = (passed, failed) => (test ? { $cond: [test, passed, failed] } : passed);
|
|
1220
1262
|
columns[entry.alias] = 1;
|
|
@@ -1242,13 +1284,13 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1242
1284
|
columns[entry.alias] = { $cond: [{ $eq: [`$${counted}`, 0] }, null, `$${entry.alias}`] };
|
|
1243
1285
|
}
|
|
1244
1286
|
}
|
|
1245
|
-
return { groupId, accumulators, columns,
|
|
1287
|
+
return { groupId, accumulators, columns, lookups: lookup.stages };
|
|
1246
1288
|
}
|
|
1247
1289
|
/** The field a grouped path reads, on the document or on the joined one its lookup unwound. */
|
|
1248
|
-
groupedPath(meta, joins, path,
|
|
1290
|
+
groupedPath(meta, joins, path, lookup) {
|
|
1249
1291
|
const { key, join } = groupPathField(joins, path);
|
|
1250
1292
|
if (!join) {
|
|
1251
|
-
|
|
1293
|
+
this.appendAggregateField(meta, key, lookup);
|
|
1252
1294
|
return this.columnOf(meta, key);
|
|
1253
1295
|
}
|
|
1254
1296
|
if (aggregateOf(join.meta.fields[key])) {
|
|
@@ -1257,67 +1299,116 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1257
1299
|
return `${joinedPath(join)}.${this.columnOf(join.meta, key)}`;
|
|
1258
1300
|
}
|
|
1259
1301
|
/**
|
|
1260
|
-
* An aggregate's own `$where` as the expression a `$cond` tests,
|
|
1261
|
-
*
|
|
1262
|
-
* the
|
|
1302
|
+
* An aggregate's own `$where` as the expression a `$cond` tests, matching the rows {@link renderFilter}
|
|
1303
|
+
* would: a `$match` cannot sit inside a `$group`, and `$text`, bound to the pipeline's first stage, is
|
|
1304
|
+
* refused by name. `lookup` gathers the lookups it tests: a relation condition's, and a relation aggregate's.
|
|
1263
1305
|
*/
|
|
1264
|
-
whereExpression(meta, where,
|
|
1306
|
+
whereExpression(meta, where, lookup) {
|
|
1265
1307
|
const terms = getKeys(where).map((key) => {
|
|
1266
1308
|
if (isGroupOp(key)) {
|
|
1267
1309
|
const { join, negate } = GROUP_OPS[key];
|
|
1268
1310
|
const clauses = groupClauses(key, where[key]).map((clause) => {
|
|
1269
1311
|
assertNoRaw(clause, 'an aggregate $where');
|
|
1270
|
-
return this.whereExpression(meta, clause,
|
|
1312
|
+
return this.whereExpression(meta, clause, lookup);
|
|
1271
1313
|
});
|
|
1272
1314
|
return negate ? { $not: [{ [join]: clauses }] } : { [join]: clauses };
|
|
1273
1315
|
}
|
|
1316
|
+
const val = where[key];
|
|
1317
|
+
assertNoRaw(val, 'an aggregate $where');
|
|
1318
|
+
if (meta.relations[key]) {
|
|
1319
|
+
return this.relationExpression(meta, key, val, lookup);
|
|
1320
|
+
}
|
|
1274
1321
|
if (key.startsWith('$')) {
|
|
1275
1322
|
throw new UqlUsageError(`aggregate $where operator '${key}' is not supported on MongoDB`);
|
|
1276
1323
|
}
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
const wire = this.holdsKeys(meta, key) ? (value) => this.toWireId(value) :
|
|
1324
|
+
this.assertKnownPathRoot(meta, key);
|
|
1325
|
+
this.appendAggregateField(meta, key, lookup);
|
|
1326
|
+
const wire = this.holdsKeys(meta, key) ? (value) => this.toWireId(value) : undefined;
|
|
1280
1327
|
return this.fieldExpression(`$${this.pathOf(meta, key)}`, val, wire);
|
|
1281
1328
|
});
|
|
1282
|
-
return terms
|
|
1329
|
+
return allOf(terms);
|
|
1283
1330
|
}
|
|
1284
|
-
/**
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1331
|
+
/**
|
|
1332
|
+
* One field's condition as an expression, read as {@link transformOperators} reads it in a query: a value
|
|
1333
|
+
* it equals, a list it is in, or operators. `wire` converts what a key field is compared with.
|
|
1334
|
+
*/
|
|
1335
|
+
fieldExpression(ref, val, wire = (value) => value) {
|
|
1336
|
+
const equals = (value) => (value === null ? isNullExpr(ref) : { $eq: [ref, literal(wire(value))] });
|
|
1337
|
+
const within = (op, values) => ({
|
|
1338
|
+
$in: [{ $ifNull: [ref, null] }, literal(wire(inOperands(op, values)))],
|
|
1339
|
+
});
|
|
1340
|
+
const ordered = (op, operand) => {
|
|
1341
|
+
const value = wire(operand);
|
|
1342
|
+
return { $and: [sameType(ref, value), { [op]: [ref, literal(value)] }] };
|
|
1343
|
+
};
|
|
1344
|
+
if (!isOperatorObject(val)) {
|
|
1345
|
+
return Array.isArray(val) ? within('$in', val) : equals(val);
|
|
1289
1346
|
}
|
|
1290
|
-
// A null or missing field compares below every value in an expression, where SQL leaves it unmatched.
|
|
1291
|
-
const present = { $not: [isNullExpr(ref)] };
|
|
1292
1347
|
const terms = Object.entries(val).map(([op, operand]) => {
|
|
1348
|
+
const like = LIKE_OPS.get(op);
|
|
1349
|
+
if (like) {
|
|
1350
|
+
return regexMatch(ref, likeRegex(like.pattern(String(operand))), like.insensitive);
|
|
1351
|
+
}
|
|
1293
1352
|
switch (op) {
|
|
1294
1353
|
case '$eq':
|
|
1295
1354
|
return equals(operand);
|
|
1296
1355
|
case '$ne':
|
|
1297
1356
|
return { $not: [equals(operand)] };
|
|
1357
|
+
case '$not':
|
|
1358
|
+
return { $not: [this.fieldExpression(ref, operand, wire)] };
|
|
1298
1359
|
case '$gt':
|
|
1299
1360
|
case '$gte':
|
|
1300
|
-
return { [op]: [ref, wire(operand)] };
|
|
1301
1361
|
case '$lt':
|
|
1302
1362
|
case '$lte':
|
|
1303
|
-
return
|
|
1304
|
-
case '$in':
|
|
1305
|
-
return { $in: [ref, wire(operand)] };
|
|
1306
|
-
case '$nin':
|
|
1307
|
-
return { $not: [{ $in: [ref, wire(operand)] }] };
|
|
1363
|
+
return ordered(op, operand);
|
|
1308
1364
|
case '$between': {
|
|
1309
|
-
const [min, max] = operand;
|
|
1310
|
-
return { $and: [
|
|
1365
|
+
const [min, max] = betweenBounds(operand);
|
|
1366
|
+
return { $and: [ordered('$gte', min), ordered('$lte', max)] };
|
|
1311
1367
|
}
|
|
1368
|
+
case '$in':
|
|
1369
|
+
return within(op, operand);
|
|
1370
|
+
case '$nin':
|
|
1371
|
+
return { $not: [within(op, operand)] };
|
|
1312
1372
|
case '$isNull':
|
|
1313
|
-
return operand ? isNullExpr(ref) : present;
|
|
1314
1373
|
case '$isNotNull':
|
|
1315
|
-
return operand ?
|
|
1374
|
+
return (op === '$isNull') === Boolean(operand) ? equals(null) : { $not: [equals(null)] };
|
|
1375
|
+
case '$regex':
|
|
1376
|
+
return regexMatch(ref, String(operand));
|
|
1377
|
+
case '$size':
|
|
1378
|
+
return arraySize(ref, operand);
|
|
1379
|
+
case '$all':
|
|
1380
|
+
return this.allHeld(ref, inOperands(op, operand));
|
|
1381
|
+
case '$elemMatch':
|
|
1382
|
+
return this.allHeld(ref, [operand]);
|
|
1383
|
+
case '$near':
|
|
1384
|
+
throw nearRefusal();
|
|
1316
1385
|
default:
|
|
1317
|
-
throw new UqlUsageError(`
|
|
1386
|
+
throw new UqlUsageError(`unknown operator: ${op}`);
|
|
1318
1387
|
}
|
|
1319
1388
|
});
|
|
1320
|
-
return terms
|
|
1389
|
+
return allOf(terms);
|
|
1390
|
+
}
|
|
1391
|
+
/**
|
|
1392
|
+
* What `ref` satisfies to hold `value`, as {@link held} reads it in a query: an array each of its
|
|
1393
|
+
* elements, an object each of its keys as a path, anything else as a field condition.
|
|
1394
|
+
*/
|
|
1395
|
+
heldExpression(ref, value) {
|
|
1396
|
+
if (Array.isArray(value)) {
|
|
1397
|
+
return this.allHeld(ref, value);
|
|
1398
|
+
}
|
|
1399
|
+
if (isJsonObject(value)) {
|
|
1400
|
+
return allOf(Object.entries(value).map(([key, item]) => this.heldExpression(`${ref}.${key}`, item)));
|
|
1401
|
+
}
|
|
1402
|
+
return this.fieldExpression(ref, value);
|
|
1403
|
+
}
|
|
1404
|
+
/**
|
|
1405
|
+
* `$all`, as {@link allHolding} reads it: the array at `ref` has an element holding each value, a scalar
|
|
1406
|
+
* by `$in`, which stops at the first match where testing each element would map them all.
|
|
1407
|
+
*/
|
|
1408
|
+
allHeld(ref, values) {
|
|
1409
|
+
return allOf(values.map((value) => isOperatorMap(value) || Array.isArray(value)
|
|
1410
|
+
? anyElement(ref, this.heldExpression('$$this', value))
|
|
1411
|
+
: { $cond: [{ $isArray: ref }, { $in: [literal(value), ref] }, false] }));
|
|
1321
1412
|
}
|
|
1322
1413
|
buildHavingFilter(having) {
|
|
1323
1414
|
const filter = {};
|
|
@@ -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.4",
|
|
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",
|