uql-orm 0.104.1 → 0.105.1
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/README.md +24 -10
- package/dist/browser/querier/httpQuerier.d.ts +3 -3
- package/dist/browser/type/clientQuerier.d.ts +1 -1
- package/dist/browser/uql-browser.min.js +2 -2
- package/dist/browser/uql-browser.min.js.map +9 -9
- package/dist/cockroachdb/cockroachDialect.d.ts +2 -2
- package/dist/dialect/abstractDialect.d.ts +3 -3
- package/dist/dialect/abstractSqlDialect.d.ts +39 -37
- package/dist/dialect/abstractSqlDialect.js +35 -32
- package/dist/dialect/aliases.d.ts +2 -0
- package/dist/dialect/aliases.js +2 -0
- package/dist/dialect/jsonSql.js +2 -2
- package/dist/dialect/mergeSqlDialect.d.ts +2 -2
- package/dist/dialect/mysqlLikeSqlDialect.d.ts +7 -7
- package/dist/dialect/mysqlLikeSqlDialect.js +1 -1
- package/dist/dialect/operators.js +2 -4
- package/dist/dialect/queryJoins.d.ts +4 -2
- package/dist/dialect/vectorSqlDialect.d.ts +1 -1
- package/dist/dialect/vectorSqlDialect.js +1 -1
- package/dist/entity/column.d.ts +3 -0
- package/dist/entity/column.js +3 -0
- package/dist/entity/columnOptionsBuilder.d.ts +54 -0
- package/dist/entity/columnOptionsBuilder.js +108 -0
- package/dist/entity/decorator/bag.d.ts +5 -5
- package/dist/entity/decorator/entity.d.ts +3 -20
- package/dist/entity/decorator/entity.js +3 -37
- package/dist/entity/decorator/members.d.ts +46 -76
- package/dist/entity/decorator/members.js +18 -7
- package/dist/entity/index.d.ts +2 -1
- package/dist/entity/index.js +2 -1
- package/dist/entity/metadata/definition.d.ts +15 -26
- package/dist/entity/metadata/definition.js +344 -148
- package/dist/entity/metadata/schemaFixture/animal.d.ts +54 -0
- package/dist/entity/metadata/schemaFixture/animal.js +7 -0
- package/dist/entity/metadata/schemaFixture/author.d.ts +140 -0
- package/dist/entity/metadata/schemaFixture/author.js +25 -0
- package/dist/entity/metadata/schemaFixture/book.d.ts +192 -0
- package/dist/entity/metadata/schemaFixture/book.js +23 -0
- package/dist/http/contract.d.ts +2 -2
- package/dist/http/handler.d.ts +2 -2
- package/dist/http/query.js +12 -13
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -1
- package/dist/mariadb/mariaDialect.d.ts +2 -2
- package/dist/mariadb/mariaDialect.js +1 -1
- package/dist/mariadb/mariadbQuerier.js +1 -3
- package/dist/migrate/builder/columnBuilder.d.ts +20 -68
- package/dist/migrate/builder/columnBuilder.js +104 -110
- package/dist/migrate/builder/migrationBuilder.d.ts +3 -3
- package/dist/migrate/builder/migrationBuilder.js +7 -6
- package/dist/migrate/builder/tableBuilder.d.ts +3 -55
- package/dist/migrate/builder/tableBuilder.js +88 -204
- package/dist/migrate/builder/types.d.ts +52 -151
- package/dist/migrate/builder/types.js +0 -6
- package/dist/migrate/codegen/entityCodeGenerator.d.ts +2 -2
- package/dist/migrate/codegen/entityCodeGenerator.js +24 -24
- package/dist/migrate/codegen/entityTypes.d.ts +2 -2
- package/dist/migrate/codegen/fieldOptionsSource.d.ts +3 -1
- package/dist/migrate/codegen/fieldOptionsSource.js +35 -34
- package/dist/migrate/codegen/indexEntrySource.d.ts +16 -0
- package/dist/migrate/codegen/{indexDecoratorSource.js → indexEntrySource.js} +54 -27
- package/dist/migrate/ddl/defaultSql.js +7 -6
- package/dist/migrate/ddl/indexCapabilities.d.ts +8 -7
- package/dist/migrate/ddl/indexCapabilities.js +26 -13
- package/dist/migrate/ddl/indexDdl.d.ts +13 -13
- package/dist/migrate/ddl/indexDdl.js +33 -30
- package/dist/migrate/ddl/mysqlIndexDdl.d.ts +6 -11
- package/dist/migrate/ddl/mysqlIndexDdl.js +17 -28
- package/dist/migrate/ddl/pgIndexDdl.d.ts +2 -2
- package/dist/migrate/ddl/sqliteIndexDdl.d.ts +2 -2
- package/dist/migrate/drift/driftDetector.js +2 -2
- package/dist/migrate/generator/definitionToNode.d.ts +8 -2
- package/dist/migrate/generator/definitionToNode.js +4 -5
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +11 -2
- package/dist/migrate/introspection/jsonIndexEntry.d.ts +6 -0
- package/dist/migrate/introspection/jsonIndexEntry.js +120 -0
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +4 -4
- package/dist/migrate/introspection/mysqlIntrospector.js +25 -21
- package/dist/migrate/introspection/postgresIntrospector.d.ts +1 -1
- package/dist/migrate/introspection/postgresIntrospector.js +3 -4
- package/dist/migrate/introspection/sqliteIntrospector.js +24 -9
- package/dist/migrate/introspection/tableSchemaAST.js +5 -1
- package/dist/migrate/migrator.d.ts +2 -2
- package/dist/migrate/schemaChange.d.ts +3 -3
- package/dist/migrate/schemaChange.js +3 -2
- package/dist/migrate/schemaGenerator.d.ts +5 -5
- package/dist/migrate/schemaGenerator.js +1 -1
- package/dist/migrate/storage/databaseStorage.js +4 -4
- package/dist/mongodb/mongoDialect.d.ts +41 -44
- package/dist/mongodb/mongoDialect.js +118 -32
- package/dist/mongodb/mongoIntrospector.d.ts +3 -1
- package/dist/mongodb/mongoIntrospector.js +11 -5
- package/dist/mongodb/mongoSchemaGenerator.d.ts +4 -4
- package/dist/mongodb/mongoSchemaGenerator.js +35 -27
- package/dist/mongodb/mongodbQuerier.d.ts +1 -11
- package/dist/mongodb/mongodbQuerier.js +9 -43
- package/dist/mssql/mssqlDialect.d.ts +4 -4
- package/dist/postgres/postgresDialect.d.ts +2 -2
- package/dist/querier/abstractQuerier.d.ts +0 -1
- package/dist/querier/abstractQuerier.js +1 -20
- package/dist/querier/abstractSqlQuerier.d.ts +1 -1
- package/dist/querier/keyset.js +3 -3
- package/dist/schema/declaredColumn.d.ts +11 -0
- package/dist/schema/declaredColumn.js +21 -0
- package/dist/schema/indexColumns.d.ts +0 -3
- package/dist/schema/indexColumns.js +11 -7
- package/dist/schema/indexDifferences.d.ts +10 -5
- package/dist/schema/indexDifferences.js +42 -28
- package/dist/schema/schemaASTBuilder.d.ts +4 -4
- package/dist/schema/schemaASTBuilder.js +28 -36
- package/dist/schema/schemaASTDiffer.js +8 -4
- package/dist/schema/sqlExpression.d.ts +21 -17
- package/dist/schema/sqlExpression.js +11 -23
- package/dist/type/column.d.ts +328 -0
- package/dist/type/column.js +2 -0
- package/dist/type/config.d.ts +3 -3
- package/dist/type/dialect.d.ts +15 -14
- package/dist/type/dialect.js +1 -0
- package/dist/type/entity.d.ts +292 -281
- package/dist/type/entity.js +6 -1
- package/dist/type/index.d.ts +2 -0
- package/dist/type/index.js +2 -0
- package/dist/type/migration.d.ts +11 -11
- package/dist/type/querier.d.ts +2 -2
- package/dist/type/query.d.ts +44 -40
- package/dist/type/query.js +2 -3
- package/dist/type/queryAggregate.d.ts +22 -12
- package/dist/type/querySql.d.ts +22 -6
- package/dist/type/querySql.js +27 -9
- package/dist/type/schemaEntity.d.ts +194 -0
- package/dist/type/schemaEntity.js +1 -0
- package/dist/type/utility.d.ts +4 -7
- package/dist/type/vector.d.ts +1 -1
- package/dist/util/ddlExpression.util.d.ts +12 -13
- package/dist/util/ddlExpression.util.js +12 -21
- package/dist/util/dialect.util.d.ts +11 -5
- package/dist/util/dialect.util.js +31 -37
- package/dist/util/field.util.d.ts +10 -9
- package/dist/util/field.util.js +14 -10
- package/dist/util/fieldOption.util.d.ts +11 -95
- package/dist/util/fieldOption.util.js +49 -16
- package/dist/util/hook.util.d.ts +3 -15
- package/dist/util/hook.util.js +3 -16
- package/dist/util/index.d.ts +1 -0
- package/dist/util/index.js +1 -0
- package/dist/util/indexEntry.util.d.ts +45 -0
- package/dist/util/indexEntry.util.js +138 -0
- package/dist/util/relationQuery.util.d.ts +0 -1
- package/dist/util/relationQuery.util.js +0 -5
- package/dist/util/sql.d.ts +13 -10
- package/dist/util/sql.js +34 -16
- package/dist/util/sql.util.d.ts +4 -5
- package/dist/util/sql.util.js +7 -16
- package/dist/util/string.util.d.ts +2 -0
- package/dist/util/string.util.js +4 -0
- package/dist/util/triggerWrite.d.ts +5 -5
- package/dist/util/uqlError.d.ts +1 -1
- package/dist/util/uqlError.js +1 -1
- package/package.json +1 -1
- package/skills/uql-orm/SKILL.md +76 -25
- package/dist/migrate/codegen/indexDecoratorSource.d.ts +0 -16
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PgLikeSqlDialect } from '../dialect/pgLikeSqlDialect.js';
|
|
2
|
-
import type { QueryContext, SqlDialectFeatures, SqlDialectName, SqlValues,
|
|
2
|
+
import type { QueryContext, SqlDialectFeatures, SqlDialectName, SqlValues, AbstractType } from '../type/index.js';
|
|
3
3
|
/** What Postgres has: pgvector, the only engine with `halfvec` and `sparsevec`. */
|
|
4
4
|
export declare const POSTGRES_FEATURES: SqlDialectFeatures;
|
|
5
5
|
/** PostgreSQL, under every driver: `pg`, Neon, PGlite, `bun:sql`. Adds pgvector. */
|
|
@@ -15,5 +15,5 @@ export declare class PostgresDialect extends PgLikeSqlDialect {
|
|
|
15
15
|
* carries `reltuples = -1`, Postgres' "no statistic" (not `NULL`, and not `0`) since PG 14 - handed
|
|
16
16
|
* back raw it would read as a negative row count.
|
|
17
17
|
*/
|
|
18
|
-
estimatedCount<E>(ctx: QueryContext, entity:
|
|
18
|
+
estimatedCount<E>(ctx: QueryContext, entity: AbstractType<E>): void;
|
|
19
19
|
}
|
|
@@ -33,7 +33,6 @@ export declare abstract class AbstractQuerier implements Querier {
|
|
|
33
33
|
* each backend's read, so the rule reaches a find, a stream and a paged count alike.
|
|
34
34
|
*/
|
|
35
35
|
private assertLockable;
|
|
36
|
-
private validateProjectionQueryRecursive;
|
|
37
36
|
findOneById<E extends object, const Q extends QueryOne<E> = {}>(entity: Type<E>, id: EntityId<E>, q?: Captured<QueryOne<E>, Q>, opts?: QueryOptions): Promise<QueryFindResult<E, Q> | undefined>;
|
|
38
37
|
/** Find one record, the entity passed first or as the query's `$entity`. */
|
|
39
38
|
findOne<E extends object, const Q extends QueryOne<E> & {
|
|
@@ -2,7 +2,7 @@ import { getMeta, idOf, namesKey, relationOf } from '../entity/index.js';
|
|
|
2
2
|
import { SAVEPOINT_PREFIX } from '../dialect/aliases.js';
|
|
3
3
|
import { namesRows } from '../dialect/operators.js';
|
|
4
4
|
import { parseQueryLock } from '../type/index.js';
|
|
5
|
-
import { childrenExcept, childrenOf, chunk, clone, entityName, fillOnFields, filterFieldKeys,
|
|
5
|
+
import { childrenExcept, childrenOf, chunk, clone, entityName, fillOnFields, filterFieldKeys, getKeys, omitKeys, deletionOf, hasKeys, getRelationRequestSummary, guardWrite, idOnlyQuery, keyJoins, keySet, isPagedQuery, isScalarId, LoggerWrapper, normalizeScalarFieldSelection, parentJoins, pointAt, queryLoggerFor, parseRelationAtKey, rowKey, runHooks, securityConditions, someKey, targetJoins, whereEach, whereIds, whereKeysIn, whereWith, withDeleted, cascadingRelations, holdsForeignKey, } from '../util/index.js';
|
|
6
6
|
import { UqlOptimisticLockError, UqlUsageError } from '../util/uqlError.js';
|
|
7
7
|
import { keysetRead } from './keyset.js';
|
|
8
8
|
import { enrichError } from './queryError.js';
|
|
@@ -208,7 +208,6 @@ export class AbstractQuerier {
|
|
|
208
208
|
/** What every read is checked for before it runs, whichever backend runs it. */
|
|
209
209
|
validateReadQuery(entity, q) {
|
|
210
210
|
this.assertLockable(entity, q);
|
|
211
|
-
this.validateProjectionQueryRecursive(entity, q, entityName(getMeta(entity)));
|
|
212
211
|
}
|
|
213
212
|
/**
|
|
214
213
|
* Refuses a `$lock` the engine cannot take, then one outside a transaction, where the lock would
|
|
@@ -224,24 +223,6 @@ export class AbstractQuerier {
|
|
|
224
223
|
throw new UqlUsageError('$lock requires an open transaction');
|
|
225
224
|
}
|
|
226
225
|
}
|
|
227
|
-
validateProjectionQueryRecursive(entity, q, path) {
|
|
228
|
-
const meta = getMeta(entity);
|
|
229
|
-
if (q.$select && q.$exclude) {
|
|
230
|
-
for (const [key, value] of Object.entries(q.$select)) {
|
|
231
|
-
if (key in meta.fields && value) {
|
|
232
|
-
throw new UqlUsageError(`Cannot combine $select and $exclude when $select includes positive scalar fields (${key}) at ${path}. Use either $select (whitelist) or $exclude (subtractive) in a single query.`);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
forEachRequestedRelation(meta, q.$populate, (relKey, relValue) => {
|
|
237
|
-
const relOpts = relationOf(meta, relKey);
|
|
238
|
-
const relEntity = relOpts.entity();
|
|
239
|
-
const parsed = parseRelationQueryValue(relValue);
|
|
240
|
-
if (parsed.nested) {
|
|
241
|
-
this.validateProjectionQueryRecursive(relEntity, parsed.query, `${path}.${relKey}`);
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
226
|
async findOneById(entity, id, q = {}, opts) {
|
|
246
227
|
assertIdValue(entity, id);
|
|
247
228
|
return this.findOne(entity, { ...q, $where: { ...q.$where, ...whereIds(getMeta(entity), id) } }, opts);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AbstractSqlDialect } from '../dialect/index.js';
|
|
2
|
-
import type { EntityData, ExtraOptions, Query, QueryAggregate, QueryConflictPaths, QueryPage, QueryOptions, QuerySearch, QueryUpdateResult, RawRow, SavepointCommand, SqlQuerier, SqlStatement,
|
|
2
|
+
import type { EntityData, ExtraOptions, Query, QueryAggregate, QueryConflictPaths, QueryPage, QueryOptions, QuerySearch, QueryUpdateResult, RawRow, SavepointCommand, SqlQuerier, SqlStatement, Type, TransactionOptions, UpdatePayload } from '../type/index.js';
|
|
3
3
|
import type { SqlExecution } from '../util/sql.util.js';
|
|
4
4
|
import { AbstractQuerier, type UpsertedId, type WritesReturning } from './abstractQuerier.js';
|
|
5
5
|
/** A statement as it is sent: its SQL and the values it binds. */
|
package/dist/querier/keyset.js
CHANGED
|
@@ -25,7 +25,7 @@ export function keysetRead(meta, q, nullsSortLowest) {
|
|
|
25
25
|
const query = {
|
|
26
26
|
...rest,
|
|
27
27
|
// Rewrite the projection as an explicit `$select` that adds the sort keys it left out, which the cursor needs.
|
|
28
|
-
...(carried.length > 0 && { $select: keySet([...projected, ...carried])
|
|
28
|
+
...(carried.length > 0 && { $select: keySet([...projected, ...carried]) }),
|
|
29
29
|
$sort: backward ? sortOf(order.map(inverted).map((key) => [key.key, spelling(key)])) : rest.$sort,
|
|
30
30
|
$where: cursor === undefined
|
|
31
31
|
? rest.$where
|
|
@@ -119,7 +119,7 @@ function totalPrefix(meta, order) {
|
|
|
119
119
|
meta.ids,
|
|
120
120
|
...declaredIndexes(meta)
|
|
121
121
|
.filter((index) => index.unique && !index.where)
|
|
122
|
-
.map((index) => index.
|
|
122
|
+
.map((index) => index.entries.map(({ column }) => column))
|
|
123
123
|
.filter((columns) => columns.every((column) => typeof column === 'string' && notNull(column))),
|
|
124
124
|
].filter((columns) => columns.length > 0);
|
|
125
125
|
const seen = new Set();
|
|
@@ -190,7 +190,7 @@ function projectedKeys(meta, q) {
|
|
|
190
190
|
if (isSelectList(q.$select)) {
|
|
191
191
|
throw new UqlUsageError(`a page of '${entityName(meta)}' selects fields, which its cursor is read off`);
|
|
192
192
|
}
|
|
193
|
-
return normalizeScalarFieldSelection(meta, q.$select
|
|
193
|
+
return normalizeScalarFieldSelection(meta, q.$select);
|
|
194
194
|
}
|
|
195
195
|
/** Hashes the entity and each sort key's direction and placement, so a cursor only works with the sort it came from. */
|
|
196
196
|
function fingerprintOf(meta, order) {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Except, FieldMeta, QuerySql } from '../type/index.js';
|
|
2
|
+
import type { ColumnNode } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The column a field's options declare, whether an entity's table or a migration holds it: `soleKey` says whether it
|
|
5
|
+
* is its table's only key, which a numeric one auto-increments as, and `compile` renders its SQL.
|
|
6
|
+
*/
|
|
7
|
+
export declare function declaredColumn(field: FieldMeta, { name, soleKey, compile, }: {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly soleKey: boolean;
|
|
10
|
+
readonly compile: (sql: QuerySql) => string;
|
|
11
|
+
}): Except<ColumnNode, 'table'>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { declaresNotNull, isAutoIncrement } from '../util/field.util.js';
|
|
2
|
+
import { resolveColumnCanonicalType } from './canonicalType.js';
|
|
3
|
+
import { schemaDefault } from './sqlExpression.js';
|
|
4
|
+
/**
|
|
5
|
+
* The column a field's options declare, whether an entity's table or a migration holds it: `soleKey` says whether it
|
|
6
|
+
* is its table's only key, which a numeric one auto-increments as, and `compile` renders its SQL.
|
|
7
|
+
*/
|
|
8
|
+
export function declaredColumn(field, { name, soleKey, compile, }) {
|
|
9
|
+
return {
|
|
10
|
+
name,
|
|
11
|
+
type: resolveColumnCanonicalType(field),
|
|
12
|
+
nullable: !declaresNotNull(field),
|
|
13
|
+
defaultValue: schemaDefault(field.defaultValue, compile),
|
|
14
|
+
isPrimaryKey: field.isId === true,
|
|
15
|
+
isAutoIncrement: isAutoIncrement(field, soleKey),
|
|
16
|
+
isUnique: field.unique ?? false,
|
|
17
|
+
// A stamp is filled by a trigger, so it is a column like any other; only `stored: true` generates.
|
|
18
|
+
generatedAs: field.stored === true && field.computed ? compile(field.computed) : undefined,
|
|
19
|
+
comment: field.comment,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import type { IndexColumnSchema } from '../type/index.js';
|
|
2
1
|
import type { ColumnNode, IndexNode } from './types.js';
|
|
3
2
|
/** The table columns an index's entries resolve to, in order; an expression entry resolves to none. */
|
|
4
3
|
export declare function indexColumns(index: IndexNode): ColumnNode[];
|
|
5
|
-
/** Whether an entry is its column as stored, rather than an expression or a path the engine reprints in its own words. */
|
|
6
|
-
export declare function isColumnEntry(entry: IndexColumnSchema): boolean;
|
|
7
4
|
/**
|
|
8
5
|
* The columns an index can look every row up by, in order, as far as its entries are whole columns: none
|
|
9
6
|
* where it holds only some rows (partial) or is of a type an equality lookup does not read.
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
+
import { entryColumns, wholeColumn } from '../util/indexEntry.util.js';
|
|
1
2
|
/** The table columns an index's entries resolve to, in order; an expression entry resolves to none. */
|
|
2
3
|
export function indexColumns(index) {
|
|
3
|
-
return index.entries.flatMap((
|
|
4
|
-
}
|
|
5
|
-
/** Whether an entry is its column as stored, rather than an expression or a path the engine reprints in its own words. */
|
|
6
|
-
export function isColumnEntry(entry) {
|
|
7
|
-
return !entry.expression && !entry.jsonPath && !entry.jsonArray;
|
|
4
|
+
return entryColumns(index.entries).flatMap((column) => index.table.columns.get(column) ?? []);
|
|
8
5
|
}
|
|
9
6
|
/** Whether an equality lookup reads each index type; the rest answer text, vector or range questions. Total, so a new type says. */
|
|
10
7
|
const ANSWERS_LOOKUP = {
|
|
@@ -27,6 +24,13 @@ export function lookupColumns(index) {
|
|
|
27
24
|
if (index.where !== undefined || !ANSWERS_LOOKUP[index.type ?? 'btree']) {
|
|
28
25
|
return [];
|
|
29
26
|
}
|
|
30
|
-
const
|
|
31
|
-
|
|
27
|
+
const columns = [];
|
|
28
|
+
for (const entry of index.entries) {
|
|
29
|
+
const column = wholeColumn(entry);
|
|
30
|
+
if (column === undefined) {
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
columns.push(column);
|
|
34
|
+
}
|
|
35
|
+
return columns;
|
|
32
36
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { IndexSchema } from '../type/index.js';
|
|
2
|
-
import type { IndexNode } from './types.js';
|
|
2
|
+
import type { IndexNode, TableNode } from './types.js';
|
|
3
3
|
/**
|
|
4
4
|
* What an introspector reports about an index, and so all a diff may compare; apart from `IndexFeature`, what an engine emits.
|
|
5
5
|
* `vector` is whether it is a vector index at all, for an engine with one vector index whatever type declared it;
|
|
@@ -39,12 +39,17 @@ export type IndexChange<I> = {
|
|
|
39
39
|
readonly to: I;
|
|
40
40
|
readonly description: string;
|
|
41
41
|
};
|
|
42
|
+
/** The indexes `current` lacks, every one it has that none declared, and the ones to rebuild, as `facets` see them. */
|
|
43
|
+
export declare function pairedIndexChanges<I extends IndexSchema>(declared: readonly I[], current: readonly IndexNode[], facets: ReadonlySet<IndexFacet>): {
|
|
44
|
+
readonly created: I[];
|
|
45
|
+
readonly dropped: IndexNode[];
|
|
46
|
+
readonly rebuilt: IndexChange<I>[];
|
|
47
|
+
};
|
|
42
48
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* dropped: any other may have been made outside the ORM, so it is `kept`.
|
|
49
|
+
* {@link pairedIndexChanges} on `table`, dropping only an unpaired index uql named, or whose name the entity
|
|
50
|
+
* claims: any other may have been made outside the ORM, so it is `kept`.
|
|
46
51
|
*/
|
|
47
|
-
export declare function indexChanges<I extends IndexSchema>(
|
|
52
|
+
export declare function indexChanges<I extends IndexSchema>(declared: readonly I[], table: Pick<TableNode, 'name' | 'indexes' | 'indexFacets'>): {
|
|
48
53
|
changes: IndexChange<I>[];
|
|
49
54
|
kept: IndexNode[];
|
|
50
55
|
};
|
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
import { indexDistance, isVectorIndexType } from '../type/vector.js';
|
|
2
|
+
import { declaredIndexName } from '../util/ddlExpression.util.js';
|
|
2
3
|
import { fulltextConfig } from '../util/dialect.util.js';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
4
|
+
import { indexEntryNamePart, isStored, storedColumns } from '../util/indexEntry.util.js';
|
|
5
|
+
import { NAME_HASH_LENGTH, TABLE_SEPARATOR } from '../util/sql.util.js';
|
|
5
6
|
import { matchByKey } from './matchByKey.js';
|
|
6
7
|
/**
|
|
7
8
|
* Whether the table has this index already, by shape rather than name, uniqueness included. An index
|
|
8
9
|
* over an expression, whose text the engine reprints, falls back to its name.
|
|
9
10
|
*/
|
|
10
11
|
export function indexSignature(index) {
|
|
11
|
-
const
|
|
12
|
-
const identity = comparable
|
|
13
|
-
? index.entries.map((entry) => entry.column).join(',')
|
|
14
|
-
: `name:${indexNameStem(index.name)}`;
|
|
12
|
+
const identity = storedColumns(index.entries)?.join(',') ?? `name:${indexNameStem(index.name)}`;
|
|
15
13
|
return `${index.unique ? 'unique' : 'plain'}(${identity})`;
|
|
16
14
|
}
|
|
17
15
|
/**
|
|
@@ -21,7 +19,7 @@ export function indexSignature(index) {
|
|
|
21
19
|
export function indexNameStem(name) {
|
|
22
20
|
const withoutSuffix = name.replace(KIND_SUFFIX, '');
|
|
23
21
|
const bare = withoutSuffix === name ? name.replace(KIND_PREFIX, '') : withoutSuffix;
|
|
24
|
-
return bare.
|
|
22
|
+
return bare.replaceAll(TABLE_SEPARATOR, '_');
|
|
25
23
|
}
|
|
26
24
|
/**
|
|
27
25
|
* Pairs by name, then what is left by shape: an index the database has under another name is still
|
|
@@ -44,33 +42,50 @@ function splitPartial(indexes) {
|
|
|
44
42
|
partial: indexes.filter((index) => index.where !== undefined),
|
|
45
43
|
};
|
|
46
44
|
}
|
|
47
|
-
/**
|
|
48
|
-
|
|
49
|
-
* `facets` let the engine report. Only an unpaired index uql named, or whose name the entity claims, is
|
|
50
|
-
* dropped: any other may have been made outside the ORM, so it is `kept`.
|
|
51
|
-
*/
|
|
52
|
-
export function indexChanges(table, declared, current, facets) {
|
|
45
|
+
/** The indexes `current` lacks, every one it has that none declared, and the ones to rebuild, as `facets` see them. */
|
|
46
|
+
export function pairedIndexChanges(declared, current, facets) {
|
|
53
47
|
const { created, dropped, matched } = pairIndexes(declared, current);
|
|
54
|
-
const claimed = new Set(declared.map((index) => index.name));
|
|
55
|
-
const owned = (index) => claimed.has(index.name) || hasDerivedName(table, index);
|
|
56
48
|
const rebuilt = matched.flatMap(([to, from]) => {
|
|
57
49
|
const differences = describeIndexDifferences(to, from, facets);
|
|
58
50
|
return differences.length ? [{ from, to, description: differences.join(', ') }] : [];
|
|
59
51
|
});
|
|
52
|
+
return { created, dropped, rebuilt };
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* {@link pairedIndexChanges} on `table`, dropping only an unpaired index uql named, or whose name the entity
|
|
56
|
+
* claims: any other may have been made outside the ORM, so it is `kept`.
|
|
57
|
+
*/
|
|
58
|
+
export function indexChanges(declared, table) {
|
|
59
|
+
const { created, dropped, rebuilt } = pairedIndexChanges(declared, table.indexes, table.indexFacets);
|
|
60
|
+
const claimed = new Set(declared.map((index) => index.name));
|
|
61
|
+
const isOwned = (index) => claimed.has(index.name) || hasDerivedName(table.name, index);
|
|
62
|
+
const { owned = [], kept = [] } = Object.groupBy(dropped, (index) => (isOwned(index) ? 'owned' : 'kept'));
|
|
60
63
|
return {
|
|
61
|
-
changes: [...created.map((to) => ({ to })), ...
|
|
62
|
-
kept
|
|
64
|
+
changes: [...created.map((to) => ({ to })), ...owned.map((from) => ({ from })), ...rebuilt],
|
|
65
|
+
kept,
|
|
63
66
|
};
|
|
64
67
|
}
|
|
65
68
|
/**
|
|
66
|
-
* Whether uql named the index itself, from
|
|
67
|
-
*
|
|
69
|
+
* Whether uql named the index itself, from the entries the database reports: the name it would derive for them, with
|
|
70
|
+
* the predicate's hash read off the name and a unique one's `_uk` as the migration builder names it, or as an earlier
|
|
71
|
+
* uql derived it: `expr<n>` for a JSON path, or `idx_<table>_<parts>`.
|
|
68
72
|
*/
|
|
69
73
|
function hasDerivedName(table, index) {
|
|
70
|
-
const
|
|
71
|
-
|
|
74
|
+
const name = index.where === undefined ? index.name : index.name.replace(PREDICATE_HASH, '');
|
|
75
|
+
const earlier = index.entries.map((entry) => entry.json === undefined ? entry : { expression: '' });
|
|
76
|
+
return [index.entries, earlier].some((entries) => {
|
|
77
|
+
const parts = entries.map(indexEntryNamePart);
|
|
78
|
+
return (name === declaredIndexName(undefined, table, entries) ||
|
|
79
|
+
(index.unique && name === declaredIndexName(undefined, table, entries, { unique: true })) ||
|
|
80
|
+
name === `idx_${table}_${parts.join('_')}`);
|
|
81
|
+
});
|
|
72
82
|
}
|
|
73
|
-
/**
|
|
83
|
+
/** The hash a partial index's derived name carries before its kind. */
|
|
84
|
+
const PREDICATE_HASH = new RegExp(`_[0-9a-f]{${NAME_HASH_LENGTH}}(?=_(?:idx|uk)$)`);
|
|
85
|
+
/**
|
|
86
|
+
* The kind a name ends in: `pk`, `fk`, `idx` and `uk` are what uql derives, and `ck` and `uq` are read too, though
|
|
87
|
+
* no name it derives ends in them.
|
|
88
|
+
*/
|
|
74
89
|
const KIND_SUFFIX = /_(?:idx|fk|ck|pk|uk|uq)$/i;
|
|
75
90
|
/**
|
|
76
91
|
* What it only ever *reads*: a database an older uql created holds `idx_User_email`, and one it did not
|
|
@@ -85,12 +100,11 @@ const KIND_PREFIX = /^(?:idx|fk|ck|pk|uk|uq)_/i;
|
|
|
85
100
|
*/
|
|
86
101
|
export function describeIndexDifferences(source, target, facets) {
|
|
87
102
|
const differences = [];
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
103
|
+
const [sourceColumns, targetColumns] = [source, target].map((index) => isStored(index.entries)
|
|
104
|
+
? textFieldOrder(index, facets, index.entries.map((entry) => entrySignature(entry, facets))).join(', ')
|
|
105
|
+
: undefined);
|
|
106
|
+
if (sourceColumns !== undefined && targetColumns !== undefined && sourceColumns !== targetColumns) {
|
|
107
|
+
differences.push(`columns: (${targetColumns}) -> (${sourceColumns})`);
|
|
94
108
|
}
|
|
95
109
|
if (facets.has('textIndex') && source.type === 'fulltext' && target.type === 'fulltext') {
|
|
96
110
|
const [expected, actual] = [fulltextConfig(source), fulltextConfig(target)];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { EntityMeta, EntityWhereMeta, FieldOptions,
|
|
1
|
+
import type { EntityMeta, EntityWhereMeta, FieldOptions, AbstractType } from '../type/index.js';
|
|
2
2
|
import { SchemaAST } from './schemaAST.js';
|
|
3
3
|
import { type ForeignKeyAction, type TriggerSchema } from './types.js';
|
|
4
4
|
/**
|
|
@@ -17,9 +17,9 @@ export interface BuildSchemaASTOptions {
|
|
|
17
17
|
* The text of SQL an entity declares - a check, a stored computed column, an index expression or
|
|
18
18
|
* predicate - which only a dialect can render. `SqlSchemaGenerator.buildAST` supplies it.
|
|
19
19
|
*/
|
|
20
|
-
compileDdl?: (sql: EntityWhereMeta<object>, entity:
|
|
20
|
+
compileDdl?: (sql: EntityWhereMeta<object>, entity: AbstractType<object>) => string;
|
|
21
21
|
/** A partial index's predicate as the engine writes it, `compileDdl` where none is given. */
|
|
22
|
-
compileIndexPredicate?: (where: EntityWhereMeta<object>, entity:
|
|
22
|
+
compileIndexPredicate?: (where: EntityWhereMeta<object>, entity: AbstractType<object>, indexName: string) => string;
|
|
23
23
|
/** Whether a weighted fulltext index declares one of its own for each heavier column, as MySQL scores through one. */
|
|
24
24
|
textScoreIndexes?: boolean;
|
|
25
25
|
/** Whether a column a vector index covers is `NOT NULL` whatever the entity declares, as MariaDB demands. */
|
|
@@ -33,4 +33,4 @@ export interface BuildSchemaASTOptions {
|
|
|
33
33
|
* Three passes, because each needs the one before it to have finished for *every* entity: a relation
|
|
34
34
|
* resolves against a table another entity declares, and an index against the columns of its own.
|
|
35
35
|
*/
|
|
36
|
-
export declare function buildSchemaAST(entities: readonly
|
|
36
|
+
export declare function buildSchemaAST(entities: readonly AbstractType<object>[], options?: BuildSchemaASTOptions): SchemaAST;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { fieldOf, foreignKeysOf, getMeta } from '../entity/metadata/definition.js';
|
|
2
|
-
import { declaredIndexes,
|
|
2
|
+
import { declaredIndexes, namedIndex, enumCheck, ownedCheck } from '../util/ddlExpression.util.js';
|
|
3
3
|
import { fulltextWeights, textWeightSteps } from '../util/dialect.util.js';
|
|
4
|
-
import {
|
|
4
|
+
import { isInlinedExpression, isSoleIdField } from '../util/field.util.js';
|
|
5
|
+
import { entryColumn, entryColumns, renamedEntry, renderIndexColumn } from '../util/indexEntry.util.js';
|
|
5
6
|
import { definedEntries, entityName } from '../util/object.util.js';
|
|
6
7
|
import { derivedForeignKeyName, derivedIndexName } from '../util/sql.util.js';
|
|
7
8
|
import { UqlUsageError } from '../util/uqlError.js';
|
|
8
|
-
import {
|
|
9
|
+
import { declaredColumn } from './declaredColumn.js';
|
|
9
10
|
import { lookupColumns } from './indexColumns.js';
|
|
10
11
|
import { createTableNode, keyOfColumns, SchemaAST } from './schemaAST.js';
|
|
11
|
-
import { schemaDefault } from './sqlExpression.js';
|
|
12
12
|
import { DEFAULT_FOREIGN_KEY_ACTION, } from './types.js';
|
|
13
13
|
/**
|
|
14
14
|
* Build a SchemaAST from entity classes (decorated with `@Entity`, `@Field`, etc.).
|
|
@@ -46,11 +46,9 @@ export function buildSchemaAST(entities, options = {}) {
|
|
|
46
46
|
function refuseDdl() {
|
|
47
47
|
throw new UqlUsageError('building the schema of an entity that declares SQL (a check, a stored computed column, an index expression or predicate) needs a dialect to render it: pass `compileDdl`, as `SqlSchemaGenerator.buildAST` does');
|
|
48
48
|
}
|
|
49
|
-
/** The
|
|
49
|
+
/** The members a vector index of `meta` covers by name. */
|
|
50
50
|
function vectorIndexedEntries(meta) {
|
|
51
|
-
return new Set((meta.indexes ?? [])
|
|
52
|
-
.filter((index) => index.type === 'vector')
|
|
53
|
-
.flatMap((index) => index.columns.map((entry) => entry.column)));
|
|
51
|
+
return new Set((meta.indexes ?? []).filter((index) => index.type === 'vector').flatMap((index) => entryColumns(index.entries)));
|
|
54
52
|
}
|
|
55
53
|
/**
|
|
56
54
|
* Add a table from entity metadata.
|
|
@@ -67,20 +65,13 @@ function addTableFromEntity(ctx, meta) {
|
|
|
67
65
|
if (isInlinedExpression(field))
|
|
68
66
|
continue;
|
|
69
67
|
const columnName = ctx.resolveColumnName(key, field);
|
|
70
|
-
const type = resolveColumnCanonicalType(field);
|
|
71
|
-
const isPrimaryKey = field.isId === true;
|
|
72
|
-
const isSoleKey = isSoleIdField(meta, field);
|
|
73
68
|
const column = {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
isUnique: field.unique ?? false,
|
|
81
|
-
// A stamp is filled by a trigger, so it is a column like any other; only `stored: true` generates.
|
|
82
|
-
generatedAs: field.stored === true && field.computed ? ctx.compileDdl(field.computed, meta.entity) : undefined,
|
|
83
|
-
comment: field.comment,
|
|
69
|
+
...declaredColumn(field, {
|
|
70
|
+
name: columnName,
|
|
71
|
+
soleKey: isSoleIdField(meta, field),
|
|
72
|
+
compile: (sql) => ctx.compileDdl(sql, meta.entity),
|
|
73
|
+
}),
|
|
74
|
+
...(notNull.has(key) && { nullable: false }),
|
|
84
75
|
table,
|
|
85
76
|
};
|
|
86
77
|
columns.set(columnName, column);
|
|
@@ -93,7 +84,7 @@ function addTableFromEntity(ctx, meta) {
|
|
|
93
84
|
}
|
|
94
85
|
/**
|
|
95
86
|
* Add a relationship for each foreign key the entity holds, whether a relation declares it or a bare
|
|
96
|
-
*
|
|
87
|
+
* `f.references()` does.
|
|
97
88
|
*/
|
|
98
89
|
function addRelationshipsFromEntity(ctx, meta, table) {
|
|
99
90
|
for (const foreignKey of foreignKeysOf(meta)) {
|
|
@@ -122,15 +113,15 @@ function addRelationshipsFromEntity(ctx, meta, table) {
|
|
|
122
113
|
type: foreignKey.cardinality === 'm1' ? 'ManyToOne' : 'OneToOne',
|
|
123
114
|
from: { table, columns: localColumns },
|
|
124
115
|
to: { table: relatedTable, columns: foreignColumns },
|
|
125
|
-
// Falls back to the FK column's own `onDelete`, which is what makes a bare
|
|
126
|
-
// references
|
|
116
|
+
// Falls back to the FK column's own `onDelete`, which is what makes a bare
|
|
117
|
+
// `f.references(() => X).onDelete(...)` work with no relation declared at all.
|
|
127
118
|
onDelete: foreignKey.onDelete ?? meta.fields[foreignKey.references[0].local]?.onDelete ?? ctx.defaultForeignKeyAction,
|
|
128
119
|
onUpdate: foreignKey.onUpdate ?? ctx.defaultForeignKeyAction,
|
|
129
120
|
});
|
|
130
121
|
}
|
|
131
122
|
}
|
|
132
123
|
/**
|
|
133
|
-
* Add indexes from field options (
|
|
124
|
+
* Add indexes from field options (`.index()`), from `indexes`, and for every foreign
|
|
134
125
|
* key none of those already serves.
|
|
135
126
|
*/
|
|
136
127
|
function addIndexesFromEntity(ctx, meta, table) {
|
|
@@ -173,27 +164,27 @@ function fieldColumn(ctx, meta, key) {
|
|
|
173
164
|
* names resolved, so the generator renders exactly what was declared. Every option not resolved here is
|
|
174
165
|
* spread through, so an option added to indexes later cannot be lost.
|
|
175
166
|
*/
|
|
176
|
-
function addCompositeIndex(ctx, table, meta, {
|
|
167
|
+
function addCompositeIndex(ctx, table, meta, { entries, include, where, ...options }) {
|
|
177
168
|
// An entry survives if it is an expression (nothing to resolve) or names a column that exists;
|
|
178
169
|
// an index left with none is dropped, as one naming only unknown columns is.
|
|
179
|
-
const resolved =
|
|
180
|
-
|
|
170
|
+
const resolved = entries.flatMap((entry) => {
|
|
171
|
+
const key = entryColumn(entry);
|
|
172
|
+
if (key === undefined)
|
|
181
173
|
return [entry];
|
|
182
|
-
const column = fieldColumn(ctx, meta,
|
|
183
|
-
return column && table.columns.has(column) ? [
|
|
174
|
+
const column = fieldColumn(ctx, meta, key);
|
|
175
|
+
return column && table.columns.has(column) ? [renamedEntry(entry, () => column)] : [];
|
|
184
176
|
});
|
|
185
177
|
if (!resolved.length)
|
|
186
178
|
return;
|
|
187
|
-
const
|
|
179
|
+
const named = namedIndex(options.name, table.name, resolved, (label) => where && ctx.compileIndexPredicate(where, meta.entity, label));
|
|
188
180
|
ctx.ast.addIndex({
|
|
189
181
|
...options,
|
|
190
|
-
|
|
182
|
+
...named,
|
|
191
183
|
table,
|
|
192
184
|
entries: resolved.map((entry) => renderIndexColumn(entry, (sql) => ctx.compileDdl(sql, meta.entity))),
|
|
193
185
|
// An `include` column is named like any other, so a custom naming has to reach it too.
|
|
194
186
|
include: include?.map((column) => fieldColumn(ctx, meta, column) ?? column),
|
|
195
187
|
unique: options.unique ?? false,
|
|
196
|
-
where: predicate,
|
|
197
188
|
});
|
|
198
189
|
if (ctx.textScoreIndexes) {
|
|
199
190
|
addTextScoreIndexes(ctx, table, options.type, resolved);
|
|
@@ -206,11 +197,12 @@ function addTextScoreIndexes(ctx, table, type, entries) {
|
|
|
206
197
|
return;
|
|
207
198
|
const { extra } = textWeightSteps(weights);
|
|
208
199
|
entries.forEach((entry, at) => {
|
|
209
|
-
|
|
200
|
+
const { column } = entry;
|
|
201
|
+
if (extra[at] && column !== undefined) {
|
|
210
202
|
ctx.ast.addIndex({
|
|
211
|
-
name: derivedIndexName(table.name, [
|
|
203
|
+
name: derivedIndexName(table.name, [column, 'score']),
|
|
212
204
|
table,
|
|
213
|
-
entries: [{ column
|
|
205
|
+
entries: [{ column }],
|
|
214
206
|
unique: false,
|
|
215
207
|
type: 'fulltext',
|
|
216
208
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isOwnedName, qualifyName } from '../util/sql.util.js';
|
|
2
2
|
import { areTypesEqual, isBreakingTypeChange } from './canonicalType.js';
|
|
3
|
-
import {
|
|
3
|
+
import { pairedIndexChanges } from './indexDifferences.js';
|
|
4
4
|
import { matchByKey, pairUnique } from './matchByKey.js';
|
|
5
5
|
import { writtenDefault } from './sqlExpression.js';
|
|
6
6
|
import { DEFAULT_FOREIGN_KEY_ACTION } from './types.js';
|
|
@@ -131,9 +131,13 @@ function matchTables(desired, actual, opts) {
|
|
|
131
131
|
* name when there is one.
|
|
132
132
|
*/
|
|
133
133
|
function diffTableIndexes(source, target) {
|
|
134
|
-
const {
|
|
135
|
-
const
|
|
136
|
-
|
|
134
|
+
const { created, dropped, rebuilt } = pairedIndexChanges(source.indexes, target.indexes, target.indexFacets);
|
|
135
|
+
const changes = [
|
|
136
|
+
...created.map((to) => ({ to })),
|
|
137
|
+
...rebuilt,
|
|
138
|
+
...dropped.map((from) => ({ from })),
|
|
139
|
+
];
|
|
140
|
+
return changes.map((change) => ({
|
|
137
141
|
...change,
|
|
138
142
|
table: source.name,
|
|
139
143
|
name: change.to === undefined ? change.from.name : change.to.name,
|
|
@@ -1,24 +1,27 @@
|
|
|
1
1
|
import { QuerySql, type SqlValueName } from '../type/index.js';
|
|
2
|
+
declare const EXPRESSION: unique symbol;
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
4
|
+
* A column default that is SQL rather than a literal: a value uql exports by name, which each engine spells for the
|
|
5
|
+
* column it fills, or raw SQL carrying its own text. Built by `SqlExpression`, branded by a symbol JSON cannot carry.
|
|
5
6
|
*/
|
|
6
|
-
export type
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
readonly kind:
|
|
13
|
-
readonly sql
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
7
|
+
export type SqlExpression = {
|
|
8
|
+
readonly [EXPRESSION]: true;
|
|
9
|
+
} & ({
|
|
10
|
+
readonly kind: SqlValueName;
|
|
11
|
+
readonly sql?: undefined;
|
|
12
|
+
} | {
|
|
13
|
+
readonly kind: 'sql';
|
|
14
|
+
readonly sql: string;
|
|
15
|
+
});
|
|
16
|
+
export declare const SqlExpression: {
|
|
17
|
+
named: (kind: SqlValueName) => SqlExpression;
|
|
18
|
+
raw: (sql: string) => SqlExpression;
|
|
17
19
|
/** Wraps `sql` in parentheses, which every engine accepts for a default expression and SQLite and MySQL require. */
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
parenthesized: (sql: string) => SqlExpression;
|
|
21
|
+
isExpression: (value: unknown) => value is SqlExpression;
|
|
22
|
+
};
|
|
23
|
+
/** Its SQL, or its kind, as diffs and drift reports show it. */
|
|
24
|
+
export declare function sqlExpressionText(expression: SqlExpression): string;
|
|
22
25
|
/**
|
|
23
26
|
* A default in schema form: a value uql exports (`currentTimestamp`) becomes its kind, so each engine spells it
|
|
24
27
|
* for the column it fills; other `raw` becomes the SQL `compile` renders, parenthesized; a literal is kept.
|
|
@@ -29,3 +32,4 @@ export declare function schemaDefault(value: unknown, compile: (sql: QuerySql) =
|
|
|
29
32
|
* holding a JSON object or list as JSON with its keys in order, equal as documents, since `jsonb` reprints them.
|
|
30
33
|
*/
|
|
31
34
|
export declare function writtenDefault(value: unknown): string;
|
|
35
|
+
export {};
|
|
@@ -1,29 +1,17 @@
|
|
|
1
1
|
import { QuerySql, SQL_VALUE_NAMES } from '../type/index.js';
|
|
2
2
|
import { isRecord } from '../util/object.util.js';
|
|
3
3
|
import { SQL_VALUES } from '../util/sql.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export class SqlExpression {
|
|
9
|
-
kind;
|
|
10
|
-
sql;
|
|
11
|
-
/** `sql` is set only for the `sql` kind, which carries its own text verbatim. */
|
|
12
|
-
constructor(kind, sql) {
|
|
13
|
-
this.kind = kind;
|
|
14
|
-
this.sql = sql;
|
|
15
|
-
}
|
|
16
|
-
static isExpression(value) {
|
|
17
|
-
return value instanceof SqlExpression;
|
|
18
|
-
}
|
|
4
|
+
const EXPRESSION = Symbol('sqlExpression');
|
|
5
|
+
export const SqlExpression = {
|
|
6
|
+
named: (kind) => ({ [EXPRESSION]: true, kind }),
|
|
7
|
+
raw: (sql) => ({ [EXPRESSION]: true, kind: 'sql', sql }),
|
|
19
8
|
/** Wraps `sql` in parentheses, which every engine accepts for a default expression and SQLite and MySQL require. */
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
9
|
+
parenthesized: (sql) => SqlExpression.raw(`(${sql})`),
|
|
10
|
+
isExpression: (value) => typeof value === 'object' && value !== null && EXPRESSION in value,
|
|
11
|
+
};
|
|
12
|
+
/** Its SQL, or its kind, as diffs and drift reports show it. */
|
|
13
|
+
export function sqlExpressionText(expression) {
|
|
14
|
+
return expression.sql ?? expression.kind;
|
|
27
15
|
}
|
|
28
16
|
const NAME_OF = new Map(SQL_VALUE_NAMES.map((name) => [SQL_VALUES[name], name]));
|
|
29
17
|
/**
|
|
@@ -33,7 +21,7 @@ const NAME_OF = new Map(SQL_VALUE_NAMES.map((name) => [SQL_VALUES[name], name]))
|
|
|
33
21
|
export function schemaDefault(value, compile) {
|
|
34
22
|
const name = NAME_OF.get(value);
|
|
35
23
|
if (name) {
|
|
36
|
-
return
|
|
24
|
+
return SqlExpression.named(name);
|
|
37
25
|
}
|
|
38
26
|
return value instanceof QuerySql ? SqlExpression.parenthesized(compile(value)) : value;
|
|
39
27
|
}
|