@jskit-ai/database-runtime 0.1.147 → 0.1.149

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.147",
3
+ "version": "0.1.149",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "node --test"
@@ -25,6 +25,112 @@
25
25
  "./shared/transactions": "./src/shared/transactions.js"
26
26
  },
27
27
  "dependencies": {
28
- "@jskit-ai/kernel": "0.1.147"
28
+ "@jskit-ai/kernel": "0.1.149"
29
+ },
30
+ "jskit": {
31
+ "kind": "runtime",
32
+ "capabilities": {
33
+ "provides": [
34
+ "runtime.database"
35
+ ],
36
+ "requires": [
37
+ "runtime.database.driver"
38
+ ]
39
+ },
40
+ "runtime": {
41
+ "server": {
42
+ "providerEntrypoint": "src/server/providers/DatabaseRuntimeServiceProvider.js",
43
+ "providers": [
44
+ {
45
+ "entrypoint": "src/server/providers/DatabaseRuntimeServiceProvider.js",
46
+ "export": "DatabaseRuntimeServiceProvider"
47
+ }
48
+ ]
49
+ },
50
+ "client": {
51
+ "providers": []
52
+ }
53
+ },
54
+ "metadata": {
55
+ "apiSummary": {
56
+ "surfaces": [
57
+ {
58
+ "subpath": "./server",
59
+ "summary": "Exports DatabaseRuntimeServiceProvider plus registerDatabaseRuntime for server container wiring."
60
+ },
61
+ {
62
+ "subpath": "./shared",
63
+ "summary": "Exports shared Knex runtime utilities (transaction manager, repository helpers, retention/json/date/dialect helpers)."
64
+ },
65
+ {
66
+ "subpath": "./client",
67
+ "summary": "Exports no runtime API today (reserved client entrypoint)."
68
+ }
69
+ ],
70
+ "containerTokens": {
71
+ "server": [
72
+ "runtime.database",
73
+ "runtime.database.driver",
74
+ "jskit.database.knex",
75
+ "jskit.database.transactionManager"
76
+ ],
77
+ "client": []
78
+ }
79
+ }
80
+ },
81
+ "ci": {
82
+ "environment": {},
83
+ "services": [],
84
+ "steps": [
85
+ {
86
+ "id": "database-migrations",
87
+ "phase": "before-verify",
88
+ "label": "Apply database migrations",
89
+ "command": "npm run db:migrate"
90
+ }
91
+ ]
92
+ },
93
+ "mutations": {
94
+ "dependencies": {
95
+ "runtime": {
96
+ "@jskit-ai/kernel": "0.1.149",
97
+ "dotenv": "^16.4.5",
98
+ "knex": "^3.1.0"
99
+ },
100
+ "dev": {}
101
+ },
102
+ "packageJson": {
103
+ "scripts": {
104
+ "db:migrations:sync": "jskit migrations sync",
105
+ "db:migrate": "npm run db:migrations:sync && knex --knexfile ./knexfile.js migrate:latest",
106
+ "db:migrate:rollback": "knex --knexfile ./knexfile.js migrate:rollback",
107
+ "db:migrate:status": "npm run db:migrations:sync && knex --knexfile ./knexfile.js migrate:list"
108
+ }
109
+ },
110
+ "procfile": {},
111
+ "files": [
112
+ {
113
+ "from": "templates/knexfile.js",
114
+ "to": "knexfile.js",
115
+ "reason": "Install root Knex configuration so app scripts can run migrations through Knex CLI.",
116
+ "category": "database-runtime",
117
+ "id": "database-runtime-knexfile"
118
+ },
119
+ {
120
+ "from": "templates/migrations/.gitkeep",
121
+ "to": "migrations/.gitkeep",
122
+ "reason": "Ensure migrations directory exists so Knex migration commands can run before any module installs migrations.",
123
+ "category": "database-runtime",
124
+ "id": "database-runtime-migrations-dir"
125
+ },
126
+ {
127
+ "from": "templates/migrations/constraints/.gitkeep",
128
+ "to": "migrations/constraints/.gitkeep",
129
+ "reason": "Ensure the ordered deferred-constraint migration directory exists.",
130
+ "category": "database-runtime",
131
+ "id": "database-runtime-constraint-migrations-dir"
132
+ }
133
+ ]
134
+ }
29
135
  }
30
136
  }
@@ -42,7 +42,7 @@ function loadKnexFactory() {
42
42
  moduleValue = symlinkSafeRequire("knex");
43
43
  } catch {
44
44
  throw new Error(
45
- "Knex package is not installed. Re-run `npx jskit update package database-runtime` to apply runtime dependencies."
45
+ "Knex package is not installed. Run `npm install`, then `npx jskit migrations sync`."
46
46
  );
47
47
  }
48
48
 
@@ -45,6 +45,155 @@ function pad(value, size = 2) {
45
45
  return String(value).padStart(size, "0");
46
46
  }
47
47
 
48
+ function requireValidDateParts(year, month, day) {
49
+ const leapYear = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
50
+ const daysByMonth = [31, leapYear ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
51
+ if (
52
+ !Number.isInteger(year) || year < 0 || year > 9999 ||
53
+ !Number.isInteger(month) || month < 1 || month > 12 ||
54
+ !Number.isInteger(day) || day < 1 || day > daysByMonth[month - 1]
55
+ ) {
56
+ throw new TypeError("Invalid date value.");
57
+ }
58
+ }
59
+
60
+ function requireValidTimeParts(hours, minutes, seconds = 0) {
61
+ if (
62
+ !Number.isInteger(hours) || hours < 0 || hours > 23 ||
63
+ !Number.isInteger(minutes) || minutes < 0 || minutes > 59 ||
64
+ !Number.isInteger(seconds) || seconds < 0 || seconds > 59
65
+ ) {
66
+ throw new TypeError("Invalid time value.");
67
+ }
68
+ }
69
+
70
+ function parseDateParts(value) {
71
+ const match = String(value || "").match(/^(\d{4})-(\d{2})-(\d{2})$/u);
72
+ if (!match) {
73
+ throw new TypeError("Invalid date value.");
74
+ }
75
+ const parts = match.slice(1).map(Number);
76
+ requireValidDateParts(parts[0], parts[1], parts[2]);
77
+ return parts;
78
+ }
79
+
80
+ function normalizeTemporalPrecision(value) {
81
+ if (value === undefined) {
82
+ return undefined;
83
+ }
84
+ if (!Number.isInteger(value) || value < 0) {
85
+ throw new TypeError("Invalid temporal precision.");
86
+ }
87
+ return value;
88
+ }
89
+
90
+ function formatFraction(milliseconds, temporalPrecision) {
91
+ const precision = normalizeTemporalPrecision(temporalPrecision);
92
+ if (precision === 0) {
93
+ return "";
94
+ }
95
+
96
+ const millisecondDigits = pad(milliseconds, 3);
97
+ if (precision === undefined || precision === 3) {
98
+ return `.${millisecondDigits}`;
99
+ }
100
+ if (precision < 3) {
101
+ const discardedUnit = 10 ** (3 - precision);
102
+ if (milliseconds % discardedUnit !== 0) {
103
+ throw new TypeError("Temporal value exceeds configured precision.");
104
+ }
105
+ return `.${millisecondDigits.slice(0, precision)}`;
106
+ }
107
+ return `.${millisecondDigits}${"0".repeat(precision - 3)}`;
108
+ }
109
+
110
+ function requireAllowedFraction(fraction, temporalPrecision) {
111
+ const precision = normalizeTemporalPrecision(temporalPrecision);
112
+ if (precision !== undefined && fraction && fraction.length - 1 > precision) {
113
+ throw new TypeError("Temporal value exceeds configured precision.");
114
+ }
115
+ }
116
+
117
+ function toJsonDate(value) {
118
+ if (value == null) {
119
+ return null;
120
+ }
121
+ if (value instanceof Date) {
122
+ const date = toDateOrThrow(value);
123
+ return `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}`;
124
+ }
125
+
126
+ const normalized = String(value).trim();
127
+ parseDateParts(normalized);
128
+ return normalized;
129
+ }
130
+
131
+ function toJsonTime(value, { temporalPrecision } = {}) {
132
+ if (value == null) {
133
+ return null;
134
+ }
135
+ if (value instanceof Date) {
136
+ const date = toDateOrThrow(value);
137
+ return [
138
+ `${pad(date.getUTCHours())}:${pad(date.getUTCMinutes())}:${pad(date.getUTCSeconds())}`,
139
+ formatFraction(date.getUTCMilliseconds(), temporalPrecision)
140
+ ].join("");
141
+ }
142
+
143
+ const normalized = String(value).trim();
144
+ const match = normalized.match(/^(\d{2}):(\d{2})(?::(\d{2})(\.\d{1,9})?)?$/u);
145
+ if (!match) {
146
+ throw new TypeError("Invalid time value.");
147
+ }
148
+ requireValidTimeParts(Number(match[1]), Number(match[2]), Number(match[3] || 0));
149
+ requireAllowedFraction(match[4], temporalPrecision);
150
+ return normalized;
151
+ }
152
+
153
+ function toJsonDateTime(value, { temporalPrecision } = {}) {
154
+ if (value == null) {
155
+ return null;
156
+ }
157
+ if (value instanceof Date) {
158
+ const date = toDateOrThrow(value);
159
+ const year = date.getUTCFullYear();
160
+ if (year < 0 || year > 9999) {
161
+ throw new TypeError("Invalid date-time value.");
162
+ }
163
+ return [
164
+ `${pad(year, 4)}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}`,
165
+ `T${pad(date.getUTCHours())}:${pad(date.getUTCMinutes())}:${pad(date.getUTCSeconds())}`,
166
+ formatFraction(date.getUTCMilliseconds(), temporalPrecision),
167
+ "Z"
168
+ ].join("");
169
+ }
170
+
171
+ const normalized = String(value).trim();
172
+ const match = normalized.match(
173
+ /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})(\.\d{1,9})?(Z|[+-]\d{2}:\d{2})?$/u
174
+ );
175
+ if (!match) {
176
+ throw new TypeError("Invalid date-time value.");
177
+ }
178
+
179
+ requireValidDateParts(Number(match[1]), Number(match[2]), Number(match[3]));
180
+ requireValidTimeParts(Number(match[4]), Number(match[5]), Number(match[6]));
181
+ requireAllowedFraction(match[7], temporalPrecision);
182
+ const offset = match[8] || "";
183
+ if (offset && offset !== "Z") {
184
+ const [offsetHours, offsetMinutes] = offset.slice(1).split(":").map(Number);
185
+ requireValidTimeParts(offsetHours, offsetMinutes, 0);
186
+ }
187
+
188
+ if (normalized.includes("T") && offset) {
189
+ return normalized;
190
+ }
191
+ if (offset) {
192
+ return `${normalized.slice(0, 10)}T${normalized.slice(11)}`;
193
+ }
194
+ return `${normalized.slice(0, 10)}T${normalized.slice(11)}Z`;
195
+ }
196
+
48
197
  function toIsoString(value) {
49
198
  return toDateOrThrow(value).toISOString();
50
199
  }
@@ -83,4 +232,13 @@ function toDatabaseDateTimeUtc(value) {
83
232
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`;
84
233
  }
85
234
 
86
- export { normalizeDateInput, toIsoString, toInsertDateTime, toNullableDateTime, toDatabaseDateTimeUtc };
235
+ export {
236
+ normalizeDateInput,
237
+ toIsoString,
238
+ toInsertDateTime,
239
+ toNullableDateTime,
240
+ toDatabaseDateTimeUtc,
241
+ toJsonDate,
242
+ toJsonTime,
243
+ toJsonDateTime
244
+ };
@@ -11,7 +11,10 @@ export {
11
11
  toIsoString,
12
12
  toInsertDateTime,
13
13
  toNullableDateTime,
14
- toDatabaseDateTimeUtc
14
+ toDatabaseDateTimeUtc,
15
+ toJsonDate,
16
+ toJsonTime,
17
+ toJsonDateTime
15
18
  } from "./dateUtils.js";
16
19
  export { normalizeDialect, detectDialectFromClient } from "./dialect.js";
17
20
  export { normalizeText, normalizeDatabaseClient, toKnexClientId } from "./databaseClient.js";
@@ -1,6 +1,12 @@
1
1
  import test from "node:test";
2
2
  import assert from "node:assert/strict";
3
- import { toIsoString, toDatabaseDateTimeUtc } from "../src/shared/dateUtils.js";
3
+ import {
4
+ toIsoString,
5
+ toDatabaseDateTimeUtc,
6
+ toJsonDate,
7
+ toJsonTime,
8
+ toJsonDateTime
9
+ } from "../src/shared/dateUtils.js";
4
10
 
5
11
  test("toIsoString normalizes valid date input", () => {
6
12
  assert.equal(toIsoString("2024-01-01T00:00:00.000Z"), "2024-01-01T00:00:00.000Z");
@@ -25,3 +31,34 @@ test("date utils throw on invalid date", () => {
25
31
  assert.throws(() => toIsoString("2024-01-01T00:00:00"), /Invalid date value\./);
26
32
  assert.throws(() => toDatabaseDateTimeUtc("not-a-date"), /Invalid date value\./);
27
33
  });
34
+
35
+ test("JSON temporal serializers produce json-rest-schema 1.0.17 string shapes", () => {
36
+ assert.equal(toJsonDate("2026-08-13"), "2026-08-13");
37
+ assert.equal(toJsonDate(new Date("2026-08-13T23:59:58.123Z")), "2026-08-13");
38
+ assert.equal(toJsonTime("07:08"), "07:08");
39
+ assert.equal(toJsonTime("07:08:09.123456"), "07:08:09.123456");
40
+ assert.equal(toJsonTime(new Date("2026-08-13T07:08:09.000Z"), { temporalPrecision: 0 }), "07:08:09");
41
+ assert.equal(toJsonDateTime("2026-08-13 07:08:09.123456"), "2026-08-13T07:08:09.123456Z");
42
+ assert.equal(toJsonDateTime("2026-08-13T07:08:09+08:00"), "2026-08-13T07:08:09+08:00");
43
+ assert.equal(toJsonDateTime(new Date("2026-08-13T07:08:09.123Z")), "2026-08-13T07:08:09.123Z");
44
+ assert.equal(
45
+ toJsonDateTime(new Date("2026-08-13T07:08:09.123Z"), { temporalPrecision: 6 }),
46
+ "2026-08-13T07:08:09.123000Z"
47
+ );
48
+ assert.equal(toJsonDateTime(null), null);
49
+ });
50
+
51
+ test("JSON temporal serializers reject invalid or ambiguous values", () => {
52
+ assert.throws(() => toJsonDate("2026-02-30"), /Invalid date value/);
53
+ assert.throws(() => toJsonTime("25:00"), /Invalid time value/);
54
+ assert.throws(() => toJsonDateTime("2026-08-13T07:08"), /Invalid date-time value/);
55
+ assert.throws(() => toJsonDateTime("2026-08-13 07:08:09+25:00"), /Invalid time value/);
56
+ assert.throws(
57
+ () => toJsonDateTime("2026-08-13T07:08:09.123Z", { temporalPrecision: 2 }),
58
+ /exceeds configured precision/
59
+ );
60
+ assert.throws(
61
+ () => toJsonTime(new Date("2026-08-13T07:08:09.123Z"), { temporalPrecision: 2 }),
62
+ /exceeds configured precision/
63
+ );
64
+ });
@@ -3,14 +3,16 @@ import { readFile } from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  import test from "node:test";
5
5
  import { fileURLToPath } from "node:url";
6
- import descriptor from "../package.descriptor.mjs";
6
+ import packageJson from "../package.json" with { type: "json" };
7
+
8
+ const packageMetadata = packageJson.jskit;
7
9
 
8
10
  const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
9
11
 
10
12
  test("database-runtime db migrate scripts sync JSKIT-managed migrations before Knex reads them", () => {
11
- const scripts = descriptor.mutations.packageJson.scripts;
13
+ const scripts = packageMetadata.mutations.packageJson.scripts;
12
14
 
13
- assert.equal(scripts["db:migrations:sync"], "jskit migrations changed");
15
+ assert.equal(scripts["db:migrations:sync"], "jskit migrations sync");
14
16
  assert.equal(
15
17
  scripts["db:migrate"],
16
18
  "npm run db:migrations:sync && knex --knexfile ./knexfile.js migrate:latest"
@@ -23,7 +25,7 @@ test("database-runtime db migrate scripts sync JSKIT-managed migrations before K
23
25
  scripts["db:migrate:rollback"],
24
26
  "knex --knexfile ./knexfile.js migrate:rollback"
25
27
  );
26
- assert.deepEqual(descriptor.ci.steps, [
28
+ assert.deepEqual(packageMetadata.ci.steps, [
27
29
  {
28
30
  id: "database-migrations",
29
31
  phase: "before-verify",
@@ -35,7 +37,7 @@ test("database-runtime db migrate scripts sync JSKIT-managed migrations before K
35
37
 
36
38
  test("database-runtime runs deferred constraints after all ordinary migrations", async () => {
37
39
  const knexfile = await readFile(path.join(PACKAGE_ROOT, "templates/knexfile.js"), "utf8");
38
- const deferredDirectoryMutation = descriptor.mutations.files.find(
40
+ const deferredDirectoryMutation = packageMetadata.mutations.files.find(
39
41
  (mutation) => mutation.id === "database-runtime-constraint-migrations-dir"
40
42
  );
41
43
 
@@ -1,112 +0,0 @@
1
- export default Object.freeze({
2
- packageVersion: 1,
3
- packageId: "@jskit-ai/database-runtime",
4
- version: "0.1.147",
5
- kind: "runtime",
6
- dependsOn: [
7
- "@jskit-ai/kernel"
8
- ],
9
- capabilities: {
10
- provides: [
11
- "runtime.database"
12
- ],
13
- requires: [
14
- "runtime.database.driver"
15
- ]
16
- },
17
- runtime: {
18
- server: {
19
- providerEntrypoint: "src/server/providers/DatabaseRuntimeServiceProvider.js",
20
- providers: [
21
- {
22
- entrypoint: "src/server/providers/DatabaseRuntimeServiceProvider.js",
23
- export: "DatabaseRuntimeServiceProvider"
24
- }
25
- ]
26
- },
27
- client: {
28
- providers: []
29
- }
30
- },
31
- metadata: {
32
- apiSummary: {
33
- surfaces: [
34
- {
35
- subpath: "./server",
36
- summary: "Exports DatabaseRuntimeServiceProvider plus registerDatabaseRuntime for server container wiring."
37
- },
38
- {
39
- subpath: "./shared",
40
- summary: "Exports shared Knex runtime utilities (transaction manager, repository helpers, retention/json/date/dialect helpers)."
41
- },
42
- {
43
- subpath: "./client",
44
- summary: "Exports no runtime API today (reserved client entrypoint)."
45
- }
46
- ],
47
- containerTokens: {
48
- server: [
49
- "runtime.database",
50
- "runtime.database.driver",
51
- "jskit.database.knex",
52
- "jskit.database.transactionManager"
53
- ],
54
- client: []
55
- }
56
- }
57
- },
58
- ci: {
59
- environment: {},
60
- services: [],
61
- steps: [
62
- {
63
- id: "database-migrations",
64
- phase: "before-verify",
65
- label: "Apply database migrations",
66
- command: "npm run db:migrate"
67
- }
68
- ]
69
- },
70
- mutations: {
71
- dependencies: {
72
- runtime: {
73
- "@jskit-ai/kernel": "0.1.147",
74
- "dotenv": "^16.4.5",
75
- "knex": "^3.1.0"
76
- },
77
- dev: {}
78
- },
79
- packageJson: {
80
- scripts: {
81
- "db:migrations:sync": "jskit migrations changed",
82
- "db:migrate": "npm run db:migrations:sync && knex --knexfile ./knexfile.js migrate:latest",
83
- "db:migrate:rollback": "knex --knexfile ./knexfile.js migrate:rollback",
84
- "db:migrate:status": "npm run db:migrations:sync && knex --knexfile ./knexfile.js migrate:list"
85
- }
86
- },
87
- procfile: {},
88
- files: [
89
- {
90
- from: "templates/knexfile.js",
91
- to: "knexfile.js",
92
- reason: "Install root Knex configuration so app scripts can run migrations through Knex CLI.",
93
- category: "database-runtime",
94
- id: "database-runtime-knexfile"
95
- },
96
- {
97
- from: "templates/migrations/.gitkeep",
98
- to: "migrations/.gitkeep",
99
- reason: "Ensure migrations directory exists so Knex migration commands can run before any module installs migrations.",
100
- category: "database-runtime",
101
- id: "database-runtime-migrations-dir"
102
- },
103
- {
104
- from: "templates/migrations/constraints/.gitkeep",
105
- to: "migrations/constraints/.gitkeep",
106
- reason: "Ensure the ordered deferred-constraint migration directory exists.",
107
- category: "database-runtime",
108
- id: "database-runtime-constraint-migrations-dir"
109
- }
110
- ]
111
- }
112
- });