@jskit-ai/database-runtime 0.1.145 → 0.1.147

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.145",
4
+ version: "0.1.147",
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.145",
3
+ "version": "0.1.147",
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
  }
@@ -131,7 +131,8 @@ function resolveKnexConnectionFromEnvironment(
131
131
  return {
132
132
  ...connection,
133
133
  supportBigNumbers: true,
134
- bigNumberStrings: true
134
+ bigNumberStrings: true,
135
+ dateStrings: ["DATE"]
135
136
  };
136
137
  }
137
138
 
@@ -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
 
@@ -3,7 +3,8 @@ import test from "node:test";
3
3
  import {
4
4
  parseDatabaseUrl,
5
5
  resolveDatabaseClientFromEnvironment,
6
- resolveDatabaseConnectionFromEnvironment
6
+ resolveDatabaseConnectionFromEnvironment,
7
+ resolveKnexConnectionFromEnvironment
7
8
  } from "../src/shared/databaseConnection.js";
8
9
 
9
10
  test("parseDatabaseUrl parses mysql url fields", () => {
@@ -71,3 +72,32 @@ test("resolveDatabaseConnectionFromEnvironment returns mutable connection fields
71
72
  assert.equal(descriptor?.configurable, true);
72
73
  assert.equal(descriptor?.writable, true);
73
74
  });
75
+
76
+ test("resolveKnexConnectionFromEnvironment keeps only MySQL DATE columns as strings", () => {
77
+ const mysqlConnection = resolveKnexConnectionFromEnvironment({
78
+ DB_CLIENT: "mysql2",
79
+ DB_HOST: "db.local",
80
+ DB_NAME: "appdb",
81
+ DB_USER: "appuser",
82
+ DB_PASSWORD: "apppass"
83
+ }, {
84
+ client: "mysql2"
85
+ });
86
+
87
+ assert.deepEqual(mysqlConnection.dateStrings, ["DATE"]);
88
+ assert.equal(mysqlConnection.supportBigNumbers, true);
89
+ assert.equal(mysqlConnection.bigNumberStrings, true);
90
+
91
+ const postgresConnection = resolveKnexConnectionFromEnvironment({
92
+ DB_CLIENT: "pg",
93
+ DB_HOST: "db.local",
94
+ DB_NAME: "appdb",
95
+ DB_USER: "appuser",
96
+ DB_PASSWORD: "apppass"
97
+ }, {
98
+ client: "pg",
99
+ defaultPort: 5432
100
+ });
101
+
102
+ assert.equal(postgresConnection.dateStrings, undefined);
103
+ });
@@ -4,6 +4,7 @@ import { toIsoString, toDatabaseDateTimeUtc } from "../src/shared/dateUtils.js";
4
4
 
5
5
  test("toIsoString normalizes valid date input", () => {
6
6
  assert.equal(toIsoString("2024-01-01T00:00:00.000Z"), "2024-01-01T00:00:00.000Z");
7
+ assert.equal(toIsoString("2024-01-01 01:02:03.045"), "2024-01-01T01:02:03.045Z");
7
8
  });
8
9
 
9
10
  test("toDatabaseDateTimeUtc formats DATETIME(3) UTC string", () => {
@@ -20,5 +21,7 @@ test("toDatabaseDateTimeUtc preserves nullable empty values", () => {
20
21
 
21
22
  test("date utils throw on invalid date", () => {
22
23
  assert.throws(() => toIsoString("not-a-date"), /Invalid date value\./);
24
+ assert.throws(() => toIsoString("January 1, 2024"), /Invalid date value\./);
25
+ assert.throws(() => toIsoString("2024-01-01T00:00:00"), /Invalid date value\./);
23
26
  assert.throws(() => toDatabaseDateTimeUtc("not-a-date"), /Invalid date value\./);
24
27
  });
@@ -178,6 +178,7 @@ test("DatabaseRuntimeServiceProvider resolves knex from app root package context
178
178
  assert.equal(knex.__config.connection.database, "appdb");
179
179
  assert.equal(knex.__config.connection.user, "appuser");
180
180
  assert.equal(knex.__config.connection.password, "apppass");
181
+ assert.deepEqual(knex.__config.connection.dateStrings, ["DATE"]);
181
182
  });
182
183
  });
183
184