uql-orm 0.91.0 → 0.92.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/browser/http/bus.js +3 -4
- package/dist/browser/http/http.d.ts +1 -2
- package/dist/browser/http/http.js +8 -7
- package/dist/browser/index.d.ts +0 -1
- package/dist/browser/index.js +0 -1
- package/dist/browser/querier/httpQuerier.d.ts +18 -20
- package/dist/browser/querier/httpQuerier.js +20 -56
- package/dist/browser/type/index.d.ts +0 -1
- package/dist/browser/type/index.js +0 -1
- package/dist/browser/uql-browser.min.js +2 -2
- package/dist/browser/uql-browser.min.js.map +11 -12
- package/dist/cockroachdb/cockroachDialect.d.ts +4 -1
- package/dist/cockroachdb/cockroachDialect.js +5 -1
- package/dist/dialect/abstractDialect.d.ts +1 -1
- package/dist/dialect/abstractDialect.js +4 -1
- package/dist/dialect/abstractSqlDialect.d.ts +24 -28
- package/dist/dialect/abstractSqlDialect.js +47 -45
- package/dist/dialect/aliases.d.ts +4 -1
- package/dist/dialect/aliases.js +5 -2
- package/dist/dialect/hydrateColumn.js +3 -10
- package/dist/dialect/jsonSql.d.ts +4 -1
- package/dist/dialect/jsonSql.js +4 -1
- package/dist/dialect/mergeSqlDialect.d.ts +4 -3
- package/dist/dialect/mergeSqlDialect.js +5 -4
- package/dist/dialect/mysqlLikeSqlDialect.d.ts +7 -21
- package/dist/dialect/mysqlLikeSqlDialect.js +9 -36
- package/dist/dialect/pgLikeSqlDialect.d.ts +3 -7
- package/dist/dialect/pgLikeSqlDialect.js +11 -21
- package/dist/http/contract.d.ts +4 -0
- package/dist/http/contract.js +1 -0
- package/dist/http/handler.d.ts +2 -2
- package/dist/http/handler.js +17 -19
- package/dist/http/query.d.ts +5 -3
- package/dist/http/query.js +42 -42
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/maria/mariaDialect.d.ts +2 -3
- package/dist/maria/mariaDialect.js +1 -5
- package/dist/migrate/builder/columnBuilder.d.ts +5 -15
- package/dist/migrate/builder/columnBuilder.js +50 -86
- package/dist/migrate/builder/expressions.d.ts +12 -18
- package/dist/migrate/builder/expressions.js +39 -42
- package/dist/migrate/builder/migrationBuilder.d.ts +3 -11
- package/dist/migrate/builder/migrationBuilder.js +41 -81
- package/dist/migrate/builder/tableBuilder.d.ts +0 -7
- package/dist/migrate/builder/tableBuilder.js +17 -28
- package/dist/migrate/builder/types.d.ts +2 -2
- package/dist/migrate/codegen/entityCodeGenerator.js +7 -13
- package/dist/migrate/codegen/entityTypes.js +6 -20
- package/dist/migrate/codegen/fieldOptionsSource.d.ts +2 -2
- package/dist/migrate/codegen/fieldOptionsSource.js +27 -12
- package/dist/migrate/codegen/indexDecoratorSource.js +64 -41
- package/dist/migrate/ddl/index.d.ts +2 -4
- package/dist/migrate/ddl/index.js +14 -5
- package/dist/migrate/ddl/mssqlTableDdl.d.ts +1 -0
- package/dist/migrate/ddl/mssqlTableDdl.js +4 -1
- package/dist/migrate/ddl/mysqlTableDdl.d.ts +17 -0
- package/dist/migrate/ddl/mysqlTableDdl.js +33 -0
- package/dist/migrate/ddl/pgTableDdl.d.ts +7 -0
- package/dist/migrate/ddl/pgTableDdl.js +17 -0
- package/dist/migrate/ddl/tableDdl.d.ts +15 -10
- package/dist/migrate/ddl/tableDdl.js +31 -32
- package/dist/migrate/drift/driftDetector.js +46 -59
- package/dist/migrate/generator/definitionToNode.d.ts +11 -7
- package/dist/migrate/generator/definitionToNode.js +27 -34
- package/dist/migrate/generator/indexNodeToSchema.d.ts +3 -3
- package/dist/migrate/generator/indexNodeToSchema.js +5 -4
- package/dist/migrate/index.js +1 -1
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.d.ts +10 -5
- package/dist/migrate/introspection/abstractSqlSchemaIntrospector.js +10 -0
- package/dist/migrate/introspection/baseSqlIntrospector.d.ts +0 -1
- package/dist/migrate/introspection/baseSqlIntrospector.js +6 -8
- package/dist/migrate/introspection/mssqlIntrospector.js +3 -8
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +11 -4
- package/dist/migrate/introspection/mysqlIntrospector.js +25 -11
- package/dist/migrate/introspection/postgresIntrospector.d.ts +2 -2
- package/dist/migrate/introspection/postgresIntrospector.js +3 -3
- package/dist/migrate/introspection/sqliteIntrospector.d.ts +1 -1
- package/dist/migrate/introspection/sqliteIntrospector.js +7 -10
- package/dist/migrate/migrationTarget.js +3 -3
- package/dist/migrate/schemaGenerator.d.ts +8 -12
- package/dist/migrate/schemaGenerator.js +35 -61
- package/dist/migrate/triggerSql.d.ts +1 -1
- package/dist/migrate/triggerSql.js +8 -6
- package/dist/mongo/mongoDialect.d.ts +4 -3
- package/dist/mongo/mongoDialect.js +52 -38
- package/dist/mongo/mongoIntrospector.js +1 -1
- package/dist/mongo/mongoSchemaGenerator.d.ts +7 -4
- package/dist/mongo/mongoSchemaGenerator.js +18 -21
- package/dist/mongo/mongodbQuerier.d.ts +2 -3
- package/dist/mongo/mongodbQuerier.js +9 -28
- package/dist/mssql/mssqlDialect.d.ts +4 -5
- package/dist/mssql/mssqlDialect.js +9 -7
- package/dist/postgres/postgresDialect.js +2 -2
- package/dist/querier/abstractQuerier.d.ts +16 -11
- package/dist/querier/abstractQuerier.js +74 -26
- package/dist/querier/abstractQuerierPool.d.ts +26 -24
- package/dist/querier/abstractQuerierPool.js +26 -64
- package/dist/querier/abstractSqlQuerier.d.ts +2 -3
- package/dist/querier/abstractSqlQuerier.js +8 -26
- package/dist/querier/cursor.d.ts +10 -0
- package/dist/querier/cursor.js +81 -0
- package/dist/querier/keyset.d.ts +19 -0
- package/dist/querier/keyset.js +198 -0
- package/dist/schema/canonicalType.d.ts +3 -3
- package/dist/schema/canonicalType.js +13 -12
- package/dist/schema/indexDifferences.d.ts +14 -7
- package/dist/schema/indexDifferences.js +7 -5
- package/dist/schema/schemaAST.d.ts +0 -1
- package/dist/schema/schemaAST.js +0 -2
- package/dist/schema/schemaASTBuilder.js +28 -20
- package/dist/schema/schemaASTDiffer.d.ts +7 -9
- package/dist/schema/schemaASTDiffer.js +54 -107
- package/dist/schema/sqlExpression.d.ts +22 -0
- package/dist/schema/sqlExpression.js +28 -0
- package/dist/schema/types.d.ts +48 -105
- package/dist/sqlite/sqliteDialect.d.ts +9 -13
- package/dist/sqlite/sqliteDialect.js +10 -29
- package/dist/type/dialect.d.ts +20 -3
- package/dist/type/entity.d.ts +7 -20
- package/dist/type/migration.d.ts +7 -7
- package/dist/type/querier.d.ts +19 -26
- package/dist/type/query.d.ts +104 -24
- package/dist/type/query.js +21 -28
- package/dist/type/universalQuerier.d.ts +31 -5
- package/dist/type/utility.d.ts +10 -0
- package/dist/util/bytes.d.ts +7 -0
- package/dist/util/bytes.js +22 -0
- package/dist/util/date.d.ts +5 -0
- package/dist/util/date.js +5 -0
- package/dist/util/dialect.util.d.ts +1 -25
- package/dist/util/dialect.util.js +4 -50
- package/dist/util/field.util.d.ts +9 -0
- package/dist/util/field.util.js +15 -0
- package/dist/util/fieldOption.util.js +2 -2
- package/dist/util/index.d.ts +1 -0
- package/dist/util/index.js +1 -0
- package/dist/util/object.util.d.ts +0 -2
- package/dist/util/object.util.js +0 -7
- package/dist/util/query.util.d.ts +40 -0
- package/dist/util/query.util.js +76 -0
- package/dist/util/raw.d.ts +5 -0
- package/dist/util/raw.js +5 -0
- package/dist/util/relationQuery.util.js +18 -16
- package/dist/util/rowKey.util.js +2 -3
- package/dist/util/sql.util.d.ts +8 -2
- package/dist/util/sql.util.js +16 -17
- package/dist/util/sqlLiteral.js +2 -12
- package/dist/util/string.util.d.ts +5 -0
- package/dist/util/string.util.js +12 -0
- package/package.json +1 -1
- package/skills/uql-orm/SKILL.md +22 -11
- package/dist/browser/options.d.ts +0 -4
- package/dist/browser/options.js +0 -13
- package/dist/browser/type/clientQuerierPool.d.ts +0 -4
- package/dist/browser/type/clientQuerierPool.js +0 -1
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { DATE_PRECISION } from '../util/date.js';
|
|
2
|
+
import { declaredIndexes } from '../util/ddlExpression.util.js';
|
|
3
|
+
import { isSelectList, isVectorSearch, normalizeScalarFieldSelection } from '../util/dialect.util.js';
|
|
4
|
+
import { aggregateOf, declaresNotNull, fieldFamily, isFieldKey } from '../util/field.util.js';
|
|
5
|
+
import { entityName, getKeys } from '../util/object.util.js';
|
|
6
|
+
import { keySet, parseSortDirection, sortOf, whereAnd, whereAnyOf, whereWith } from '../util/query.util.js';
|
|
7
|
+
import { fnv1a } from '../util/string.util.js';
|
|
8
|
+
import { UqlUsageError } from '../util/uqlError.js';
|
|
9
|
+
import { mintCursor, readCursor } from './cursor.js';
|
|
10
|
+
/**
|
|
11
|
+
* Turns a page query into the query to run: checks that `$sort` is total, inverts it for `$before`, turns
|
|
12
|
+
* the cursor into a `$where` for the rows past it, and reads one row over the page size to tell whether
|
|
13
|
+
* another page follows. `nullsSortLowest` says where the engine sorts nulls, for keys with no placement.
|
|
14
|
+
*/
|
|
15
|
+
export function keysetRead(meta, q, nullsSortLowest) {
|
|
16
|
+
const { $after, $before, ...rest } = q;
|
|
17
|
+
assertPageable(meta, q);
|
|
18
|
+
const order = pageOrder(meta, q, nullsSortLowest);
|
|
19
|
+
const keys = totalPrefix(meta, order);
|
|
20
|
+
const fingerprint = fingerprintOf(meta, order);
|
|
21
|
+
const backward = $before !== undefined;
|
|
22
|
+
const cursor = $before ?? $after;
|
|
23
|
+
const projected = projectedKeys(meta, q);
|
|
24
|
+
const carried = keys.map(({ key }) => key).filter((key) => !projected.includes(key));
|
|
25
|
+
const query = {
|
|
26
|
+
...rest,
|
|
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]), $exclude: undefined }),
|
|
29
|
+
$sort: backward ? sortOf(order.map(inverted).map((key) => [key.key, spelling(key)])) : rest.$sort,
|
|
30
|
+
$where: cursor === undefined
|
|
31
|
+
? rest.$where
|
|
32
|
+
: whereAnd(rest.$where, keysetClauses(backward ? keys.map(inverted) : keys, readCursor(meta, fingerprint, keys, cursor))),
|
|
33
|
+
$limit: q.$limit + 1,
|
|
34
|
+
};
|
|
35
|
+
const mint = (row) => mintCursor(meta, fingerprint, keys, row);
|
|
36
|
+
const page = (rows) => {
|
|
37
|
+
const more = rows.length > q.$limit;
|
|
38
|
+
const items = rows.slice(0, q.$limit);
|
|
39
|
+
if (backward) {
|
|
40
|
+
items.reverse();
|
|
41
|
+
}
|
|
42
|
+
// A page knows rows lie behind it only when it was itself read past a cursor.
|
|
43
|
+
const flags = { hasNextPage: backward || more, hasPrevPage: backward ? more : cursor !== undefined };
|
|
44
|
+
if (!items.length) {
|
|
45
|
+
return { items, ...flags };
|
|
46
|
+
}
|
|
47
|
+
const cursors = { startCursor: mint(items[0]), endCursor: mint(items[items.length - 1]) };
|
|
48
|
+
for (const item of items) {
|
|
49
|
+
for (const key of carried) {
|
|
50
|
+
Reflect.deleteProperty(item, key);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return { items, ...flags, ...cursors };
|
|
54
|
+
};
|
|
55
|
+
return { query, page };
|
|
56
|
+
}
|
|
57
|
+
/** Refuses a page query with `$skip`, both cursors, `$distinct`, or a `$limit` that is not a positive integer. */
|
|
58
|
+
function assertPageable(meta, q) {
|
|
59
|
+
const refuse = (why) => new UqlUsageError(`a page of '${entityName(meta)}' ${why}`);
|
|
60
|
+
// The type forbids `$skip`, but untyped input from `/http` can still carry one.
|
|
61
|
+
if ('$skip' in q && q.$skip !== undefined) {
|
|
62
|
+
throw refuse('takes no $skip: an offset reintroduces the drift a cursor removes');
|
|
63
|
+
}
|
|
64
|
+
if (q.$after !== undefined && q.$before !== undefined) {
|
|
65
|
+
throw refuse('reads past one cursor, $after or $before, not both');
|
|
66
|
+
}
|
|
67
|
+
if (q.$distinct) {
|
|
68
|
+
throw refuse('takes no $distinct: its rows are a grouping, which the cursor compares no column of');
|
|
69
|
+
}
|
|
70
|
+
if (!Number.isInteger(q.$limit) || q.$limit < 1) {
|
|
71
|
+
throw refuse(`holds a positive whole number of rows, and $limit is ${q.$limit}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** Resolves each key of the page's `$sort`, refusing one without a stored value a cursor can compare. */
|
|
75
|
+
function pageOrder(meta, q, nullsSortLowest) {
|
|
76
|
+
const sort = q.$sort ?? {};
|
|
77
|
+
return getKeys(sort).map((key) => {
|
|
78
|
+
const refuse = (why) => new UqlUsageError(`cannot page '${entityName(meta)}' by '${key}': ${why}`);
|
|
79
|
+
if (key === '$text') {
|
|
80
|
+
throw refuse('a relevance is scored per row, not stored on it');
|
|
81
|
+
}
|
|
82
|
+
const field = meta.fields[key];
|
|
83
|
+
if (!field || !isFieldKey(meta, key)) {
|
|
84
|
+
throw refuse(meta.relations[key]
|
|
85
|
+
? "a relation's value is read through a join that its filter would not keep"
|
|
86
|
+
: 'a cursor compares fields, and this is none');
|
|
87
|
+
}
|
|
88
|
+
if (aggregateOf(field)) {
|
|
89
|
+
throw refuse('an aggregate is computed per row, not stored on it');
|
|
90
|
+
}
|
|
91
|
+
const value = sort[key];
|
|
92
|
+
if (isVectorSearch(value)) {
|
|
93
|
+
throw refuse('a vector distance is approximate, so "past this one" is no stable page');
|
|
94
|
+
}
|
|
95
|
+
const family = fieldFamily(field);
|
|
96
|
+
if (family === 'json' || family === 'vector' || family === 'blob') {
|
|
97
|
+
throw refuse(`a ${family} column holds no one value a cursor can compare past`);
|
|
98
|
+
}
|
|
99
|
+
if (family === 'date' && (field.precision ?? DATE_PRECISION) > DATE_PRECISION) {
|
|
100
|
+
throw refuse(`it keeps ${field.precision} fractional digits, and a cursor the milliseconds a Date holds`);
|
|
101
|
+
}
|
|
102
|
+
const { desc, nulls } = parseSortDirection(value);
|
|
103
|
+
// With no placement, the engine decides: nulls come first ascending if it sorts them lowest, else descending.
|
|
104
|
+
const nullsFirst = nulls ? nulls === 'first' : desc !== nullsSortLowest;
|
|
105
|
+
return { key, desc, nullsFirst, placed: nulls !== undefined, nullable: !declaresNotNull(field) };
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The shortest prefix of `order` that covers every column of the primary key, or of a unique field or index
|
|
110
|
+
* whose columns are all non-null. A sort on which two rows can tie skips or repeats rows silently, so it is
|
|
111
|
+
* refused. Rows never tie on that prefix, so the cursor needs none of the later keys.
|
|
112
|
+
*/
|
|
113
|
+
function totalPrefix(meta, order) {
|
|
114
|
+
const notNull = (key) => {
|
|
115
|
+
const field = meta.fields[key];
|
|
116
|
+
return field !== undefined && declaresNotNull(field);
|
|
117
|
+
};
|
|
118
|
+
const uniques = [
|
|
119
|
+
meta.ids,
|
|
120
|
+
...declaredIndexes(meta)
|
|
121
|
+
.filter((index) => index.unique && !index.where)
|
|
122
|
+
.map((index) => index.columns.map(({ column }) => column))
|
|
123
|
+
.filter((columns) => columns.every((column) => typeof column === 'string' && notNull(column))),
|
|
124
|
+
].filter((columns) => columns.length > 0);
|
|
125
|
+
const seen = new Set();
|
|
126
|
+
for (let at = 0; at < order.length; at++) {
|
|
127
|
+
seen.add(order[at].key);
|
|
128
|
+
if (uniques.some((columns) => columns.every((column) => seen.has(column)))) {
|
|
129
|
+
return order.slice(0, at + 1);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
throw new UqlUsageError(`a page of '${entityName(meta)}' sorts by ${order.map(({ key }) => key).join(', ') || 'nothing'}, which two rows ` +
|
|
133
|
+
`can share: add ${meta.ids.length ? `'${meta.ids.join("', '")}' or ` : ''}a unique, non-null field to the end of $sort`);
|
|
134
|
+
}
|
|
135
|
+
/** The key in the opposite direction, nulls included, which `$before` reads so its extra row comes first. */
|
|
136
|
+
function inverted(key) {
|
|
137
|
+
return { ...key, desc: !key.desc, nullsFirst: !key.nullsFirst };
|
|
138
|
+
}
|
|
139
|
+
/** The key's direction as a `$sort` value, with a nulls placement only when the caller gave one. */
|
|
140
|
+
function spelling({ desc, nullsFirst, placed }) {
|
|
141
|
+
if (!placed) {
|
|
142
|
+
return desc ? 'desc' : 'asc';
|
|
143
|
+
}
|
|
144
|
+
return `${desc ? 'desc' : 'asc'}Nulls${nullsFirst ? 'First' : 'Last'}`;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* The clauses matching the rows past `values` in the order of `keys`, as the bounded chain
|
|
148
|
+
* `a <= x AND (a < x OR ...)`. Like `a = x` in the plain `a < x OR (a = x AND ...)`, each bound keeps rows tied
|
|
149
|
+
* on a key for the next key to decide, but it lets Postgres seek an index where the plain form scans from the
|
|
150
|
+
* top. Never empty, since the last key of a total prefix is never null.
|
|
151
|
+
*/
|
|
152
|
+
function keysetClauses(keys, values) {
|
|
153
|
+
const { bound, alternatives } = keys.reduceRight((inner, key, at) => ({
|
|
154
|
+
// The last key needs no bound: no two rows tie on it, so the strict comparison alone decides.
|
|
155
|
+
bound: at === keys.length - 1 ? undefined : atOrPastOf(key, values[at]),
|
|
156
|
+
alternatives: [...pastOf(key, values[at]), ...nested(inner)],
|
|
157
|
+
}), { alternatives: [] });
|
|
158
|
+
return bound ? [bound, anyOf(alternatives)] : [anyOf(alternatives)];
|
|
159
|
+
}
|
|
160
|
+
/** An inner level as one alternative of its outer level, or as its own alternatives when it has no bound. */
|
|
161
|
+
function nested({ bound, alternatives }) {
|
|
162
|
+
return bound ? [whereAnd(undefined, [bound, anyOf(alternatives)])] : alternatives;
|
|
163
|
+
}
|
|
164
|
+
/** The rows strictly past `value` on `key`, as alternatives, including null rows only when nulls sort last. */
|
|
165
|
+
function pastOf({ key, desc, nullsFirst, nullable }, value) {
|
|
166
|
+
if (value === null) {
|
|
167
|
+
return nullsFirst ? [whereWith(key, { $ne: null })] : [];
|
|
168
|
+
}
|
|
169
|
+
return [whereWith(key, { [desc ? '$lt' : '$gt']: value }), ...nullsAhead({ key, nullsFirst, nullable })];
|
|
170
|
+
}
|
|
171
|
+
/** The rows at or past `value` on `key`, or `undefined` when that is every row. */
|
|
172
|
+
function atOrPastOf({ key, desc, nullsFirst, nullable }, value) {
|
|
173
|
+
if (value === null) {
|
|
174
|
+
return nullsFirst ? undefined : whereWith(key, null);
|
|
175
|
+
}
|
|
176
|
+
return anyOf([whereWith(key, { [desc ? '$lte' : '$gte']: value }), ...nullsAhead({ key, nullsFirst, nullable })]);
|
|
177
|
+
}
|
|
178
|
+
/** An `IS NULL` alternative when the key is nullable and its nulls sort after every value, else none. */
|
|
179
|
+
function nullsAhead({ key, nullsFirst, nullable, }) {
|
|
180
|
+
return nullable && !nullsFirst ? [whereWith(key, null)] : [];
|
|
181
|
+
}
|
|
182
|
+
function anyOf(alternatives) {
|
|
183
|
+
return alternatives.length === 1 ? alternatives[0] : whereAnyOf(alternatives);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* The fields the query's projection reads, resolved by `normalizeScalarFieldSelection` the same way on every
|
|
187
|
+
* backend. A raw `$select` is refused: it names no fields, and the cursor is read off the fields.
|
|
188
|
+
*/
|
|
189
|
+
function projectedKeys(meta, q) {
|
|
190
|
+
if (isSelectList(q.$select)) {
|
|
191
|
+
throw new UqlUsageError(`a page of '${entityName(meta)}' selects fields, which its cursor is read off`);
|
|
192
|
+
}
|
|
193
|
+
return normalizeScalarFieldSelection(meta, q.$select, q.$exclude);
|
|
194
|
+
}
|
|
195
|
+
/** Hashes the entity and each sort key's direction and placement, so a cursor only works with the sort it came from. */
|
|
196
|
+
function fingerprintOf(meta, order) {
|
|
197
|
+
return fnv1a(`${entityName(meta)}|${order.map((key) => `${key.key}:${spelling(key)}`).join(',')}`).toString(36);
|
|
198
|
+
}
|
|
@@ -32,14 +32,14 @@ export declare function canonicalToSql(type: CanonicalType, dialect: AbstractDia
|
|
|
32
32
|
export declare function canonicalToTypeScript(type: CanonicalType): string;
|
|
33
33
|
/** The fractional-second digits an engine's timestamp holds when its type states none; `undefined` where it counts none. */
|
|
34
34
|
export declare function defaultTimestampPrecision(dialectName: DialectName): number | undefined;
|
|
35
|
+
/** Gives a timestamp type that states no precision `digits` fractional seconds; other types pass through. */
|
|
36
|
+
export declare function withTimestampPrecision(type: CanonicalType, digits: number | undefined): CanonicalType;
|
|
35
37
|
/**
|
|
36
38
|
* A type as `dialect` stores it, rendered and read back: several types share one storage type, and only
|
|
37
39
|
* the engine settles an unstated bound. Migrations and drift both compare through it.
|
|
38
40
|
*/
|
|
39
41
|
export declare function engineType(dialect: AbstractDialect): (type: CanonicalType) => CanonicalType;
|
|
40
|
-
/**
|
|
41
|
-
* Convert UQL FieldOptions to a canonical type.
|
|
42
|
-
*/
|
|
42
|
+
/** A field's canonical type; a timestamp without precision gets a `Date`'s milliseconds, unless it is `raw` SQL. */
|
|
43
43
|
export declare function fieldOptionsToCanonical(options: FieldOptions): CanonicalType;
|
|
44
44
|
/** Whether two canonical types are equal, guessing no engine default: `DiffOptions.normalizeType` settles those. */
|
|
45
45
|
export declare function areTypesEqual(a: CanonicalType, b: CanonicalType): boolean;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Canonical types, between an engine's SQL types and TypeScript's.
|
|
2
2
|
import { fieldOf, getMeta, soleIdOf } from '../entity/metadata/definition.js';
|
|
3
3
|
import { QueryRaw } from '../type/index.js';
|
|
4
|
+
import { DATE_PRECISION } from '../util/date.js';
|
|
4
5
|
import { columnFamily, isIntegerColumn } from '../util/field.util.js';
|
|
5
6
|
import { constantSql } from '../util/raw.js';
|
|
6
7
|
/** Whether a category is one of the vector types, narrowing it to the cast pgvector names use. */
|
|
@@ -120,8 +121,7 @@ const MYSQL_SCALAR_MAP = {
|
|
|
120
121
|
boolean: 'TINYINT(1)',
|
|
121
122
|
date: 'DATE',
|
|
122
123
|
time: 'TIME',
|
|
123
|
-
|
|
124
|
-
timestamp: 'DATETIME(3)',
|
|
124
|
+
timestamp: 'DATETIME',
|
|
125
125
|
json: 'JSON',
|
|
126
126
|
uuid: 'CHAR(36)',
|
|
127
127
|
blob: 'BLOB',
|
|
@@ -325,7 +325,7 @@ export function canonicalToSql(type, dialect) {
|
|
|
325
325
|
sqlType = 'TIMESTAMPTZ';
|
|
326
326
|
}
|
|
327
327
|
if (type.category === 'timestamp' && type.precision !== undefined && engine.timestampPrecision !== undefined) {
|
|
328
|
-
sqlType = `${sqlType
|
|
328
|
+
sqlType = `${sqlType}(${type.precision})`;
|
|
329
329
|
}
|
|
330
330
|
return type.unsigned && features.supportsUnsigned ? `${sqlType} UNSIGNED` : sqlType;
|
|
331
331
|
}
|
|
@@ -357,23 +357,24 @@ export function canonicalToTypeScript(type) {
|
|
|
357
357
|
export function defaultTimestampPrecision(dialectName) {
|
|
358
358
|
return ENGINE_TYPES[dialectName].timestampPrecision;
|
|
359
359
|
}
|
|
360
|
+
/** Gives a timestamp type that states no precision `digits` fractional seconds; other types pass through. */
|
|
361
|
+
export function withTimestampPrecision(type, digits) {
|
|
362
|
+
return type.category === 'timestamp' && type.precision === undefined ? { ...type, precision: digits } : type;
|
|
363
|
+
}
|
|
360
364
|
/**
|
|
361
365
|
* A type as `dialect` stores it, rendered and read back: several types share one storage type, and only
|
|
362
366
|
* the engine settles an unstated bound. Migrations and drift both compare through it.
|
|
363
367
|
*/
|
|
364
368
|
export function engineType(dialect) {
|
|
365
369
|
const timestampPrecision = defaultTimestampPrecision(dialect.dialectName);
|
|
366
|
-
return (type) =>
|
|
367
|
-
const stored = sqlToCanonical(canonicalToSql(type, dialect));
|
|
368
|
-
return stored.category === 'timestamp' && stored.precision === undefined
|
|
369
|
-
? { ...stored, precision: timestampPrecision }
|
|
370
|
-
: stored;
|
|
371
|
-
};
|
|
370
|
+
return (type) => withTimestampPrecision(sqlToCanonical(canonicalToSql(type, dialect)), timestampPrecision);
|
|
372
371
|
}
|
|
373
|
-
/**
|
|
374
|
-
* Convert UQL FieldOptions to a canonical type.
|
|
375
|
-
*/
|
|
372
|
+
/** A field's canonical type; a timestamp without precision gets a `Date`'s milliseconds, unless it is `raw` SQL. */
|
|
376
373
|
export function fieldOptionsToCanonical(options) {
|
|
374
|
+
const type = fieldType(options);
|
|
375
|
+
return options.columnType instanceof QueryRaw ? type : withTimestampPrecision(type, DATE_PRECISION);
|
|
376
|
+
}
|
|
377
|
+
function fieldType(options) {
|
|
377
378
|
// A SQL type is read exactly as an introspected one is, whichever option named it, so a bound stated
|
|
378
379
|
// beside it is read the same way either way.
|
|
379
380
|
const declared = declaredSqlType(options);
|
|
@@ -22,23 +22,30 @@ export declare function indexNameStem(name: string): string;
|
|
|
22
22
|
* Pairs by name, then what is left by shape: an index the database has under another name is still
|
|
23
23
|
* the one asked for. What stays unpaired is created or dropped.
|
|
24
24
|
*/
|
|
25
|
-
export declare function pairIndexes<S extends ComparableIndex, T extends ComparableIndex>(source: readonly S[], target: readonly T[]
|
|
25
|
+
export declare function pairIndexes<S extends ComparableIndex, T extends ComparableIndex>(source: readonly S[], target: readonly T[]): {
|
|
26
26
|
created: S[];
|
|
27
27
|
dropped: T[];
|
|
28
28
|
matched: (readonly [S, T])[];
|
|
29
29
|
};
|
|
30
|
+
/** One index change: create (`to`), drop (`from`), or rebuild (both, with a `description` of what differs). */
|
|
31
|
+
export type IndexChange<I> = {
|
|
32
|
+
readonly from?: undefined;
|
|
33
|
+
readonly to: I;
|
|
34
|
+
} | {
|
|
35
|
+
readonly from: IndexNode;
|
|
36
|
+
readonly to?: undefined;
|
|
37
|
+
} | {
|
|
38
|
+
readonly from: IndexNode;
|
|
39
|
+
readonly to: I;
|
|
40
|
+
readonly description: string;
|
|
41
|
+
};
|
|
30
42
|
/**
|
|
31
43
|
* The indexes a table lacks, the ones it no longer needs, and the ones to rebuild, differing in what
|
|
32
44
|
* `facets` let the engine report. Only an unpaired index uql named, or whose name the entity claims, is
|
|
33
45
|
* dropped: any other may have been made outside the ORM, so it is `kept`.
|
|
34
46
|
*/
|
|
35
47
|
export declare function indexChanges<I extends IndexSchema>(table: string, declared: readonly I[], current: readonly IndexNode[], facets: ReadonlySet<IndexFacet>): {
|
|
36
|
-
|
|
37
|
-
toDrop: IndexNode[];
|
|
38
|
-
toAlter: {
|
|
39
|
-
from: IndexNode;
|
|
40
|
-
to: I;
|
|
41
|
-
}[];
|
|
48
|
+
changes: IndexChange<I>[];
|
|
42
49
|
kept: IndexNode[];
|
|
43
50
|
};
|
|
44
51
|
/**
|
|
@@ -30,8 +30,8 @@ export function indexNameStem(name) {
|
|
|
30
30
|
* Pairs by name, then what is left by shape: an index the database has under another name is still
|
|
31
31
|
* the one asked for. What stays unpaired is created or dropped.
|
|
32
32
|
*/
|
|
33
|
-
export function pairIndexes(source, target
|
|
34
|
-
const byName = matchByKey(source, target, (index) =>
|
|
33
|
+
export function pairIndexes(source, target) {
|
|
34
|
+
const byName = matchByKey(source, target, (index) => indexNameStem(index.name));
|
|
35
35
|
const byShape = matchByKey(byName.created, byName.dropped, indexSignature);
|
|
36
36
|
return { created: byShape.created, dropped: byShape.dropped, matched: [...byName.matched, ...byShape.matched] };
|
|
37
37
|
}
|
|
@@ -44,11 +44,13 @@ export function indexChanges(table, declared, current, facets) {
|
|
|
44
44
|
const { created, dropped, matched } = pairIndexes(declared, current);
|
|
45
45
|
const claimed = new Set(declared.map((index) => index.name));
|
|
46
46
|
const owned = (index) => claimed.has(index.name) || hasDerivedName(table, index);
|
|
47
|
+
const rebuilt = matched.flatMap(([to, from]) => {
|
|
48
|
+
const differences = describeIndexDifferences(to, from, facets);
|
|
49
|
+
return differences.length ? [{ from, to, description: differences.join(', ') }] : [];
|
|
50
|
+
});
|
|
47
51
|
return {
|
|
48
|
-
|
|
49
|
-
toDrop: dropped.filter(owned),
|
|
52
|
+
changes: [...created.map((to) => ({ to })), ...dropped.filter(owned).map((from) => ({ from })), ...rebuilt],
|
|
50
53
|
kept: dropped.filter((index) => !owned(index)),
|
|
51
|
-
toAlter: matched.flatMap(([to, from]) => (describeIndexDifferences(to, from, facets).length ? [{ from, to }] : [])),
|
|
52
54
|
};
|
|
53
55
|
}
|
|
54
56
|
/**
|
|
@@ -9,7 +9,6 @@ export declare function keyOfColumns(columns: Iterable<Pick<ColumnNode, 'name' |
|
|
|
9
9
|
export declare class SchemaAST {
|
|
10
10
|
readonly tables: Map<string, TableNode>;
|
|
11
11
|
readonly relationships: RelationshipNode[];
|
|
12
|
-
readonly indexes: IndexNode[];
|
|
13
12
|
/** A table by the key it is stored under: schema-qualified where it has one (see `qualifyName`). */
|
|
14
13
|
getTable(name: string): TableNode | undefined;
|
|
15
14
|
addTable(table: TableNode): void;
|
package/dist/schema/schemaAST.js
CHANGED
|
@@ -23,7 +23,6 @@ export function keyOfColumns(columns) {
|
|
|
23
23
|
export class SchemaAST {
|
|
24
24
|
tables = new Map();
|
|
25
25
|
relationships = [];
|
|
26
|
-
indexes = [];
|
|
27
26
|
/** A table by the key it is stored under: schema-qualified where it has one (see `qualifyName`). */
|
|
28
27
|
getTable(name) {
|
|
29
28
|
return this.tables.get(name);
|
|
@@ -43,7 +42,6 @@ export class SchemaAST {
|
|
|
43
42
|
return dropOrder(this.tables.values(), referencedTables);
|
|
44
43
|
}
|
|
45
44
|
addIndex(index) {
|
|
46
|
-
this.indexes.push(index);
|
|
47
45
|
if (!index.table.indexes.includes(index)) {
|
|
48
46
|
index.table.indexes.push(index);
|
|
49
47
|
}
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { fieldOf, foreignKeysOf, getMeta } from '../entity/metadata/definition.js';
|
|
2
|
+
import { QueryRaw } from '../type/queryRaw.js';
|
|
2
3
|
import { declaredIndexes, declaredIndexName, renderIndexColumn } from '../util/ddlExpression.util.js';
|
|
3
4
|
import { fulltextWeights, textWeightSteps } from '../util/dialect.util.js';
|
|
4
|
-
import { isAutoIncrement, isInlinedExpression, isSoleIdField } from '../util/field.util.js';
|
|
5
|
+
import { declaresNotNull, isAutoIncrement, isInlinedExpression, isSoleIdField } from '../util/field.util.js';
|
|
5
6
|
import { definedEntries } from '../util/object.util.js';
|
|
7
|
+
import { currentTimestamp } from '../util/raw.js';
|
|
6
8
|
import { derivedForeignKeyName, derivedIndexName, qualifyName } from '../util/sql.util.js';
|
|
7
9
|
import { UqlUsageError } from '../util/uqlError.js';
|
|
8
10
|
import { resolveColumnCanonicalType } from './canonicalType.js';
|
|
9
11
|
import { createTableNode, keyOfColumns, SchemaAST } from './schemaAST.js';
|
|
12
|
+
import { SqlExpression } from './sqlExpression.js';
|
|
10
13
|
import { DEFAULT_FOREIGN_KEY_ACTION } from './types.js';
|
|
11
14
|
/**
|
|
12
15
|
* Build a SchemaAST from entity classes (decorated with `@Entity`, `@Field`, etc.).
|
|
@@ -67,10 +70,8 @@ function addTableFromEntity(ctx, meta) {
|
|
|
67
70
|
const column = {
|
|
68
71
|
name: columnName,
|
|
69
72
|
type,
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
nullable: isPrimaryKey || notNull.has(key) ? false : (field.nullable ?? true),
|
|
73
|
-
defaultValue: field.defaultValue,
|
|
73
|
+
nullable: !declaresNotNull(field) && !notNull.has(key),
|
|
74
|
+
defaultValue: columnDefault(ctx, meta, field.defaultValue),
|
|
74
75
|
isPrimaryKey,
|
|
75
76
|
isAutoIncrement: isAutoIncrement(field, isSoleKey),
|
|
76
77
|
isUnique: field.unique ?? false,
|
|
@@ -87,6 +88,18 @@ function addTableFromEntity(ctx, meta) {
|
|
|
87
88
|
table.primaryKey = keyOfColumns(columns.values());
|
|
88
89
|
ctx.ast.addTable(table);
|
|
89
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* A field's default in schema form: SQL becomes an {@link SqlExpression}, and `currentTimestamp` stays
|
|
93
|
+
* symbolic (`now`) so each engine spells it for the column it fills.
|
|
94
|
+
*/
|
|
95
|
+
function columnDefault(ctx, meta, value) {
|
|
96
|
+
if (!(value instanceof QueryRaw)) {
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
return value === currentTimestamp
|
|
100
|
+
? new SqlExpression('now')
|
|
101
|
+
: SqlExpression.parenthesized(ctx.compileDdl(value, meta.entity));
|
|
102
|
+
}
|
|
90
103
|
/** The node an entity maps to, found under the key {@link SchemaAST} stores it by. */
|
|
91
104
|
function tableOf(ctx, meta) {
|
|
92
105
|
return ctx.ast.getTable(qualifyName(ctx.resolveTableName(meta), ctx.resolveSchema(meta)));
|
|
@@ -177,13 +190,13 @@ function resolveIncludeColumn(ctx, meta, column) {
|
|
|
177
190
|
}
|
|
178
191
|
/**
|
|
179
192
|
* One index the entity declares. Its entries keep the authored form (expression, prefix length, order) with
|
|
180
|
-
* names resolved, so the generator renders exactly what was declared
|
|
181
|
-
*
|
|
193
|
+
* names resolved, so the generator renders exactly what was declared. Every option not resolved here is
|
|
194
|
+
* spread through, so an option added to indexes later cannot be lost.
|
|
182
195
|
*/
|
|
183
|
-
function addCompositeIndex(ctx, table, meta,
|
|
196
|
+
function addCompositeIndex(ctx, table, meta, { columns, include, where, ...options }) {
|
|
184
197
|
// An entry survives if it is an expression (nothing to resolve) or names a column that exists;
|
|
185
198
|
// an index left with none is dropped, the same as one naming only unknown columns always was.
|
|
186
|
-
const resolved =
|
|
199
|
+
const resolved = columns.flatMap((entry) => {
|
|
187
200
|
if (typeof entry.column !== 'string')
|
|
188
201
|
return [entry];
|
|
189
202
|
const field = meta.fields[entry.column];
|
|
@@ -192,23 +205,18 @@ function addCompositeIndex(ctx, table, meta, idxMeta) {
|
|
|
192
205
|
});
|
|
193
206
|
if (!resolved.length)
|
|
194
207
|
return;
|
|
195
|
-
const name = declaredIndexName(
|
|
208
|
+
const name = declaredIndexName(options.name, table.name, resolved);
|
|
196
209
|
ctx.ast.addIndex({
|
|
210
|
+
...options,
|
|
197
211
|
name,
|
|
198
212
|
table,
|
|
199
213
|
entries: resolved.map((entry) => renderIndexColumn(entry, (sql) => ctx.compileDdl(sql, meta.entity))),
|
|
200
|
-
include:
|
|
201
|
-
unique:
|
|
202
|
-
|
|
203
|
-
where: idxMeta.where && ctx.compileIndexPredicate(idxMeta.where, meta.entity, name),
|
|
204
|
-
distance: idxMeta.distance,
|
|
205
|
-
m: idxMeta.m,
|
|
206
|
-
efConstruction: idxMeta.efConstruction,
|
|
207
|
-
lists: idxMeta.lists,
|
|
208
|
-
config: idxMeta.config,
|
|
214
|
+
include: include?.map((column) => resolveIncludeColumn(ctx, meta, column)),
|
|
215
|
+
unique: options.unique ?? false,
|
|
216
|
+
where: where && ctx.compileIndexPredicate(where, meta.entity, name),
|
|
209
217
|
});
|
|
210
218
|
if (ctx.textScoreIndexes) {
|
|
211
|
-
addTextScoreIndexes(ctx, table,
|
|
219
|
+
addTextScoreIndexes(ctx, table, options.type, resolved);
|
|
212
220
|
}
|
|
213
221
|
}
|
|
214
222
|
/** A fulltext index of its own for each column heavier than the index's lightest, which its score reads. */
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ColumnRenames, Rename } from '../type/migration.js';
|
|
2
2
|
import type { SchemaAST } from './schemaAST.js';
|
|
3
3
|
import type { CanonicalType } from './types.js';
|
|
4
|
-
import type {
|
|
4
|
+
import type { ForeignKeyAction, RelationshipDiff, RelationshipNode, SchemaDiffResult, TableDiff, TableNode } from './types.js';
|
|
5
5
|
/**
|
|
6
6
|
* Options for schema diffing.
|
|
7
7
|
*/
|
|
@@ -10,8 +10,6 @@ export interface DiffOptions {
|
|
|
10
10
|
compareIndexes?: boolean;
|
|
11
11
|
/** Compare foreign keys/relationships */
|
|
12
12
|
compareRelationships?: boolean;
|
|
13
|
-
/** Ignore case differences in names */
|
|
14
|
-
ignoreCase?: boolean;
|
|
15
13
|
/** Tables to exclude from comparison */
|
|
16
14
|
excludeTables?: string[];
|
|
17
15
|
/**
|
|
@@ -25,10 +23,7 @@ export interface DiffOptions {
|
|
|
25
23
|
/** The differences between the expected schema (the entities) and the actual one (the database). */
|
|
26
24
|
export declare function diffSchemas(source: SchemaAST, target: SchemaAST, options?: DiffOptions): SchemaDiffResult;
|
|
27
25
|
/** The differences between two tables, shared by migrations and drift detection so they cannot disagree. */
|
|
28
|
-
export declare function diffTable(source: TableNode, target: TableNode, options?: DiffOptions):
|
|
29
|
-
readonly columnDiffs: ColumnDiff[];
|
|
30
|
-
readonly indexDiffs: IndexDiff[];
|
|
31
|
-
}) | undefined;
|
|
26
|
+
export declare function diffTable(source: TableNode, target: TableNode, options?: DiffOptions): TableDiff | undefined;
|
|
32
27
|
/**
|
|
33
28
|
* The columns renamed in the tables both sides name, by qualified table:
|
|
34
29
|
* a new column identical to exactly one the entity no longer names, and to no other. The dropped side is
|
|
@@ -42,11 +37,14 @@ export declare function columnRenames(desired: SchemaAST, actual: SchemaAST, opt
|
|
|
42
37
|
*/
|
|
43
38
|
export declare function tableRenameCandidates(desired: SchemaAST, actual: SchemaAST, options?: DiffOptions): Rename[];
|
|
44
39
|
/** The differences between two lists of foreign keys, matched by their columns and never by the name the engine gave them. */
|
|
45
|
-
export declare function diffRelationshipNodes(source: readonly RelationshipNode[], target: readonly RelationshipNode[]
|
|
40
|
+
export declare function diffRelationshipNodes(source: readonly RelationshipNode[], target: readonly RelationshipNode[]): RelationshipDiff[];
|
|
46
41
|
/** A relationship's `ON DELETE` and `ON UPDATE`, an unstated one read as the action the database applies. */
|
|
47
42
|
export declare function referentialActions(rel: RelationshipNode): {
|
|
48
43
|
readonly onDelete: ForeignKeyAction;
|
|
49
44
|
readonly onUpdate: ForeignKeyAction;
|
|
50
45
|
};
|
|
51
|
-
/**
|
|
46
|
+
/**
|
|
47
|
+
* Compares two defaults exactly as written, for when no dialect renders them: SQL must match to the letter.
|
|
48
|
+
* Migrations and drift use the generator's equality instead, which knows how its engine reprints SQL.
|
|
49
|
+
*/
|
|
52
50
|
export declare function defaultsEqualAsWritten(expected: unknown, actual: unknown): boolean;
|