@jskit-ai/resource-runtime 0.2.64 → 0.2.66

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/resource-runtime",
3
- "version": "0.2.64",
3
+ "version": "0.2.66",
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.230",
20
- "@jskit-ai/resource-crud-core": "0.1.233",
19
+ "@jskit-ai/resource-core": "0.1.232",
20
+ "@jskit-ai/resource-crud-core": "0.1.235",
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.297",
73
- "@jskit-ai/kernel": "0.2.39"
72
+ "@jskit-ai/database-runtime": "0.1.299",
73
+ "@jskit-ai/kernel": "0.2.41"
74
74
  }
75
75
  }
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
2
2
  import test from "node:test";
3
3
  import knexLib from "knex";
4
4
  import { defineTableResource } from "@jskit-ai/resource-core/shared/tableResource";
5
- import { requireTestDatabase } from "@jskit-ai/database-runtime/server/testDatabases";
5
+ import { createPackageTestKnex, requireTestDatabase } from "@jskit-ai/database-runtime/server/testDatabases";
6
6
  import { UnsupportedDatabaseError } from "../src/server/index.js";
7
7
  import { createTestResourceRuntime } from "../src/test/index.js";
8
8
 
@@ -11,7 +11,6 @@ import { createTestResourceRuntime } from "../src/test/index.js";
11
11
  // other's lock leaves the row out when it no longer matches (a compare-and-set on the filters).
12
12
 
13
13
  const WORKSPACE = Object.freeze({ workspace: { id: "1" } });
14
- const RUNTIME_MYSQL = Object.freeze({ supportBigNumbers: true, bigNumberStrings: true, dateStrings: ["DATE"], timezone: "Z" });
15
14
 
16
15
  const slotResource = defineTableResource({
17
16
  name: "slots",
@@ -27,32 +26,30 @@ const slotResource = defineTableResource({
27
26
  });
28
27
 
29
28
  /**
30
- * A database of the test's own beside TEST_DB_NAME, with one slots table, dropped after the test. Every connection
31
- * runs with MariaDB's innodb_snapshot_isolation set to `snapshotIsolation` (default off). A server without the
32
- * setting (MySQL, older MariaDB) behaves as off, so asking it for on answers null.
29
+ * A lettered copy of TEST_DB_NAME the test claims (createPackageTestKnex() on MySQL), with one slots table, dropped
30
+ * after the test. Every connection runs with MariaDB's innodb_snapshot_isolation set to `snapshotIsolation` (default
31
+ * off). A server without the setting (MySQL, older MariaDB) behaves as off, so asking it for on answers null.
33
32
  */
34
- async function ownDatabase(context, suffix, { snapshotIsolation = false } = {}) {
35
- const name = `${requireTestDatabase(process.env)}_${suffix}`;
33
+ async function ownDatabase(context, { snapshotIsolation = false } = {}) {
36
34
  const server = { host: process.env.DB_HOST, port: Number(process.env.DB_PORT) || 3306, user: process.env.DB_USER, password: process.env.DB_PASSWORD };
37
35
  const admin = knexLib({ client: "mysql2", connection: server, pool: { min: 0, max: 1 } });
38
- const [variables] = await admin.raw("SHOW VARIABLES LIKE 'innodb_snapshot_isolation'");
39
- if (snapshotIsolation && variables.length === 0) {
36
+ let variables;
37
+ try {
38
+ [variables] = await admin.raw("SHOW VARIABLES LIKE 'innodb_snapshot_isolation'");
39
+ } finally {
40
40
  await admin.destroy();
41
- return null;
42
41
  }
43
- await admin.raw("DROP DATABASE IF EXISTS ??", [name]);
44
- await admin.raw("CREATE DATABASE ??", [name]);
42
+ if (snapshotIsolation && variables.length === 0) return null;
43
+ let knex = null;
44
+ // Added before the claim's, so this Knex closes before the claim's teardown drops the copy.
45
+ context.after(() => knex?.destroy());
46
+ const claimed = await createPackageTestKnex(context, { environment: { ...process.env, DB_CLIENT: "mysql2" } });
45
47
  const setting = variables.length === 0 ? null : `SET SESSION innodb_snapshot_isolation = ${snapshotIsolation ? "ON" : "OFF"}`;
46
- const knex = knexLib({
48
+ knex = knexLib({
47
49
  client: "mysql2",
48
- connection: { ...server, database: name, ...RUNTIME_MYSQL },
50
+ connection: claimed.client.config.connection,
49
51
  pool: { min: 0, max: 6, ...(setting ? { afterCreate: (connection, done) => connection.query(setting, (error) => done(error, connection)) } : {}) }
50
52
  });
51
- context.after(async () => {
52
- await knex.destroy();
53
- await admin.raw("DROP DATABASE IF EXISTS ??", [name]);
54
- await admin.destroy();
55
- });
56
53
  await knex.schema.createTable("slots", (table) => {
57
54
  table.increments("id");
58
55
  table.integer("workspace_id").nullable();
@@ -109,8 +106,8 @@ async function raceOnLockedRow(knex, id, writers) {
109
106
  }
110
107
  }
111
108
 
112
- async function takeTwice(t, suffix, options) {
113
- const database = await ownDatabase(t, suffix, options);
109
+ async function takeTwice(t, options) {
110
+ const database = await ownDatabase(t, options);
114
111
  if (!database) {
115
112
  t.skip("the server has no innodb_snapshot_isolation setting");
116
113
  return;
@@ -128,17 +125,17 @@ async function takeTwice(t, suffix, options) {
128
125
 
129
126
  test("of two concurrent patchWhere calls whose filters both matched one row, exactly one writes it", async (t) => {
130
127
  requireTestDatabase(process.env);
131
- await takeTwice(t, "da_pw_race");
128
+ await takeTwice(t);
132
129
  });
133
130
 
134
131
  test("with innodb_snapshot_isolation on, the waiting patchWhere fails as changed since its read and runs again, leaving the row out", async (t) => {
135
132
  requireTestDatabase(process.env);
136
- await takeTwice(t, "da_pw_snapshot", { snapshotIsolation: true });
133
+ await takeTwice(t, { snapshotIsolation: true });
137
134
  });
138
135
 
139
136
  test("a deleteWhere whose filter matched a row a concurrent patchWhere changed leaves it in place", async (t) => {
140
137
  requireTestDatabase(process.env);
141
- const { knex, slots } = await ownDatabase(t, "da_dw_race");
138
+ const { knex, slots } = await ownDatabase(t);
142
139
  const [id] = await slots.createMany([{ status: "open" }], { context: WORKSPACE, returning: "ids" });
143
140
  const outcomes = await raceOnLockedRow(knex, id, [
144
141
  () => slots.patchWhere({ id, status: "open" }, { status: "taken", takenBy: "first" }, { context: WORKSPACE, returning: "ids" }),
@@ -152,7 +149,7 @@ test("a deleteWhere whose filter matched a row a concurrent patchWhere changed l
152
149
  });
153
150
 
154
151
  test("changeHorizon() throws on MySQL and MariaDB, which stamp no row with its transaction, before any statement", async (t) => {
155
- const { knex, slots } = await ownDatabase(t, "horizon");
152
+ const { knex, slots } = await ownDatabase(t);
156
153
  const statements = [];
157
154
  knex.on("query", ({ sql }) => statements.push(sql));
158
155
  await assert.rejects(slots.changeHorizon({ context: WORKSPACE }), UnsupportedDatabaseError);
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
2
2
  import test from "node:test";
3
3
  import knexLib from "knex";
4
4
  import { defineTableResource } from "@jskit-ai/resource-core/shared/tableResource";
5
- import { requireTestDatabase } from "@jskit-ai/database-runtime/server/testDatabases";
5
+ import { createPackageTestKnex, requireTestDatabase } from "@jskit-ai/database-runtime/server/testDatabases";
6
6
  import { createTestResourceRuntime } from "../../src/test/index.js";
7
7
 
8
8
  // Whole numbers stored in BIGINT columns that are not ids (a counter, a byte count, a read position), read through a
@@ -11,7 +11,6 @@ import { createTestResourceRuntime } from "../../src/test/index.js";
11
11
  // id field as text.
12
12
 
13
13
  const WORKSPACE = Object.freeze({ workspace: { id: "1" } });
14
- const RUNTIME_MYSQL = Object.freeze({ supportBigNumbers: true, bigNumberStrings: true, dateStrings: ["DATE"], timezone: "Z" });
15
14
  // The largest whole number a JavaScript number holds exactly (2^53 - 1), and the next odd one past it.
16
15
  const LARGEST_SAFE = "9007199254740991";
17
16
  const PAST_SAFE = "9007199254740993";
@@ -37,20 +36,9 @@ const counterResource = defineTableResource({
37
36
  });
38
37
  const FIELDS = Object.freeze(["workspaceId", "total", "readUpTo", "hiddenCount", "lastMessageId"]);
39
38
 
40
- /** A MariaDB/MySQL database of the test's own beside TEST_DB_NAME, dropped after it. */
41
- async function mysqlKnex(context, suffix) {
42
- const name = `${requireTestDatabase(process.env)}_${suffix}`;
43
- const server = { host: process.env.DB_HOST, port: Number(process.env.DB_PORT) || 3306, user: process.env.DB_USER, password: process.env.DB_PASSWORD };
44
- const admin = knexLib({ client: "mysql2", connection: server, pool: { min: 0, max: 1 } });
45
- await admin.raw("DROP DATABASE IF EXISTS ??", [name]);
46
- await admin.raw("CREATE DATABASE ??", [name]);
47
- const knex = knexLib({ client: "mysql2", connection: { ...server, database: name, ...RUNTIME_MYSQL }, pool: { min: 0, max: 2 } });
48
- context.after(async () => {
49
- await knex.destroy();
50
- await admin.raw("DROP DATABASE IF EXISTS ??", [name]);
51
- await admin.destroy();
52
- });
53
- return knex;
39
+ /** A lettered copy of TEST_DB_NAME the test claims (createPackageTestKnex() on MySQL), dropped after it. */
40
+ function mysqlKnex(context) {
41
+ return createPackageTestKnex(context, { environment: { ...process.env, DB_CLIENT: "mysql2" } });
54
42
  }
55
43
 
56
44
  /** A PostgreSQL schema of the test's own in TEST_DB_NAME, dropped after it. */
@@ -1,15 +1,13 @@
1
1
  import assert from "node:assert/strict";
2
2
  import test from "node:test";
3
- import knexLib from "knex";
4
3
  import { defineTableResource } from "@jskit-ai/resource-core/shared/tableResource";
5
- import { requireTestDatabase } from "@jskit-ai/database-runtime/server/testDatabases";
4
+ import { createPackageTestKnex } from "@jskit-ai/database-runtime/server/testDatabases";
6
5
  import { createTestResourceRuntime } from "../src/test/index.js";
7
6
 
8
7
  // Values a handle writes and compares on MySQL or MariaDB: timestamps a job sets itself, with milliseconds, on a
9
8
  // table that stores whole seconds, and a patch's onlyIf with no changes, compared in SQL.
10
9
 
11
10
  const WORKSPACE = Object.freeze({ workspace: { id: "1" } });
12
- const RUNTIME_MYSQL = Object.freeze({ supportBigNumbers: true, bigNumberStrings: true, dateStrings: ["DATE"], timezone: "Z" });
13
11
 
14
12
  const eventResource = defineTableResource({
15
13
  name: "events",
@@ -27,19 +25,9 @@ const eventResource = defineTableResource({
27
25
  }
28
26
  });
29
27
 
30
- /** A database of the test's own beside TEST_DB_NAME, with one events table storing whole seconds, dropped after it. */
31
- async function ownDatabase(context, suffix) {
32
- const name = `${requireTestDatabase(process.env)}_${suffix}`;
33
- const server = { host: process.env.DB_HOST, port: Number(process.env.DB_PORT) || 3306, user: process.env.DB_USER, password: process.env.DB_PASSWORD };
34
- const admin = knexLib({ client: "mysql2", connection: server, pool: { min: 0, max: 1 } });
35
- await admin.raw("DROP DATABASE IF EXISTS ??", [name]);
36
- await admin.raw("CREATE DATABASE ??", [name]);
37
- const knex = knexLib({ client: "mysql2", connection: { ...server, database: name, ...RUNTIME_MYSQL }, pool: { min: 0, max: 2 } });
38
- context.after(async () => {
39
- await knex.destroy();
40
- await admin.raw("DROP DATABASE IF EXISTS ??", [name]);
41
- await admin.destroy();
42
- });
28
+ /** A lettered copy of TEST_DB_NAME the test claims (createPackageTestKnex() on MySQL), with one events table storing whole seconds, dropped after it. */
29
+ async function ownDatabase(context) {
30
+ const knex = await createPackageTestKnex(context, { environment: { ...process.env, DB_CLIENT: "mysql2" } });
43
31
  await knex.schema.createTable("events", (table) => {
44
32
  table.increments("id");
45
33
  table.integer("workspace_id").nullable();
@@ -62,7 +50,7 @@ async function storedTimes(knex, id) {
62
50
  }
63
51
 
64
52
  test("a job's own timestamps with milliseconds are stored to the whole second on a temporalPrecision 0 table", async (t) => {
65
- const { knex, events, job } = await ownDatabase(t, "job_stamps");
53
+ const { knex, events, job } = await ownDatabase(t);
66
54
  const options = { context: job };
67
55
  // A job's Date#toISOString(): its milliseconds are cut, as the handle cuts its own stamps, never rounded up.
68
56
  const created = await events.create(
@@ -90,7 +78,7 @@ test("a job's own timestamps with milliseconds are stored to the whole second on
90
78
  });
91
79
 
92
80
  test("a patch with onlyIf and no changes compares in SQL: an id given as a number or as text, and a hidden field, match", async (t) => {
93
- const { events } = await ownDatabase(t, "only_if");
81
+ const { events } = await ownDatabase(t);
94
82
  const options = { context: WORKSPACE };
95
83
  const event = await events.create({ title: "talk", ownerUserId: "7", secret: "s1" }, options);
96
84
  // A record never answers the hidden secret, so only SQL can compare it.