@jskit-ai/resource-runtime 0.2.19 → 0.2.21
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/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- A paged `aggregate()` (`page: { size, number }`) answers `{ rows, page: { size, number, hasMore, next } }`, as `query()` answers a page, instead of a bare array a caller could take for every group; read the next page while `page.hasMore`. An unpaged grouped aggregate still answers every group or throws `GroupLimitError`, whose message now says to read pages. Upgrading: a caller passing `page` reads the groups from `.rows`.
|
|
6
|
+
- Depends on json-rest-api 3.1.3 exactly (from 3.1.2), from the registry with its published integrity: a write checks each linked target type with one read of the targets' ids (their minimal records only for a target resource with a `checkPermissions` hook), which also takes the shared lock on the belongs-to and polymorphic targets it stores, before `beforeSchemaValidate` rather than after the `beforeDataCall` hooks; in a system context a belongs-to its declared foreign key protects is left to that key, so a missing target there fails with the database's foreign key error instead of `not_found`. json-rest-api now asks for json-rest-schema ^1.0.20.
|
|
5
7
|
- Depends on json-rest-api 3.1.2 exactly (from 3.1.1), from the registry with its published integrity: json-rest-api bounds a blob field itself too (`maxBytes` required, a blob answered only where `fields` names it, at most 16 MiB of named blobs per read, none in an include, `queryAll` or HTTP response, many-row write statements chunked by blob bytes, and the MySQL/MariaDB column type chosen by `maxBytes`), its `contains`, `startsWith` and `endsWith` filters match `%`, `_` and `\` literally on every database, a relationship write checks and locks each related type's targets in one query, and an AnyAPI many-to-many read costs its own link rows instead of growing with the square of them.
|
|
6
8
|
- A search filter of an `owns` entry may read other owners' tables through their join sources: `joinSources: { alias: ({ requires, owned }) => owner }` names, by alias, the owner answering each (an owner API the feature requires, or one of its own handles), and its `applyFilter(query, value, { column, joinSource })` gets `joinSource(alias)`, that owner's `{ query, column }` for the caller's scope (its ownership, its row policy, never a hidden column), for a `whereExists` or `whereNotExists` tied to the row with `whereColumn`. A handle builds the join sources a read's filters use before the read, in the call's own context and transaction, so a handle's `query`, `queryAll`, `queryPages`, `aggregate` and `joinSource` take such a filter in one statement, on SQLite, PostgreSQL and MariaDB. A search for the rows that have, or lack, a related owner's rows no longer resolves that owner's ids first or keeps a raw-SQL exception. `own()` refuses at startup a `joinSources` that is not a record of functions under valid aliases, and such a filter with `oneOf` or without `applyFilter`; a read throws naming the filter when its picker answers no owner with `joinSource`. On MySQL and MariaDB `patchWhere`, `deleteWhere`, `lockWhere` and `claim` refuse such a filter as json-rest-api refuses a filter on another table's field (`RestApiValidationError`, rule `selection_cross_table`, the cause of a write's `RestApiWriteError`), since those databases read another table from the transaction's snapshot without locking it, as json-rest-api refuses a filter on another table's field; PostgreSQL and SQLite take it. The data-access guide's Layer 5 shows the filter and how an app switches a report or a pre-resolved id list to it.
|
|
7
9
|
- `lock`, `lockMany`, `lockWhere` and `claim` take `include` again, for belongs-to relationships: after the lock's own rows, the records they name are locked through each related table's own handle (its scope, its row policy, its hidden fields), in id order, with the call's lock mode, each included table once however many rows name it, and nested as a read's include nests them, `include: "batch.yard"` locking the yards after the batches. On MySQL and MariaDB the links are read with a locking read of the rows the lock holds, so a record committed after the transaction's first plain read is found and locked, which a plain read after the lock misses (`lockedReads.mysql.test.js`). A link to a record the caller may not see reads as in a read: its belongs-to field null, the relationship undefined. A collection, or a name that is no belongs-to relationship, is still a TypeError before any statement, now saying to lock a collection's rows with that table's own `lockWhere()`; `claim` refuses `include` named both in its params and its options. Upgrading: none; a read after the lock that only fetched the locked rows' parents may become the lock's `include`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/resource-runtime",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.21",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24.0.0"
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
"./locales/en.json": "./locales/en.json"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@jskit-ai/resource-core": "0.1.
|
|
19
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
20
|
-
"json-rest-api": "3.1.
|
|
18
|
+
"@jskit-ai/resource-core": "0.1.209",
|
|
19
|
+
"@jskit-ai/resource-crud-core": "0.1.210",
|
|
20
|
+
"json-rest-api": "3.1.3"
|
|
21
21
|
},
|
|
22
22
|
"description": "The resource runtime: features own their tables through it and read and write them with scoped handles on json-rest-api.",
|
|
23
23
|
"jskit": {
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"@jskit-ai/database-runtime": "0.1.
|
|
68
|
-
"@jskit-ai/kernel": "0.2.
|
|
67
|
+
"@jskit-ai/database-runtime": "0.1.267",
|
|
68
|
+
"@jskit-ai/kernel": "0.2.18"
|
|
69
69
|
}
|
|
70
70
|
}
|
package/src/server/errors.js
CHANGED
|
@@ -120,8 +120,8 @@ class GroupLimitError extends AppError {
|
|
|
120
120
|
super(
|
|
121
121
|
400,
|
|
122
122
|
`${resourceName}.aggregate() has more than ${groupLimit} groups, the most one statement answers. Read them in pages, ` +
|
|
123
|
-
`page: { size, number } with size up to ${groupLimit} and number from 1,
|
|
124
|
-
"
|
|
123
|
+
`page: { size, number } with size up to ${groupLimit} and number from 1, each answering { rows, page }, while ` +
|
|
124
|
+
"page.hasMore, or narrow the filters.",
|
|
125
125
|
{ code: "RESOURCE_GROUP_LIMIT", messageCode: "resource.groupLimit", messageParams: { groupLimit } }
|
|
126
126
|
);
|
|
127
127
|
this.name = "GroupLimitError";
|
|
@@ -122,6 +122,24 @@ function noRowsAggregate(measures, groupBy) {
|
|
|
122
122
|
return Object.freeze([Object.freeze(Object.fromEntries(totals))]);
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
+
/**
|
|
126
|
+
* What aggregate() answers: every group's rows without `page`, and with it the page's groups and the page, as query()
|
|
127
|
+
* answers a page, so a caller never takes one page of groups for every group.
|
|
128
|
+
* @param {readonly Record<string, unknown>[]} rows
|
|
129
|
+
* @param {{ size: number, number?: number } | undefined} page
|
|
130
|
+
* @param {boolean} hasMore
|
|
131
|
+
*/
|
|
132
|
+
function aggregateAnswer(rows, page, hasMore) {
|
|
133
|
+
if (page === undefined) {
|
|
134
|
+
return rows;
|
|
135
|
+
}
|
|
136
|
+
const number = page.number ?? 1;
|
|
137
|
+
return Object.freeze({
|
|
138
|
+
rows,
|
|
139
|
+
page: Object.freeze({ size: Number(page.size), number, hasMore, next: hasMore ? number + 1 : null })
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
125
143
|
/**
|
|
126
144
|
* The read parameters of a handle call as json-rest-api's queryParams.
|
|
127
145
|
* @param {{ name: string, schema: Record<string, any> }} resource
|
|
@@ -476,6 +494,7 @@ export {
|
|
|
476
494
|
manyResult,
|
|
477
495
|
namedFields,
|
|
478
496
|
noRowsAggregate,
|
|
497
|
+
aggregateAnswer,
|
|
479
498
|
patchAnswer,
|
|
480
499
|
upsertConflict,
|
|
481
500
|
pickReturning,
|
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
inputData,
|
|
27
27
|
manyResult,
|
|
28
28
|
noRowsAggregate,
|
|
29
|
+
aggregateAnswer,
|
|
29
30
|
patchAnswer,
|
|
30
31
|
pickReturning,
|
|
31
32
|
quietCollections,
|
|
@@ -315,10 +316,10 @@ function createResourceHandle({ api, resource, hooks, settings, resourceNamed, h
|
|
|
315
316
|
/**
|
|
316
317
|
* Totals: `measures` ({ name: { count | countDistinct | sum | avg | min | max: field, where? } })
|
|
317
318
|
* over the filtered rows, grouped by `groupBy` fields or date buckets. Answers the rows.
|
|
318
|
-
* One statement answers at most pageLimits().request groups: a grouped
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
319
|
+
* One statement answers at most pageLimits().request groups: a grouped aggregate without `page` answers every group
|
|
320
|
+
* or, when there are more, throws GroupLimitError rather than answer part of them. `page: { size, number }` (number
|
|
321
|
+
* from 1) answers one page of groups as query() does, `{ rows, page: { size, number, hasMore, next } }`: read the
|
|
322
|
+
* next while `page.hasMore`.
|
|
322
323
|
* @param {{ measures: object, groupBy?: Array<string | object>, filters?: object, sort?: string[], page?: { size: number, number?: number } }} spec
|
|
323
324
|
* @param {{ context?: object | null, trx?: object | null }} [options]
|
|
324
325
|
*/
|
|
@@ -329,7 +330,7 @@ function createResourceHandle({ api, resource, hooks, settings, resourceNamed, h
|
|
|
329
330
|
}
|
|
330
331
|
const context = contextOf(options);
|
|
331
332
|
if (matchesNoRow(filters, jsonRest().vars.schemaInfo)) {
|
|
332
|
-
return noRowsAggregate(measures, groupBy);
|
|
333
|
+
return aggregateAnswer(noRowsAggregate(measures, groupBy), page, false);
|
|
333
334
|
}
|
|
334
335
|
// json-rest-api pages grouped rows at its default page size when none is named, and says so only in meta.
|
|
335
336
|
const groupLimit = page === undefined && Array.isArray(groupBy) && groupBy.length > 0 ? pageLimits().request : undefined;
|
|
@@ -350,7 +351,7 @@ function createResourceHandle({ api, resource, hooks, settings, resourceNamed, h
|
|
|
350
351
|
if (groupLimit !== undefined && result.meta?.page?.hasMore === true) {
|
|
351
352
|
throw new GroupLimitError(name, groupLimit);
|
|
352
353
|
}
|
|
353
|
-
return result.data;
|
|
354
|
+
return aggregateAnswer(result.data, page, result.meta?.page?.hasMore === true);
|
|
354
355
|
}
|
|
355
356
|
|
|
356
357
|
/**
|
|
@@ -138,7 +138,7 @@ function pagedReadsCases(connect) {
|
|
|
138
138
|
await assert.rejects(pagesOf(notes.queryPages({ take: 2 }, request)), /notes\.queryPages\(\) does not take take/u);
|
|
139
139
|
}],
|
|
140
140
|
|
|
141
|
-
["a grouped aggregate without a page answers every group one statement holds and throws past them, never part; a page answers its groups", async (t) => {
|
|
141
|
+
["a grouped aggregate without a page answers every group one statement holds and throws past them, never part; a page answers its groups and whether more remain", async (t) => {
|
|
142
142
|
const knex = await connect(t, "da_pr_groups");
|
|
143
143
|
const { notes } = await createNotes(knex);
|
|
144
144
|
const options = { context: WORKSPACE_A };
|
|
@@ -155,9 +155,20 @@ function pagedReadsCases(connect) {
|
|
|
155
155
|
error.code === "RESOURCE_GROUP_LIMIT" && error.messageCode === "resource.groupLimit" && error.messageParams.groupLimit === 3 &&
|
|
156
156
|
/notes\.aggregate\(\) has more than 3 groups, the most one statement answers\. Read them in pages/u.test(error.message));
|
|
157
157
|
const page = (number) => notes.aggregate({ ...byStatus, page: { size: 2, number } }, options);
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
assert.
|
|
158
|
+
// A page says whether groups remain, as query() does, and is no array a caller could take for every group.
|
|
159
|
+
const first = await page(1);
|
|
160
|
+
assert.equal(Array.isArray(first), false);
|
|
161
|
+
assert.deepEqual(first.rows.map(({ status }) => status), ["a", "b"]);
|
|
162
|
+
assert.deepEqual(first.page, { size: 2, number: 1, hasMore: true, next: 2 });
|
|
163
|
+
const second = await page(2);
|
|
164
|
+
assert.deepEqual(second.rows.map(({ status }) => status), ["c", "d"]);
|
|
165
|
+
assert.deepEqual(second.page, { size: 2, number: 2, hasMore: false, next: null });
|
|
166
|
+
assert.deepEqual(await page(3), { rows: [], page: { size: 2, number: 3, hasMore: false, next: null } });
|
|
167
|
+
// A filter that matches no row answers an empty page without a statement.
|
|
168
|
+
assert.deepEqual(
|
|
169
|
+
await notes.aggregate({ ...byStatus, filters: { id: [] }, page: { size: 2 } }, options),
|
|
170
|
+
{ rows: [], page: { size: 2, number: 1, hasMore: false, next: null } }
|
|
171
|
+
);
|
|
161
172
|
await assert.rejects(notes.aggregate({ ...byStatus, page: { number: 2 } }, options), /notes\.aggregate\(\) page needs size/u);
|
|
162
173
|
// Without groupBy an aggregate is one row, whatever the number of rows.
|
|
163
174
|
assert.deepEqual(await notes.aggregate({ measures: { notes: { count: "*" } } }, options), [{ notes: 5 }]);
|