uql-orm 0.104.1 → 0.105.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/README.md +24 -10
- package/dist/browser/uql-browser.min.js +2 -2
- package/dist/browser/uql-browser.min.js.map +7 -7
- package/dist/dialect/abstractSqlDialect.d.ts +6 -4
- 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/mysqlLikeSqlDialect.d.ts +1 -1
- package/dist/dialect/mysqlLikeSqlDialect.js +1 -1
- package/dist/dialect/operators.js +2 -4
- package/dist/dialect/queryJoins.d.ts +2 -0
- 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 +4 -4
- package/dist/entity/decorator/entity.d.ts +2 -19
- 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 +10 -24
- package/dist/entity/metadata/definition.js +329 -147
- 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 +22 -0
- package/dist/http/query.js +12 -13
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -1
- 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/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/schemaChange.d.ts +3 -3
- package/dist/migrate/schemaChange.js +3 -2
- package/dist/migrate/schemaGenerator.js +1 -1
- package/dist/migrate/storage/databaseStorage.js +4 -4
- package/dist/mongodb/mongoDialect.d.ts +33 -36
- 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.js +35 -27
- package/dist/mongodb/mongodbQuerier.d.ts +1 -11
- package/dist/mongodb/mongodbQuerier.js +9 -43
- package/dist/querier/abstractQuerier.d.ts +0 -1
- package/dist/querier/abstractQuerier.js +1 -20
- 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.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 +1 -1
- package/dist/type/dialect.d.ts +2 -1
- package/dist/type/dialect.js +1 -0
- package/dist/type/entity.d.ts +290 -279
- 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 +5 -5
- 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 +20 -4
- package/dist/type/querySql.js +27 -9
- package/dist/type/schemaEntity.d.ts +190 -0
- package/dist/type/schemaEntity.js +1 -0
- package/dist/type/utility.d.ts +0 -6
- 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 +2 -14
- 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 +10 -7
- 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/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
package/dist/util/hook.util.d.ts
CHANGED
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
/**
|
|
3
|
-
* What a lifecycle hook is handed: the querier, to write in the same transaction, and `onCommit`, for what
|
|
4
|
-
* must wait until that transaction commits.
|
|
5
|
-
*/
|
|
6
|
-
export type HookContext = {
|
|
7
|
-
readonly querier: Querier;
|
|
8
|
-
readonly onCommit: Querier['onCommit'];
|
|
9
|
-
};
|
|
10
|
-
/**
|
|
11
|
-
* Run all registered hooks for the given event on each payload.
|
|
12
|
-
* Hooks are invoked with `this` bound to the payload via `call`,
|
|
13
|
-
* so mutations go directly to the original object.
|
|
14
|
-
*/
|
|
1
|
+
import type { HookContext, HookEvent, Type } from '../type/index.js';
|
|
2
|
+
/** Runs each hook registered for `event` on each payload, in order, awaiting each: a hook mutates the payload itself. */
|
|
15
3
|
export declare function runHooks<E extends object>(entity: Type<E>, event: HookEvent, payloads: readonly E[], ctx: HookContext): Promise<void>;
|
package/dist/util/hook.util.js
CHANGED
|
@@ -1,26 +1,13 @@
|
|
|
1
1
|
import { getMeta } from '../entity/index.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Run all registered hooks for the given event on each payload.
|
|
5
|
-
* Hooks are invoked with `this` bound to the payload via `call`,
|
|
6
|
-
* so mutations go directly to the original object.
|
|
7
|
-
*/
|
|
2
|
+
/** Runs each hook registered for `event` on each payload, in order, awaiting each: a hook mutates the payload itself. */
|
|
8
3
|
export async function runHooks(entity, event, payloads, ctx) {
|
|
9
4
|
const meta = getMeta(entity);
|
|
10
5
|
const registrations = meta.hooks?.[event];
|
|
11
6
|
if (!registrations?.length)
|
|
12
7
|
return;
|
|
13
|
-
// A prototype is typed `any`, so what is read off it is typed here, where it enters.
|
|
14
|
-
const prototype = entity.prototype;
|
|
15
8
|
for (const payload of payloads) {
|
|
16
|
-
for (const
|
|
17
|
-
|
|
18
|
-
if (typeof method !== 'function') {
|
|
19
|
-
throw new UqlUsageError(`'${entity.name}' runs '${methodName}' on ${event}, but has no such method`);
|
|
20
|
-
}
|
|
21
|
-
const result = method.call(payload, ctx);
|
|
22
|
-
if (result instanceof Promise)
|
|
23
|
-
await result;
|
|
9
|
+
for (const hook of registrations) {
|
|
10
|
+
await hook.run(payload, ctx);
|
|
24
11
|
}
|
|
25
12
|
}
|
|
26
13
|
}
|
package/dist/util/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from './fieldOption.util.js';
|
|
|
5
5
|
export * from './filters.util.js';
|
|
6
6
|
export * from './hook.util.js';
|
|
7
7
|
export * from './ddlExpression.util.js';
|
|
8
|
+
export * from './indexEntry.util.js';
|
|
8
9
|
export * from './logger.js';
|
|
9
10
|
export * from './object.util.js';
|
|
10
11
|
export * from './sql.js';
|
package/dist/util/index.js
CHANGED
|
@@ -5,6 +5,7 @@ export * from './fieldOption.util.js';
|
|
|
5
5
|
export * from './filters.util.js';
|
|
6
6
|
export * from './hook.util.js';
|
|
7
7
|
export * from './ddlExpression.util.js';
|
|
8
|
+
export * from './indexEntry.util.js';
|
|
8
9
|
export * from './logger.js';
|
|
9
10
|
export * from './object.util.js';
|
|
10
11
|
export * from './sql.js';
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type EntityIndexColumnInput, type FieldType, type IndexColumnInput, type IndexEntry, type IndexEntryModifiers, type IndexValueType, QuerySql } from '../type/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Reduces an index entry, authored or the migration builder's, to the one form metadata keeps: a column read
|
|
4
|
+
* off the refs as its key, a JSON path as its column and dotted path, any other `sql` as its expression.
|
|
5
|
+
*/
|
|
6
|
+
export declare function normalizeIndexColumn(entry: IndexColumnInput | EntityIndexColumnInput): IndexEntry;
|
|
7
|
+
/** An index entry as the schema holds it, its expression rendered to text by `render`. */
|
|
8
|
+
export declare function renderIndexColumn(entry: IndexEntry, render: (statement: QuerySql) => string): IndexEntry<string>;
|
|
9
|
+
/** An entry over a column as stored. */
|
|
10
|
+
export type StoredEntry<Sql> = IndexEntry<Sql> & {
|
|
11
|
+
readonly column: string;
|
|
12
|
+
};
|
|
13
|
+
/** Whether every entry is a column as stored. */
|
|
14
|
+
export declare function isStored<Sql>(entries: readonly IndexEntry<Sql>[]): entries is readonly StoredEntry<Sql>[];
|
|
15
|
+
/** Each entry's column as stored, or `undefined` where any entry is not one. */
|
|
16
|
+
export declare function storedColumns(entries: readonly IndexEntry<unknown>[]): string[] | undefined;
|
|
17
|
+
/** The column an entry keys whole, with no prefix; a JSON path or an expression keys none. */
|
|
18
|
+
export declare function wholeColumn(entry: IndexEntry<unknown>): string | undefined;
|
|
19
|
+
/** The column an entry reads, as stored or as the document a JSON path reads into; an expression reads none. */
|
|
20
|
+
export declare function entryColumn(entry: IndexEntry<unknown>): string | undefined;
|
|
21
|
+
/** The columns `entries` read, a JSON path's included. */
|
|
22
|
+
export declare function entryColumns(entries: readonly IndexEntry<unknown>[]): string[];
|
|
23
|
+
/** What an entry keys: its column, then the keys of a JSON path inside it, if any; an expression keys none. */
|
|
24
|
+
export declare function entryKeys(entry: IndexEntry<unknown>): string[] | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* What an entry adds to a derived index name: its column and its path's keys (`kind_theme`), or `expr<at>` for
|
|
27
|
+
* an expression, which has none. The one rule both naming an index and recognising a name read.
|
|
28
|
+
*/
|
|
29
|
+
export declare function indexEntryNamePart(entry: IndexEntry<unknown>, at: number): string;
|
|
30
|
+
/** How an entry keys, without what it keys: its order, nulls, operator class and weight. */
|
|
31
|
+
export declare function entryModifiers<Sql>(entry: IndexEntry<Sql>): IndexEntryModifiers;
|
|
32
|
+
/** `entry` with the column it reads, as stored or as the document a JSON path reads into, renamed by `rename`. */
|
|
33
|
+
export declare function renamedEntry<Sql>(entry: IndexEntry<Sql>, rename: (column: string) => string): IndexEntry<Sql>;
|
|
34
|
+
/** How an index reads a JSON column: the value at `path` as `type`, or with `elements`, each element of the array there. */
|
|
35
|
+
export type JsonIndexRead = {
|
|
36
|
+
readonly column: string;
|
|
37
|
+
readonly path: string;
|
|
38
|
+
readonly type: FieldType;
|
|
39
|
+
readonly elements: boolean;
|
|
40
|
+
readonly charLength?: number;
|
|
41
|
+
};
|
|
42
|
+
/** The JSON read an entry indexes, `undefined` for a column or an expression; a path says nothing to be read as text. */
|
|
43
|
+
export declare function jsonIndexRead(entry: IndexEntry<unknown>): JsonIndexRead | undefined;
|
|
44
|
+
/** Whether an index reads each element of an array, `[type]`, rather than one value. */
|
|
45
|
+
export declare function isElementsType(type: IndexValueType | undefined): type is readonly [FieldType];
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { ColumnRef, JSON_PATH, JsonPathRef, QuerySql, REF_KEY, } from '../type/index.js';
|
|
2
|
+
import { jsonPathKeys } from './field.util.js';
|
|
3
|
+
import { UqlUsageError } from './uqlError.js';
|
|
4
|
+
/**
|
|
5
|
+
* Every key an index entry takes. Checked at run time because TypeScript never checks a callback's returned
|
|
6
|
+
* literal for excess properties, so a misspelt option would otherwise build an index without it.
|
|
7
|
+
*/
|
|
8
|
+
const INDEX_ENTRY_KEYS = {
|
|
9
|
+
column: true,
|
|
10
|
+
json: true,
|
|
11
|
+
path: true,
|
|
12
|
+
type: true,
|
|
13
|
+
expression: true,
|
|
14
|
+
prefixLength: true,
|
|
15
|
+
charLength: true,
|
|
16
|
+
order: true,
|
|
17
|
+
nulls: true,
|
|
18
|
+
opsClass: true,
|
|
19
|
+
weight: true,
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Reduces an index entry, authored or the migration builder's, to the one form metadata keeps: a column read
|
|
23
|
+
* off the refs as its key, a JSON path as its column and dotted path, any other `sql` as its expression.
|
|
24
|
+
*/
|
|
25
|
+
export function normalizeIndexColumn(entry) {
|
|
26
|
+
if (typeof entry === 'string' || entry instanceof QuerySql || entry instanceof JsonPathRef) {
|
|
27
|
+
return entryTarget(entry);
|
|
28
|
+
}
|
|
29
|
+
const unknown = Object.keys(entry).find((key) => !Object.hasOwn(INDEX_ENTRY_KEYS, key));
|
|
30
|
+
if (unknown) {
|
|
31
|
+
throw new UqlUsageError(`an index entry has no option '${unknown}'`);
|
|
32
|
+
}
|
|
33
|
+
if (entry.column === undefined && entry.json === undefined && entry.expression === undefined) {
|
|
34
|
+
throw new UqlUsageError('an index entry names no column or expression');
|
|
35
|
+
}
|
|
36
|
+
if (entry.column === undefined) {
|
|
37
|
+
return entry;
|
|
38
|
+
}
|
|
39
|
+
if (typeof entry.column === 'string') {
|
|
40
|
+
refuseLength('charLength', entry.charLength, 'a stored column');
|
|
41
|
+
return entry;
|
|
42
|
+
}
|
|
43
|
+
const { column, type, prefixLength, charLength, ...modifiers } = entry;
|
|
44
|
+
const target = entryTarget(column);
|
|
45
|
+
if (target.column !== undefined && type === undefined) {
|
|
46
|
+
refuseLength('charLength', charLength, 'a stored column');
|
|
47
|
+
return { ...modifiers, column: target.column, ...(prefixLength !== undefined && { prefixLength }) };
|
|
48
|
+
}
|
|
49
|
+
if (target.expression !== undefined) {
|
|
50
|
+
refuseLength('prefixLength', prefixLength, 'an expression');
|
|
51
|
+
refuseLength('charLength', charLength, 'an expression');
|
|
52
|
+
return { ...modifiers, expression: target.expression };
|
|
53
|
+
}
|
|
54
|
+
refuseLength('prefixLength', prefixLength, 'a JSON path');
|
|
55
|
+
return {
|
|
56
|
+
...modifiers,
|
|
57
|
+
json: target.json ?? target.column,
|
|
58
|
+
...(target.path !== undefined && { path: target.path }),
|
|
59
|
+
...(type !== undefined && { type }),
|
|
60
|
+
...(charLength !== undefined && { charLength }),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function entryTarget(target) {
|
|
64
|
+
if (typeof target === 'string') {
|
|
65
|
+
return { column: target };
|
|
66
|
+
}
|
|
67
|
+
if (target instanceof JsonPathRef) {
|
|
68
|
+
return { json: target[REF_KEY], path: target[JSON_PATH] };
|
|
69
|
+
}
|
|
70
|
+
return target instanceof ColumnRef ? { column: target[REF_KEY] } : { expression: target };
|
|
71
|
+
}
|
|
72
|
+
/** Refuses a length an entry's target does not take, which only a column's ref and other `sql` leave to run time. */
|
|
73
|
+
function refuseLength(option, length, target) {
|
|
74
|
+
if (length !== undefined) {
|
|
75
|
+
throw new UqlUsageError(`an index entry over ${target} takes no ${option}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** An index entry as the schema holds it, its expression rendered to text by `render`. */
|
|
79
|
+
export function renderIndexColumn(entry, render) {
|
|
80
|
+
return entry.expression === undefined ? entry : { ...entry, expression: render(entry.expression) };
|
|
81
|
+
}
|
|
82
|
+
/** Whether every entry is a column as stored. */
|
|
83
|
+
export function isStored(entries) {
|
|
84
|
+
return entries.every((entry) => entry.column !== undefined);
|
|
85
|
+
}
|
|
86
|
+
/** Each entry's column as stored, or `undefined` where any entry is not one. */
|
|
87
|
+
export function storedColumns(entries) {
|
|
88
|
+
return isStored(entries) ? entries.map((entry) => entry.column) : undefined;
|
|
89
|
+
}
|
|
90
|
+
/** The column an entry keys whole, with no prefix; a JSON path or an expression keys none. */
|
|
91
|
+
export function wholeColumn(entry) {
|
|
92
|
+
return entry.prefixLength === undefined ? entry.column : undefined;
|
|
93
|
+
}
|
|
94
|
+
/** The column an entry reads, as stored or as the document a JSON path reads into; an expression reads none. */
|
|
95
|
+
export function entryColumn(entry) {
|
|
96
|
+
return entry.column ?? entry.json;
|
|
97
|
+
}
|
|
98
|
+
/** The columns `entries` read, a JSON path's included. */
|
|
99
|
+
export function entryColumns(entries) {
|
|
100
|
+
return entries.flatMap((entry) => entryColumn(entry) ?? []);
|
|
101
|
+
}
|
|
102
|
+
/** What an entry keys: its column, then the keys of a JSON path inside it, if any; an expression keys none. */
|
|
103
|
+
export function entryKeys(entry) {
|
|
104
|
+
const column = entryColumn(entry);
|
|
105
|
+
return column === undefined ? undefined : [column, ...(entry.path ? jsonPathKeys(entry.path) : [])];
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* What an entry adds to a derived index name: its column and its path's keys (`kind_theme`), or `expr<at>` for
|
|
109
|
+
* an expression, which has none. The one rule both naming an index and recognising a name read.
|
|
110
|
+
*/
|
|
111
|
+
export function indexEntryNamePart(entry, at) {
|
|
112
|
+
return entryKeys(entry)?.join('_') ?? `expr${at}`;
|
|
113
|
+
}
|
|
114
|
+
/** How an entry keys, without what it keys: its order, nulls, operator class and weight. */
|
|
115
|
+
export function entryModifiers(entry) {
|
|
116
|
+
const { column: _column, json: _json, path: _path, type: _type, expression: _expression, prefixLength: _prefixLength, charLength: _charLength, ...modifiers } = entry;
|
|
117
|
+
return modifiers;
|
|
118
|
+
}
|
|
119
|
+
/** `entry` with the column it reads, as stored or as the document a JSON path reads into, renamed by `rename`. */
|
|
120
|
+
export function renamedEntry(entry, rename) {
|
|
121
|
+
if (entry.column !== undefined) {
|
|
122
|
+
return { ...entry, column: rename(entry.column) };
|
|
123
|
+
}
|
|
124
|
+
return entry.json === undefined ? entry : { ...entry, json: rename(entry.json) };
|
|
125
|
+
}
|
|
126
|
+
/** The JSON read an entry indexes, `undefined` for a column or an expression; a path says nothing to be read as text. */
|
|
127
|
+
export function jsonIndexRead(entry) {
|
|
128
|
+
if (entry.json === undefined) {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
const { json: column, path = '', type = String, charLength } = entry;
|
|
132
|
+
const elements = isElementsType(type);
|
|
133
|
+
return { column, path, type: elements ? type[0] : type, elements, charLength };
|
|
134
|
+
}
|
|
135
|
+
/** Whether an index reads each element of an array, `[type]`, rather than one value. */
|
|
136
|
+
export function isElementsType(type) {
|
|
137
|
+
return Array.isArray(type);
|
|
138
|
+
}
|
|
@@ -68,5 +68,4 @@ export type ParsedRelationQuery<E extends object = object> = {
|
|
|
68
68
|
export declare function parseRelationQueryValue<E extends object = object>(value: unknown): ParsedRelationQuery<E>;
|
|
69
69
|
/** Parses the relation payload for `relKey` */
|
|
70
70
|
export declare function parseRelationAtKey<E>(relKey: RelationKey<E>, populate?: QueryPopulate<E>): ParsedRelationQuery;
|
|
71
|
-
export declare function forEachRequestedRelation<E extends object>(meta: EntityMeta<E>, populate: QueryPopulate<E> | undefined, fn: (relKey: RelationKey<E>, rawValue: unknown) => void): void;
|
|
72
71
|
export {};
|
|
@@ -182,11 +182,6 @@ export function parseRelationQueryValue(value) {
|
|
|
182
182
|
export function parseRelationAtKey(relKey, populate) {
|
|
183
183
|
return parseRelationQueryValue(populate?.[relKey]);
|
|
184
184
|
}
|
|
185
|
-
export function forEachRequestedRelation(meta, populate, fn) {
|
|
186
|
-
for (const relKey of getRelationRequestSummary(meta, populate).requestedKeys) {
|
|
187
|
-
fn(relKey, populate?.[relKey]);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
185
|
function isBooleanLikeValue(value) {
|
|
191
186
|
return value === true || value === false || value === 0 || value === 1;
|
|
192
187
|
}
|
package/dist/util/sql.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ColumnRef, type
|
|
1
|
+
import { ColumnRef, type IndexRefMap, QuerySql, type QuerySqlFn, type ComputedRefs, type RefMap, type RelationRef, type SqlArg, type SqlStatement, type SqlValueName, type TriggerRowName, type Type } from '../type/index.js';
|
|
2
2
|
/**
|
|
3
3
|
* Raw SQL, where an interpolated value binds, a `refs` field renders its column, and a `sql` renders
|
|
4
4
|
* in place: `sql`GREATEST(0, ${user.credits} - ${amount})``. A callback writes whatever it writes, so
|
|
@@ -34,19 +34,22 @@ export declare const uuidv7: QuerySql;
|
|
|
34
34
|
export declare const SQL_VALUES: Readonly<Record<SqlValueName, QuerySql>>;
|
|
35
35
|
/**
|
|
36
36
|
* The fields of `entity` as {@link ColumnRef}s, each rendering inside `sql` as its column: named the way
|
|
37
|
-
* the dialect names it, so the naming strategy and
|
|
37
|
+
* the dialect names it, so the naming strategy and `.name()` apply, and qualified by the alias
|
|
38
38
|
* in scope. Metadata is read when a ref renders, so the map serves before the fields are registered.
|
|
39
39
|
*/
|
|
40
40
|
export declare function refs<E>(entity: Type<E>): RefMap<E>;
|
|
41
41
|
/**
|
|
42
|
-
* The refs a definition's callbacks read:
|
|
42
|
+
* The refs a definition's callbacks read: a check's, a computed field's, a predicate's. A member decorator
|
|
43
43
|
* sees no class, so these name no entity and resolve against the one rendering them.
|
|
44
44
|
*/
|
|
45
45
|
export declare function memberRefs<E>(): ComputedRefs<E>;
|
|
46
|
-
/**
|
|
47
|
-
export declare function
|
|
48
|
-
/**
|
|
49
|
-
|
|
46
|
+
/** Each to-many as a schema's aggregates read it, by key: the same refs {@link memberRefs} gives a decorated entity. */
|
|
47
|
+
export declare function relationRefs(): Readonly<Record<string, RelationRef<object>>>;
|
|
48
|
+
/**
|
|
49
|
+
* The refs an index's callbacks read: each field's column, a JSON one also read into along its paths,
|
|
50
|
+
* `(user) => [user.settings.theme]`. Named for no entity, as {@link memberRefs} are.
|
|
51
|
+
*/
|
|
52
|
+
export declare function indexRefs<E>(): IndexRefMap<E>;
|
|
50
53
|
/**
|
|
51
54
|
* The fields of `entity` as the row a trigger body reads them off, qualified by the side it names:
|
|
52
55
|
* `NEW."col"` against the incoming row, `OLD."col"` against the outgoing one. Bound to the entity, as
|
package/dist/util/sql.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getMeta } from '../entity/metadata/definition.js';
|
|
2
|
-
import { ColumnRef, QuerySql, SQL_TEXT, RelationAggregate, TriggerWriteSql, } from '../type/index.js';
|
|
2
|
+
import { ColumnRef, JSON_PATH, JsonPathRef, QuerySql, REF_KEY, renderSql, SQL_TEXT, RelationAggregate, TriggerWriteSql, } from '../type/index.js';
|
|
3
3
|
import { aggregateOf, isInlinedExpression } from './field.util.js';
|
|
4
4
|
import { entityName, hasKeys } from './object.util.js';
|
|
5
5
|
import { UqlUsageError } from './uqlError.js';
|
|
@@ -20,7 +20,7 @@ export function sql(source, ...values) {
|
|
|
20
20
|
ctx.append(source[0]);
|
|
21
21
|
values.forEach((value, i) => {
|
|
22
22
|
if (value instanceof QuerySql) {
|
|
23
|
-
value
|
|
23
|
+
renderSql(value, opts);
|
|
24
24
|
}
|
|
25
25
|
else {
|
|
26
26
|
ctx.addValue(value);
|
|
@@ -36,7 +36,7 @@ sql.join = function join(parts, separator = ', ') {
|
|
|
36
36
|
if (index) {
|
|
37
37
|
opts.ctx.append(separator);
|
|
38
38
|
}
|
|
39
|
-
part
|
|
39
|
+
renderSql(part, opts);
|
|
40
40
|
});
|
|
41
41
|
});
|
|
42
42
|
};
|
|
@@ -88,27 +88,45 @@ export const SQL_VALUES = {
|
|
|
88
88
|
};
|
|
89
89
|
/**
|
|
90
90
|
* The fields of `entity` as {@link ColumnRef}s, each rendering inside `sql` as its column: named the way
|
|
91
|
-
* the dialect names it, so the naming strategy and
|
|
91
|
+
* the dialect names it, so the naming strategy and `.name()` apply, and qualified by the alias
|
|
92
92
|
* in scope. Metadata is read when a ref renders, so the map serves before the fields are registered.
|
|
93
93
|
*/
|
|
94
94
|
export function refs(entity) {
|
|
95
|
-
return
|
|
95
|
+
return refMap((key) => columnRef(entity, key));
|
|
96
|
+
}
|
|
97
|
+
/** A map answering each string key with `ref(key)`, typed as the caller's map, whose keys say what it holds. */
|
|
98
|
+
function refMap(ref) {
|
|
99
|
+
return new Proxy({}, { get: (_, key) => ref(String(key)) });
|
|
96
100
|
}
|
|
97
|
-
const MEMBER_REFS = new Proxy({}, { get: (_, key) => memberRef(String(key)) });
|
|
98
101
|
/**
|
|
99
|
-
* The refs a definition's callbacks read:
|
|
102
|
+
* The refs a definition's callbacks read: a check's, a computed field's, a predicate's. A member decorator
|
|
100
103
|
* sees no class, so these name no entity and resolve against the one rendering them.
|
|
101
104
|
*/
|
|
102
105
|
export function memberRefs() {
|
|
103
|
-
return
|
|
106
|
+
return refMap(memberRef);
|
|
104
107
|
}
|
|
105
|
-
/**
|
|
106
|
-
export function
|
|
107
|
-
return
|
|
108
|
+
/** Each to-many as a schema's aggregates read it, by key: the same refs {@link memberRefs} gives a decorated entity. */
|
|
109
|
+
export function relationRefs() {
|
|
110
|
+
return refMap(memberRef);
|
|
108
111
|
}
|
|
109
|
-
/**
|
|
110
|
-
|
|
111
|
-
|
|
112
|
+
/**
|
|
113
|
+
* The refs an index's callbacks read: each field's column, a JSON one also read into along its paths,
|
|
114
|
+
* `(user) => [user.settings.theme]`. Named for no entity, as {@link memberRefs} are.
|
|
115
|
+
*/
|
|
116
|
+
export function indexRefs() {
|
|
117
|
+
return refMap((key) => readsPaths(columnRef(undefined, key)));
|
|
118
|
+
}
|
|
119
|
+
/** `ref`, where any string key it has not got of its own reads one key further down the column's document. */
|
|
120
|
+
function readsPaths(ref) {
|
|
121
|
+
return new Proxy(ref, {
|
|
122
|
+
get: (target, prop, receiver) => {
|
|
123
|
+
if (typeof prop !== 'string' || prop in target) {
|
|
124
|
+
return Reflect.get(target, prop, receiver);
|
|
125
|
+
}
|
|
126
|
+
const path = target instanceof JsonPathRef ? `${target[JSON_PATH]}.${prop}` : prop;
|
|
127
|
+
return readsPaths(new JsonPathRef(target[REF_KEY], path));
|
|
128
|
+
},
|
|
129
|
+
});
|
|
112
130
|
}
|
|
113
131
|
/**
|
|
114
132
|
* One member as a definition reads it: a {@link ColumnRef} where it names a field, and the same object
|
|
@@ -116,7 +134,7 @@ export function entityWhere(where) {
|
|
|
116
134
|
* a member decorator sees no class and so cannot know which the key is; the types keep them apart.
|
|
117
135
|
*/
|
|
118
136
|
function memberRef(relation) {
|
|
119
|
-
const over = (op) => (pick, q) => relationAggregate({ relation, op, field: pick(memberRefs())
|
|
137
|
+
const over = (op) => (pick, q) => relationAggregate({ relation, op, field: pick(memberRefs())[REF_KEY], ...rowsOf(q) });
|
|
120
138
|
return Object.assign(columnRef(undefined, relation), {
|
|
121
139
|
count: (q) => relationAggregate({ relation, op: '$count', ...rowsOf(q) }),
|
|
122
140
|
sum: over('$sum'),
|
|
@@ -178,7 +196,7 @@ function renderColumn(meta, key, opts, qualifier) {
|
|
|
178
196
|
throw new UqlUsageError(`'${entityName(meta)}.${key}' reads a relation, which a trigger's row cannot: it fires on one row, ` +
|
|
179
197
|
'with no table in scope to correlate a subquery to. Name the columns it is derived from instead.');
|
|
180
198
|
}
|
|
181
|
-
grouped(field.computed)
|
|
199
|
+
renderSql(grouped(field.computed), scope);
|
|
182
200
|
return;
|
|
183
201
|
}
|
|
184
202
|
scope.ctx.append(scope.escapedPrefix + scope.dialect.escapeId(scope.dialect.columnOf(meta, key), true));
|
package/dist/util/sql.util.d.ts
CHANGED
|
@@ -12,6 +12,10 @@ export declare function splitQualifiedName(qualified: string): {
|
|
|
12
12
|
readonly name: string;
|
|
13
13
|
readonly schema?: string;
|
|
14
14
|
};
|
|
15
|
+
/** Hex chars of hash kept when a name has to be shortened. 24 bits over one table's constraints. */
|
|
16
|
+
export declare const NAME_HASH_LENGTH = 6;
|
|
17
|
+
/** Between table and columns, doubled: index names share one namespace per database, where `a` + `b_c` and `a_b` + `c` would collide. */
|
|
18
|
+
export declare const TABLE_SEPARATOR = "__";
|
|
15
19
|
/**
|
|
16
20
|
* Whether uql installed the object called `name`. Ownership is the prefix and nothing else, since no
|
|
17
21
|
* engine records who created one - so this is the only thing standing between a hand-written trigger or
|
|
@@ -29,11 +33,6 @@ export declare function ownedName(table: string, label: string, content: string)
|
|
|
29
33
|
* unique one. A partial index also carries a hash of its `where`, so an edited predicate is a new name.
|
|
30
34
|
*/
|
|
31
35
|
export declare function derivedIndexName(table: string, columns: readonly string[], unique?: boolean, where?: string): string;
|
|
32
|
-
/**
|
|
33
|
-
* Whether uql named the index `name` itself from `columns`: `derivedIndexName`'s forms, with a predicate's hash or
|
|
34
|
-
* without, and the `idx_<table>_<columns>` spelling an older uql gave one.
|
|
35
|
-
*/
|
|
36
|
-
export declare function isDerivedIndexName(table: string, columns: readonly string[], name: string): boolean;
|
|
37
36
|
/**
|
|
38
37
|
* The constraint name a primary key gets when we name one: `Enrolment__studentId_courseId_pk`.
|
|
39
38
|
*
|
package/dist/util/sql.util.js
CHANGED
|
@@ -21,19 +21,22 @@ export function splitQualifiedName(qualified) {
|
|
|
21
21
|
*/
|
|
22
22
|
const MAX_IDENTIFIER_LENGTH = 63;
|
|
23
23
|
/** Hex chars of hash kept when a name has to be shortened. 24 bits over one table's constraints. */
|
|
24
|
-
const NAME_HASH_LENGTH = 6;
|
|
24
|
+
export const NAME_HASH_LENGTH = 6;
|
|
25
25
|
/**
|
|
26
26
|
* The name of a derived index or constraint, such as `Order__total_idx`, with the kind last as Postgres names
|
|
27
27
|
* its own. It leaves out the table's schema, since it is a single identifier. The AST, the DDL and a `DROP`
|
|
28
28
|
* all use this one rule.
|
|
29
29
|
*/
|
|
30
30
|
function derivedConstraintName(table, parts, kind) {
|
|
31
|
-
const
|
|
32
|
-
const body = parts.length ? `${name}${TABLE_SEPARATOR}${parts.join('_')}` : name;
|
|
31
|
+
const body = parts.length ? `${tableStem(table)}${parts.join('_')}` : splitQualifiedName(table).name;
|
|
33
32
|
return clampIdentifier(`${body}_${kind}`);
|
|
34
33
|
}
|
|
34
|
+
/** What a derived name over parts starts with: its table, unqualified, and the separator. */
|
|
35
|
+
function tableStem(table) {
|
|
36
|
+
return splitQualifiedName(table).name + TABLE_SEPARATOR;
|
|
37
|
+
}
|
|
35
38
|
/** Between table and columns, doubled: index names share one namespace per database, where `a` + `b_c` and `a_b` + `c` would collide. */
|
|
36
|
-
const TABLE_SEPARATOR = '__';
|
|
39
|
+
export const TABLE_SEPARATOR = '__';
|
|
37
40
|
/** What every name uql installs begins with, so the two ends asking about one cannot spell it apart. */
|
|
38
41
|
const OWNED_START = `${OWNED_PREFIX}_`;
|
|
39
42
|
/**
|
|
@@ -74,18 +77,6 @@ export function derivedIndexName(table, columns, unique = false, where) {
|
|
|
74
77
|
const parts = where === undefined ? columns : [...columns, hashIdentifier(where)];
|
|
75
78
|
return derivedConstraintName(table, parts, unique ? 'uk' : 'idx');
|
|
76
79
|
}
|
|
77
|
-
/** The hash a partial index's derived name carries before its kind. */
|
|
78
|
-
const PREDICATE_HASH = new RegExp(`_[0-9a-f]{${NAME_HASH_LENGTH}}(?=_(?:idx|uk)$)`);
|
|
79
|
-
/**
|
|
80
|
-
* Whether uql named the index `name` itself from `columns`: `derivedIndexName`'s forms, with a predicate's hash or
|
|
81
|
-
* without, and the `idx_<table>_<columns>` spelling an older uql gave one.
|
|
82
|
-
*/
|
|
83
|
-
export function isDerivedIndexName(table, columns, name) {
|
|
84
|
-
const plain = [derivedIndexName(table, columns), derivedIndexName(table, columns, true)];
|
|
85
|
-
return (plain.includes(name) ||
|
|
86
|
-
plain.includes(name.replace(PREDICATE_HASH, '')) ||
|
|
87
|
-
name === `idx_${table}_${columns.join('_')}`);
|
|
88
|
-
}
|
|
89
80
|
/**
|
|
90
81
|
* The constraint name a primary key gets when we name one: `Enrolment__studentId_courseId_pk`.
|
|
91
82
|
*
|
|
@@ -31,3 +31,5 @@ export declare function pluralize(name: string): string;
|
|
|
31
31
|
* browser and edge entries cannot reach `node:crypto`. It is not a security hash; it only tells inputs apart.
|
|
32
32
|
*/
|
|
33
33
|
export declare function fnv1a(value: string): number;
|
|
34
|
+
/** `text` as a regex matching it literally. */
|
|
35
|
+
export declare function escapeRegExp(text: string): string;
|
package/dist/util/string.util.js
CHANGED
package/dist/util/uqlError.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ export declare class UqlSecurityError extends UqlError {
|
|
|
32
32
|
/** What a value is, for a refusal naming what `/http` handed over instead of what the types require. */
|
|
33
33
|
export declare function kindOf(value: unknown): string;
|
|
34
34
|
/**
|
|
35
|
-
* Thrown when an update's
|
|
35
|
+
* Thrown when an update's `f.version()` column no longer matches the row: another writer moved it on,
|
|
36
36
|
* or it is gone. `expected` is what the payload carried, `actual` what the row holds now, `undefined`
|
|
37
37
|
* where there is no row left.
|
|
38
38
|
*/
|
package/dist/util/uqlError.js
CHANGED
|
@@ -25,7 +25,7 @@ export function kindOf(value) {
|
|
|
25
25
|
return value === null ? 'null' : Array.isArray(value) ? 'array' : typeof value;
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
* Thrown when an update's
|
|
28
|
+
* Thrown when an update's `f.version()` column no longer matches the row: another writer moved it on,
|
|
29
29
|
* or it is gone. `expected` is what the payload carried, `actual` what the row holds now, `undefined`
|
|
30
30
|
* where there is no row left.
|
|
31
31
|
*/
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"homepage": "https://uql-orm.dev",
|
|
4
4
|
"description": "JSON-native TypeScript ORM for Bun, Browsers, Edge, Node, Workers. Supports PostgreSQL, PGlite, MySQL, MariaDB, SQLite, CockroachDB, SQL Server, Turso, Neon, Cloudflare D1 and MongoDB. Queries are plain JSON, typed to the leaf.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.105.0",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"engines": {
|
|
9
9
|
"node": ">=22.18"
|