@jskit-ai/database-runtime 0.1.256 → 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.256",
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.7"
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
- return { database: createDatabase({ driver, env }) };
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
@@ -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
  " },",
@@ -160,3 +166,39 @@ test("DatabaseProvider rejects environment dialect mismatch", async () => {
160
166
  );
161
167
  });
162
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
+ });