@jskit-ai/database-runtime 0.1.17 → 0.1.18
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.descriptor.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
export default Object.freeze({
|
|
2
2
|
packageVersion: 1,
|
|
3
3
|
packageId: "@jskit-ai/database-runtime",
|
|
4
|
-
version: "0.1.
|
|
4
|
+
version: "0.1.18",
|
|
5
|
+
kind: "runtime",
|
|
5
6
|
dependsOn: [
|
|
6
7
|
"@jskit-ai/kernel"
|
|
7
8
|
],
|
|
@@ -46,7 +47,9 @@ export default Object.freeze({
|
|
|
46
47
|
containerTokens: {
|
|
47
48
|
server: [
|
|
48
49
|
"runtime.database",
|
|
49
|
-
"runtime.database.driver"
|
|
50
|
+
"runtime.database.driver",
|
|
51
|
+
"jskit.database.knex",
|
|
52
|
+
"jskit.database.transactionManager"
|
|
50
53
|
],
|
|
51
54
|
client: []
|
|
52
55
|
}
|
|
@@ -55,7 +58,7 @@ export default Object.freeze({
|
|
|
55
58
|
mutations: {
|
|
56
59
|
dependencies: {
|
|
57
60
|
runtime: {
|
|
58
|
-
"@jskit-ai/kernel": "0.1.
|
|
61
|
+
"@jskit-ai/kernel": "0.1.18",
|
|
59
62
|
"dotenv": "^16.4.5",
|
|
60
63
|
"knex": "^3.1.0"
|
|
61
64
|
},
|
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.18",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test"
|
|
@@ -25,6 +25,6 @@
|
|
|
25
25
|
"./shared/transactions": "./src/shared/transactions.js"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@jskit-ai/kernel": "0.1.
|
|
28
|
+
"@jskit-ai/kernel": "0.1.18"
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -1,25 +1,18 @@
|
|
|
1
|
-
import { KERNEL_TOKENS } from "@jskit-ai/kernel/shared/support/tokens";
|
|
2
1
|
import { symlinkSafeRequire } from "@jskit-ai/kernel/server/support";
|
|
3
2
|
import * as databaseRuntime from "../../shared/index.js";
|
|
4
3
|
import { createTransactionManager } from "../../shared/transactionManager.js";
|
|
5
4
|
import {
|
|
6
|
-
normalizeText,
|
|
7
5
|
normalizeDatabaseClient,
|
|
8
6
|
toKnexClientId
|
|
9
7
|
} from "../../shared/databaseClient.js";
|
|
10
8
|
import { resolveDatabaseConnectionFromEnvironment } from "../../shared/databaseConnection.js";
|
|
11
9
|
|
|
12
|
-
const DATABASE_RUNTIME_TOKEN = "runtime.database";
|
|
13
|
-
const DATABASE_DRIVER_TOKEN = "runtime.database.driver";
|
|
14
|
-
const MYSQL_DRIVER_TOKEN = "runtime.database.driver.mysql";
|
|
15
|
-
const POSTGRES_DRIVER_TOKEN = "runtime.database.driver.postgres";
|
|
16
|
-
|
|
17
10
|
const DATABASE_RUNTIME_SERVER_API = Object.freeze({
|
|
18
11
|
...databaseRuntime
|
|
19
12
|
});
|
|
20
13
|
|
|
21
14
|
function resolveDatabaseEnv(scope) {
|
|
22
|
-
const envFromScope = scope?.has?.(
|
|
15
|
+
const envFromScope = scope?.has?.("jskit.env") ? scope.make("jskit.env") : {};
|
|
23
16
|
const source = envFromScope && typeof envFromScope === "object" ? envFromScope : {};
|
|
24
17
|
return {
|
|
25
18
|
...process.env,
|
|
@@ -94,12 +87,12 @@ function resolveRegisteredDriver(scope) {
|
|
|
94
87
|
function resolveRegisteredDrivers(scope) {
|
|
95
88
|
const drivers = [];
|
|
96
89
|
|
|
97
|
-
if (scope.has(
|
|
98
|
-
drivers.push(scope.make(
|
|
90
|
+
if (scope.has("runtime.database.driver.mysql")) {
|
|
91
|
+
drivers.push(scope.make("runtime.database.driver.mysql"));
|
|
99
92
|
}
|
|
100
93
|
|
|
101
|
-
if (scope.has(
|
|
102
|
-
drivers.push(scope.make(
|
|
94
|
+
if (scope.has("runtime.database.driver.postgres")) {
|
|
95
|
+
drivers.push(scope.make("runtime.database.driver.postgres"));
|
|
103
96
|
}
|
|
104
97
|
|
|
105
98
|
return drivers;
|
|
@@ -145,26 +138,26 @@ function createKnexInstance(scope) {
|
|
|
145
138
|
}
|
|
146
139
|
|
|
147
140
|
class DatabaseRuntimeServiceProvider {
|
|
148
|
-
static id =
|
|
141
|
+
static id = "runtime.database";
|
|
149
142
|
|
|
150
143
|
register(app) {
|
|
151
144
|
if (!app || typeof app.singleton !== "function" || typeof app.has !== "function") {
|
|
152
145
|
throw new Error("DatabaseRuntimeServiceProvider requires application singleton()/has().");
|
|
153
146
|
}
|
|
154
147
|
|
|
155
|
-
app.singleton(
|
|
148
|
+
app.singleton("runtime.database", () => DATABASE_RUNTIME_SERVER_API);
|
|
156
149
|
|
|
157
|
-
if (!app.has(
|
|
158
|
-
app.singleton(
|
|
150
|
+
if (!app.has("runtime.database.driver")) {
|
|
151
|
+
app.singleton("runtime.database.driver", (scope) => resolveSingleRegisteredDriver(scope));
|
|
159
152
|
}
|
|
160
153
|
|
|
161
|
-
if (!app.has(
|
|
162
|
-
app.singleton(
|
|
154
|
+
if (!app.has("jskit.database.knex")) {
|
|
155
|
+
app.singleton("jskit.database.knex", (scope) => createKnexInstance(scope));
|
|
163
156
|
}
|
|
164
157
|
|
|
165
|
-
if (!app.has(
|
|
166
|
-
app.singleton(
|
|
167
|
-
const knex = scope.make(
|
|
158
|
+
if (!app.has("jskit.database.transactionManager")) {
|
|
159
|
+
app.singleton("jskit.database.transactionManager", (scope) => {
|
|
160
|
+
const knex = scope.make("jskit.database.knex");
|
|
168
161
|
return createTransactionManager({ knex });
|
|
169
162
|
});
|
|
170
163
|
}
|
package/src/shared/runtime.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { KERNEL_TOKENS } from "@jskit-ai/kernel/shared/support/tokens";
|
|
2
1
|
import { createTransactionManager } from "./transactionManager.js";
|
|
3
2
|
import { DatabaseRuntimeError } from "./runtimeErrors.js";
|
|
4
3
|
|
|
@@ -21,24 +20,24 @@ function ensureKnexInterface(knex) {
|
|
|
21
20
|
}
|
|
22
21
|
|
|
23
22
|
function ensureKnexBinding(app, knex) {
|
|
24
|
-
if (!app.has(
|
|
25
|
-
app.instance(
|
|
23
|
+
if (!app.has("jskit.database.knex")) {
|
|
24
|
+
app.instance("jskit.database.knex", knex);
|
|
26
25
|
return;
|
|
27
26
|
}
|
|
28
27
|
|
|
29
|
-
const existingKnex = app.make(
|
|
28
|
+
const existingKnex = app.make("jskit.database.knex");
|
|
30
29
|
if (existingKnex !== knex) {
|
|
31
30
|
throw new DatabaseRuntimeError("registerDatabaseRuntime received knex that differs from existing Knex binding.");
|
|
32
31
|
}
|
|
33
32
|
}
|
|
34
33
|
|
|
35
34
|
function ensureTransactionManagerBinding(app) {
|
|
36
|
-
if (app.has(
|
|
35
|
+
if (app.has("jskit.database.transactionManager")) {
|
|
37
36
|
return;
|
|
38
37
|
}
|
|
39
38
|
|
|
40
|
-
app.singleton(
|
|
41
|
-
const knex = scope.make(
|
|
39
|
+
app.singleton("jskit.database.transactionManager", (scope) => {
|
|
40
|
+
const knex = scope.make("jskit.database.knex");
|
|
42
41
|
return createTransactionManager({ knex });
|
|
43
42
|
});
|
|
44
43
|
}
|
|
@@ -51,8 +50,8 @@ function registerDatabaseRuntime(app, { knex } = {}) {
|
|
|
51
50
|
ensureTransactionManagerBinding(app);
|
|
52
51
|
|
|
53
52
|
return {
|
|
54
|
-
knex: app.make(
|
|
55
|
-
transactionManager: app.make(
|
|
53
|
+
knex: app.make("jskit.database.knex"),
|
|
54
|
+
transactionManager: app.make("jskit.database.transactionManager")
|
|
56
55
|
};
|
|
57
56
|
}
|
|
58
57
|
|
|
@@ -4,7 +4,6 @@ import path from "node:path";
|
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
5
|
import test from "node:test";
|
|
6
6
|
|
|
7
|
-
import { KERNEL_TOKENS } from "@jskit-ai/kernel/shared/support/tokens";
|
|
8
7
|
import { DatabaseRuntimeServiceProvider } from "../src/server/providers/DatabaseRuntimeServiceProvider.js";
|
|
9
8
|
|
|
10
9
|
function createSingletonApp() {
|
|
@@ -111,13 +110,13 @@ test("DatabaseRuntimeServiceProvider registers runtime api", () => {
|
|
|
111
110
|
|
|
112
111
|
test("DatabaseRuntimeServiceProvider registers transaction manager when Knex is pre-bound", async () => {
|
|
113
112
|
const app = createSingletonApp();
|
|
114
|
-
app.instance(
|
|
113
|
+
app.instance("jskit.database.knex", createKnexStub());
|
|
115
114
|
|
|
116
115
|
const provider = new DatabaseRuntimeServiceProvider();
|
|
117
116
|
provider.register(app);
|
|
118
117
|
|
|
119
|
-
assert.equal(app.has(
|
|
120
|
-
const transactionManager = app.make(
|
|
118
|
+
assert.equal(app.has("jskit.database.transactionManager"), true);
|
|
119
|
+
const transactionManager = app.make("jskit.database.transactionManager");
|
|
121
120
|
const result = await transactionManager.inTransaction(async (trx) => trx.trxId);
|
|
122
121
|
assert.equal(result, "trx-1");
|
|
123
122
|
});
|
|
@@ -156,7 +155,7 @@ test("DatabaseRuntimeServiceProvider resolves knex from app root package context
|
|
|
156
155
|
await withAppRootKnexStub(async () => {
|
|
157
156
|
const app = createSingletonApp();
|
|
158
157
|
app.instance("runtime.database.driver.mysql", Object.freeze({ DIALECT_ID: "mysql2" }));
|
|
159
|
-
app.instance(
|
|
158
|
+
app.instance("jskit.env", {
|
|
160
159
|
DB_HOST: "db.local",
|
|
161
160
|
DB_PORT: "3307",
|
|
162
161
|
DB_NAME: "appdb",
|
|
@@ -167,7 +166,7 @@ test("DatabaseRuntimeServiceProvider resolves knex from app root package context
|
|
|
167
166
|
const provider = new DatabaseRuntimeServiceProvider();
|
|
168
167
|
provider.register(app);
|
|
169
168
|
|
|
170
|
-
const knex = app.make(
|
|
169
|
+
const knex = app.make("jskit.database.knex");
|
|
171
170
|
assert.equal(knex.__source, "app-root-knex");
|
|
172
171
|
assert.equal(knex.__config.client, "mysql2");
|
|
173
172
|
assert.equal(knex.__config.connection.host, "db.local");
|
|
@@ -182,14 +181,14 @@ test("DatabaseRuntimeServiceProvider resolves knex config from DATABASE_URL when
|
|
|
182
181
|
await withAppRootKnexStub(async () => {
|
|
183
182
|
const app = createSingletonApp();
|
|
184
183
|
app.instance("runtime.database.driver.mysql", Object.freeze({ DIALECT_ID: "mysql2" }));
|
|
185
|
-
app.instance(
|
|
184
|
+
app.instance("jskit.env", {
|
|
186
185
|
DATABASE_URL: "mysql://urluser:urlpass@db.url.local:3308/url_db_name"
|
|
187
186
|
});
|
|
188
187
|
|
|
189
188
|
const provider = new DatabaseRuntimeServiceProvider();
|
|
190
189
|
provider.register(app);
|
|
191
190
|
|
|
192
|
-
const knex = app.make(
|
|
191
|
+
const knex = app.make("jskit.database.knex");
|
|
193
192
|
assert.equal(knex.__source, "app-root-knex");
|
|
194
193
|
assert.equal(knex.__config.client, "mysql2");
|
|
195
194
|
assert.equal(knex.__config.connection.host, "db.url.local");
|
package/test/runtimeCore.test.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
3
|
|
|
4
|
-
import { KERNEL_TOKENS } from "@jskit-ai/kernel/shared/support/tokens";
|
|
5
4
|
import {
|
|
6
5
|
BaseRepository,
|
|
7
6
|
buildPaginationMeta,
|
|
@@ -94,5 +93,5 @@ test("registerDatabaseRuntime binds knex and transaction manager tokens", () =>
|
|
|
94
93
|
const runtime = registerDatabaseRuntime(app, { knex });
|
|
95
94
|
assert.strictEqual(runtime.knex, knex);
|
|
96
95
|
assert.equal(typeof runtime.transactionManager.inTransaction, "function");
|
|
97
|
-
assert.strictEqual(app.make(
|
|
96
|
+
assert.strictEqual(app.make("jskit.database.knex"), knex);
|
|
98
97
|
});
|