@jskit-ai/resource-runtime 0.2.48 → 0.2.49

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
+ - `@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.
5
6
  - Depends on json-rest-api 3.3.3 exactly (from 3.3.2): on a descending sort, rows tied on its last key come in descending id order (the id that breaks ties takes the last key's direction), so MySQL and MariaDB read a descending page from an index leading with that key instead of sorting every row, and leave out the `IS NULL` term where the server already places NULLs last; and capabilities a start could not observe (the server not up yet) are observed again on the next storage call until that succeeds.
6
7
  - json-rest-api's log details reach the application's log: `createResourceHost` hands each of json-rest-api's (message, details) calls to the runtime logger as pino's (fields, message), where pino dropped what followed the message. A database version observation that fails at boot now logs its backend and error ("Database version observation failed", `backend`, `phase`, `error`), "Database capabilities detected:" its database, version and window functions, and every json-rest-api warning and error its details. A detail that is not one object is logged as `details`; a message with none stays a message alone; trace and fatal reach a logger that has them.
7
8
  - `createTestResourceRuntime({ createTables: true })` and `startTestFeatures({ createTables: true })` create each owned table after the tables its belongs-to fields reference, where they followed the order the resources were owned in: on MySQL, MariaDB and PostgreSQL, which check a foreign key as its table is created, a table that references one owned after it was refused (MariaDB: `Foreign key constraint is incorrectly formed`, for workspaces-core's memberships before its invitations). `./test`'s `createPackageTestKnex` gives a `DB_CLIENT=mysql2` run a MySQL or MariaDB database per test (see database-runtime), so the package's own data tests run on MariaDB in such a run, as they run on PostgreSQL in a `pg` one; the thirteen `*.mysql.test.js` files that ran the same cases on a database of their own are gone, and three fixtures (`notes`, `visits`, `cards`) carry the workspace index `lockWhere` needs on MySQL and MariaDB.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/resource-runtime",
3
- "version": "0.2.48",
3
+ "version": "0.2.49",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24.0.0"
@@ -10,13 +10,14 @@
10
10
  },
11
11
  "exports": {
12
12
  "./server": "./src/server/index.js",
13
+ "./server/anyapi": "./src/server/anyapi.js",
13
14
  "./server/types": "./src/server/types.js",
14
15
  "./test": "./src/test/index.js",
15
16
  "./locales/en.json": "./locales/en.json"
16
17
  },
17
18
  "dependencies": {
18
- "@jskit-ai/resource-core": "0.1.224",
19
- "@jskit-ai/resource-crud-core": "0.1.226",
19
+ "@jskit-ai/resource-core": "0.1.225",
20
+ "@jskit-ai/resource-crud-core": "0.1.227",
20
21
  "json-rest-api": "3.3.3"
21
22
  },
22
23
  "description": "The resource runtime: features own their tables through it and read and write them with scoped handles on json-rest-api.",
@@ -51,6 +52,10 @@
51
52
  "subpath": "./server",
52
53
  "summary": "Provides runtime.resources (ResourceRuntimeProvider): own(featureId, owns) registers a feature's tables and answers their scoped handles, and declareSystemScope(featureId, systemScope) answers the feature's systemContext(reason) for work across every workspace and user. Exports createStandaloneResources, passedOn, withoutHiddenFields, isSystemContext, buildJsonRestQueryParams, the workspace and user scope resolvers, the default row limit and page size, and the runtime's error classes."
53
54
  },
55
+ {
56
+ "subpath": "./server/anyapi",
57
+ "summary": "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 json-rest-api copy's own objects that the runtime's handles run on, so the application's AnyAPI writes join a handle's transaction (txOptions.trx) and the application declares no json-rest-api of its own."
58
+ },
54
59
  {
55
60
  "subpath": "./server/types",
56
61
  "summary": "Types of runtime.resources, its handles, call options and system contexts."
@@ -64,7 +69,7 @@
64
69
  }
65
70
  },
66
71
  "peerDependencies": {
67
- "@jskit-ai/database-runtime": "0.1.284",
68
- "@jskit-ai/kernel": "0.2.33"
72
+ "@jskit-ai/database-runtime": "0.1.285",
73
+ "@jskit-ai/kernel": "0.2.34"
69
74
  }
70
75
  }
@@ -0,0 +1,12 @@
1
+ // json-rest-api's AnyAPI storage, for tables an application defines while it runs, which no table resource describes.
2
+ // These are the json-rest-api copy's own objects that the runtime's handles run on, never wrappers: json-rest-api knows
3
+ // the transactions api.transaction opened by identity, and AnyAPI its registry with instanceof, so an application's
4
+ // own AnyAPI instances join a handle's transaction only on this copy, and the application declares no json-rest-api.
5
+ export {
6
+ AnyapiRegistry,
7
+ JsonRestApi,
8
+ RestApiAnyapiKnexPlugin,
9
+ RestApiPlugin,
10
+ RowPolicyPlugin,
11
+ ensureAnyApiSchema
12
+ } from "json-rest-api";
@@ -42,6 +42,62 @@ test("package exports its provider and helpers, its types and its test helpers,
42
42
  assert.equal(typeof server.withoutHiddenFields, "function");
43
43
  });
44
44
 
45
+ // json-rest-api knows the transactions api.transaction opened by identity, in its own module, and AnyAPI its registry
46
+ // with instanceof: an application's own AnyAPI instances (tables it defines while it runs, which no table resource
47
+ // describes) work with the runtime's handles only on the json-rest-api copy the runtime runs. ./server/anyapi hands on
48
+ // that copy's own objects, so the application declares no json-rest-api, and a JSKIT update moves its one copy.
49
+ const ANYAPI_EXPORTS = Object.freeze([
50
+ "AnyapiRegistry",
51
+ "JsonRestApi",
52
+ "RestApiAnyapiKnexPlugin",
53
+ "RestApiPlugin",
54
+ "RowPolicyPlugin",
55
+ "ensureAnyApiSchema"
56
+ ]);
57
+
58
+ test("./server/anyapi exports json-rest-api's own AnyAPI objects, those and no others", async () => {
59
+ const packageJson = JSON.parse(await readFile(new URL("../package.json", import.meta.url), "utf8"));
60
+ assert.equal(packageJson.exports["./server/anyapi"], "./src/server/anyapi.js");
61
+ const anyapi = await import("@jskit-ai/resource-runtime/server/anyapi");
62
+ const jsonRestApi = await import("json-rest-api");
63
+ assert.deepEqual(Object.keys(anyapi).sort(), [...ANYAPI_EXPORTS].sort());
64
+ for (const name of ANYAPI_EXPORTS) {
65
+ assert.equal(anyapi[name], jsonRestApi[name], name);
66
+ }
67
+ });
68
+
69
+ test("an AnyAPI write from ./server/anyapi joins the transaction a handle opened, and rolls back with it", async (t) => {
70
+ const { AnyapiRegistry, JsonRestApi: AnyapiRestApi, RestApiAnyapiKnexPlugin, RestApiPlugin } =
71
+ await import("@jskit-ai/resource-runtime/server/anyapi");
72
+ const knex = knexLib({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true });
73
+ t.after(() => knex.destroy());
74
+ await knex.schema.createTable("notes", (table) => {
75
+ table.increments("id");
76
+ table.string("title").notNullable();
77
+ });
78
+ const { resources: runtime } = await ResourceRuntimeProvider.setup({ database: { knex }, logger: silentLogger });
79
+ const { notes } = await runtime.own("notes.feature", [
80
+ defineTableResource({ name: "notes", tableName: "notes", ownership: "public", schema: { title: { type: "string", required: true } } })
81
+ ]);
82
+ const api = new AnyapiRestApi({ name: "records" });
83
+ await api.use(RestApiPlugin, { format: "plain", returning: "full" });
84
+ // The plugin takes only a registry of its own json-rest-api's class.
85
+ await api.use(RestApiAnyapiKnexPlugin, { knex, registry: new AnyapiRegistry({ knex }), ownsKnex: false });
86
+ await api.addResource("records", { schema: { title: { type: "string" } } });
87
+
88
+ await notes.withTransaction({}, async (txOptions) => {
89
+ await notes.create({ title: "kept" }, txOptions);
90
+ await api.resources.records.post({ data: { title: "kept" }, transaction: txOptions.trx });
91
+ });
92
+ await assert.rejects(notes.withTransaction({}, async (txOptions) => {
93
+ await notes.create({ title: "undone" }, txOptions);
94
+ await api.resources.records.post({ data: { title: "undone" }, transaction: txOptions.trx });
95
+ throw new Error("undo");
96
+ }), { message: "undo" });
97
+ assert.deepEqual((await api.resources.records.query({})).data.map((record) => record.title), ["kept"]);
98
+ assert.deepEqual(await knex("notes").pluck("title"), ["kept"]);
99
+ });
100
+
45
101
  test("standalone resources on a server's Knex client never own a table one of the server's features owns", async (t) => {
46
102
  const { createStandaloneResources } = await import("../src/server/index.js");
47
103
  const knex = knexLib({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true });