@jskit-ai/resource-runtime 0.2.60 → 0.2.62
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,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- A `joinSource()`'s `query` is one derived table: it passes the columns it carries (the id, the stored columns a read answers, or those `columns` lists, and the ownership fields; never another hidden column or a blob) to json-rest-api's `scopedQuery({ columns })`, which 3.3.5 takes, where the join source wrapped scopedQuery()'s derived table of every column but the blobs in a second derived table that kept those columns. A search filter or report that reads a join source in a subquery (`whereExists`, `whereIn`, a join) leaves the database one derived table to plan for it instead of two: a row policy of 88 such subqueries, one statement of 177 SELECTs and 88 derived tables where it held 265 and 176, optimized in 13.5 ms instead of 22.2 ms on MariaDB 11.4.13 and planned in 11.7 ms instead of 15.0 ms on PostgreSQL 18.6 at 4 rows a table; at 10,000 rows a table MariaDB runs it in 785 ms instead of 8.4 s, and PostgreSQL in 288 ms against 255 ms (medians, the same rows each way). Rows, columns, `column(field)` and its refusals are unchanged, conditions a caller adds still only narrow the rows (scopedQuery()'s builder is read-only, as the wrapped one was), and `forceIndex` still hints the owner's table.
|
|
5
6
|
- `joinSource({ alias, filters, columns }, options)` takes `columns`, the visible fields a report reads: its derived table then lists those, the id and the ownership fields alone, where it listed every visible column, so a report reading two fields of a wide table no longer spells out the columns it does not read in its statement. A name that is no visible field (a hidden field other than the ownership fields, a blob, a virtual or unknown field) throws a TypeError when the source is built, before any statement, and `column(field)` throws for a field `columns` does not list. The rows stay the caller's scoped rows, `filters` still narrow by any search field, and without `columns` the statement is unchanged.
|
|
6
7
|
- Depends on json-rest-api 3.3.5 exactly (from 3.3.4), with json-rest-schema 1.0.21 in the lock. A read validates and parses only what it must (page parameters checked by one schema built once, temporal values by json-rest-schema's own checks, the response pass normalizing only values changed since the database pass), and json-rest-api's scopedQuery takes columns, the fields its derived table selects. A patch answering nothing or its id, and a delete, read the stored row only when something uses it (a hook, a precondition, a revision, a field setter, relationships, a cleared link or a full response); otherwise the UPDATE or DELETE carries the caller's scope in its WHERE, and on MySQL and MariaDB the read takes the id alone. patchMany's and deleteMany's visibility reads take only the ids unless a permission hook or a cleared link uses the rows. Errors are unchanged: a missing, hidden or another owner's row is json-rest-api's not-found error, a shared row outside a system context its refusal. A patch with onlyIf and changes but no lockVersion sends its UPDATE first and locks the row only when it changed nothing (#498).
|
|
7
8
|
- Depends on json-rest-api 3.3.4 exactly (from 3.3.3): a write finds the relationships that reference its resource, for the revision of a versioned resource it reaches, in an index each API builds once per published schema (compileResourceSchemas, an AnyAPI publish or a failed load drop it), where it walked every resource's relationships on each write. A write's statements are unchanged.
|
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.62",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24.0.0"
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
"./locales/en.json": "./locales/en.json"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@jskit-ai/resource-core": "0.1.
|
|
20
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
19
|
+
"@jskit-ai/resource-core": "0.1.229",
|
|
20
|
+
"@jskit-ai/resource-crud-core": "0.1.232",
|
|
21
21
|
"json-rest-api": "3.3.5"
|
|
22
22
|
},
|
|
23
23
|
"description": "The resource runtime: features own their tables through it and read and write them with scoped handles on json-rest-api.",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
}
|
|
70
70
|
},
|
|
71
71
|
"peerDependencies": {
|
|
72
|
-
"@jskit-ai/database-runtime": "0.1.
|
|
73
|
-
"@jskit-ai/kernel": "0.2.
|
|
72
|
+
"@jskit-ai/database-runtime": "0.1.295",
|
|
73
|
+
"@jskit-ai/kernel": "0.2.38"
|
|
74
74
|
}
|
|
75
75
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RestApiResourceError
|
|
1
|
+
import { RestApiResourceError } from "json-rest-api";
|
|
2
2
|
import { isRecord } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
3
|
import { isJobContext } from "./contexts.js";
|
|
4
4
|
import { namedFields } from "./handleRecords.js";
|
|
@@ -177,17 +177,18 @@ function isAsyncGenerator(value) {
|
|
|
177
177
|
* since a report joins on them and they name the caller's own scope; never another hidden column, and never a blob,
|
|
178
178
|
* so a report's `select("alias.*")` holds no unbounded bytes. `columns`, when given, lists the visible fields the report
|
|
179
179
|
* reads: the derived table then carries those, the id and the ownership fields alone, so a source of a wide table that
|
|
180
|
-
* a report reads two fields of names four columns in its statement, not every one.
|
|
181
|
-
*
|
|
182
|
-
*
|
|
180
|
+
* a report reads two fields of names four columns in its statement, not every one. The query is scopedQuery()'s one
|
|
181
|
+
* derived table, which selects those columns alone (json-rest-api's `columns`), so a report's subquery over it is one
|
|
182
|
+
* derived table the database plans, not two, read-only as scopedQuery()'s builder is. `column(field)` names each under
|
|
183
|
+
* that alias. A CRUD feature's repository answers it to any feature, where it refuses scopedQuery(). The builder comes
|
|
184
|
+
* wrapped, since a builder is thenable.
|
|
183
185
|
* @param {object} dependencies
|
|
184
|
-
* @param {{ knex: { instance: any } }} dependencies.api
|
|
185
186
|
* @param {{ name: string, schema: Record<string, any> }} dependencies.resource
|
|
186
187
|
* @param {() => { vars: { schemaInfo: { schemaStructure: Record<string, any>, ownershipFields?: string[] } } }} dependencies.jsonRest
|
|
187
|
-
* @param {(params: { alias: string, filters?: object }, options: object) => Promise<{ query: any }>} dependencies.scopedQuery
|
|
188
|
+
* @param {(params: { alias: string, columns: string[], filters?: object }, options: object) => Promise<{ query: any }>} dependencies.scopedQuery
|
|
188
189
|
* @param {(field: string, alias?: string) => string} dependencies.column
|
|
189
190
|
*/
|
|
190
|
-
function createJoinSource({
|
|
191
|
+
function createJoinSource({ resource, jsonRest, scopedQuery, column }) {
|
|
191
192
|
const { name } = resource;
|
|
192
193
|
/**
|
|
193
194
|
* The fields a join source's `columns` lists, each a visible field the source carries.
|
|
@@ -229,16 +230,12 @@ function createJoinSource({ api, resource, jsonRest, scopedQuery, column }) {
|
|
|
229
230
|
});
|
|
230
231
|
const listed = columns === undefined ? null : listedColumns(columns, visible);
|
|
231
232
|
const carried = ["id", ...(listed ? visible.filter((field) => joinKeys.has(field) || listed.has(field)) : visible)];
|
|
232
|
-
|
|
233
|
-
//
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
(options.trx || api.knex.instance).from(query.select(carried.map((field) => column(field, alias))).as(alias)),
|
|
237
|
-
{ resourceType: name }
|
|
238
|
-
);
|
|
239
|
-
JOIN_SOURCES.add(source);
|
|
233
|
+
// The scoped rows' visible columns (those `columns` lists, when given) and ownership fields, as scopedQuery()'s one
|
|
234
|
+
// derived table under the alias: nothing a caller adds reaches the others.
|
|
235
|
+
const { query } = await scopedQuery({ alias, columns: carried, ...(filters === undefined ? {} : { filters }) }, options);
|
|
236
|
+
JOIN_SOURCES.add(query);
|
|
240
237
|
return Object.freeze({
|
|
241
|
-
query
|
|
238
|
+
query,
|
|
242
239
|
column(field) {
|
|
243
240
|
if (resource.schema[field]?.type === "blob") {
|
|
244
241
|
throw new TypeError(`${name}.joinSource() carries no blob, so it has no column for "${field}": a report reads no blob, and get() reads one record's.`);
|
|
@@ -156,7 +156,7 @@ function createResourceHandle({ api, resource, hooks, settings, resourceNamed, h
|
|
|
156
156
|
// The hidden fields a plain read names, which json-rest-api leaves out (see handleHidden.js).
|
|
157
157
|
const hiddenFields = createHiddenFieldReads({ resource, scopedQuery, column });
|
|
158
158
|
// The table as a join source, with its visible columns only (see handleHidden.js).
|
|
159
|
-
const joinSource = createJoinSource({
|
|
159
|
+
const joinSource = createJoinSource({ resource, jsonRest, scopedQuery, column });
|
|
160
160
|
// lock, lockMany, lockWhere, lockPages and claim (see handleLocks.js).
|
|
161
161
|
const { lock, lockMany, lockWhere, lockPages, claim } = createLockReads({ api, name, resource, jsonRest, readFields, contextOf, hiddenFields, handleNamed, resourceNamed, settings });
|
|
162
162
|
|
|
@@ -138,6 +138,21 @@ async function projectsAndTasks(knex) {
|
|
|
138
138
|
|
|
139
139
|
const names = (rows) => rows.map(({ name }) => name);
|
|
140
140
|
const sorted = { sort: ["id"] };
|
|
141
|
+
// The derived tables a statement selects from, and a read's statements that read the projects.
|
|
142
|
+
const derivedTables = (sql) => sql.match(/\bfrom\s*\(\s*select\b/giu)?.length ?? 0;
|
|
143
|
+
const projectReads = (queries) => queries.filter((sql) => /\bprojects\b/u.test(sql));
|
|
144
|
+
// Search filters, each with the join sources its statement reads and the projects it answers in a request and in
|
|
145
|
+
// background work (projectsAndTasks).
|
|
146
|
+
const SEARCHES = Object.freeze([
|
|
147
|
+
{ filters: { taskStatus: "open" }, sources: 1, inRequest: ["Alpha"], inJob: ["Alpha", "Gamma"] },
|
|
148
|
+
{ filters: { taskStatus: "done" }, sources: 1, inRequest: ["Alpha", "Beta"], inJob: ["Alpha", "Beta"] },
|
|
149
|
+
{ filters: { idle: true }, sources: 1, inRequest: ["Gamma"], inJob: [] },
|
|
150
|
+
{ filters: { idle: false }, sources: 1, inRequest: ["Alpha", "Beta"], inJob: ["Alpha", "Beta", "Gamma"] },
|
|
151
|
+
{ filters: { named: "lph" }, sources: 2, inRequest: ["Alpha"], inJob: ["Alpha"] },
|
|
152
|
+
{ filters: { named: "done" }, sources: 2, inRequest: ["Alpha", "Beta"], inJob: ["Alpha", "Beta"] },
|
|
153
|
+
{ filters: { named: "elt" }, sources: 2, inRequest: [], inJob: [] },
|
|
154
|
+
{ filters: { taskStatus: "open", idle: false }, sources: 2, inRequest: ["Alpha"], inJob: ["Alpha", "Gamma"] }
|
|
155
|
+
]);
|
|
141
156
|
|
|
142
157
|
/**
|
|
143
158
|
* @param {(t: import("node:test").TestContext, suffix: string) => Promise<import("knex").Knex>} connect
|
|
@@ -200,6 +215,75 @@ function filterJoinSourcesCases(connect) {
|
|
|
200
215
|
}
|
|
201
216
|
}],
|
|
202
217
|
|
|
218
|
+
["a join source is one derived table, the owner's scoped rows with the columns it carries, and a search filter's statement holds one for each join source it reads", async (t) => {
|
|
219
|
+
const knex = await connect(t, "da_fjs_shape");
|
|
220
|
+
const { projects, tasks, job } = await projectsAndTasks(knex);
|
|
221
|
+
const request = { context: WORKSPACE_A };
|
|
222
|
+
// Where it wrapped the scoped rows' every column in a second derived table that kept the columns it carries.
|
|
223
|
+
for (const options of [request, job]) {
|
|
224
|
+
for (const filters of [undefined, { id: ["1", "4", "5"] }]) {
|
|
225
|
+
const source = await tasks.joinSource({ alias: "k", ...(filters === undefined ? {} : { filters }) }, options);
|
|
226
|
+
const sql = source.query.clone().select(source.column("id")).toSQL().sql;
|
|
227
|
+
assert.equal(derivedTables(sql), 1, sql);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
for (const options of [request, job]) {
|
|
231
|
+
const plain = await countQueries(knex, () => projects.queryAll(sorted, options));
|
|
232
|
+
const baseline = derivedTables(projectReads(plain.queries)[0]);
|
|
233
|
+
for (const { filters, sources } of SEARCHES) {
|
|
234
|
+
const { queries } = await countQueries(knex, () => projects.queryAll({ ...sorted, filters }, options));
|
|
235
|
+
const reads = projectReads(queries);
|
|
236
|
+
assert.equal(reads.length, 1, JSON.stringify(filters));
|
|
237
|
+
assert.equal(derivedTables(reads[0]), baseline + sources, `${JSON.stringify(filters)}: ${reads[0]}`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}],
|
|
241
|
+
|
|
242
|
+
["a join source carries the owner's scoped rows and their values, and every search filter answers the same rows, in a request and in background work", async (t) => {
|
|
243
|
+
const knex = await connect(t, "da_fjs_rows");
|
|
244
|
+
const { projects, tasks, job } = await projectsAndTasks(knex);
|
|
245
|
+
const request = { context: WORKSPACE_A };
|
|
246
|
+
const carried = ["id", "workspaceId", "projectId", "status", "confidential"];
|
|
247
|
+
const read = (builder, column) => builder.select(Object.fromEntries(carried.map((field) => [field, column(field)]))).orderBy(column("id"));
|
|
248
|
+
for (const [options, visible] of [[request, ["1", "2", "3"]], [job, ["1", "2", "3", "4"]]]) {
|
|
249
|
+
for (const filters of [undefined, { id: ["1", "4", "5"] }]) {
|
|
250
|
+
const params = { alias: "k", ...(filters === undefined ? {} : { filters }) };
|
|
251
|
+
const source = await tasks.joinSource(params, options);
|
|
252
|
+
const rows = await read(source.query, source.column);
|
|
253
|
+
const { query: scoped } = await tasks.scopedQuery(params, options);
|
|
254
|
+
assert.deepEqual(rows, await read(scoped, (field) => tasks.column(field, "k")));
|
|
255
|
+
assert.deepEqual(rows.map(({ id }) => String(id)), filters ? visible.filter((id) => ["1", "4"].includes(id)) : visible);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
for (const { filters, inRequest: inRequestNames, inJob } of SEARCHES) {
|
|
259
|
+
for (const [options, expected] of [[request, inRequestNames], [job, inJob]]) {
|
|
260
|
+
assert.deepEqual(names(await projects.queryAll({ ...sorted, filters }, options)), expected, JSON.stringify(filters));
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}],
|
|
264
|
+
|
|
265
|
+
["nothing a caller adds to a join source widens it, and it carries no hidden column but the ownership fields", async (t) => {
|
|
266
|
+
const knex = await connect(t, "da_fjs_narrow");
|
|
267
|
+
const { tasks, job } = await projectsAndTasks(knex);
|
|
268
|
+
// An orWhere on another workspace, on a row the policy hides in a request, or on anything at all reaches no row the
|
|
269
|
+
// join source does not carry.
|
|
270
|
+
const source = await tasks.joinSource({ alias: "k" }, { context: WORKSPACE_A });
|
|
271
|
+
const widened = source.query.select({ id: source.column("id") })
|
|
272
|
+
.where(source.column("status"), "none")
|
|
273
|
+
.orWhere(source.column("workspaceId"), 2)
|
|
274
|
+
.orWhere(source.column("confidential"), true)
|
|
275
|
+
.orWhereRaw("1 = 1")
|
|
276
|
+
.orderBy(source.column("id"));
|
|
277
|
+
assert.deepEqual((await widened).map(({ id }) => String(id)), ["1", "2", "3"]);
|
|
278
|
+
|
|
279
|
+
assert.throws(() => source.column("reviewNote"), /tasks\.joinSource\(\) carries no hidden field but its ownership fields, so it has no column for "reviewNote"/u);
|
|
280
|
+
// Background work, which reads the hidden field, gets a join source without it all the same.
|
|
281
|
+
const all = await tasks.joinSource({ alias: "k" }, job);
|
|
282
|
+
const [first] = await all.query.clone().select("k.*").orderBy(all.column("id")).limit(1);
|
|
283
|
+
assert.deepEqual(Object.keys(first).sort(), ["confidential", "id", "project_id", "status", "workspace_id"]);
|
|
284
|
+
await assert.rejects(async () => await all.query.clone().select("k.review_note"), /review_note/u);
|
|
285
|
+
}],
|
|
286
|
+
|
|
203
287
|
["own() refuses, at startup, join sources it cannot use: no alias map, a name that is no alias, an owner that is no function, a filter with no applyFilter", async (t) => {
|
|
204
288
|
const knex = await connect(t, "da_fjs_startup");
|
|
205
289
|
await knex.schema.createTable("things", (table) => {
|