@jskit-ai/resource-runtime 0.2.58 → 0.2.60
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 +1 -0
- package/package.json +5 -5
- package/src/server/handleHidden.js +40 -8
- package/test/joinSourceColumns.test.js +121 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- `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.
|
|
5
6
|
- 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).
|
|
6
7
|
- 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.
|
|
7
8
|
- `@jskit-ai/resource-runtime/server/anyapi` exports json-rest-api's AnyAPI storage, for tables an application defines while it runs, which no table resource describes: `AnyapiRegistry`, `JsonRestApi`, `RestApiAnyapiKnexPlugin`, `RestApiPlugin`, `RowPolicyPlugin` and `ensureAnyApiSchema`, the very objects of the json-rest-api copy the runtime's handles run on. json-rest-api knows the transactions `api.transaction` opened by identity, and AnyAPI its registry with `instanceof`, so an AnyAPI write given a handle's `txOptions.trx` joins that transaction only on this copy; an application imports these from here and declares no json-rest-api of its own, and `npm run jskit:update` moves its one copy with the catalog.
|
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.60",
|
|
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.228",
|
|
20
|
+
"@jskit-ai/resource-crud-core": "0.1.230",
|
|
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.294",
|
|
73
|
+
"@jskit-ai/kernel": "0.2.37"
|
|
74
74
|
}
|
|
75
75
|
}
|
|
@@ -175,7 +175,9 @@ function isAsyncGenerator(value) {
|
|
|
175
175
|
* statement. It answers `{ query, column }`: the rows scopedQuery() gives under `alias` (the caller's ownership, the
|
|
176
176
|
* row policy and `filters` applied) with the stored columns a read answers and the ownership fields, hidden or not,
|
|
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
|
-
* so a report's `select("alias.*")` holds no unbounded bytes. `
|
|
178
|
+
* so a report's `select("alias.*")` holds no unbounded bytes. `columns`, when given, lists the visible fields the report
|
|
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. `column(field)`
|
|
179
181
|
* names each under that alias. A CRUD feature's repository answers it to any feature, where it
|
|
180
182
|
* refuses scopedQuery(). The builder comes wrapped, since a builder is thenable.
|
|
181
183
|
* @param {object} dependencies
|
|
@@ -188,26 +190,50 @@ function isAsyncGenerator(value) {
|
|
|
188
190
|
function createJoinSource({ api, resource, jsonRest, scopedQuery, column }) {
|
|
189
191
|
const { name } = resource;
|
|
190
192
|
/**
|
|
191
|
-
*
|
|
193
|
+
* The fields a join source's `columns` lists, each a visible field the source carries.
|
|
194
|
+
* @param {unknown} columns
|
|
195
|
+
* @param {string[]} visible The fields a source carries without `columns`, but the id.
|
|
196
|
+
*/
|
|
197
|
+
function listedColumns(columns, visible) {
|
|
198
|
+
if (!Array.isArray(columns)) {
|
|
199
|
+
throw new TypeError(`${name}.joinSource() columns must list the visible fields the report reads. Received: ${JSON.stringify(columns)}`);
|
|
200
|
+
}
|
|
201
|
+
for (const field of columns) {
|
|
202
|
+
if (field !== "id" && !visible.includes(/** @type {string} */ (field))) {
|
|
203
|
+
throw new TypeError(
|
|
204
|
+
`${name}.joinSource() columns names ${JSON.stringify(field)}, which is not a visible field of ${name}: a join ` +
|
|
205
|
+
"source carries the stored fields a read answers, the id and the ownership fields; never another hidden " +
|
|
206
|
+
"field, a blob or a virtual field."
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return new Set(/** @type {string[]} */ (columns));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* @param {{ alias: string, filters?: object, columns?: string[] }} params `columns`: the visible fields the report
|
|
215
|
+
* reads, beside the id and the ownership fields, which the source always carries; without it, every visible field.
|
|
192
216
|
* @param {{ context?: object | null, trx?: object | null }} [options]
|
|
193
217
|
* @returns {Promise<Readonly<{ query: any, column: (field: string) => string }>>}
|
|
194
218
|
*/
|
|
195
|
-
return async function joinSource({ alias, filters } = {}, options = {}) {
|
|
219
|
+
return async function joinSource({ alias, filters, columns } = {}, options = {}) {
|
|
196
220
|
if (typeof alias !== "string" || !alias) {
|
|
197
221
|
throw new TypeError(`${name}.joinSource() needs the alias the report joins the ${name} under.`);
|
|
198
222
|
}
|
|
199
|
-
const { query } = await scopedQuery({ alias, ...(filters === undefined ? {} : { filters }) }, options);
|
|
200
223
|
const { schemaStructure, ownershipFields = [] } = jsonRest().vars.schemaInfo;
|
|
201
224
|
const joinKeys = new Set(ownershipFields);
|
|
202
|
-
const
|
|
225
|
+
const visible = Object.keys(schemaStructure).filter((field) => {
|
|
203
226
|
const definition = schemaStructure[field];
|
|
204
227
|
return field !== "id" && (definition.hidden !== true || joinKeys.has(field)) &&
|
|
205
228
|
definition.virtual !== true && !definition.belongsToPolymorphic && resource.schema[field]?.type !== "blob";
|
|
206
229
|
});
|
|
207
|
-
|
|
208
|
-
|
|
230
|
+
const listed = columns === undefined ? null : listedColumns(columns, visible);
|
|
231
|
+
const carried = ["id", ...(listed ? visible.filter((field) => joinKeys.has(field) || listed.has(field)) : visible)];
|
|
232
|
+
const { query } = await scopedQuery({ alias, ...(filters === undefined ? {} : { filters }) }, options);
|
|
233
|
+
// The scoped rows' visible columns (those `columns` lists, when given) and ownership fields, as a derived table
|
|
234
|
+
// under the alias: nothing a caller adds reaches the others.
|
|
209
235
|
const source = readOnlyQueryBuilder(
|
|
210
|
-
(options.trx || api.knex.instance).from(query.select(
|
|
236
|
+
(options.trx || api.knex.instance).from(query.select(carried.map((field) => column(field, alias))).as(alias)),
|
|
211
237
|
{ resourceType: name }
|
|
212
238
|
);
|
|
213
239
|
JOIN_SOURCES.add(source);
|
|
@@ -220,6 +246,12 @@ function createJoinSource({ api, resource, jsonRest, scopedQuery, column }) {
|
|
|
220
246
|
if (resource.schema[field]?.hidden === true && !joinKeys.has(field)) {
|
|
221
247
|
throw new TypeError(`${name}.joinSource() carries no hidden field but its ownership fields, so it has no column for "${field}".`);
|
|
222
248
|
}
|
|
249
|
+
if (listed && !carried.includes(field)) {
|
|
250
|
+
throw new TypeError(
|
|
251
|
+
`${name}.joinSource() was given columns ${JSON.stringify([...listed])}, so it carries no column for "${field}" ` +
|
|
252
|
+
"(only those, the id and the ownership fields): name it in columns."
|
|
253
|
+
);
|
|
254
|
+
}
|
|
223
255
|
return column(field, alias);
|
|
224
256
|
}
|
|
225
257
|
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { defineTableResource } from "@jskit-ai/resource-core/shared/tableResource";
|
|
4
|
+
import { createPackageTestKnex, createTestResourceRuntime } from "../src/test/index.js";
|
|
5
|
+
|
|
6
|
+
// SQLite in memory, or on a DB_CLIENT=pg or mysql2 run a schema or database of its own (see createPackageTestKnex).
|
|
7
|
+
const testKnex = (t) => createPackageTestKnex(t, { environment: process.env });
|
|
8
|
+
|
|
9
|
+
const WORKSPACE_A = Object.freeze({ workspace: { id: "1" } });
|
|
10
|
+
// The columns a narrow join source of the pins carries: the id, the ownership field and the two fields it lists.
|
|
11
|
+
const NARROW = ["p.id", "p.workspace_id", "p.workflow_record_id", "p.module_version_id"];
|
|
12
|
+
|
|
13
|
+
// A wide table another owner's report reads two fields of: pins with a hidden owner and a hidden secret, two in
|
|
14
|
+
// workspace 1 and one in workspace 2.
|
|
15
|
+
async function createPinFixture(t) {
|
|
16
|
+
const knex = await testKnex(t);
|
|
17
|
+
await knex.schema.createTable("jc_record_pins", (table) => {
|
|
18
|
+
table.increments("id");
|
|
19
|
+
table.integer("workspace_id").notNullable();
|
|
20
|
+
table.integer("workflow_record_id").notNullable();
|
|
21
|
+
table.integer("module_version_id").notNullable();
|
|
22
|
+
table.string("label").notNullable();
|
|
23
|
+
table.string("note");
|
|
24
|
+
table.string("pin_secret");
|
|
25
|
+
table.index(["workspace_id"], "jc_record_pins_workspace");
|
|
26
|
+
});
|
|
27
|
+
await knex("jc_record_pins").insert([
|
|
28
|
+
{ workspace_id: 1, workflow_record_id: 10, module_version_id: 100, label: "A", note: "first", pin_secret: "s1" },
|
|
29
|
+
{ workspace_id: 1, workflow_record_id: 11, module_version_id: 101, label: "B", note: null, pin_secret: "s2" },
|
|
30
|
+
{ workspace_id: 2, workflow_record_id: 12, module_version_id: 102, label: "C", note: "other", pin_secret: "s3" }
|
|
31
|
+
]);
|
|
32
|
+
const resource = defineTableResource({
|
|
33
|
+
name: "recordPins",
|
|
34
|
+
tableName: "jc_record_pins",
|
|
35
|
+
ownership: "workspace",
|
|
36
|
+
schema: {
|
|
37
|
+
workspaceId: { type: "id", required: true, hidden: true, storage: { column: "workspace_id" } },
|
|
38
|
+
workflowRecordId: { type: "integer", required: true, search: true, storage: { column: "workflow_record_id" } },
|
|
39
|
+
moduleVersionId: { type: "integer", required: true, storage: { column: "module_version_id" } },
|
|
40
|
+
label: { type: "string", required: true, search: true },
|
|
41
|
+
note: { type: "string", nullable: true },
|
|
42
|
+
pinSecret: { type: "string", nullable: true, hidden: true, storage: { column: "pin_secret" } }
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
const runtime = await createTestResourceRuntime({ knex });
|
|
46
|
+
const { recordPins } = await runtime.own("pins.feature", [resource]);
|
|
47
|
+
const system = runtime.declareSystemScope("pins.feature", "report every workspace's pins")("count the pins");
|
|
48
|
+
return { pins: recordPins, system, options: { context: WORKSPACE_A } };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A report's statement over a source, without identifier quotes, and the select list of each SELECT in it.
|
|
53
|
+
* @param {{ query: any, column: (field: string) => string }} source
|
|
54
|
+
*/
|
|
55
|
+
function statementOf(source) {
|
|
56
|
+
const { sql, bindings } = source.query.select({ record: source.column("id") }).toSQL();
|
|
57
|
+
const text = sql.replace(/[`"]/gu, "");
|
|
58
|
+
return { text, bindings, lists: [...text.matchAll(/select (.+?) from /gu)].map(([, list]) => list) };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
test("a join source given columns selects only those, the id and the ownership fields, in the statement it builds without them", async (t) => {
|
|
62
|
+
const { pins, options } = await createPinFixture(t);
|
|
63
|
+
// Without columns, the source's derived table lists every visible field and the ownership field, never the secret.
|
|
64
|
+
const every = statementOf(await pins.joinSource({ alias: "p" }, options));
|
|
65
|
+
const visible = every.lists.find((list) => list.includes("p.label"));
|
|
66
|
+
assert.ok(visible, every.text);
|
|
67
|
+
assert.deepEqual(visible.split(", ").sort(), [...NARROW, "p.label", "p.note"].sort());
|
|
68
|
+
assert.ok(!every.text.includes("pin_secret"), every.text);
|
|
69
|
+
|
|
70
|
+
// With them, that list holds the listed fields, the id and the ownership field: the same derived tables, scope and
|
|
71
|
+
// bindings otherwise.
|
|
72
|
+
const narrow = statementOf(await pins.joinSource({ alias: "p", columns: ["workflowRecordId", "moduleVersionId"] }, options));
|
|
73
|
+
const listed = visible.split(", ").filter((column) => NARROW.includes(column)).join(", ");
|
|
74
|
+
assert.equal(narrow.text, every.text.replace(visible, listed));
|
|
75
|
+
assert.deepEqual(narrow.bindings, every.bindings);
|
|
76
|
+
for (const left of ["p.label", "p.note", "pin_secret"]) {
|
|
77
|
+
assert.ok(!narrow.text.includes(left), narrow.text);
|
|
78
|
+
}
|
|
79
|
+
// The scope's own ownership field and the id need no naming, and naming them changes nothing.
|
|
80
|
+
const named = statementOf(await pins.joinSource({ alias: "p", columns: ["id", "workspaceId", "moduleVersionId", "workflowRecordId"] }, options));
|
|
81
|
+
assert.equal(named.text, narrow.text);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("a join source refuses a columns entry that is no visible field, a hidden one included, before it reads anything", async (t) => {
|
|
85
|
+
const { pins, options } = await createPinFixture(t);
|
|
86
|
+
await assert.rejects(pins.joinSource({ alias: "p", columns: ["workflowRecordId", "secret_column"] }, options),
|
|
87
|
+
/recordPins\.joinSource\(\) columns names "secret_column", which is not a visible field of recordPins/u);
|
|
88
|
+
await assert.rejects(pins.joinSource({ alias: "p", columns: ["pinSecret"] }, options),
|
|
89
|
+
/recordPins\.joinSource\(\) columns names "pinSecret", which is not a visible field of recordPins: a join source carries the stored fields a read answers, the id and the ownership fields; never another hidden field/u);
|
|
90
|
+
await assert.rejects(pins.joinSource({ alias: "p", columns: "label" }, options),
|
|
91
|
+
/recordPins\.joinSource\(\) columns must list the visible fields the report reads/u);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("a join source given columns has no column for a field it does not list, and names the id, the ownership fields and the listed ones", async (t) => {
|
|
95
|
+
const { pins, options } = await createPinFixture(t);
|
|
96
|
+
const source = await pins.joinSource({ alias: "p", columns: ["workflowRecordId", "moduleVersionId"] }, options);
|
|
97
|
+
assert.throws(() => source.column("label"),
|
|
98
|
+
/recordPins\.joinSource\(\) was given columns \["workflowRecordId","moduleVersionId"\], so it carries no column for "label" \(only those, the id and the ownership fields\): name it in columns/u);
|
|
99
|
+
assert.throws(() => source.column("pinSecret"), /recordPins\.joinSource\(\) carries no hidden field but its ownership fields, so it has no column for "pinSecret"/u);
|
|
100
|
+
assert.deepEqual(["id", "workspaceId", "workflowRecordId", "moduleVersionId"].map((field) => source.column(field)),
|
|
101
|
+
["p.id", "p.workspace_id", "p.workflow_record_id", "p.module_version_id"]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("a join source given columns still answers the caller's scoped rows alone, its filters applied on fields it does not list", async (t) => {
|
|
105
|
+
const { pins, system, options } = await createPinFixture(t);
|
|
106
|
+
const rowsOf = async (source) => (await source.query.select("p.*").orderBy(source.column("id")))
|
|
107
|
+
.map((row) => Object.fromEntries(Object.entries(row).map(([key, value]) => [key, Number(value)])));
|
|
108
|
+
|
|
109
|
+
const narrow = await pins.joinSource({ alias: "p", columns: ["workflowRecordId"] }, options);
|
|
110
|
+
assert.deepEqual(await rowsOf(narrow), [
|
|
111
|
+
{ id: 1, workspace_id: 1, workflow_record_id: 10 },
|
|
112
|
+
{ id: 2, workspace_id: 1, workflow_record_id: 11 }
|
|
113
|
+
]);
|
|
114
|
+
const filtered = await pins.joinSource({ alias: "p", filters: { label: "B" }, columns: ["workflowRecordId"] }, options);
|
|
115
|
+
assert.deepEqual(await rowsOf(filtered), [{ id: 2, workspace_id: 1, workflow_record_id: 11 }]);
|
|
116
|
+
// Another workspace's pin stays out of a report's subquery over the source; a system context reads every workspace's.
|
|
117
|
+
const outside = await pins.joinSource({ alias: "p", filters: { label: "C" }, columns: ["moduleVersionId"] }, options);
|
|
118
|
+
assert.deepEqual(await rowsOf(outside), []);
|
|
119
|
+
const everyWorkspace = await pins.joinSource({ alias: "p", columns: ["moduleVersionId"] }, { context: system });
|
|
120
|
+
assert.deepEqual((await rowsOf(everyWorkspace)).map((row) => [row.workspace_id, row.module_version_id]), [[1, 100], [1, 101], [2, 102]]);
|
|
121
|
+
});
|