@jskit-ai/database-runtime 0.1.255 → 0.1.257
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/database-runtime",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.257",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test --test-concurrency=1"
|
|
@@ -78,6 +78,6 @@
|
|
|
78
78
|
}
|
|
79
79
|
},
|
|
80
80
|
"peerDependencies": {
|
|
81
|
-
"@jskit-ai/kernel": "0.2.
|
|
81
|
+
"@jskit-ai/kernel": "0.2.8"
|
|
82
82
|
}
|
|
83
83
|
}
|
|
@@ -7,16 +7,24 @@ const DatabaseProvider = defineProvider({
|
|
|
7
7
|
env: "runtime.env",
|
|
8
8
|
driver: "runtime.database.driver"
|
|
9
9
|
},
|
|
10
|
+
optional: {
|
|
11
|
+
health: "runtime.health"
|
|
12
|
+
},
|
|
10
13
|
provides: {
|
|
11
14
|
database: "runtime.database"
|
|
12
15
|
},
|
|
13
16
|
/**
|
|
17
|
+
* Opens the database and, when the application has health checks, adds one that the database
|
|
18
|
+
* answers a trivial query.
|
|
14
19
|
* @param {object} dependencies
|
|
15
20
|
* @param {import("../types.js").RuntimeDatabaseDriver} dependencies.driver
|
|
16
21
|
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} dependencies.env
|
|
22
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeHealth | null} dependencies.health
|
|
17
23
|
*/
|
|
18
|
-
setup({ driver, env }) {
|
|
19
|
-
|
|
24
|
+
setup({ driver, env, health }) {
|
|
25
|
+
const database = createDatabase({ driver, env });
|
|
26
|
+
health?.register("database", () => database.knex.raw("select 1"));
|
|
27
|
+
return { database };
|
|
20
28
|
},
|
|
21
29
|
/**
|
|
22
30
|
* @param {unknown} _dependencies
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
|
-
import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
|
|
2
|
+
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import test from "node:test";
|
|
@@ -50,6 +50,12 @@ async function withAppRootKnexStub(callback) {
|
|
|
50
50
|
" __source: 'app-root-knex',",
|
|
51
51
|
" __config: config,",
|
|
52
52
|
" destroyCalls: 0,",
|
|
53
|
+
" rawQueries: [],",
|
|
54
|
+
" raw: async function raw(sql) {",
|
|
55
|
+
" this.rawQueries.push(sql);",
|
|
56
|
+
" if (config.connection.host === 'db.down') throw new Error('connect ECONNREFUSED');",
|
|
57
|
+
" return [];",
|
|
58
|
+
" },",
|
|
53
59
|
" transaction: async function transaction(callback) {",
|
|
54
60
|
" return callback({ trxId: 'trx-app-root' });",
|
|
55
61
|
" },",
|
|
@@ -67,6 +73,7 @@ async function withAppRootKnexStub(callback) {
|
|
|
67
73
|
await callback();
|
|
68
74
|
} finally {
|
|
69
75
|
process.chdir(previousCwd);
|
|
76
|
+
await rm(appRoot, { recursive: true, force: true });
|
|
70
77
|
}
|
|
71
78
|
}
|
|
72
79
|
|
|
@@ -159,3 +166,39 @@ test("DatabaseProvider rejects environment dialect mismatch", async () => {
|
|
|
159
166
|
);
|
|
160
167
|
});
|
|
161
168
|
});
|
|
169
|
+
|
|
170
|
+
test("DatabaseProvider adds a database health check that runs a trivial query", async () => {
|
|
171
|
+
await withAppRootKnexStub(async () => {
|
|
172
|
+
for (const [host, healthy] of [["db.local", true], ["db.down", false]]) {
|
|
173
|
+
const checks = new Map();
|
|
174
|
+
let database;
|
|
175
|
+
const runtime = createCapabilityRuntime({
|
|
176
|
+
inputs: {
|
|
177
|
+
"runtime.env": { DB_HOST: host, DB_NAME: "appdb", DB_USER: "appuser" },
|
|
178
|
+
"runtime.health": { register: (id, check) => checks.set(id, check) }
|
|
179
|
+
},
|
|
180
|
+
providers: [driverProvider(), DatabaseProvider, databaseConsumer((value) => { database = value; })]
|
|
181
|
+
});
|
|
182
|
+
await runtime.start();
|
|
183
|
+
assert.deepEqual([...checks.keys()], ["database"]);
|
|
184
|
+
if (healthy) {
|
|
185
|
+
await checks.get("database")();
|
|
186
|
+
} else {
|
|
187
|
+
await assert.rejects(checks.get("database")(), /ECONNREFUSED/u);
|
|
188
|
+
}
|
|
189
|
+
assert.deepEqual(database.knex.rawQueries, ["select 1"]);
|
|
190
|
+
await runtime.shutdown();
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("DatabaseProvider starts without health checks", async () => {
|
|
196
|
+
await withAppRootKnexStub(async () => {
|
|
197
|
+
const runtime = createCapabilityRuntime({
|
|
198
|
+
inputs: { "runtime.env": { DB_NAME: "appdb", DB_USER: "appuser" } },
|
|
199
|
+
providers: [driverProvider(), DatabaseProvider]
|
|
200
|
+
});
|
|
201
|
+
await runtime.start();
|
|
202
|
+
await runtime.shutdown();
|
|
203
|
+
});
|
|
204
|
+
});
|