uql-orm 0.111.2 → 0.112.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 +4 -4
- package/dist/browser/querier/httpQuerier.js +14 -8
- package/dist/dialect/abstractSqlDialect.js +3 -3
- package/dist/dialect/jsonSql.js +2 -3
- package/dist/dialect/operators.js +4 -5
- package/dist/entity/decorator/members.d.ts +4 -13
- package/dist/entity/decorator/members.js +19 -10
- package/dist/entity/metadata/definedFixture/author.d.ts +2 -1
- package/dist/entity/metadata/definedFixture/folder.d.ts +126 -0
- package/dist/entity/metadata/definedFixture/folder.js +12 -0
- package/dist/entity/metadata/definition.js +3 -3
- package/dist/http/contract.d.ts +15 -0
- package/dist/http/contract.js +14 -14
- package/dist/http/handler.js +6 -3
- package/dist/http/query.d.ts +3 -5
- package/dist/http/query.js +10 -10
- package/dist/http/wireRow.d.ts +18 -0
- package/dist/http/wireRow.js +157 -0
- package/dist/mariadb/mariadbQuerierPool.d.ts +7 -1
- package/dist/mariadb/mariadbQuerierPool.js +0 -1
- package/dist/migrate/codegen/entityCodeGenerator.js +2 -3
- package/dist/migrate/introspection/mysqlIntrospector.d.ts +4 -1
- package/dist/migrate/introspection/mysqlIntrospector.js +12 -2
- package/dist/mongodb/mongoDialect.js +6 -6
- package/dist/mssql/mssqlDialect.js +2 -2
- package/dist/sqlite/hranaQuerier.d.ts +2 -2
- package/dist/sqlite/localSqliteQuerierPool.d.ts +1 -1
- package/dist/sqlite/sqliteQuerierPool.d.ts +2 -2
- package/dist/sqlite/sqliteQuerierPool.js +2 -2
- package/dist/type/definedEntity.d.ts +8 -1
- package/dist/util/dialect.util.d.ts +0 -7
- package/dist/util/dialect.util.js +2 -16
- package/dist/util/object.util.d.ts +5 -2
- package/dist/util/object.util.js +12 -15
- package/dist/util/query.util.js +3 -10
- package/package.json +5 -10
- package/skills/uql-orm/SKILL.md +6 -5
- package/dist/browser/uql-browser.min.js +0 -4
- package/dist/browser/uql-browser.min.js.map +0 -19
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { EQUALITY_OPS, isGroupOp, isOrderedOp } from '../dialect/operators.js';
|
|
2
|
+
import { getMeta, relationOf, valueFieldOf } from '../entity/metadata/definition.js';
|
|
3
|
+
import { decodeDate } from '../util/date.js';
|
|
4
|
+
import { aggregateOf, columnFamily } from '../util/field.util.js';
|
|
5
|
+
import { cachedIn, definedEntries, isPlainObject } from '../util/object.util.js';
|
|
6
|
+
/** What JSON cannot hold as the server holds it: a `bigint`, sent as its digits. */
|
|
7
|
+
const sent = (field) => (valueFieldOf(field).type === BigInt ? String : undefined);
|
|
8
|
+
/**
|
|
9
|
+
* What JSON carried as text: a `bigint`'s digits, and a date's ISO text. A time of day stays the text it is, and so
|
|
10
|
+
* does text no `bigint` spells, for the engine to refuse as it refuses any malformed value.
|
|
11
|
+
*/
|
|
12
|
+
const received = (field) => {
|
|
13
|
+
const { type } = valueFieldOf(field);
|
|
14
|
+
if (type === BigInt) {
|
|
15
|
+
return (text) => {
|
|
16
|
+
try {
|
|
17
|
+
return BigInt(String(text));
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return text;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return columnFamily(type) === 'date' ? (text) => decodeDate(String(text)) : undefined;
|
|
25
|
+
};
|
|
26
|
+
/** `data` as JSON is to carry it, by what its route `answers`; the client reads it back by {@link readWireData}. */
|
|
27
|
+
export function writeWireData(meta, data, answers) {
|
|
28
|
+
return across(meta, data, answers, SENDING);
|
|
29
|
+
}
|
|
30
|
+
/** `data` JSON carried, a response or a write's body, read back as the entity holds it by what it `answers`. */
|
|
31
|
+
export function readWireData(meta, data, answers) {
|
|
32
|
+
return across(meta, data, answers, RECEIVING);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* `query` with each value it compares a field with read back as the entity holds it, its `$populate` and `$count`
|
|
36
|
+
* relations' included: JSON carried each date and `bigint` as text, which an engine would compare as text.
|
|
37
|
+
*/
|
|
38
|
+
export function readWireQuery(meta, query) {
|
|
39
|
+
const plan = planIn(RECEIVING, meta);
|
|
40
|
+
const related = (clause) => Object.fromEntries(Object.entries(clause).map(([key, nested]) => {
|
|
41
|
+
const target = plan.relations.get(key);
|
|
42
|
+
return [key, target && isPlainObject(nested) ? readWireQuery(target, nested) : nested];
|
|
43
|
+
}));
|
|
44
|
+
return {
|
|
45
|
+
...query,
|
|
46
|
+
...(query.$where !== undefined && { $where: whereOf(meta, query.$where) }),
|
|
47
|
+
...(query.$populate !== undefined && { $populate: related(query.$populate) }),
|
|
48
|
+
...(query.$count !== undefined && { $count: related(query.$count) }),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const SENDING = { field: sent, plans: new WeakMap() };
|
|
52
|
+
const RECEIVING = { field: received, plans: new WeakMap() };
|
|
53
|
+
function planIn(way, meta) {
|
|
54
|
+
return cachedIn(way.plans, meta, () => planOf(meta, way.field));
|
|
55
|
+
}
|
|
56
|
+
function across(meta, data, answers, way) {
|
|
57
|
+
switch (answers) {
|
|
58
|
+
case 'count':
|
|
59
|
+
return data;
|
|
60
|
+
case 'rows':
|
|
61
|
+
return convert(meta, data, way);
|
|
62
|
+
case 'page': {
|
|
63
|
+
if (!isPlainObject(data)) {
|
|
64
|
+
return data;
|
|
65
|
+
}
|
|
66
|
+
const items = convert(meta, data['items'], way);
|
|
67
|
+
return items === data['items'] ? data : { ...data, items };
|
|
68
|
+
}
|
|
69
|
+
case 'ids': {
|
|
70
|
+
const [idKey] = meta.ids;
|
|
71
|
+
const sole = meta.ids.length === 1 ? planIn(way, meta).fields.get(idKey) : undefined;
|
|
72
|
+
// A composite key is a row of its columns; a sole one its value.
|
|
73
|
+
const id = (key) => isPlainObject(key) ? convert(meta, key, way) : sole && key != null ? sole(key) : key;
|
|
74
|
+
return Array.isArray(data) ? data.map(id) : id(data);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function convert(meta, rows, way) {
|
|
79
|
+
if (Array.isArray(rows)) {
|
|
80
|
+
const converted = rows.map((row) => convert(meta, row, way));
|
|
81
|
+
return converted.every((row, index) => row === rows[index]) ? rows : converted;
|
|
82
|
+
}
|
|
83
|
+
if (!isPlainObject(rows)) {
|
|
84
|
+
return rows;
|
|
85
|
+
}
|
|
86
|
+
const plan = planIn(way, meta);
|
|
87
|
+
let row;
|
|
88
|
+
for (const [key, to] of plan.fields) {
|
|
89
|
+
const value = rows[key];
|
|
90
|
+
if (value != null) {
|
|
91
|
+
row ??= { ...rows };
|
|
92
|
+
// An update's operator, `{ $inc: 1 }`, holds the values it applies.
|
|
93
|
+
row[key] = isPlainObject(value)
|
|
94
|
+
? Object.fromEntries(Object.entries(value).map(([op, operand]) => [op, to(operand)]))
|
|
95
|
+
: to(value);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
for (const [key, target] of plan.relations) {
|
|
99
|
+
const read = convert(target, rows[key], way);
|
|
100
|
+
if (read !== rows[key]) {
|
|
101
|
+
row ??= { ...rows };
|
|
102
|
+
row[key] = read;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return row ?? rows;
|
|
106
|
+
}
|
|
107
|
+
function planOf(meta, convertField) {
|
|
108
|
+
return {
|
|
109
|
+
fields: new Map(definedEntries(meta.fields).flatMap(([key, field]) => {
|
|
110
|
+
const source = readField(meta, field);
|
|
111
|
+
const to = source && convertField(source);
|
|
112
|
+
return to ? [[key, to]] : [];
|
|
113
|
+
})),
|
|
114
|
+
relations: new Map(definedEntries(meta.relations).map(([key, relation]) => [key, getMeta(relation.entity())])),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/** A `$where` of `meta` with each value a field is compared with read back, through groups and relations. */
|
|
118
|
+
function whereOf(meta, where) {
|
|
119
|
+
if (Array.isArray(where)) {
|
|
120
|
+
return where.map((clause) => whereOf(meta, clause));
|
|
121
|
+
}
|
|
122
|
+
if (!isPlainObject(where)) {
|
|
123
|
+
return where;
|
|
124
|
+
}
|
|
125
|
+
const plan = planIn(RECEIVING, meta);
|
|
126
|
+
return Object.fromEntries(Object.entries(where).map(([key, value]) => {
|
|
127
|
+
const to = plan.fields.get(key);
|
|
128
|
+
if (to) {
|
|
129
|
+
return [key, compared(value, to)];
|
|
130
|
+
}
|
|
131
|
+
const target = plan.relations.get(key);
|
|
132
|
+
return [key, target ? whereOf(target, value) : isGroupOp(key) ? whereOf(meta, value) : value];
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
/** A field's condition with each value it compares with read by `to`: a pattern or a null check stays as it is. */
|
|
136
|
+
function compared(condition, to) {
|
|
137
|
+
if (Array.isArray(condition)) {
|
|
138
|
+
return condition.map(to);
|
|
139
|
+
}
|
|
140
|
+
if (!isPlainObject(condition)) {
|
|
141
|
+
return condition == null ? condition : to(condition);
|
|
142
|
+
}
|
|
143
|
+
return Object.fromEntries(Object.entries(condition).map(([op, value]) => [
|
|
144
|
+
op,
|
|
145
|
+
op === '$not' || EQUALITY_OPS.has(op) || isOrderedOp(op) ? compared(value, to) : value,
|
|
146
|
+
]));
|
|
147
|
+
}
|
|
148
|
+
/** The field a field's value reads as: a relation aggregate's, none for a tally or a mean, which read as numbers. */
|
|
149
|
+
function readField(meta, field) {
|
|
150
|
+
const spec = aggregateOf(field);
|
|
151
|
+
if (!spec) {
|
|
152
|
+
return field;
|
|
153
|
+
}
|
|
154
|
+
return spec.field && spec.op !== '$avg'
|
|
155
|
+
? getMeta(relationOf(meta, spec.relation).entity()).fields[spec.field]
|
|
156
|
+
: undefined;
|
|
157
|
+
}
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import { createPool, type Pool } from 'mariadb';
|
|
1
|
+
import { createPool, type Pool, type SqlError } from 'mariadb';
|
|
2
2
|
import { AbstractSqlQuerierPool } from '../querier/index.js';
|
|
3
3
|
import type { ExtraOptions } from '../type/index.js';
|
|
4
4
|
import { MariaDialect } from './mariaDialect.js';
|
|
5
5
|
import { MariadbQuerier } from './mariadbQuerier.js';
|
|
6
|
+
declare module 'mariadb' {
|
|
7
|
+
/** The pool is an `EventEmitter` firing 'error' on a connection it cannot open or clean up, which its types leave out. */
|
|
8
|
+
interface Pool {
|
|
9
|
+
on(ev: 'error', callback: (err: SqlError) => void): Pool;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
6
12
|
type PoolConfig = Exclude<Parameters<typeof createPool>[0], string>;
|
|
7
13
|
export declare class MariadbQuerierPool extends AbstractSqlQuerierPool<MariadbQuerier, MariaDialect> {
|
|
8
14
|
readonly pool: Pool;
|
|
@@ -13,7 +13,6 @@ export class MariadbQuerierPool extends AbstractSqlQuerierPool {
|
|
|
13
13
|
// reads as its UTC text, which hydration decodes, since the connector would take it for local time
|
|
14
14
|
// whatever `timezone` says; that only sets the session's zone, UTC, so `NOW()` agrees.
|
|
15
15
|
this.pool = createPool({ timezone: 'Z', dateStrings: true, ...opts });
|
|
16
|
-
// `mariadb` fires 'error' at runtime without declaring it, hence the cast; this makes it visible.
|
|
17
16
|
attachPoolErrorHandler(this.pool, 'MariaDB pool connection encountered an error', extra?.logger);
|
|
18
17
|
}
|
|
19
18
|
async getQuerier() {
|
|
@@ -169,14 +169,13 @@ export class EntityCodeGenerator {
|
|
|
169
169
|
const relatedClassName = this.options.classNameTransformer(rel.to.table.name);
|
|
170
170
|
const references = this.referencesSource(rel, relatedClassName);
|
|
171
171
|
const options = [
|
|
172
|
-
`entity: () => ${relatedClassName}`,
|
|
173
172
|
...(references ? [`references: ${references}`] : []),
|
|
174
173
|
...(rel.onDelete && rel.onDelete !== DEFAULT_FOREIGN_KEY_ACTION ? [`onDelete: '${rel.onDelete}'`] : []),
|
|
175
174
|
...(rel.onUpdate && rel.onUpdate !== DEFAULT_FOREIGN_KEY_ACTION ? [`onUpdate: '${rel.onUpdate}'`] : []),
|
|
176
175
|
];
|
|
177
176
|
return [
|
|
178
177
|
...this.syncDoc(`Relation to ${rel.to.table.name} via ${rel.from.columns.map((c) => c.name).join(', ')}`),
|
|
179
|
-
` @${rel.type}({ ${options.join(', ')} })`,
|
|
178
|
+
` @${rel.type}(() => ${relatedClassName}${options.length ? `, { ${options.join(', ')} }` : ''})`,
|
|
180
179
|
` ${this.owningPropertyName(rel)}?: ${relatedClassName};`,
|
|
181
180
|
].join('\n');
|
|
182
181
|
}
|
|
@@ -225,7 +224,7 @@ export class EntityCodeGenerator {
|
|
|
225
224
|
const many = inverseType === 'OneToMany' || inverseType === 'ManyToMany';
|
|
226
225
|
return [
|
|
227
226
|
...this.syncDoc(`Inverse relation from ${rel.from.table.name}`),
|
|
228
|
-
` @${inverseType}(
|
|
227
|
+
` @${inverseType}(() => ${relatedClassName}, { mappedBy: (${param}) => ${inverse} })`,
|
|
229
228
|
` ${this.inversePropertyName(rel)}?: ${relatedClassName}${many ? '[]' : ''};`,
|
|
230
229
|
].join('\n');
|
|
231
230
|
}
|
|
@@ -46,7 +46,10 @@ export declare class MariadbSchemaIntrospector extends MysqlSchemaIntrospector {
|
|
|
46
46
|
* JSON column's `json_valid()` is its type, read by {@link getColumns}, so it is left out.
|
|
47
47
|
*/
|
|
48
48
|
protected getChecks(read: TableRowReader, tableName: string): Promise<CheckSchema[]>;
|
|
49
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* MariaDB prints a literal quoted, as SQL reads it (`'it''s'`), so an unquoted default is SQL, a function it
|
|
51
|
+
* prints by an alias named as declared ({@link MARIADB_ALIASES}).
|
|
52
|
+
*/
|
|
50
53
|
protected sqlText(defaultValue: string): string | undefined;
|
|
51
54
|
/** Whether an index is MariaDB's vector index, and the distance it was built for. */
|
|
52
55
|
protected readonly indexFacets: ReadonlySet<IndexFacet>;
|
|
@@ -175,6 +175,10 @@ export class MysqlSchemaIntrospector extends AbstractSqlSchemaIntrospector {
|
|
|
175
175
|
}
|
|
176
176
|
}
|
|
177
177
|
const TIMESTAMP_TYPES = new Set(['datetime', 'timestamp']);
|
|
178
|
+
/** The functions MariaDB prints by an alias, each with the name SQL declares it by. */
|
|
179
|
+
const MARIADB_ALIASES = { lcase: 'lower', ucase: 'upper' };
|
|
180
|
+
/** A quoted literal, kept as it is, or an alias called. */
|
|
181
|
+
const MARIADB_ALIAS = new RegExp(`'(?:[^']|'')*'|\\b(?:${Object.keys(MARIADB_ALIASES).join('|')})(?=\\()`, 'gi');
|
|
178
182
|
/**
|
|
179
183
|
* MariaDB reads out of the same `information_schema` as MySQL, save for one column type it does not
|
|
180
184
|
* have: `JSON` there is an alias for `LONGTEXT` plus a `json_valid()` check constraint named after
|
|
@@ -196,9 +200,15 @@ export class MariadbSchemaIntrospector extends MysqlSchemaIntrospector {
|
|
|
196
200
|
AND CHECK_CLAUSE <> CONCAT('json_valid(\`', CONSTRAINT_NAME, '\`)')
|
|
197
201
|
`);
|
|
198
202
|
}
|
|
199
|
-
/**
|
|
203
|
+
/**
|
|
204
|
+
* MariaDB prints a literal quoted, as SQL reads it (`'it''s'`), so an unquoted default is SQL, a function it
|
|
205
|
+
* prints by an alias named as declared ({@link MARIADB_ALIASES}).
|
|
206
|
+
*/
|
|
200
207
|
sqlText(defaultValue) {
|
|
201
|
-
|
|
208
|
+
if (defaultValue.startsWith("'")) {
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
return defaultValue.replace(MARIADB_ALIAS, (match) => MARIADB_ALIASES[match.toLowerCase()] ?? match);
|
|
202
212
|
}
|
|
203
213
|
/** Whether an index is MariaDB's vector index, and the distance it was built for. */
|
|
204
214
|
indexFacets = new Set(['vector', 'distance']);
|
|
@@ -7,7 +7,7 @@ import { assertSoleId, fieldOf, getMeta, relationOf, soleIdOf } from '../entity/
|
|
|
7
7
|
import { referenceKeys } from '../entity/metadata/definition.js';
|
|
8
8
|
import { COUNT_RESULT_KEY } from '../type/query.js';
|
|
9
9
|
import { ColumnRef, QuerySql, REF_ENTITY, REF_KEY } from '../type/querySql.js';
|
|
10
|
-
import { aggregateOf, isSelectList, assertAggregateColumns, assertNoUndefined, assertNonNegativeInteger, columnFamily, countedRelations, entityName, fieldUpdateOf, fillOnFields, filterFieldKeys, findVectorIndex, findVectorSort, definedEntries, getKeys, holdsForeignKey, jsonPathKeys, getRelationRequestSummary, hasKeys, isFieldUpdateOp, isJsonObject, foldJson, writesKeys, isJsonUpdateOp,
|
|
10
|
+
import { aggregateOf, isSelectList, assertAggregateColumns, assertNoUndefined, assertNonNegativeInteger, columnFamily, countedRelations, entityName, fieldUpdateOf, fillOnFields, filterFieldKeys, findVectorIndex, findVectorSort, definedEntries, getKeys, holdsForeignKey, jsonPathKeys, getRelationRequestSummary, hasKeys, isFieldUpdateOp, isJsonObject, foldJson, writesKeys, isJsonUpdateOp, isPlainObject, isOperatorObject, isRecord, isVectorSearch, normalizeScalarFieldSelection, parentJoins, parseGroupMap, parseRelationAtKey, parseRelationSize, parseSortDirection, rankedTextSearch, someKey, targetJoins, textSortOf, indexedDistance, vectorDistanceOf, vectorCandidates, populatesRelations, cachedIn, } from '../util/index.js';
|
|
11
11
|
import { UqlUsageError } from '../util/uqlError.js';
|
|
12
12
|
import { decodeBigIntsExcept } from '../util/wideNumber.js';
|
|
13
13
|
import { textLanguage } from './textLanguage.js';
|
|
@@ -470,7 +470,7 @@ export class MongoDialect extends AbstractDialect {
|
|
|
470
470
|
}
|
|
471
471
|
// An object or an array is matched by what it holds, as the SQL engines read it, where native `$all`
|
|
472
472
|
// compares the whole element. MongoDB takes `$elemMatch` there only when every value is one.
|
|
473
|
-
if (op === '$all' && Array.isArray(val) && val.some((value) => Array.isArray(value) ||
|
|
473
|
+
if (op === '$all' && Array.isArray(val) && val.some((value) => Array.isArray(value) || isPlainObject(value))) {
|
|
474
474
|
result[op] = this.allHolding(val);
|
|
475
475
|
continue;
|
|
476
476
|
}
|
|
@@ -515,7 +515,7 @@ export class MongoDialect extends AbstractDialect {
|
|
|
515
515
|
if (Array.isArray(value)) {
|
|
516
516
|
return this.transformOperators({ $all: value });
|
|
517
517
|
}
|
|
518
|
-
if (!
|
|
518
|
+
if (!isPlainObject(value)) {
|
|
519
519
|
return value;
|
|
520
520
|
}
|
|
521
521
|
return isOperatorObject(value) ? this.transformOperators(value) : this.containment(value);
|
|
@@ -529,7 +529,7 @@ export class MongoDialect extends AbstractDialect {
|
|
|
529
529
|
if (Array.isArray(value)) {
|
|
530
530
|
return { $elemMatch: { $all: this.allHolding(value) } };
|
|
531
531
|
}
|
|
532
|
-
return { $elemMatch:
|
|
532
|
+
return { $elemMatch: isPlainObject(value) ? this.held(value) : { $eq: value } };
|
|
533
533
|
});
|
|
534
534
|
}
|
|
535
535
|
/** An object's keys as {@link held} reads each, a nested object's by its dotted path rather than whole. */
|
|
@@ -1504,7 +1504,7 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1504
1504
|
* by `$in`, which stops at the first match where testing each element would map them all.
|
|
1505
1505
|
*/
|
|
1506
1506
|
allHeld(ref, values, read) {
|
|
1507
|
-
return allOf(values.map((value) =>
|
|
1507
|
+
return allOf(values.map((value) => isPlainObject(value) || Array.isArray(value)
|
|
1508
1508
|
? anyElement(ref, this.heldExpression('$$this', value, read))
|
|
1509
1509
|
: { $cond: [{ $isArray: ref }, { $in: [read(value), ref] }, false] }));
|
|
1510
1510
|
}
|
|
@@ -1515,7 +1515,7 @@ export class MongoDialect extends AbstractDialect {
|
|
|
1515
1515
|
// Classified exactly as the SQL side classifies a `$where`/`$having` value, so the two agree
|
|
1516
1516
|
// on identical input. Keeping only numbers and objects dropped a string or boolean without a
|
|
1517
1517
|
// word, handing back every group instead of the filtered ones.
|
|
1518
|
-
if (
|
|
1518
|
+
if (isPlainObject(condition)) {
|
|
1519
1519
|
filter[alias] = this.transformOperators(condition, 'unsupported HAVING operator');
|
|
1520
1520
|
}
|
|
1521
1521
|
else {
|
|
@@ -10,7 +10,7 @@ import { QuerySql } from '../type/index.js';
|
|
|
10
10
|
import { parseQueryLock } from '../type/index.js';
|
|
11
11
|
import { bytesToHex } from '../util/bytes.js';
|
|
12
12
|
import { isExactDecimal, isInlinedExpression, jsonPathKeys } from '../util/field.util.js';
|
|
13
|
-
import { assertNonNegativeInteger, definedEntries,
|
|
13
|
+
import { assertNonNegativeInteger, definedEntries, isPlainObject, keySet, RETURNED_ID } from '../util/index.js';
|
|
14
14
|
import { escapeSingleQuotes } from '../util/sqlLiteral.js';
|
|
15
15
|
import { UqlUsageError } from '../util/uqlError.js';
|
|
16
16
|
/** What SQL Server has. */
|
|
@@ -59,7 +59,7 @@ function openJsonType(value) {
|
|
|
59
59
|
if (Array.isArray(value)) {
|
|
60
60
|
return OPENJSON_TYPE.array;
|
|
61
61
|
}
|
|
62
|
-
if (
|
|
62
|
+
if (isPlainObject(value)) {
|
|
63
63
|
return OPENJSON_TYPE.object;
|
|
64
64
|
}
|
|
65
65
|
if (typeof value === 'number') {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { AbstractSqlQuerier, type BoundStatement } from '../querier/index.js';
|
|
2
|
-
import type { ExtraOptions, TransactionOptions } from '../type/index.js';
|
|
2
|
+
import type { ExtraOptions, RawRow, TransactionOptions } from '../type/index.js';
|
|
3
3
|
import type { SqlExecution } from '../util/sql.util.js';
|
|
4
4
|
import { type SqliteBindValue } from './bindValues.js';
|
|
5
5
|
import type { SqliteDialect } from './sqliteDialect.js';
|
|
6
6
|
export type HranaInValue = SqliteBindValue | ArrayBuffer | Date;
|
|
7
7
|
export type HranaResultSet = {
|
|
8
|
-
rows:
|
|
8
|
+
rows: RawRow[];
|
|
9
9
|
rowsAffected: number;
|
|
10
10
|
};
|
|
11
11
|
export type HranaStatement = {
|
|
@@ -18,7 +18,7 @@ export type LocalSqlitePoolOptions = {
|
|
|
18
18
|
export type LocalSqliteDatabase = {
|
|
19
19
|
prepare(sql: string): SqlitePreparedStatement;
|
|
20
20
|
loadExtension(path: string): void;
|
|
21
|
-
/** Defines a SQL function, which `bun:sqlite`
|
|
21
|
+
/** Defines a SQL function, which `bun:sqlite` has from Bun 1.4.4. */
|
|
22
22
|
function?(name: string, options: {
|
|
23
23
|
deterministic: boolean;
|
|
24
24
|
}, fn: (...args: SqlValue[]) => SqlValue): unknown;
|
|
@@ -13,8 +13,8 @@ export declare class SqliteQuerierPool extends AbstractLocalSqliteQuerierPool<Lo
|
|
|
13
13
|
readonly opts?: SqlitePoolOptions | undefined;
|
|
14
14
|
constructor(filename?: string | Buffer, opts?: SqlitePoolOptions | undefined, extra?: ExtraOptions);
|
|
15
15
|
/**
|
|
16
|
-
* `
|
|
17
|
-
*
|
|
16
|
+
* `extensions` is uql's, so it never reaches the driver. `bun:sqlite` opens only a path, so a serialized
|
|
17
|
+
* database, which better-sqlite3 takes as its filename, is deserialized there instead.
|
|
18
18
|
*/
|
|
19
19
|
protected createDb(): Promise<LocalSqliteDatabase>;
|
|
20
20
|
}
|
|
@@ -14,8 +14,8 @@ export class SqliteQuerierPool extends AbstractLocalSqliteQuerierPool {
|
|
|
14
14
|
this.opts = opts;
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
|
-
* `
|
|
18
|
-
*
|
|
17
|
+
* `extensions` is uql's, so it never reaches the driver. `bun:sqlite` opens only a path, so a serialized
|
|
18
|
+
* database, which better-sqlite3 takes as its filename, is deserialized there instead.
|
|
19
19
|
*/
|
|
20
20
|
async createDb() {
|
|
21
21
|
const { extensions, ...driverOpts } = this.opts ?? {};
|
|
@@ -76,6 +76,13 @@ export type AggregateFields = {
|
|
|
76
76
|
readonly computed: QuerySql;
|
|
77
77
|
}>;
|
|
78
78
|
};
|
|
79
|
+
/**
|
|
80
|
+
* What `relations` constrains its aggregates to: a thunk returning `void`, which TypeScript checks without reading what it
|
|
81
|
+
* returns. Reading it would read a tree's own to-many while its type is inferred (TS7022); first read refuses a wrong value.
|
|
82
|
+
*/
|
|
83
|
+
type AggregateThunks = {
|
|
84
|
+
readonly [key: string]: () => void;
|
|
85
|
+
};
|
|
79
86
|
/** The aggregate fields a `relations` callback declares, as a row reads them, never written. */
|
|
80
87
|
export type AggregateRow<A> = {
|
|
81
88
|
readonly [K in keyof A]-?: A[K] extends () => infer F ? FieldValue<F> : never;
|
|
@@ -160,7 +167,7 @@ type DefinedSections<Row, Key, Columns, B, Abstract> = {
|
|
|
160
167
|
* The relations, each read lazily so entities pointing at each other declare in either order, and the values
|
|
161
168
|
* read over the to-many ones, its base's too: `(author) => ({ postCount: () => author.posts.count() })`.
|
|
162
169
|
*/
|
|
163
|
-
relations<const R, const A extends
|
|
170
|
+
relations<const R, const A extends AggregateThunks = {}>(relations: (rel: RelationBuilder<Columns>) => R, aggregates?: (refs: RefMap<Columns> & AggregateRefs<R> & ToManyRefs<B, Exclude<ToManyRelationKey<B>, keyof R>>) => A): RelatedEntity<Row & RelationRow<R> & AggregateRow<A>, Key, Columns, Abstract>;
|
|
164
171
|
};
|
|
165
172
|
/** A defined entity's class once its relations are in: its filters may follow, and read through them. */
|
|
166
173
|
export type RelatedEntity<Row, Key, Columns, Abstract = false> = DefinedClass<Row, Key, Columns, Abstract> & {
|
|
@@ -178,13 +178,6 @@ export declare function parseSortByCount(val: unknown): unknown;
|
|
|
178
178
|
* entries consumable by any dialect. Grouped columns come first, then computed columns.
|
|
179
179
|
*/
|
|
180
180
|
export declare function parseGroupMap<E>(group?: QueryGroupMap<E>, select?: QueryAggMap<E>): ParsedGroupEntry<E>[];
|
|
181
|
-
/**
|
|
182
|
-
* Whether `value` is a map of comparison operators rather than a value to compare against. Only a
|
|
183
|
-
* plain object qualifies: `Date`, `QuerySql`, `Uint8Array` and arrays are all `typeof 'object'`, and
|
|
184
|
-
* reading their keys as operators drops the condition (a `Date` has none) or throws on an array's
|
|
185
|
-
* indices. Shared by the SQL and MongoDB builders, whose `$where` and `$having` all face this.
|
|
186
|
-
*/
|
|
187
|
-
export declare function isOperatorMap(value: unknown): value is Record<string, unknown>;
|
|
188
181
|
/** A JSON object, matched by what it holds: a plain object with no operator key. */
|
|
189
182
|
export declare function isJsonObject(value: unknown): value is Record<string, unknown>;
|
|
190
183
|
/**
|
|
@@ -4,7 +4,7 @@ import { QuerySql, resolveAggregateOp, } from '../type/index.js';
|
|
|
4
4
|
import { DEFAULT_VECTOR_DISTANCE, VECTOR_INDEX_TYPES } from '../type/vector.js';
|
|
5
5
|
import { defaultReadKeys, fieldKeys, isDatabaseWritten, jsonKey, jsonPathKeys } from './field.util.js';
|
|
6
6
|
import { withDeleted } from './filters.util.js';
|
|
7
|
-
import { entityName, getKeys, omitKeys, hasKeys, isJsonScalar, isOperatorObject, isRecord, someKey, } from './object.util.js';
|
|
7
|
+
import { entityName, getKeys, omitKeys, hasKeys, isJsonScalar, isOperatorObject, isPlainObject, isRecord, someKey, } from './object.util.js';
|
|
8
8
|
import { assertNoUndefined, whereAnd } from './query.util.js';
|
|
9
9
|
import { UqlSecurityError, UqlUsageError } from './uqlError.js';
|
|
10
10
|
/** The keys of `payload` a write persists as columns. */
|
|
@@ -571,23 +571,9 @@ function aggregateField(alias, arg) {
|
|
|
571
571
|
function namedKeys(map) {
|
|
572
572
|
return isRecord(map) ? getKeys(map).filter((key) => map[key]) : [];
|
|
573
573
|
}
|
|
574
|
-
/**
|
|
575
|
-
* Whether `value` is a map of comparison operators rather than a value to compare against. Only a
|
|
576
|
-
* plain object qualifies: `Date`, `QuerySql`, `Uint8Array` and arrays are all `typeof 'object'`, and
|
|
577
|
-
* reading their keys as operators drops the condition (a `Date` has none) or throws on an array's
|
|
578
|
-
* indices. Shared by the SQL and MongoDB builders, whose `$where` and `$having` all face this.
|
|
579
|
-
*/
|
|
580
|
-
export function isOperatorMap(value) {
|
|
581
|
-
return (typeof value === 'object' &&
|
|
582
|
-
value !== null &&
|
|
583
|
-
!Array.isArray(value) &&
|
|
584
|
-
!(value instanceof Date) &&
|
|
585
|
-
!(value instanceof Uint8Array) &&
|
|
586
|
-
!(value instanceof QuerySql));
|
|
587
|
-
}
|
|
588
574
|
/** A JSON object, matched by what it holds: a plain object with no operator key. */
|
|
589
575
|
export function isJsonObject(value) {
|
|
590
|
-
return
|
|
576
|
+
return isPlainObject(value) && !isOperatorObject(value);
|
|
591
577
|
}
|
|
592
578
|
/**
|
|
593
579
|
* A page operand, checked before it reaches an engine. These arrive from page arithmetic and from
|
|
@@ -23,6 +23,11 @@ export declare function isOperatorKey(key: string): boolean;
|
|
|
23
23
|
export declare function isOperatorObject(value: unknown): value is Record<string, unknown>;
|
|
24
24
|
/** Whether `value` is an object that is not an array, whose keys can be read. */
|
|
25
25
|
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
26
|
+
/**
|
|
27
|
+
* Whether `value` is an object literal, or one with no prototype, which a query-string parser hands back: what a
|
|
28
|
+
* query, a payload and JSON are built of. A `Date`, bytes, SQL and any class instance are objects too, but not maps.
|
|
29
|
+
*/
|
|
30
|
+
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
26
31
|
/** A shallow copy of `value` without `keys`. */
|
|
27
32
|
export declare function omitKeys<T extends object>(value: T, keys: Iterable<string>): T;
|
|
28
33
|
/** The value `cache` holds for `key`, computed and held the first time it is asked for. */
|
|
@@ -45,7 +50,5 @@ export declare function entityName<E>(meta: EntityMeta<E>): string;
|
|
|
45
50
|
* is what a `$where` map and a composite key's id object both are; an array is a list of either.
|
|
46
51
|
*/
|
|
47
52
|
export declare function isScalarId(value: unknown): boolean;
|
|
48
|
-
/** Whether `value` is a plain object naming columns, the one shape a `$where` takes. */
|
|
49
|
-
export declare function isWhereMap(value: unknown): value is Record<string, unknown>;
|
|
50
53
|
/** What `text` spells as JSON, or nothing where it is no JSON at all. */
|
|
51
54
|
export declare function jsonDocument(text: string): unknown;
|
package/dist/util/object.util.js
CHANGED
|
@@ -63,6 +63,17 @@ export function isOperatorObject(value) {
|
|
|
63
63
|
export function isRecord(value) {
|
|
64
64
|
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
65
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Whether `value` is an object literal, or one with no prototype, which a query-string parser hands back: what a
|
|
68
|
+
* query, a payload and JSON are built of. A `Date`, bytes, SQL and any class instance are objects too, but not maps.
|
|
69
|
+
*/
|
|
70
|
+
export function isPlainObject(value) {
|
|
71
|
+
if (typeof value !== 'object' || value === null) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
const prototype = Object.getPrototypeOf(value);
|
|
75
|
+
return prototype === Object.prototype || prototype === null;
|
|
76
|
+
}
|
|
66
77
|
/** A shallow copy of `value` without `keys`. */
|
|
67
78
|
export function omitKeys(value, keys) {
|
|
68
79
|
const copy = { ...value };
|
|
@@ -101,21 +112,7 @@ export function entityName(meta) {
|
|
|
101
112
|
* is what a `$where` map and a composite key's id object both are; an array is a list of either.
|
|
102
113
|
*/
|
|
103
114
|
export function isScalarId(value) {
|
|
104
|
-
|
|
105
|
-
return true;
|
|
106
|
-
}
|
|
107
|
-
if (Array.isArray(value)) {
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
// `null` as well as `Object.prototype`: an object with no prototype is what a query-string parser
|
|
111
|
-
// hands back (`qs`, express's `req.params`), and reading one as a bare id would name one column
|
|
112
|
-
// with a map of several.
|
|
113
|
-
const proto = Object.getPrototypeOf(value);
|
|
114
|
-
return proto !== Object.prototype && proto !== null;
|
|
115
|
-
}
|
|
116
|
-
/** Whether `value` is a plain object naming columns, the one shape a `$where` takes. */
|
|
117
|
-
export function isWhereMap(value) {
|
|
118
|
-
return !Array.isArray(value) && !isScalarId(value);
|
|
115
|
+
return !Array.isArray(value) && !isPlainObject(value);
|
|
119
116
|
}
|
|
120
117
|
/** What `text` spells as JSON, or nothing where it is no JSON at all. */
|
|
121
118
|
export function jsonDocument(text) {
|
package/dist/util/query.util.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { soleIdOf } from '../entity/metadata/definition.js';
|
|
2
|
-
import { entityName, getKeys,
|
|
2
|
+
import { entityName, getKeys, isPlainObject, isScalarId } from './object.util.js';
|
|
3
3
|
import { UqlUsageError } from './uqlError.js';
|
|
4
4
|
/**
|
|
5
5
|
* Maps each of `keys` to `true`, the shape of a `$select` or conflict keys. This and the builders below hold
|
|
@@ -61,7 +61,7 @@ export function whereIds(meta, ids) {
|
|
|
61
61
|
* and a scalar read as a map has no keys, so the statement would match every row.
|
|
62
62
|
*/
|
|
63
63
|
export function assertWhere(meta, where) {
|
|
64
|
-
if (!
|
|
64
|
+
if (!isPlainObject(where)) {
|
|
65
65
|
throw new UqlUsageError(`$where on '${entityName(meta)}' must be a map of conditions, such as { id: 1 }`);
|
|
66
66
|
}
|
|
67
67
|
}
|
|
@@ -97,14 +97,7 @@ function undefinedPath(value) {
|
|
|
97
97
|
}
|
|
98
98
|
/** An array or a plain object: what a filter nests, and not a `Date`, a `sql` or a driver's value. */
|
|
99
99
|
function isWalkable(value) {
|
|
100
|
-
|
|
101
|
-
return true;
|
|
102
|
-
}
|
|
103
|
-
if (!isRecord(value)) {
|
|
104
|
-
return false;
|
|
105
|
-
}
|
|
106
|
-
const proto = Object.getPrototypeOf(value);
|
|
107
|
-
return proto === Object.prototype || proto === null;
|
|
100
|
+
return Array.isArray(value) || isPlainObject(value);
|
|
108
101
|
}
|
|
109
102
|
const SORT_DIRECTIONS = new Map([
|
|
110
103
|
[1, { desc: false }],
|
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.112.1",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"engines": {
|
|
9
9
|
"node": ">=22.18"
|
|
@@ -26,11 +26,7 @@
|
|
|
26
26
|
"./http": "./dist/http/index.js",
|
|
27
27
|
"./express": "./dist/express/index.js",
|
|
28
28
|
"./nestjs": "./dist/nestjs/index.js",
|
|
29
|
-
"./browser":
|
|
30
|
-
"types": "./dist/browser/index.d.ts",
|
|
31
|
-
"import": "./dist/browser/index.js",
|
|
32
|
-
"default": "./dist/browser/uql-browser.min.js"
|
|
33
|
-
},
|
|
29
|
+
"./browser": "./dist/browser/index.js",
|
|
34
30
|
"./libsql": "./dist/libsql/index.js",
|
|
35
31
|
"./turso": "./dist/turso/index.js",
|
|
36
32
|
"./turso/local": "./dist/turso/local.js",
|
|
@@ -49,8 +45,7 @@
|
|
|
49
45
|
"scripts": {
|
|
50
46
|
"prepack": "bun run build && cp ../../README.md . && cp -R ../../skills .",
|
|
51
47
|
"postpack": "rm -r README.md skills && npm pkg delete gitHead",
|
|
52
|
-
"
|
|
53
|
-
"build": "bun run clean && tsc -b tsconfig.build.json && bun run compile.browser && bun run verify-dist.ts",
|
|
48
|
+
"build": "bun run clean && tsc -b tsconfig.build.json && bun run verify-dist.ts",
|
|
54
49
|
"start": "tsc --watch",
|
|
55
50
|
"clean": "rimraf dist .tsconfig.tsbuildinfo"
|
|
56
51
|
},
|
|
@@ -139,9 +134,9 @@
|
|
|
139
134
|
"@types/ws": "^8.18.2",
|
|
140
135
|
"better-sqlite3": "^13.0.3",
|
|
141
136
|
"express": "^5.3.0",
|
|
142
|
-
"mariadb": "^3.5.
|
|
137
|
+
"mariadb": "^3.5.5",
|
|
143
138
|
"mongodb": "^7.8.0",
|
|
144
|
-
"mssql": "^12.7.
|
|
139
|
+
"mssql": "^12.7.14",
|
|
145
140
|
"mysql2": "^3.24.6",
|
|
146
141
|
"pg": "^8.23.1",
|
|
147
142
|
"pg-query-stream": "^4.17.1",
|