uql-orm 0.102.1 → 0.102.2
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/uql-browser.min.js +2 -2
- package/dist/browser/uql-browser.min.js.map +2 -2
- package/dist/dialect/abstractSqlDialect.d.ts +14 -13
- package/dist/dialect/abstractSqlDialect.js +33 -32
- package/dist/dialect/aliases.d.ts +3 -1
- package/dist/dialect/aliases.js +3 -1
- package/dist/dialect/jsonSql.d.ts +4 -0
- package/dist/dialect/jsonSql.js +9 -0
- package/dist/dialect/mysqlLikeSqlDialect.d.ts +2 -1
- package/dist/dialect/mysqlLikeSqlDialect.js +8 -3
- package/dist/dialect/pgLikeSqlDialect.d.ts +4 -3
- package/dist/dialect/pgLikeSqlDialect.js +10 -7
- package/dist/dialect/queryJoins.d.ts +1 -1
- package/dist/mariadb/mariaDialect.d.ts +2 -2
- package/dist/mariadb/mariaDialect.js +2 -2
- package/dist/mongodb/mongoDialect.d.ts +6 -1
- package/dist/mongodb/mongoDialect.js +77 -33
- package/dist/mssql/mssqlDialect.d.ts +8 -6
- package/dist/mssql/mssqlDialect.js +32 -20
- package/dist/sqlite/sqliteDialect.d.ts +1 -1
- package/dist/sqlite/sqliteDialect.js +3 -3
- package/dist/turso/tursoLocalDialect.d.ts +2 -2
- package/dist/turso/tursoLocalDialect.js +7 -5
- package/dist/type/entity.d.ts +25 -11
- package/dist/type/utility.d.ts +2 -0
- package/dist/util/dialect.util.d.ts +13 -13
- package/dist/util/dialect.util.js +47 -13
- package/dist/util/object.util.d.ts +3 -3
- package/dist/util/object.util.js +1 -1
- package/package.json +1 -1
- package/skills/uql-orm/SKILL.md +1 -1
|
@@ -222,17 +222,43 @@ export function isJsonUpdateOp(value) {
|
|
|
222
222
|
export function writesKeys({ set, arrays }) {
|
|
223
223
|
return hasKeys(set) || Object.values(arrays).some((write) => write.appends.length > 0);
|
|
224
224
|
}
|
|
225
|
+
/** A JSON update folded as an array column's, where its operands are lists of elements, else as an object's. */
|
|
226
|
+
export function foldJson(value) {
|
|
227
|
+
return isJsonArrayUpdate(value) ? { array: foldJsonArrayUpdate(value) } : { object: foldJsonUpdate(value) };
|
|
228
|
+
}
|
|
229
|
+
/** Whether a JSON update writes the column as an array, its operands lists of elements rather than maps of keys. */
|
|
230
|
+
function isJsonArrayUpdate(value) {
|
|
231
|
+
return [value.$pull, value.$push, value.$addToSet].some(Array.isArray);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* A JSON array column's update as its writes: each `$pull`, then each `$push`, then each `$addToSet` once. Its
|
|
235
|
+
* operands are lists, so one that is a map of keys means the update was written for an object.
|
|
236
|
+
*/
|
|
237
|
+
function foldJsonArrayUpdate(value) {
|
|
238
|
+
const { $pull = [], $push = [], $addToSet = [], ...others } = value;
|
|
239
|
+
if (hasKeys(others) || ![$pull, $push, $addToSet].every(Array.isArray)) {
|
|
240
|
+
throw new UqlUsageError('a JSON array takes $pull, $push and $addToSet, each a list of elements');
|
|
241
|
+
}
|
|
242
|
+
const write = { pulls: [...$pull], appends: [] };
|
|
243
|
+
for (const item of $push) {
|
|
244
|
+
append(write, item, false);
|
|
245
|
+
}
|
|
246
|
+
for (const item of $addToSet) {
|
|
247
|
+
append(write, assertSetElement('$addToSet', item), true);
|
|
248
|
+
}
|
|
249
|
+
return write;
|
|
250
|
+
}
|
|
225
251
|
/**
|
|
226
252
|
* A JSON update as one write per path, its operators applied in order: `$pull`, `$set` in key order, `$push`,
|
|
227
253
|
* `$addToSet`, `$unset`. Where one reaches a value another wrote, the result is worked out here, so every engine
|
|
228
254
|
* writes each path once and reads only what is stored. A `$set` of `undefined` writes nothing.
|
|
229
255
|
*/
|
|
230
|
-
|
|
256
|
+
function foldJsonUpdate(value) {
|
|
231
257
|
const set = new Map();
|
|
232
258
|
const arrays = new Map();
|
|
233
259
|
const unset = new Set();
|
|
234
260
|
for (const [key, item] of Object.entries(value.$pull ?? {})) {
|
|
235
|
-
arrays.set(jsonKey(key), {
|
|
261
|
+
arrays.set(jsonKey(key), { pulls: [item], appends: [] });
|
|
236
262
|
}
|
|
237
263
|
for (const [path, item] of Object.entries(value.$set ?? {})) {
|
|
238
264
|
if (item !== undefined) {
|
|
@@ -248,14 +274,11 @@ export function foldJsonUpdate(value) {
|
|
|
248
274
|
}
|
|
249
275
|
}
|
|
250
276
|
}
|
|
251
|
-
for (const
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
appendTo(set, arrays, jsonKey(key), item, unique);
|
|
258
|
-
}
|
|
277
|
+
for (const [key, item] of Object.entries(value.$push ?? {})) {
|
|
278
|
+
appendTo(set, arrays, jsonKey(key), item, false);
|
|
279
|
+
}
|
|
280
|
+
for (const [key, item] of Object.entries(value.$addToSet ?? {})) {
|
|
281
|
+
appendTo(set, arrays, jsonKey(key), assertSetElement(`$addToSet on '${key}'`, item), true);
|
|
259
282
|
}
|
|
260
283
|
for (const path of value.$unset ?? []) {
|
|
261
284
|
const keys = jsonPathKeys(path);
|
|
@@ -287,11 +310,22 @@ function appendTo(set, arrays, key, item, unique) {
|
|
|
287
310
|
if ([...set.keys()].some((path) => path.startsWith(`${key}.`))) {
|
|
288
311
|
return;
|
|
289
312
|
}
|
|
290
|
-
const write = arrays.get(key) ?? { appends: [] };
|
|
313
|
+
const write = arrays.get(key) ?? { pulls: [], appends: [] };
|
|
291
314
|
arrays.set(key, write);
|
|
292
|
-
|
|
293
|
-
|
|
315
|
+
append(write, item, unique);
|
|
316
|
+
}
|
|
317
|
+
/** `item` appended to `write`: a `unique` one only once, and unguarded where a pull removed it. */
|
|
318
|
+
function append(write, item, unique) {
|
|
319
|
+
if (!(unique && write.appends.some((appended) => appended.item === item))) {
|
|
320
|
+
write.appends.push({ item, unique: unique && !write.pulls.includes(item) });
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
/** `item`, refused unless a scalar: the one element every engine compares alike, which `$addToSet` needs. */
|
|
324
|
+
function assertSetElement(operator, item) {
|
|
325
|
+
if (!isJsonScalar(item)) {
|
|
326
|
+
throw new UqlUsageError(`${operator} takes a string, a finite number or a boolean`);
|
|
294
327
|
}
|
|
328
|
+
return item;
|
|
295
329
|
}
|
|
296
330
|
/** Removes `path` and every path inside it from `set`. */
|
|
297
331
|
function dropWithin(set, path) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { EntityMeta } from '../type/index.js';
|
|
2
|
-
/**
|
|
3
|
-
export declare function isJsonScalar(value: unknown): value is
|
|
1
|
+
import type { EntityMeta, JsonScalar } from '../type/index.js';
|
|
2
|
+
/** Whether `value` is a {@link JsonScalar}, which `$addToSet` and an `=` on a JSON path compare by equality. */
|
|
3
|
+
export declare function isJsonScalar(value: unknown): value is JsonScalar;
|
|
4
4
|
export declare function clone<T>(value: T): T;
|
|
5
5
|
/** `items` in consecutive lists of at most `size`, in order: one list where `size` is unbounded. */
|
|
6
6
|
export declare function chunk<T>(items: readonly T[], size: number): T[][];
|
package/dist/util/object.util.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** Whether `value` is a {@link JsonScalar}, which `$addToSet` and an `=` on a JSON path compare by equality. */
|
|
2
2
|
export function isJsonScalar(value) {
|
|
3
3
|
return typeof value === 'string' || typeof value === 'boolean' || Number.isFinite(value);
|
|
4
4
|
}
|
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.102.
|
|
6
|
+
"version": "0.102.2",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"engines": {
|
|
9
9
|
"node": ">=22.18"
|
package/skills/uql-orm/SKILL.md
CHANGED
|
@@ -118,7 +118,7 @@ const users = await pool.findMany(User, {
|
|
|
118
118
|
- `upsertOne(Entity, { email: true }, row, update?)`: a conflicting row takes `update`, an update's payload with its operators (`{ uses: { $inc: 1 } }`), instead of `row`; a new one inserts `row`. An empty `{}` leaves a conflicting row as it is: insert if absent. Cascaded relations write on either branch, a found row's replaced, as `updateMany` does. It resolves to the row's id.
|
|
119
119
|
- Every write takes `{ returning: { id: true, createdAt: true } }` as its last argument and then resolves to those rows (an update or delete by id: the row or `undefined`), read back in the same transaction; without it, inserts, saves and upserts resolve to ids, updates and deletes to a count. An update or delete writes exactly the rows it reports: on Postgres, CockroachDB and the SQLite family a plain one is a single statement whose `RETURNING` reports them; elsewhere, or paged, through a relation, versioned, or with hooks or cascades on a delete, it reads them first in a transaction that holds them, so a racing writer waits for the commit (SQLite and MongoDB fail it with a retryable error instead).
|
|
120
120
|
- `updateMany` and `deleteMany` naming no rows - no `$where` holding a value (an empty group holds none), no `$limit` - throw; `{ unfiltered: true }` means the whole table.
|
|
121
|
-
- An update takes `{ stock: { $inc: -1 } }` to add, or `$mul` to multiply, in the statement, a NULL counting as 0, so a guard in `$where` (`stock: { $gte: 1 }`) makes a decrement race-safe. On SQL the step may be a ref or `sql` of the field's type (`{ total: { $inc: newRow.amount } }` in a trigger). JSON fields take `$set` and `$unset`, of keys or dotted paths (`settings: { $set: { 'billing.plan': 'pro' } }`, creating the parents), `$push`, `$pull`, and `$addToSet` (a scalar the array lacks), applied in that order on one value: `$pull`, `$set`, `$push`, `$addToSet`, `$unset`.
|
|
121
|
+
- An update takes `{ stock: { $inc: -1 } }` to add, or `$mul` to multiply, in the statement, a NULL counting as 0, so a guard in `$where` (`stock: { $gte: 1 }`) makes a decrement race-safe. On SQL the step may be a ref or `sql` of the field's type (`{ total: { $inc: newRow.amount } }` in a trigger). JSON fields take `$set` and `$unset`, of keys or dotted paths (`settings: { $set: { 'billing.plan': 'pro' } }`, creating the parents), `$push`, `$pull`, and `$addToSet` (a scalar the array lacks), applied in that order on one value: `$pull`, `$set`, `$push`, `$addToSet`, `$unset`. A JSON array column (`tags?: Json<string[]>`) takes `$pull`, `$push` and `$addToSet` with lists of elements: `{ tags: { $pull: ['old'], $addToSet: ['new'] } }`.
|
|
122
122
|
- `$lock: true` locks the rows a read returns (`{ $wait: 'skip' | 'nowait' }` says what to do about a row someone else holds) and needs an open transaction; SQLite, libSQL, Turso, D1 and MongoDB have no row lock and refuse it.
|
|
123
123
|
- `queryErrorKind(err)` names any failure the same on every engine - `uniqueViolation`, `foreignKeyViolation`, `notNullViolation`, `checkViolation`, `optimisticLock`, `retryable`, `usage`, `security` - so catch by kind rather than by a driver's code or an `instanceof`. Every error UQL raises itself is a `UqlError`.
|
|
124
124
|
- ``pool.all`SELECT ... WHERE id = ${id}` `` runs a `SELECT` and ``pool.run`...` `` a write, one statement per call: both are tags, each interpolated value bound: a scalar, `null` or a list of them, never `undefined` or a plain object (bind `JSON.stringify(doc)`). `sql` builds a fragment to embed anywhere a value or field goes, or to compose a statement, and `sql.join(fragments, ' AND ')` joins them. `sql.text(sql)` runs trusted SQL held in a string, binding nothing: never build it from user input. Migrations write ``querier.run /*sql*/ `CREATE TABLE ...` ``, the comment being what es6-string-html highlights as SQL. `sql` is a tag, so ``sql(`...`)`` does not compile. `raw` is its deprecated former name. A field read off `refs(Entity)` carries its type: on its own as a value it fits only a field of that type.
|