uql-orm 0.101.5 → 0.102.0
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 +12 -3
- package/dist/dialect/abstractSqlDialect.js +24 -14
- package/dist/dialect/mysqlLikeSqlDialect.d.ts +2 -2
- package/dist/dialect/mysqlLikeSqlDialect.js +5 -2
- package/dist/dialect/pgLikeSqlDialect.js +1 -0
- package/dist/libsql/libsqlDialect.d.ts +5 -0
- package/dist/libsql/libsqlDialect.js +5 -0
- package/dist/migrate/builder/migrationBuilder.js +1 -1
- package/dist/migrate/builder/tableBuilder.js +9 -4
- package/dist/migrate/builder/types.d.ts +5 -1
- package/dist/migrate/generator/definitionToNode.d.ts +7 -4
- package/dist/migrate/generator/definitionToNode.js +12 -13
- package/dist/migrate/schemaGenerator.js +1 -1
- package/dist/mongodb/mongoDialect.js +1 -0
- package/dist/mongodb/mongoIntrospector.js +3 -2
- package/dist/mongodb/mongoSchemaGenerator.js +8 -8
- package/dist/mssql/mssqlDialect.d.ts +4 -4
- package/dist/mssql/mssqlDialect.js +7 -4
- package/dist/querier/abstractQuerier.d.ts +52 -17
- package/dist/querier/abstractQuerier.js +140 -79
- package/dist/querier/abstractSqlQuerier.d.ts +5 -1
- package/dist/querier/abstractSqlQuerier.js +16 -1
- package/dist/schema/indexDifferences.d.ts +5 -4
- package/dist/schema/indexDifferences.js +23 -12
- package/dist/schema/schemaASTBuilder.js +3 -3
- package/dist/sqlite/hranaQuerier.d.ts +2 -2
- package/dist/sqlite/hranaQuerier.js +2 -2
- package/dist/sqlite/sqliteDialect.d.ts +4 -3
- package/dist/sqlite/sqliteDialect.js +5 -1
- package/dist/type/dialect.d.ts +6 -0
- package/dist/type/querier.d.ts +6 -0
- package/dist/util/ddlExpression.util.d.ts +8 -2
- package/dist/util/ddlExpression.util.js +6 -3
- package/dist/util/sql.util.d.ts +7 -2
- package/dist/util/sql.util.js +16 -3
- package/package.json +1 -1
- package/skills/uql-orm/SKILL.md +3 -3
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ColumnFamily, type SavepointCommand, type DdlRenderOptions, type EntityData, type EntityMeta, type EntityWhereMeta, type FieldKey, type FieldOptions, type IsolationLevel, type JsonColumnType, type JsonUpdateOp, type Query, type QueryAggMap, type QueryAggregate, type QueryBuildFn, type QueryComparisonOptions, type QueryConflictPaths, type QueryContext, type QueryContextOptions, type QueryExclude, type QueryGroupMap, type QueryGroupOp, type QueryHavingMap, type QueryRenderOptions, type QueryPage, type QueryPager, QuerySql, type QuerySqlFnOptions, type QuerySearch, type QuerySelectValue, type QuerySizeComparisonOps, type QueryTextSearchOptions, type QueryWhere, type QueryWhereArray, type QueryWhereFieldOp, type QueryWhereOptions, type RelationAggregateSpec, type RelationMeta, type SqlDialectName, type SqlQueryDialect, type SqlValues, type TriggerRows, type TriggerWrite, type Type, type UpdatePayload } from '../type/index.js';
|
|
1
|
+
import { type ColumnFamily, type SavepointCommand, type DdlRenderOptions, type EntityData, type EntityMeta, type EntityWhereMeta, type FieldKey, type FieldOptions, type IsolationLevel, type TransactionOptions, type JsonColumnType, type JsonUpdateOp, type Query, type QueryAggMap, type QueryAggregate, type QueryBuildFn, type QueryComparisonOptions, type QueryConflictPaths, type QueryContext, type QueryContextOptions, type QueryExclude, type QueryGroupMap, type QueryGroupOp, type QueryHavingMap, type QueryRenderOptions, type QueryPage, type QueryPager, QuerySql, type QuerySqlFnOptions, type QuerySearch, type QuerySelectValue, type QuerySizeComparisonOps, type QueryTextSearchOptions, type QueryWhere, type QueryWhereArray, type QueryWhereFieldOp, type QueryWhereOptions, type RelationAggregateSpec, type RelationMeta, type SqlDialectName, type SqlQueryDialect, type SqlValues, type TriggerRows, type TriggerWrite, type Type, type UpdatePayload } from '../type/index.js';
|
|
2
2
|
import { type SortDirection } from '../util/index.js';
|
|
3
3
|
import type { HydrateKind } from './hydrateColumn.js';
|
|
4
4
|
import { type JsonAccessMode, type JsonSlot } from './jsonSql.js';
|
|
@@ -117,7 +117,6 @@ export declare abstract class AbstractSqlDialect extends VectorSqlDialect implem
|
|
|
117
117
|
* MariaDB accept the same statement, where it means a database.
|
|
118
118
|
*/
|
|
119
119
|
createSchemaSql(schema: string): string;
|
|
120
|
-
readonly isolationLevelStrategy: 'inline' | 'setBefore' | 'none';
|
|
121
120
|
readonly booleanLiteral: 'native' | 'integer';
|
|
122
121
|
readonly sqlValues: SqlValues;
|
|
123
122
|
/** The most values one statement binds: a statement past it is refused, and a list the ORM builds is split. */
|
|
@@ -131,7 +130,8 @@ export declare abstract class AbstractSqlDialect extends VectorSqlDialect implem
|
|
|
131
130
|
* update, is spread over nested calls. No cap binds unless a dialect declares one.
|
|
132
131
|
*/
|
|
133
132
|
readonly maxFunctionArgs: number;
|
|
134
|
-
|
|
133
|
+
/** The statements opening a transaction, its modes inline in the `BEGIN` as the Postgres family takes them. */
|
|
134
|
+
getBeginTransactionStatements({ isolationLevel, readOnly }?: TransactionOptions): string[];
|
|
135
135
|
createContext(options?: QueryContextOptions): QueryContext;
|
|
136
136
|
/** A statement as it is sent: the SQL `build` writes, or a `sql` renders, and the values it binds. */
|
|
137
137
|
compile(build: QueryBuildFn | QuerySql): {
|
|
@@ -162,6 +162,11 @@ export declare abstract class AbstractSqlDialect extends VectorSqlDialect implem
|
|
|
162
162
|
placeholder(_index: number): string;
|
|
163
163
|
/** `RETURNING <id column> id`, or nothing on a composite key, whose every column the payload already names. */
|
|
164
164
|
returningId<E>(meta: EntityMeta<E>): string;
|
|
165
|
+
/**
|
|
166
|
+
* ` RETURNING` the fields `keys` names, after an `UPDATE` or `DELETE` this context holds, projected and read as
|
|
167
|
+
* a read's are: the terms its rows answer under. Only where `features.writeReturning`.
|
|
168
|
+
*/
|
|
169
|
+
returning<E>(ctx: QueryContext, entity: Type<E>, keys: readonly FieldKey<E>[]): readonly SelectTerm[];
|
|
165
170
|
/** What the returned row is read off, for an engine that names it: SQL Server's `INSERTED.`. */
|
|
166
171
|
protected readonly returnedRowPrefix: string;
|
|
167
172
|
/** `<id column> AS id` on its own, for a statement composing a `RETURNING` list of several items. */
|
|
@@ -221,6 +226,8 @@ export declare abstract class AbstractSqlDialect extends VectorSqlDialect implem
|
|
|
221
226
|
* projects.
|
|
222
227
|
*/
|
|
223
228
|
protected projection<E>(ctx: QueryContext, entity: Type<E>, q: Query<E>, opts: SelectOptions, joins: QueryJoins): SelectTerm[];
|
|
229
|
+
/** A projection's list of terms. */
|
|
230
|
+
private termsSql;
|
|
224
231
|
/** A term as a projection writes it, aliased unless its SQL already answers under its key. */
|
|
225
232
|
private termSql;
|
|
226
233
|
/** What a `$sort` projects, each under the name it asked for: a vector's distance, the `$text` relevance. */
|
|
@@ -767,3 +774,5 @@ export declare abstract class AbstractSqlDialect extends VectorSqlDialect implem
|
|
|
767
774
|
/** Reads extracted JSON text as a number, which every engine spells its own way. */
|
|
768
775
|
protected abstract numericCast(expr: string): string;
|
|
769
776
|
}
|
|
777
|
+
/** `begin` after the statement setting the level, as T-SQL and the MySQL family take it, having no inline form. */
|
|
778
|
+
export declare function isolationSetBefore(isolationLevel: IsolationLevel | undefined, begin: string): string[];
|
|
@@ -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 { isExactDecimal, isInlinedExpression, jsonKey, jsonPathKeys } from '../util/field.util.js';
|
|
5
|
-
import { isSelectList, assertNonNegativeInteger, assertNoUndefined, assertWhere, definedEntries, escapeSqlId, fillOnFields, filterFieldKeys, getInsertFieldKeys, getKeys, getRelationRequestSummary, getSoftDeleteValue, hasKeys, idOnlyQuery, whereAnd, columnFamily, fieldFamily, countedRelations, fieldUpdateOf, fulltextIndexOver, fulltextWeights, isFieldUpdateOp, isJsonObject, isJsonUpdateOp, 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, isOperatorMap, isOperatorKey, isVectorSearch, normalizeScalarFieldSelection, parentJoins, rankedTextSearch, targetJoins, textSearchFields, textSortOf, textWeightSteps, parseGroupMap, parseRelationAtKey, parseRelationSize, parseSortDirection, populatesRelations, aggregateOf, sql, refs, throwUnknownAggregateColumn, withoutSoftDeleteFilter, } from '../util/index.js';
|
|
6
6
|
import { escapeAnsiSqlLiteral } from '../util/sqlLiteral.js';
|
|
7
7
|
import { kindOf, UqlUsageError } from '../util/uqlError.js';
|
|
8
8
|
import { AGGREGATE_PAGE_ALIAS, AGGREGATE_VALUE_ALIAS, ROWS_ALIAS, JSON_ELEM_ALIAS, JSON_PULL_ALIAS, relationSortColumn, } from './aliases.js';
|
|
@@ -121,7 +121,6 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
121
121
|
createSchemaSql(schema) {
|
|
122
122
|
return `CREATE SCHEMA IF NOT EXISTS ${this.escapeId(schema, true)}`;
|
|
123
123
|
}
|
|
124
|
-
isolationLevelStrategy = 'inline';
|
|
125
124
|
booleanLiteral = 'native';
|
|
126
125
|
sqlValues = ANSI_SQL_VALUES;
|
|
127
126
|
/** The most rows one `INSERT ... VALUES` takes, which a narrow row can reach before the bind budget. */
|
|
@@ -135,17 +134,11 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
135
134
|
* update, is spread over nested calls. No cap binds unless a dialect declares one.
|
|
136
135
|
*/
|
|
137
136
|
maxFunctionArgs = Infinity;
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
if (strategy === 'inline') {
|
|
145
|
-
return [`${this.beginTransactionCommand} ISOLATION LEVEL ${level}`];
|
|
146
|
-
}
|
|
147
|
-
// 'setBefore' - MySQL/MariaDB pattern
|
|
148
|
-
return [`SET TRANSACTION ISOLATION LEVEL ${level}`, this.beginTransactionCommand];
|
|
137
|
+
/** The statements opening a transaction, its modes inline in the `BEGIN` as the Postgres family takes them. */
|
|
138
|
+
getBeginTransactionStatements({ isolationLevel, readOnly } = {}) {
|
|
139
|
+
const modes = [isolationLevel && `ISOLATION LEVEL ${isolationLevel.toUpperCase()}`, readOnly && 'READ ONLY'];
|
|
140
|
+
const inline = modes.filter((mode) => typeof mode === 'string');
|
|
141
|
+
return [inline.length ? `${this.beginTransactionCommand} ${inline.join(', ')}` : this.beginTransactionCommand];
|
|
149
142
|
}
|
|
150
143
|
createContext(options = {}) {
|
|
151
144
|
return new SqlQueryContext(this, [], undefined, options.inlineValues);
|
|
@@ -217,6 +210,15 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
217
210
|
const expression = this.returningIdExpression(meta);
|
|
218
211
|
return expression ? `RETURNING ${expression}` : '';
|
|
219
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* ` RETURNING` the fields `keys` names, after an `UPDATE` or `DELETE` this context holds, projected and read as
|
|
215
|
+
* a read's are: the terms its rows answer under. Only where `features.writeReturning`.
|
|
216
|
+
*/
|
|
217
|
+
returning(ctx, entity, keys) {
|
|
218
|
+
const terms = this.selectTerms(ctx, entity, keySet(keys));
|
|
219
|
+
ctx.append(` RETURNING ${this.termsSql(terms)}`);
|
|
220
|
+
return terms;
|
|
221
|
+
}
|
|
220
222
|
/** What the returned row is read off, for an engine that names it: SQL Server's `INSERTED.`. */
|
|
221
223
|
returnedRowPrefix = '';
|
|
222
224
|
/** `<id column> AS id` on its own, for a statement composing a `RETURNING` list of several items. */
|
|
@@ -360,7 +362,7 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
360
362
|
}
|
|
361
363
|
ctx.append(q.$distinct ? 'SELECT DISTINCT ' : 'SELECT ');
|
|
362
364
|
ctx.append(this.selectModifier(q));
|
|
363
|
-
ctx.append(
|
|
365
|
+
ctx.append(this.termsSql(columns));
|
|
364
366
|
ctx.append(` FROM ${ref}${this.lockHint(q)}`);
|
|
365
367
|
this.selectRelationJoins(ctx, meta, alias, joins);
|
|
366
368
|
return { terms, order: carried?.order };
|
|
@@ -382,6 +384,10 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
382
384
|
...this.selectSortProjections(ctx, meta, q, opts.prefix),
|
|
383
385
|
];
|
|
384
386
|
}
|
|
387
|
+
/** A projection's list of terms. */
|
|
388
|
+
termsSql(terms) {
|
|
389
|
+
return terms.map((term) => this.termSql(term)).join(', ');
|
|
390
|
+
}
|
|
385
391
|
/** A term as a projection writes it, aliased unless its SQL already answers under its key. */
|
|
386
392
|
termSql({ sql, key, bare }) {
|
|
387
393
|
return bare || key === undefined ? sql : `${sql} ${this.escapeId(key, true)}`;
|
|
@@ -2131,3 +2137,7 @@ export class AbstractSqlDialect extends VectorSqlDialect {
|
|
|
2131
2137
|
return `${operand} ${negate ? 'NOT IN' : 'IN'} (${phs})`;
|
|
2132
2138
|
}
|
|
2133
2139
|
}
|
|
2140
|
+
/** `begin` after the statement setting the level, as T-SQL and the MySQL family take it, having no inline form. */
|
|
2141
|
+
export function isolationSetBefore(isolationLevel, begin) {
|
|
2142
|
+
return isolationLevel ? [`SET TRANSACTION ISOLATION LEVEL ${isolationLevel.toUpperCase()}`, begin] : [begin];
|
|
2143
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { EntityMeta, InsertIdSource, Query, QueryConflictPaths, QueryContext, QueryOptions, QueryTextSearchOptions, RowLockFeatures, SqlDialectFeatures, SqlDialectName, SqlValues, Type, UpdatePayload } from '../type/index.js';
|
|
1
|
+
import type { EntityMeta, InsertIdSource, Query, QueryConflictPaths, QueryContext, QueryOptions, QueryTextSearchOptions, RowLockFeatures, SqlDialectFeatures, SqlDialectName, SqlValues, Type, UpdatePayload, TransactionOptions } from '../type/index.js';
|
|
2
2
|
import { AbstractSqlDialect, type DerivedRelation, type RelationRows, type SelectTerm } from './abstractSqlDialect.js';
|
|
3
3
|
import { type JsonSlot } from './jsonSql.js';
|
|
4
4
|
/** Declared apart so MariaDB, which has no `FOR UPDATE OF`, can restate that one part of it. */
|
|
@@ -25,7 +25,7 @@ export declare abstract class MysqlLikeSqlDialect extends AbstractSqlDialect {
|
|
|
25
25
|
readonly autoIncrementSuffix = "AUTO_INCREMENT";
|
|
26
26
|
readonly escapeIdChar = "`";
|
|
27
27
|
readonly beginTransactionCommand = "START TRANSACTION";
|
|
28
|
-
|
|
28
|
+
getBeginTransactionStatements({ isolationLevel, readOnly }?: TransactionOptions): string[];
|
|
29
29
|
readonly booleanLiteral = "integer";
|
|
30
30
|
readonly sqlValues: SqlValues;
|
|
31
31
|
readonly insertIdSource: InsertIdSource;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getMeta } from '../entity/index.js';
|
|
2
2
|
import { textSearchFields } from '../util/index.js';
|
|
3
3
|
import { escapeMysqlSqlLiteral } from '../util/sqlLiteral.js';
|
|
4
|
-
import { ANSI_SQL_VALUES, AbstractSqlDialect, } from './abstractSqlDialect.js';
|
|
4
|
+
import { ANSI_SQL_VALUES, AbstractSqlDialect, isolationSetBefore, } from './abstractSqlDialect.js';
|
|
5
5
|
import { AGGREGATE_VALUE_ALIAS } from './aliases.js';
|
|
6
6
|
import { BYTES_PREFIX } from './hydrateColumn.js';
|
|
7
7
|
import { jsonPath } from './jsonSql.js';
|
|
@@ -29,6 +29,7 @@ export const MYSQL_FEATURES = {
|
|
|
29
29
|
serverSideCursors: false,
|
|
30
30
|
correlatedWrites: true,
|
|
31
31
|
rowLocks: MYSQL_ROW_LOCKS,
|
|
32
|
+
writeReturning: false,
|
|
32
33
|
nullsSortLowest: true,
|
|
33
34
|
nullsOrdering: 'expression',
|
|
34
35
|
textScoreIndexes: true,
|
|
@@ -90,7 +91,9 @@ export class MysqlLikeSqlDialect extends AbstractSqlDialect {
|
|
|
90
91
|
autoIncrementSuffix = 'AUTO_INCREMENT';
|
|
91
92
|
escapeIdChar = '`';
|
|
92
93
|
beginTransactionCommand = 'START TRANSACTION';
|
|
93
|
-
|
|
94
|
+
getBeginTransactionStatements({ isolationLevel, readOnly } = {}) {
|
|
95
|
+
return isolationSetBefore(isolationLevel, `${this.beginTransactionCommand}${readOnly ? ' READ ONLY' : ''}`);
|
|
96
|
+
}
|
|
94
97
|
booleanLiteral = 'integer';
|
|
95
98
|
sqlValues = MYSQL_SQL_VALUES;
|
|
96
99
|
// No `RETURNING` support, so multi-row insert IDs are inferred from the header - see the
|
|
@@ -10,6 +10,11 @@ export declare const LIBSQL_FEATURES: SqlDialectFeatures;
|
|
|
10
10
|
*/
|
|
11
11
|
export declare class LibsqlDialect extends SqliteDialect {
|
|
12
12
|
readonly features: SqlDialectFeatures;
|
|
13
|
+
/**
|
|
14
|
+
* Every transaction a write one, as `@libsql/client`'s `transaction('write')` opens it: a deferred one that
|
|
15
|
+
* reads, then writes, cannot upgrade while another connection writes, and fails rather than waiting.
|
|
16
|
+
*/
|
|
17
|
+
readonly beginTransactionCommand = "BEGIN IMMEDIATE";
|
|
13
18
|
/** libSQL's built-in vector functions, and the metric its DiskANN index names; no `inner` (only the Rust engine has it) and no `l1`. */
|
|
14
19
|
readonly vectorMetrics: ReadonlyMap<VectorDistance, VectorMetric>;
|
|
15
20
|
}
|
|
@@ -9,6 +9,11 @@ export const LIBSQL_FEATURES = { ...SQLITE_FEATURES, orderedJsonAggregates: fals
|
|
|
9
9
|
*/
|
|
10
10
|
export class LibsqlDialect extends SqliteDialect {
|
|
11
11
|
features = LIBSQL_FEATURES;
|
|
12
|
+
/**
|
|
13
|
+
* Every transaction a write one, as `@libsql/client`'s `transaction('write')` opens it: a deferred one that
|
|
14
|
+
* reads, then writes, cannot upgrade while another connection writes, and fails rather than waiting.
|
|
15
|
+
*/
|
|
16
|
+
beginTransactionCommand = 'BEGIN IMMEDIATE';
|
|
12
17
|
/** libSQL's built-in vector functions, and the metric its DiskANN index names; no `inner` (only the Rust engine has it) and no `l1`. */
|
|
13
18
|
vectorMetrics = new Map([
|
|
14
19
|
['cosine', { fn: 'vector_distance_cos', index: 'cosine' }],
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* Fluent API for defining tables in migrations.
|
|
5
5
|
*/
|
|
6
6
|
import { DATE_PRECISION } from '../../util/date.js';
|
|
7
|
+
import { declaredIndexName } from '../../util/ddlExpression.util.js';
|
|
7
8
|
import { currentTimestamp } from '../../util/sql.js';
|
|
8
9
|
import { columnForeignKey, columnIndex, indexDefinition } from '../generator/definitionToNode.js';
|
|
9
10
|
import { ColumnDefinitionBuilder } from './columnBuilder.js';
|
|
@@ -158,11 +159,11 @@ export class TableDefinitionBuilder {
|
|
|
158
159
|
return this;
|
|
159
160
|
}
|
|
160
161
|
unique(columns, options) {
|
|
161
|
-
this._indexes.push(indexDefinition(
|
|
162
|
+
this._indexes.push(indexDefinition(columns, { ...namedOptions(options), unique: true }, true));
|
|
162
163
|
return this;
|
|
163
164
|
}
|
|
164
165
|
index(columns, options) {
|
|
165
|
-
this._indexes.push(indexDefinition(
|
|
166
|
+
this._indexes.push(indexDefinition(columns, namedOptions(options)));
|
|
166
167
|
return this;
|
|
167
168
|
}
|
|
168
169
|
foreignKey(columns) {
|
|
@@ -180,11 +181,15 @@ export class TableDefinitionBuilder {
|
|
|
180
181
|
build() {
|
|
181
182
|
// Build all columns from builders
|
|
182
183
|
const columns = this._columnBuilders.map((cb) => cb.build());
|
|
183
|
-
// Collect column-level indexes, skipping
|
|
184
|
+
// Collect column-level indexes, skipping one a table-level index already names.
|
|
185
|
+
const taken = new Set(this._indexes
|
|
186
|
+
.filter((idx) => idx.where === undefined)
|
|
187
|
+
.map((idx) => declaredIndexName(idx.name, this._name, idx.entries, { unique: idx.uniqueName })));
|
|
184
188
|
for (const col of columns) {
|
|
185
189
|
const index = columnIndex(this._name, col);
|
|
186
|
-
if (index && !
|
|
190
|
+
if (index && !taken.has(index.name)) {
|
|
187
191
|
this._indexes.push(index);
|
|
192
|
+
taken.add(index.name);
|
|
188
193
|
}
|
|
189
194
|
}
|
|
190
195
|
// Build foreign keys
|
|
@@ -90,9 +90,13 @@ export interface FullColumnDefinition extends ColumnDefinition {
|
|
|
90
90
|
* An index as the builder records it, with the entries and options `@Index` takes: its SQL kept as `raw`
|
|
91
91
|
* until a generator renders it for the engine the migration runs on.
|
|
92
92
|
*/
|
|
93
|
-
export type IndexDefinition = Except<IndexSchema, 'entries' | 'where'> & {
|
|
93
|
+
export type IndexDefinition = Except<IndexSchema, 'name' | 'entries' | 'where'> & {
|
|
94
|
+
/** As declared; an unnamed index is named when it is rendered, from its predicate as written. */
|
|
95
|
+
readonly name?: string;
|
|
94
96
|
readonly entries: readonly EntityIndexColumn[];
|
|
95
97
|
readonly where?: QuerySql;
|
|
98
|
+
/** Whether an unnamed index takes the `_uk` kind. Only `table.unique` sets it: earlier migrations installed `_uk` there, so `index` keeps `_idx`. */
|
|
99
|
+
readonly uniqueName?: boolean;
|
|
96
100
|
};
|
|
97
101
|
/**
|
|
98
102
|
* Complete table definition.
|
|
@@ -21,10 +21,13 @@ export declare function columnIndex(tableName: string, col: FullColumnDefinition
|
|
|
21
21
|
/**
|
|
22
22
|
* The index that `table.index`, `table.unique` and `createIndex` record. Its entries are normalized, since
|
|
23
23
|
* an entry left as written reaches the generator as a column named `[object Object]`. An unnamed index is
|
|
24
|
-
* named
|
|
24
|
+
* named when rendered, with `_uk` only for `table.unique`, so names earlier migrations installed stay.
|
|
25
25
|
*/
|
|
26
|
-
export declare function indexDefinition(
|
|
27
|
-
/**
|
|
28
|
-
|
|
26
|
+
export declare function indexDefinition(columns: readonly IndexColumnInput[], { name, unique, ...options }?: IndexOptions, uniqueName?: boolean): IndexDefinition;
|
|
27
|
+
/**
|
|
28
|
+
* An index the builder recorded, its SQL rendered by `render` into the text the schema holds, and named
|
|
29
|
+
* from that text the way an entity's is.
|
|
30
|
+
*/
|
|
31
|
+
export declare function renderIndexDefinition(table: string, { uniqueName, ...index }: IndexDefinition, render: (sql: QuerySql) => string): IndexSchema;
|
|
29
32
|
/** The foreign key a column-level `references` declares, or nothing. Shared for the same reason. */
|
|
30
33
|
export declare function columnForeignKey(col: FullColumnDefinition): ForeignKeySchema | undefined;
|
|
@@ -24,7 +24,7 @@ export function tableDefinitionToNode(def, render) {
|
|
|
24
24
|
? { columns: def.primaryKey.filter((name) => columns.has(name)) }
|
|
25
25
|
: keyOfColumns(columns.values());
|
|
26
26
|
for (const idxDef of def.indexes) {
|
|
27
|
-
table.indexes.push({ ...renderIndexDefinition(idxDef, render), table });
|
|
27
|
+
table.indexes.push({ ...renderIndexDefinition(name, idxDef, render), table });
|
|
28
28
|
}
|
|
29
29
|
return table;
|
|
30
30
|
}
|
|
@@ -51,23 +51,22 @@ export function columnIndex(tableName, col) {
|
|
|
51
51
|
/**
|
|
52
52
|
* The index that `table.index`, `table.unique` and `createIndex` record. Its entries are normalized, since
|
|
53
53
|
* an entry left as written reaches the generator as a column named `[object Object]`. An unnamed index is
|
|
54
|
-
* named
|
|
54
|
+
* named when rendered, with `_uk` only for `table.unique`, so names earlier migrations installed stay.
|
|
55
55
|
*/
|
|
56
|
-
export function indexDefinition(
|
|
57
|
-
|
|
58
|
-
return {
|
|
59
|
-
...options,
|
|
60
|
-
name: declaredIndexName(name, tableName, entries, uniqueName),
|
|
61
|
-
entries,
|
|
62
|
-
unique,
|
|
63
|
-
};
|
|
56
|
+
export function indexDefinition(columns, { name, unique = false, ...options } = {}, uniqueName = false) {
|
|
57
|
+
return { ...options, name, entries: columns.map(normalizeIndexColumn), unique, uniqueName };
|
|
64
58
|
}
|
|
65
|
-
/**
|
|
66
|
-
|
|
59
|
+
/**
|
|
60
|
+
* An index the builder recorded, its SQL rendered by `render` into the text the schema holds, and named
|
|
61
|
+
* from that text the way an entity's is.
|
|
62
|
+
*/
|
|
63
|
+
export function renderIndexDefinition(table, { uniqueName, ...index }, render) {
|
|
64
|
+
const where = index.where && render(index.where);
|
|
67
65
|
return {
|
|
68
66
|
...index,
|
|
67
|
+
name: declaredIndexName(index.name, table, index.entries, { unique: uniqueName, where }),
|
|
68
|
+
where,
|
|
69
69
|
entries: index.entries.map((entry) => renderIndexColumn(entry, render)),
|
|
70
|
-
where: index.where && render(index.where),
|
|
71
70
|
};
|
|
72
71
|
}
|
|
73
72
|
/** The foreign key a column-level `references` declares, or nothing. Shared for the same reason. */
|
|
@@ -397,7 +397,7 @@ export class SqlSchemaGenerator {
|
|
|
397
397
|
return this.tableDdl.alterColumns(operation.tableName, [{ to: this.definitionColumn(operation.changes) }]);
|
|
398
398
|
case 'createIndex':
|
|
399
399
|
return this.addIndexStatements(operation.tableName, [
|
|
400
|
-
renderIndexDefinition(operation.index, (sql) => this.dialect.compileDdl(sql)),
|
|
400
|
+
renderIndexDefinition(operation.tableName, operation.index, (sql) => this.dialect.compileDdl(sql)),
|
|
401
401
|
]);
|
|
402
402
|
case 'dropIndex':
|
|
403
403
|
return [this.tableDdl.dropIndex(operation.tableName, operation.indexName)];
|
|
@@ -28,6 +28,7 @@ export const mongoDialectFeatures = {
|
|
|
28
28
|
serverSideCursors: false,
|
|
29
29
|
correlatedWrites: false,
|
|
30
30
|
rowLocks: false, // its concurrency control is the transaction plus atomic document updates
|
|
31
|
+
writeReturning: false,
|
|
31
32
|
nullsSortLowest: true,
|
|
32
33
|
};
|
|
33
34
|
/** What `toWireId` converts: the hex spelling of an `ObjectId`, and nothing looser. */
|
|
@@ -41,9 +41,10 @@ export class MongoSchemaIntrospector {
|
|
|
41
41
|
columns: [],
|
|
42
42
|
...(validator && { checks: [validatorCheck(tableName, validator)] }),
|
|
43
43
|
indexes: [
|
|
44
|
-
...indexes.map(({ name, key, unique, weights, default_language }) => ({
|
|
44
|
+
...indexes.map(({ name, key, unique, weights, default_language, partialFilterExpression }) => ({
|
|
45
45
|
name,
|
|
46
46
|
unique: !!unique,
|
|
47
|
+
...(partialFilterExpression && { where: JSON.stringify(partialFilterExpression) }),
|
|
47
48
|
...(weights
|
|
48
49
|
? {
|
|
49
50
|
entries: Object.entries(weights).map(textIndexEntry),
|
|
@@ -99,7 +100,7 @@ async function hasCollection(db, name) {
|
|
|
99
100
|
const collections = await db.listCollections({ name, type: 'collection' }, { nameOnly: true }).toArray();
|
|
100
101
|
return collections.length > 0;
|
|
101
102
|
}
|
|
102
|
-
/** `listIndexes`
|
|
103
|
+
/** `listIndexes` keeps a text index's fields in no declared order, so they are compared as a set. */
|
|
103
104
|
const INDEX_FACETS = new Set(['textIndex']);
|
|
104
105
|
/**
|
|
105
106
|
* Mongo has no columns to read, so a table's are the fields its indexes name, one node per field. Its
|
|
@@ -64,15 +64,15 @@ export class MongoSchemaGenerator extends MongoDialect {
|
|
|
64
64
|
.map((entry) => ({ ...entry, column: this.columnOf(meta, entry.column) }));
|
|
65
65
|
const [first] = columns;
|
|
66
66
|
const vector = options.type === 'vectorSearch' && typeof first?.column === 'string' ? meta.fields[first.column] : undefined;
|
|
67
|
-
const
|
|
68
|
-
? this.vectorSearchIndexName(options.name, entries[0].column)
|
|
69
|
-
: declaredIndexName(options.name, collectionName, entries);
|
|
67
|
+
const predicate = where && this.indexFilter(where, meta.entity, declaredIndexName(options.name, collectionName, entries));
|
|
70
68
|
return {
|
|
71
69
|
...options,
|
|
72
|
-
name
|
|
70
|
+
name: vector
|
|
71
|
+
? this.vectorSearchIndexName(options.name, entries[0].column)
|
|
72
|
+
: declaredIndexName(options.name, collectionName, entries, { where: predicate }),
|
|
73
73
|
entries,
|
|
74
74
|
unique: options.unique ?? false,
|
|
75
|
-
where:
|
|
75
|
+
where: predicate,
|
|
76
76
|
distance: options.distance ?? vector?.distance,
|
|
77
77
|
dimensions: vector?.dimensions,
|
|
78
78
|
};
|
|
@@ -192,21 +192,21 @@ export class MongoSchemaGenerator extends MongoDialect {
|
|
|
192
192
|
}
|
|
193
193
|
/** A collection and its indexes, which is all a document store has: a column, a constraint or SQL throws. */
|
|
194
194
|
generateOperation(operation) {
|
|
195
|
-
const render = (index) => renderIndexDefinition(index, refuseSql);
|
|
195
|
+
const render = (table, index) => renderIndexDefinition(table, index, refuseSql);
|
|
196
196
|
switch (operation.type) {
|
|
197
197
|
case 'createTable': {
|
|
198
198
|
const { name, columns, indexes } = operation.table;
|
|
199
199
|
if (columns.length) {
|
|
200
200
|
throw new UqlUsageError(`mongodb does not support columns in a migration (collection "${name}")`);
|
|
201
201
|
}
|
|
202
|
-
return this.createCollection(name, indexes.map(render));
|
|
202
|
+
return this.createCollection(name, indexes.map((index) => render(name, index)));
|
|
203
203
|
}
|
|
204
204
|
case 'dropTable':
|
|
205
205
|
return [serializeMongoCommand({ action: 'dropCollection', name: operation.tableName })];
|
|
206
206
|
case 'renameTable':
|
|
207
207
|
return [serializeMongoCommand({ action: 'renameCollection', from: operation.oldName, to: operation.newName })];
|
|
208
208
|
case 'createIndex':
|
|
209
|
-
return [this.generateCreateIndex(operation.tableName, render(operation.index))];
|
|
209
|
+
return [this.generateCreateIndex(operation.tableName, render(operation.tableName, operation.index))];
|
|
210
210
|
case 'dropIndex':
|
|
211
211
|
return [dropIndex(operation.tableName, operation.indexName)];
|
|
212
212
|
default:
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type RelationRows, type SelectTerm } from '../dialect/abstractSqlDialect.js';
|
|
2
2
|
import { type JsonAccessMode, type JsonSlot } from '../dialect/jsonSql.js';
|
|
3
3
|
import { MergeSqlDialect } from '../dialect/mergeSqlDialect.js';
|
|
4
|
-
import type { EntityMeta, SavepointCommand, FieldOptions, Query, QueryConflictPaths, QueryContext, QueryOptions, QueryPager, SqlDialectFeatures, SqlValues, TriggerRows, Type, UpdatePayload, VectorDistance, VectorMetric } from '../type/index.js';
|
|
4
|
+
import type { EntityMeta, SavepointCommand, FieldOptions, Query, QueryConflictPaths, QueryContext, QueryOptions, QueryPager, SqlDialectFeatures, SqlValues, TriggerRows, Type, UpdatePayload, VectorDistance, VectorMetric, TransactionOptions } from '../type/index.js';
|
|
5
5
|
/** Microsoft SQL Server 2017 and up. Identifiers are `"`-quoted, the ANSI spelling `tedious` enables. */
|
|
6
6
|
export declare class MsSqlDialect extends MergeSqlDialect {
|
|
7
7
|
#private;
|
|
@@ -14,10 +14,10 @@ export declare class MsSqlDialect extends MergeSqlDialect {
|
|
|
14
14
|
/** SQL Server saves a transaction under a name and rolls back to it, and releases none: the commit does. */
|
|
15
15
|
savepointStatement(command: SavepointCommand, name: string): string;
|
|
16
16
|
/**
|
|
17
|
-
* T-SQL has no inline form, so the level is set before the `BEGIN
|
|
18
|
-
* as statements would run; `MsSqlQuerier` opens its transactions through the driver
|
|
17
|
+
* T-SQL has no inline form, so the level is set before the `BEGIN`, and no read-only transaction. What a
|
|
18
|
+
* driver that sends these as statements would run; `MsSqlQuerier` opens its transactions through the driver.
|
|
19
19
|
*/
|
|
20
|
-
|
|
20
|
+
getBeginTransactionStatements({ isolationLevel }?: TransactionOptions): string[];
|
|
21
21
|
readonly booleanLiteral = "integer";
|
|
22
22
|
/**
|
|
23
23
|
* The clock in UTC, since `CURRENT_TIMESTAMP` is local time in the server's zone. No v7: `NEWSEQUENTIALID()` is
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ANSI_SQL_VALUES, fromRows, relationTermKey, } from '../dialect/abstractSqlDialect.js';
|
|
1
|
+
import { ANSI_SQL_VALUES, fromRows, relationTermKey, isolationSetBefore, } from '../dialect/abstractSqlDialect.js';
|
|
2
2
|
import { AGGREGATE_VALUE_ALIAS, JSON_PULL_ALIAS } from '../dialect/aliases.js';
|
|
3
3
|
import { BYTES_PREFIX } from '../dialect/hydrateColumn.js';
|
|
4
4
|
import { jsonArraySlotArgs, jsonPath, jsonSlotArgs } from '../dialect/jsonSql.js';
|
|
@@ -30,6 +30,7 @@ const MSSQL_FEATURES = {
|
|
|
30
30
|
serverSideCursors: false,
|
|
31
31
|
correlatedWrites: true,
|
|
32
32
|
rowLocks: { of: true, withWindow: true, placement: 'tableHint' },
|
|
33
|
+
writeReturning: false,
|
|
33
34
|
nullsSortLowest: true,
|
|
34
35
|
nullsOrdering: 'case',
|
|
35
36
|
textScoreIndexes: false,
|
|
@@ -90,10 +91,12 @@ export class MsSqlDialect extends MergeSqlDialect {
|
|
|
90
91
|
return statements[command];
|
|
91
92
|
}
|
|
92
93
|
/**
|
|
93
|
-
* T-SQL has no inline form, so the level is set before the `BEGIN
|
|
94
|
-
* as statements would run; `MsSqlQuerier` opens its transactions through the driver
|
|
94
|
+
* T-SQL has no inline form, so the level is set before the `BEGIN`, and no read-only transaction. What a
|
|
95
|
+
* driver that sends these as statements would run; `MsSqlQuerier` opens its transactions through the driver.
|
|
95
96
|
*/
|
|
96
|
-
|
|
97
|
+
getBeginTransactionStatements({ isolationLevel } = {}) {
|
|
98
|
+
return isolationSetBefore(isolationLevel, this.beginTransactionCommand);
|
|
99
|
+
}
|
|
97
100
|
booleanLiteral = 'integer';
|
|
98
101
|
/**
|
|
99
102
|
* The clock in UTC, since `CURRENT_TIMESTAMP` is local time in the server's zone. No v7: `NEWSEQUENTIALID()` is
|