uql-orm 0.102.3 → 0.102.5
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/dialect/abstractSqlDialect.d.ts +3 -4
- package/dist/dialect/abstractSqlDialect.js +18 -23
- package/dist/migrate/migrator.d.ts +2 -2
- package/dist/migrate/migrator.js +9 -5
- package/dist/migrate/schemaChange.d.ts +10 -0
- package/dist/migrate/schemaChange.js +17 -0
- package/dist/migrate/schemaGenerator.js +8 -4
- package/dist/migrate/triggerSql.d.ts +2 -0
- package/dist/migrate/triggerSql.js +14 -0
- package/dist/mongodb/mongoDialect.d.ts +17 -4
- package/dist/mongodb/mongoDialect.js +130 -47
- package/dist/type/migration.d.ts +1 -1
- package/dist/util/sql.d.ts +6 -1
- package/dist/util/sql.js +9 -4
- package/package.json +1 -1
|
@@ -287,10 +287,9 @@ export declare abstract class AbstractSqlDialect extends VectorSqlDialect implem
|
|
|
287
287
|
*/
|
|
288
288
|
protected resolveOperandField<E>(ctx: QueryContext, entity: Type<E>, key: string, opts: QueryRenderOptions): string;
|
|
289
289
|
/**
|
|
290
|
-
* The expression an inlined computed field stands for, or nothing when the field is a real column.
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
* alias exists only when the field was also selected, which `$where` and `$sort` cannot assume.
|
|
290
|
+
* The expression an inlined computed field stands for, or nothing when the field is a real column. Every
|
|
291
|
+
* clause names it by the expression, never the output alias, which exists only when it was selected; and
|
|
292
|
+
* qualified even when nothing else is, since one opening a correlated subquery has the inner table in scope.
|
|
294
293
|
*/
|
|
295
294
|
private inlinedOperand;
|
|
296
295
|
/** {@link fieldCondition}, appended. */
|
|
@@ -2,7 +2,7 @@ import { fieldOf, getMeta, relationOf } from '../entity/index.js';
|
|
|
2
2
|
import { COUNT_RESULT_KEY, parseQueryLock, QuerySql, RAW_ALIAS, } from '../type/index.js';
|
|
3
3
|
import { utcTimestamp } from '../util/date.js';
|
|
4
4
|
import { ancestorPaths, isExactDecimal, isInlinedExpression, jsonPathKeys } from '../util/field.util.js';
|
|
5
|
-
import { isSelectList, assertNonNegativeInteger, assertNoUndefined, assertWhere, definedEntries, escapeSqlId, fillOnFields, filterFieldKeys, getInsertFieldKeys, getKeys, getRelationRequestSummary, getSoftDeleteValue, hasKeys, idOnlyQuery, keySet, whereAnd, columnFamily, fieldFamily, countedRelations, fieldUpdateOf, fulltextIndexOver, fulltextWeights, isFieldUpdateOp, isJsonObject, isJsonUpdateOp, foldJson, writesKeys, isOperatorMap, isOperatorKey, isVectorSearch, normalizeScalarFieldSelection, parentJoins, rankedTextSearch, targetJoins, textSearchFields, textSortOf, textWeightSteps, parseGroupMap, parseRelationAtKey, parseRelationSize, parseSortDirection, populatesRelations, aggregateOf, sql, refs, throwUnknownAggregateColumn, withoutSoftDeleteFilter, } from '../util/index.js';
|
|
5
|
+
import { isSelectList, assertNonNegativeInteger, assertNoUndefined, assertWhere, definedEntries, escapeSqlId, fillOnFields, filterFieldKeys, getInsertFieldKeys, getKeys, getRelationRequestSummary, getSoftDeleteValue, hasKeys, idOnlyQuery, keySet, whereAnd, columnFamily, fieldFamily, countedRelations, fieldUpdateOf, fulltextIndexOver, fulltextWeights, isFieldUpdateOp, isJsonObject, isJsonUpdateOp, foldJson, writesKeys, isOperatorMap, isOperatorKey, isVectorSearch, normalizeScalarFieldSelection, parentJoins, rankedTextSearch, targetJoins, textSearchFields, textSortOf, textWeightSteps, parseGroupMap, parseRelationAtKey, parseRelationSize, parseSortDirection, populatesRelations, aggregateOf, sql, refs, columnRef, throwUnknownAggregateColumn, withoutSoftDeleteFilter, } from '../util/index.js';
|
|
6
6
|
import { isJsonScalar } from '../util/object.util.js';
|
|
7
7
|
import { escapeAnsiSqlLiteral } from '../util/sqlLiteral.js';
|
|
8
8
|
import { kindOf, UqlUsageError } from '../util/uqlError.js';
|
|
@@ -276,12 +276,9 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
276
276
|
fieldTerm(ctx, meta, key, opts) {
|
|
277
277
|
const field = fieldOf(meta, key);
|
|
278
278
|
const kind = this.fieldKind(meta, field, opts.json);
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
// would bind to *that* table instead of this one.
|
|
283
|
-
const sql = this.rawFragment(ctx, field.computed, opts.prefix ?? this.resolveTableAlias(meta), meta.entity);
|
|
284
|
-
return { sql: opts.json ? this.carried(`(${sql})`, field) : sql, key, kind };
|
|
279
|
+
const inlined = this.inlinedOperand(ctx, meta, key, opts.prefix);
|
|
280
|
+
if (inlined) {
|
|
281
|
+
return { sql: opts.json ? this.carried(inlined, field) : inlined, key, kind };
|
|
285
282
|
}
|
|
286
283
|
const columnName = this.resolveColumnName(key, field);
|
|
287
284
|
const column = this.escapeId(opts.prefix, true, true) + this.escapeId(columnName);
|
|
@@ -636,18 +633,18 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
636
633
|
resolveOperandField(ctx, entity, key, opts) {
|
|
637
634
|
const meta = getMeta(entity);
|
|
638
635
|
const field = meta.fields[key];
|
|
639
|
-
return
|
|
640
|
-
this.columnWithPrefix(key, field, opts));
|
|
636
|
+
return this.inlinedOperand(ctx, meta, key, opts.prefix) ?? this.columnWithPrefix(key, field, opts);
|
|
641
637
|
}
|
|
642
638
|
/**
|
|
643
|
-
* The expression an inlined computed field stands for, or nothing when the field is a real column.
|
|
644
|
-
*
|
|
645
|
-
*
|
|
646
|
-
* alias exists only when the field was also selected, which `$where` and `$sort` cannot assume.
|
|
639
|
+
* The expression an inlined computed field stands for, or nothing when the field is a real column. Every
|
|
640
|
+
* clause names it by the expression, never the output alias, which exists only when it was selected; and
|
|
641
|
+
* qualified even when nothing else is, since one opening a correlated subquery has the inner table in scope.
|
|
647
642
|
*/
|
|
648
|
-
inlinedOperand(ctx,
|
|
649
|
-
const
|
|
650
|
-
return
|
|
643
|
+
inlinedOperand(ctx, meta, key, prefix) {
|
|
644
|
+
const field = meta.fields[key];
|
|
645
|
+
return field && isInlinedExpression(field)
|
|
646
|
+
? this.rawFragment(ctx, columnRef(meta.entity, key), prefix ?? this.resolveTableAlias(meta), meta.entity)
|
|
647
|
+
: undefined;
|
|
651
648
|
}
|
|
652
649
|
/** {@link fieldCondition}, appended. */
|
|
653
650
|
compareFieldOperator(ctx, entity, key, op, val, opts = {}) {
|
|
@@ -947,8 +944,7 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
947
944
|
sortColumns(ctx, meta, key, prefix) {
|
|
948
945
|
const field = meta.fields[key];
|
|
949
946
|
if (field) {
|
|
950
|
-
const expr = this.inlinedOperand(ctx,
|
|
951
|
-
this.columnWithPrefix(key, field, { prefix });
|
|
947
|
+
const expr = this.inlinedOperand(ctx, meta, key, prefix) ?? this.columnWithPrefix(key, field, { prefix });
|
|
952
948
|
return [{ expr, output: false }];
|
|
953
949
|
}
|
|
954
950
|
const json = this.resolveJsonDotPath(meta, key, prefix);
|
|
@@ -1134,11 +1130,10 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
1134
1130
|
}
|
|
1135
1131
|
/** A field as an aggregate reads it: its column, or the expression an inlined one stands for. */
|
|
1136
1132
|
aggregateOperand(ctx, entity, key, prefix) {
|
|
1137
|
-
const
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
};
|
|
1133
|
+
const meta = getMeta(entity);
|
|
1134
|
+
const field = meta.fields[key];
|
|
1135
|
+
const inlined = this.inlinedOperand(ctx, meta, key, prefix);
|
|
1136
|
+
return { sql: inlined ?? this.columnWithPrefix(key, field, { prefix }), bare: !inlined };
|
|
1142
1137
|
}
|
|
1143
1138
|
/**
|
|
1144
1139
|
* ORDER BY for aggregate queries - handles both entity-field and alias references. A grouped
|
|
@@ -71,8 +71,8 @@ export declare class Migrator {
|
|
|
71
71
|
generateFromEntities(name: string): Promise<string>;
|
|
72
72
|
/**
|
|
73
73
|
* What a generated migration does that its reader must not miss: each column it drops or retypes, which
|
|
74
|
-
* can lose data, and each table it creates empty while the database holds
|
|
75
|
-
* same columns, which may be the table renamed
|
|
74
|
+
* can lose data, or its rollback puts back nullable; and each table it creates empty while the database holds
|
|
75
|
+
* one no entity names with the same columns, which may be the table renamed, never renamed here.
|
|
76
76
|
*/
|
|
77
77
|
private noteChanges;
|
|
78
78
|
/**
|
package/dist/migrate/migrator.js
CHANGED
|
@@ -13,7 +13,7 @@ import { buildMigrationModule } from './codegen/migrationFile.js';
|
|
|
13
13
|
import { introspectorFor } from './introspection/registry.js';
|
|
14
14
|
import { DEFAULT_LOCK_TIMEOUT } from './migrationLock.js';
|
|
15
15
|
import { migrationBuilderFor, migrationTargetFor, } from './migrationTarget.js';
|
|
16
|
-
import { dropped, lacksValue, newlyRequired, nonEmpty,
|
|
16
|
+
import { dropped, lacksValue, newlyRequired, nonEmpty, rollbackDiff, sides, unfillableRestores, withoutRebuild, } from './schemaChange.js';
|
|
17
17
|
import { constraintNameOf, dropsForeignKeysFirst } from './schemaGenerator.js';
|
|
18
18
|
import { DEFAULT_MIGRATIONS_TABLE } from './storage/databaseStorage.js';
|
|
19
19
|
/**
|
|
@@ -186,7 +186,7 @@ export class Migrator {
|
|
|
186
186
|
}
|
|
187
187
|
// Diff by diff in reverse, each rolled back in the order its generator wrote it.
|
|
188
188
|
const down = [
|
|
189
|
-
...altered.toReversed().flatMap((diff) => generator.generateAlterTable(
|
|
189
|
+
...altered.toReversed().flatMap((diff) => generator.generateAlterTable(rollbackDiff(diff))),
|
|
190
190
|
...generator.generateDropSchema(this.createdEntities(created), { ifExists: true }),
|
|
191
191
|
];
|
|
192
192
|
const { emit } = this.target.source;
|
|
@@ -200,16 +200,20 @@ export class Migrator {
|
|
|
200
200
|
}
|
|
201
201
|
/**
|
|
202
202
|
* What a generated migration does that its reader must not miss: each column it drops or retypes, which
|
|
203
|
-
* can lose data, and each table it creates empty while the database holds
|
|
204
|
-
* same columns, which may be the table renamed
|
|
203
|
+
* can lose data, or its rollback puts back nullable; and each table it creates empty while the database holds
|
|
204
|
+
* one no entity names with the same columns, which may be the table renamed, never renamed here.
|
|
205
205
|
*/
|
|
206
206
|
async noteChanges(generator, created, altered) {
|
|
207
|
-
for (const
|
|
207
|
+
for (const diff of altered) {
|
|
208
|
+
const { tableName, columns = [] } = diff;
|
|
208
209
|
for (const { from, to } of columns.filter((change) => change.isBreaking)) {
|
|
209
210
|
this.logger.logWarn(to
|
|
210
211
|
? `Retypes "${tableName}"."${to.name}" from ${from?.type} to ${to.type}: a value that does not fit is lost or refused.`
|
|
211
212
|
: `Drops "${tableName}"."${from?.name}", losing what it holds.`);
|
|
212
213
|
}
|
|
214
|
+
for (const column of unfillableRestores(diff)) {
|
|
215
|
+
this.logger.logWarn(`Rolls "${tableName}"."${column.name}" back nullable, with nothing to fill it: require it in the down once filled.`);
|
|
216
|
+
}
|
|
213
217
|
}
|
|
214
218
|
for (const { from, to } of await this.renamedTables(generator, created)) {
|
|
215
219
|
this.logger.logWarn(`Creates "${to}" empty, while "${from}", which no entity names, holds the same columns. If it was ` +
|
|
@@ -18,6 +18,16 @@ export declare function lacksValue(column: ColumnSchema): boolean;
|
|
|
18
18
|
export declare function needsRebuild(diff: SchemaDiff): boolean;
|
|
19
19
|
/** `diff` less its rebuild and everything only a rebuild makes: what an `ALTER` can still apply alone. */
|
|
20
20
|
export declare function withoutRebuild(diff: SchemaDiff): SchemaDiff;
|
|
21
|
+
/**
|
|
22
|
+
* The columns `diff` drops that its rollback puts back nullable: required, with nothing to fill the rows. A
|
|
23
|
+
* rebuild restores its table verbatim instead.
|
|
24
|
+
*/
|
|
25
|
+
export declare function unfillableRestores(diff: SchemaDiff): ColumnSchema[];
|
|
26
|
+
/**
|
|
27
|
+
* `diff` as a rollback runs it: reversed, its {@link unfillableRestores} put back nullable, since no engine
|
|
28
|
+
* adds a required column to rows holding none.
|
|
29
|
+
*/
|
|
30
|
+
export declare function rollbackDiff(diff: SchemaDiff): SchemaDiff;
|
|
21
31
|
/** `items`, or nothing where it has none, so an empty change list is left off a diff. */
|
|
22
32
|
export declare function nonEmpty<T>(items: readonly T[]): readonly T[] | undefined;
|
|
23
33
|
/**
|
|
@@ -44,6 +44,23 @@ export function withoutRebuild(diff) {
|
|
|
44
44
|
rebuild: undefined,
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* The columns `diff` drops that its rollback puts back nullable: required, with nothing to fill the rows. A
|
|
49
|
+
* rebuild restores its table verbatim instead.
|
|
50
|
+
*/
|
|
51
|
+
export function unfillableRestores(diff) {
|
|
52
|
+
return diff.rebuild ? [] : dropped(diff.columns).filter(lacksValue);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* `diff` as a rollback runs it: reversed, its {@link unfillableRestores} put back nullable, since no engine
|
|
56
|
+
* adds a required column to rows holding none.
|
|
57
|
+
*/
|
|
58
|
+
export function rollbackDiff(diff) {
|
|
59
|
+
const unfillable = new Set(unfillableRestores(diff));
|
|
60
|
+
const reversed = reverseDiff(diff);
|
|
61
|
+
const columns = reversed.columns?.map((change) => change.to && unfillable.has(change.to) ? { ...change, to: { ...change.to, nullable: true } } : change);
|
|
62
|
+
return { ...reversed, columns };
|
|
63
|
+
}
|
|
47
64
|
/** `items`, or nothing where it has none, so an empty change list is left off a diff. */
|
|
48
65
|
export function nonEmpty(items) {
|
|
49
66
|
return items.length ? items : undefined;
|
|
@@ -17,7 +17,7 @@ import { columnForeignKey, columnIndex, bareColumn, renderIndexDefinition, table
|
|
|
17
17
|
import { indexNodeToSchema } from './generator/indexNodeToSchema.js';
|
|
18
18
|
import { assertIndexPredicate } from './indexPredicate.js';
|
|
19
19
|
import { added, alterations, dropped, needsRebuild, newlyRequired, nonEmpty, sides } from './schemaChange.js';
|
|
20
|
-
import { dropTrigger, dropTriggerBody, renderTrigger, stampTriggers } from './triggerSql.js';
|
|
20
|
+
import { dropTrigger, dropTriggerBody, namesAny, renderTrigger, stampTriggers } from './triggerSql.js';
|
|
21
21
|
/** The SQL schema generator, one for every SQL engine, its spellings taken from the dialect and its DDL classes. */
|
|
22
22
|
export class SqlSchemaGenerator {
|
|
23
23
|
dialect;
|
|
@@ -154,8 +154,12 @@ export class SqlSchemaGenerator {
|
|
|
154
154
|
generateAlterTable(diff) {
|
|
155
155
|
const { tableName, primaryKey, columns, rebuild } = diff;
|
|
156
156
|
const fills = this.defaultFills(columns);
|
|
157
|
-
const moved =
|
|
158
|
-
|
|
157
|
+
const moved = [
|
|
158
|
+
...sides(columns, 'from').map(({ name }) => name),
|
|
159
|
+
...(diff.renamedColumns ?? []).flatMap(({ from, to }) => [from, to]),
|
|
160
|
+
];
|
|
161
|
+
// A rebuild drops every trigger with its table; an alter, only a kept one naming a column it moves.
|
|
162
|
+
const triggers = (diff.triggers ?? []).filter(({ from, to }) => rebuild || !from || !to || to.statements.some((statement) => namesAny(statement, moved)));
|
|
159
163
|
const triggerDrops = terminated(sides(triggers, 'from').flatMap((trigger) => dropTrigger(this.dialect, tableName, trigger.name)));
|
|
160
164
|
const triggerCreates = terminated(sides(triggers, 'to').flatMap((trigger) => trigger.statements));
|
|
161
165
|
if (rebuild) {
|
|
@@ -266,7 +270,7 @@ export class SqlSchemaGenerator {
|
|
|
266
270
|
}))),
|
|
267
271
|
foreignKeys: nonEmpty(foreignKeys),
|
|
268
272
|
checks: nonEmpty(tableDiff?.checks ?? []),
|
|
269
|
-
// A kept trigger as both sides, which the alter cycles only where a column moves under it.
|
|
273
|
+
// A kept trigger as both sides, which the alter cycles only where a column it names moves under it.
|
|
270
274
|
triggers: nonEmpty([
|
|
271
275
|
...(tableDiff?.triggers ?? []),
|
|
272
276
|
...matchByKey(desired.triggers, currentTable.triggers, (it) => it.name).matched.map(([to, from]) => ({
|
|
@@ -20,3 +20,5 @@ export declare function dropTriggerBody(dialect: AbstractSqlDialect, schema: str
|
|
|
20
20
|
* one declaration.
|
|
21
21
|
*/
|
|
22
22
|
export declare function stampTriggers<E extends object>(dialect: AbstractSqlDialect, meta: EntityMeta<E>): EntityTriggerMeta<E>[];
|
|
23
|
+
/** Whether `statement` names any of `columns` as a whole identifier, quoted or not, in any case. */
|
|
24
|
+
export declare function namesAny(statement: string, columns: readonly string[]): boolean;
|
|
@@ -245,3 +245,17 @@ function rowsFrom(dialect, meta, operation, { $new, $old }, filter) {
|
|
|
245
245
|
const from = operation === 'UPDATE' ? `${$new} JOIN ${$old} ON ${keyed.join(' AND ')}` : operation === 'INSERT' ? $new : $old;
|
|
246
246
|
return { from, where: filter || undefined };
|
|
247
247
|
}
|
|
248
|
+
const IDENTIFIER_CHAR = /[\p{L}\p{N}_$]/u;
|
|
249
|
+
/** Whether `statement` names any of `columns` as a whole identifier, quoted or not, in any case. */
|
|
250
|
+
export function namesAny(statement, columns) {
|
|
251
|
+
const text = statement.toLowerCase();
|
|
252
|
+
return columns.some((column) => {
|
|
253
|
+
const name = column.toLowerCase();
|
|
254
|
+
for (let at = text.indexOf(name); at >= 0; at = text.indexOf(name, at + 1)) {
|
|
255
|
+
if (!IDENTIFIER_CHAR.test(text.charAt(at - 1)) && !IDENTIFIER_CHAR.test(text.charAt(at + name.length))) {
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return false;
|
|
260
|
+
});
|
|
261
|
+
}
|
|
@@ -253,13 +253,26 @@ export declare class MongoDialect extends AbstractDialect {
|
|
|
253
253
|
/** The field a grouped path reads, on the document or on the joined one its lookup unwound. */
|
|
254
254
|
private groupedPath;
|
|
255
255
|
/**
|
|
256
|
-
* An aggregate's own `$where` as the expression a `$cond` tests,
|
|
257
|
-
*
|
|
258
|
-
* gathers the lookups it tests: a relation condition's, and a relation aggregate's
|
|
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.
|
|
259
259
|
*/
|
|
260
260
|
private whereExpression;
|
|
261
|
-
/**
|
|
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
|
+
*/
|
|
262
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;
|
|
263
276
|
private buildHavingFilter;
|
|
264
277
|
/**
|
|
265
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
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, GROUP_OPS, groupClauses, isGroupOp, LIKE_OPS, likeRegex, whereOperators, } from '../dialect/operators.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';
|
|
@@ -133,27 +133,67 @@ function andExpr(filter, expr) {
|
|
|
133
133
|
filter['$expr'] = filter['$expr'] ? { $and: [filter['$expr'], expr] } : expr;
|
|
134
134
|
}
|
|
135
135
|
/**
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
* 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.
|
|
139
138
|
*/
|
|
140
|
-
function
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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)] };
|
|
144
154
|
}
|
|
145
155
|
/** `count` compared with `size`, a number or its bounds, as an aggregation expression. */
|
|
146
156
|
function compareCount(count, size) {
|
|
147
157
|
if (typeof size === 'number') {
|
|
148
158
|
return { $eq: [count, size] };
|
|
149
159
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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
|
+
});
|
|
153
170
|
if (!comparisons.length) {
|
|
154
171
|
throw new UqlUsageError('$size needs at least one comparison');
|
|
155
172
|
}
|
|
156
|
-
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.");
|
|
157
197
|
}
|
|
158
198
|
/** MongoDB native operators - pass through as-is. */
|
|
159
199
|
const NATIVE_OPS = new Set([
|
|
@@ -272,7 +312,7 @@ export class MongoDialect extends AbstractDialect {
|
|
|
272
312
|
filter[key] = operators(val);
|
|
273
313
|
}
|
|
274
314
|
else {
|
|
275
|
-
andExpr(filter, arraySize(key
|
|
315
|
+
andExpr(filter, arraySize(`$${key}`, size));
|
|
276
316
|
if (hasKeys(ops)) {
|
|
277
317
|
filter[key] = operators(ops);
|
|
278
318
|
}
|
|
@@ -398,8 +438,9 @@ export class MongoDialect extends AbstractDialect {
|
|
|
398
438
|
result[op] = this.held(val);
|
|
399
439
|
continue;
|
|
400
440
|
}
|
|
401
|
-
// `$not` wraps a condition too
|
|
402
|
-
|
|
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') {
|
|
403
444
|
result[op] = this.transformOperators(val, refusal);
|
|
404
445
|
continue;
|
|
405
446
|
}
|
|
@@ -437,10 +478,7 @@ export class MongoDialect extends AbstractDialect {
|
|
|
437
478
|
result[val ? '$ne' : '$eq'] = null;
|
|
438
479
|
break;
|
|
439
480
|
case '$near':
|
|
440
|
-
|
|
441
|
-
// reads: converting a distance would mean guessing the metric, so this refuses.
|
|
442
|
-
throw new UqlUsageError('$near is not supported on MongoDB: Atlas scores by index-defined similarity, not distance. ' +
|
|
443
|
-
"Project the score with $sort's $project and filter on it instead.");
|
|
481
|
+
throw nearRefusal();
|
|
444
482
|
}
|
|
445
483
|
}
|
|
446
484
|
return result;
|
|
@@ -1261,9 +1299,9 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1261
1299
|
return `${joinedPath(join)}.${this.columnOf(join.meta, key)}`;
|
|
1262
1300
|
}
|
|
1263
1301
|
/**
|
|
1264
|
-
* An aggregate's own `$where` as the expression a `$cond` tests,
|
|
1265
|
-
*
|
|
1266
|
-
* gathers the lookups it tests: a relation condition's, and a relation aggregate's
|
|
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.
|
|
1267
1305
|
*/
|
|
1268
1306
|
whereExpression(meta, where, lookup) {
|
|
1269
1307
|
const terms = getKeys(where).map((key) => {
|
|
@@ -1275,57 +1313,102 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1275
1313
|
});
|
|
1276
1314
|
return negate ? { $not: [{ [join]: clauses }] } : { [join]: clauses };
|
|
1277
1315
|
}
|
|
1316
|
+
const val = where[key];
|
|
1317
|
+
assertNoRaw(val, 'an aggregate $where');
|
|
1278
1318
|
if (meta.relations[key]) {
|
|
1279
|
-
|
|
1280
|
-
return this.relationExpression(meta, key, where[key], lookup);
|
|
1319
|
+
return this.relationExpression(meta, key, val, lookup);
|
|
1281
1320
|
}
|
|
1282
1321
|
if (key.startsWith('$')) {
|
|
1283
1322
|
throw new UqlUsageError(`aggregate $where operator '${key}' is not supported on MongoDB`);
|
|
1284
1323
|
}
|
|
1285
|
-
|
|
1324
|
+
this.assertKnownPathRoot(meta, key);
|
|
1286
1325
|
this.appendAggregateField(meta, key, lookup);
|
|
1287
|
-
const wire = this.holdsKeys(meta, key) ? (value) => this.toWireId(value) :
|
|
1326
|
+
const wire = this.holdsKeys(meta, key) ? (value) => this.toWireId(value) : undefined;
|
|
1288
1327
|
return this.fieldExpression(`$${this.pathOf(meta, key)}`, val, wire);
|
|
1289
1328
|
});
|
|
1290
|
-
return terms
|
|
1329
|
+
return allOf(terms);
|
|
1291
1330
|
}
|
|
1292
|
-
/**
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
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);
|
|
1297
1346
|
}
|
|
1298
|
-
// A null or missing field compares below every value in an expression, where SQL leaves it unmatched.
|
|
1299
|
-
const present = { $not: [isNullExpr(ref)] };
|
|
1300
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
|
+
}
|
|
1301
1352
|
switch (op) {
|
|
1302
1353
|
case '$eq':
|
|
1303
1354
|
return equals(operand);
|
|
1304
1355
|
case '$ne':
|
|
1305
1356
|
return { $not: [equals(operand)] };
|
|
1357
|
+
case '$not':
|
|
1358
|
+
return { $not: [this.fieldExpression(ref, operand, wire)] };
|
|
1306
1359
|
case '$gt':
|
|
1307
1360
|
case '$gte':
|
|
1308
|
-
return { [op]: [ref, wire(operand)] };
|
|
1309
1361
|
case '$lt':
|
|
1310
1362
|
case '$lte':
|
|
1311
|
-
return
|
|
1312
|
-
case '$in':
|
|
1313
|
-
return { $in: [ref, wire(operand)] };
|
|
1314
|
-
case '$nin':
|
|
1315
|
-
return { $not: [{ $in: [ref, wire(operand)] }] };
|
|
1363
|
+
return ordered(op, operand);
|
|
1316
1364
|
case '$between': {
|
|
1317
|
-
const [min, max] = operand;
|
|
1318
|
-
return { $and: [
|
|
1365
|
+
const [min, max] = betweenBounds(operand);
|
|
1366
|
+
return { $and: [ordered('$gte', min), ordered('$lte', max)] };
|
|
1319
1367
|
}
|
|
1368
|
+
case '$in':
|
|
1369
|
+
return within(op, operand);
|
|
1370
|
+
case '$nin':
|
|
1371
|
+
return { $not: [within(op, operand)] };
|
|
1320
1372
|
case '$isNull':
|
|
1321
|
-
return operand ? isNullExpr(ref) : present;
|
|
1322
1373
|
case '$isNotNull':
|
|
1323
|
-
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();
|
|
1324
1385
|
default:
|
|
1325
|
-
throw new UqlUsageError(`
|
|
1386
|
+
throw new UqlUsageError(`unknown operator: ${op}`);
|
|
1326
1387
|
}
|
|
1327
1388
|
});
|
|
1328
|
-
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] }));
|
|
1329
1412
|
}
|
|
1330
1413
|
buildHavingFilter(having) {
|
|
1331
1414
|
const filter = {};
|
package/dist/type/migration.d.ts
CHANGED
|
@@ -257,7 +257,7 @@ export interface SchemaGenerator {
|
|
|
257
257
|
* rejected once the foreign keys are really there.
|
|
258
258
|
*/
|
|
259
259
|
generateDropSchema(entities: readonly Type<object>[], options?: DropSchemaOptions): string[];
|
|
260
|
-
/** The statements taking a table through `diff`; its rollback is
|
|
260
|
+
/** The statements taking a table through `diff`; its rollback is `rollbackDiff(diff)`. */
|
|
261
261
|
generateAlterTable(diff: SchemaDiff): string[];
|
|
262
262
|
/**
|
|
263
263
|
* The statements one migration builder operation runs as, one string each. An operation the engine
|
package/dist/util/sql.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type EntitySql, type EntityWhere, type EntityWhereMeta, QuerySql, type QuerySqlFn, type ComputedRefs, type RefMap, type SqlArg, type SqlStatement, type SqlValueName, type TriggerRowName, type Type } from '../type/index.js';
|
|
1
|
+
import { ColumnRef, type EntitySql, type EntityWhere, type EntityWhereMeta, QuerySql, type QuerySqlFn, type ComputedRefs, type RefMap, type SqlArg, type SqlStatement, type SqlValueName, type TriggerRowName, type Type } from '../type/index.js';
|
|
2
2
|
/**
|
|
3
3
|
* Raw SQL, where an interpolated value binds, a `refs` field renders its column, and a `sql` renders
|
|
4
4
|
* in place: `sql`GREATEST(0, ${user.credits} - ${amount})``. A callback writes whatever it writes, so
|
|
@@ -54,5 +54,10 @@ export declare function entityWhere<E>(where: EntityWhere<E>): EntityWhereMeta<E
|
|
|
54
54
|
* Columns only: a relation's aggregate is a subquery, and a trigger fires on one row, not over a table.
|
|
55
55
|
*/
|
|
56
56
|
export declare function rowRefs<E>(entity: Type<E>, qualifier: TriggerRowName): RefMap<E>;
|
|
57
|
+
/**
|
|
58
|
+
* One field as SQL, against its own entity or, read off a definition, the entity rendering it. A
|
|
59
|
+
* `qualifier` names the row it reads from, `NEW` or `OLD`, instead of the alias in scope.
|
|
60
|
+
*/
|
|
61
|
+
export declare function columnRef(entity: Type<unknown> | undefined, key: string, qualifier?: string): ColumnRef;
|
|
57
62
|
/** @deprecated Renamed to {@link sql}, the tag editors recognise for SQL highlighting. */
|
|
58
63
|
export declare const raw: typeof sql;
|
package/dist/util/sql.js
CHANGED
|
@@ -155,7 +155,7 @@ export function rowRefs(entity, qualifier) {
|
|
|
155
155
|
* One field as SQL, against its own entity or, read off a definition, the entity rendering it. A
|
|
156
156
|
* `qualifier` names the row it reads from, `NEW` or `OLD`, instead of the alias in scope.
|
|
157
157
|
*/
|
|
158
|
-
function columnRef(entity, key, qualifier) {
|
|
158
|
+
export function columnRef(entity, key, qualifier) {
|
|
159
159
|
return new ColumnRef(key, (opts) => {
|
|
160
160
|
const owner = entity ?? opts.entity;
|
|
161
161
|
if (!owner) {
|
|
@@ -178,13 +178,18 @@ function renderColumn(meta, key, opts, qualifier) {
|
|
|
178
178
|
throw new UqlUsageError(`'${entityName(meta)}.${key}' reads a relation, which a trigger's row cannot: it fires on one row, ` +
|
|
179
179
|
'with no table in scope to correlate a subquery to. Name the columns it is derived from instead.');
|
|
180
180
|
}
|
|
181
|
-
|
|
182
|
-
field.computed.render(scope);
|
|
183
|
-
scope.ctx.append(')');
|
|
181
|
+
grouped(field.computed).render(scope);
|
|
184
182
|
return;
|
|
185
183
|
}
|
|
186
184
|
scope.ctx.append(scope.escapedPrefix + scope.dialect.escapeId(scope.dialect.columnOf(meta, key), true));
|
|
187
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* An inlined computed field's expression as every clause splices it: grouped, so a comparison inside cannot
|
|
188
|
+
* chain with one around it (`a <= b = ?`). A relation aggregate is a subquery, grouped already.
|
|
189
|
+
*/
|
|
190
|
+
function grouped(computed) {
|
|
191
|
+
return computed instanceof RelationAggregate ? computed : sql `(${computed})`;
|
|
192
|
+
}
|
|
188
193
|
/** @deprecated Renamed to {@link sql}, the tag editors recognise for SQL highlighting. */
|
|
189
194
|
export const raw = sql;
|
|
190
195
|
/** A tag call passes the frozen strings array, which carries its own `sql` counterpart. */
|
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.5",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"engines": {
|
|
9
9
|
"node": ">=22.18"
|