@jskit-ai/database-runtime 0.1.146 → 0.1.148

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.
@@ -1,7 +1,7 @@
1
1
  export default Object.freeze({
2
2
  packageVersion: 1,
3
3
  packageId: "@jskit-ai/database-runtime",
4
- version: "0.1.146",
4
+ version: "0.1.148",
5
5
  kind: "runtime",
6
6
  dependsOn: [
7
7
  "@jskit-ai/kernel"
@@ -70,7 +70,7 @@ export default Object.freeze({
70
70
  mutations: {
71
71
  dependencies: {
72
72
  runtime: {
73
- "@jskit-ai/kernel": "0.1.146",
73
+ "@jskit-ai/kernel": "0.1.147",
74
74
  "dotenv": "^16.4.5",
75
75
  "knex": "^3.1.0"
76
76
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/database-runtime",
3
- "version": "0.1.146",
3
+ "version": "0.1.148",
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.146"
28
+ "@jskit-ai/kernel": "0.1.147"
29
29
  }
30
30
  }
@@ -1,9 +1,30 @@
1
+ const DATABASE_UTC_DATE_TIME_PATTERN = /^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}(?:\.\d+)?)$/u;
2
+ const RFC_3339_DATE_TIME_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/u;
3
+
1
4
  function normalizeDateInput(value) {
2
5
  if (!value) {
3
6
  return null;
4
7
  }
5
8
 
6
- const date = value instanceof Date ? value : new Date(value);
9
+ let date;
10
+ if (value instanceof Date) {
11
+ date = value;
12
+ } else if (typeof value === "string") {
13
+ const normalized = value.trim();
14
+ const databaseMatch = DATABASE_UTC_DATE_TIME_PATTERN.exec(normalized);
15
+ const dateTime = databaseMatch
16
+ ? `${databaseMatch[1]}T${databaseMatch[2]}Z`
17
+ : RFC_3339_DATE_TIME_PATTERN.test(normalized)
18
+ ? normalized
19
+ : "";
20
+ if (!dateTime) {
21
+ return null;
22
+ }
23
+ date = new Date(dateTime);
24
+ } else {
25
+ return null;
26
+ }
27
+
7
28
  if (Number.isNaN(date.getTime())) {
8
29
  return null;
9
30
  }
@@ -12,8 +33,8 @@ function normalizeDateInput(value) {
12
33
  }
13
34
 
14
35
  function toDateOrThrow(value) {
15
- const date = value instanceof Date ? value : new Date(value);
16
- if (Number.isNaN(date.getTime())) {
36
+ const date = normalizeDateInput(value);
37
+ if (!date) {
17
38
  throw new TypeError("Invalid date value.");
18
39
  }
19
40
 
@@ -24,6 +45,155 @@ function pad(value, size = 2) {
24
45
  return String(value).padStart(size, "0");
25
46
  }
26
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
+
27
197
  function toIsoString(value) {
28
198
  return toDateOrThrow(value).toISOString();
29
199
  }
@@ -62,4 +232,13 @@ function toDatabaseDateTimeUtc(value) {
62
232
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`;
63
233
  }
64
234
 
65
- 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,9 +1,16 @@
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");
13
+ assert.equal(toIsoString("2024-01-01 01:02:03.045"), "2024-01-01T01:02:03.045Z");
7
14
  });
8
15
 
9
16
  test("toDatabaseDateTimeUtc formats DATETIME(3) UTC string", () => {
@@ -20,5 +27,38 @@ test("toDatabaseDateTimeUtc preserves nullable empty values", () => {
20
27
 
21
28
  test("date utils throw on invalid date", () => {
22
29
  assert.throws(() => toIsoString("not-a-date"), /Invalid date value\./);
30
+ assert.throws(() => toIsoString("January 1, 2024"), /Invalid date value\./);
31
+ assert.throws(() => toIsoString("2024-01-01T00:00:00"), /Invalid date value\./);
23
32
  assert.throws(() => toDatabaseDateTimeUtc("not-a-date"), /Invalid date value\./);
24
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
+ });