@jskit-ai/database-runtime 0.1.246 → 0.1.248

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.246",
3
+ "version": "0.1.248",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "node --test --test-concurrency=1"
@@ -67,6 +67,6 @@
67
67
  }
68
68
  },
69
69
  "peerDependencies": {
70
- "@jskit-ai/kernel": "0.1.246"
70
+ "@jskit-ai/kernel": "0.1.248"
71
71
  }
72
72
  }
@@ -228,6 +228,7 @@ function toDatabaseDateTimeUtc(value) {
228
228
  }
229
229
 
230
230
  export {
231
+ normalizeDateInput,
231
232
  toIsoString,
232
233
  toInsertDateTime,
233
234
  toDatabaseDateTimeUtc,
@@ -1,4 +1,5 @@
1
1
  export {
2
+ normalizeDateInput,
2
3
  toIsoString,
3
4
  toInsertDateTime,
4
5
  toDatabaseDateTimeUtc,
@@ -19,7 +20,9 @@ export {
19
20
  normalizeNullableString,
20
21
  normalizeDbRecordId,
21
22
  resolveInsertedRecordId,
23
+ parseJsonValue,
22
24
  toDbJson,
23
25
  runInTransaction,
24
- createWithTransaction
26
+ createWithTransaction,
27
+ lockScopedRecordId
25
28
  } from "./repositoryOptions.js";
@@ -106,6 +106,41 @@ function resolveInsertedRecordId(insertResult, { fallback = null } = {}) {
106
106
  return normalizeDbRecordId(insertResult, { fallback });
107
107
  }
108
108
 
109
+ function parseJsonValue(value, fallback = null, options = {}) {
110
+ const source = options && typeof options === "object" ? options : {};
111
+ const effectiveFallback = Object.hasOwn(source, "fallback") ? source.fallback : fallback;
112
+ const allowNull = source.allowNull === true;
113
+ const allowObject = source.allowObject !== false;
114
+ const trim = source.trim !== false;
115
+
116
+ if (value == null) {
117
+ return effectiveFallback;
118
+ }
119
+
120
+ if (allowObject && typeof value === "object") {
121
+ return value;
122
+ }
123
+
124
+ let text = String(value || "");
125
+ if (trim) {
126
+ text = text.trim();
127
+ }
128
+
129
+ if (!text) {
130
+ return effectiveFallback;
131
+ }
132
+
133
+ try {
134
+ const parsed = JSON.parse(text);
135
+ if (parsed == null && !allowNull) {
136
+ return effectiveFallback;
137
+ }
138
+ return parsed;
139
+ } catch {
140
+ return effectiveFallback;
141
+ }
142
+ }
143
+
109
144
  function toDbJson(value) {
110
145
  if (value == null) {
111
146
  return JSON.stringify({});
@@ -136,6 +171,42 @@ function createWithTransaction(knex) {
136
171
  };
137
172
  }
138
173
 
174
+ function requireSqlIdentifier(value, label) {
175
+ const identifier = String(value || "").trim();
176
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(identifier)) {
177
+ throw new TypeError(`${label} must be a safe SQL identifier.`);
178
+ }
179
+ return identifier;
180
+ }
181
+
182
+ async function lockScopedRecordId({
183
+ trx,
184
+ tableName,
185
+ recordId,
186
+ scopeId,
187
+ idColumn = "id",
188
+ scopeColumn = "workspace_id"
189
+ } = {}) {
190
+ if (typeof trx !== "function") {
191
+ throw new TypeError("lockScopedRecordId requires a transaction.");
192
+ }
193
+ const table = requireSqlIdentifier(tableName, "lockScopedRecordId tableName");
194
+ const recordKey = requireSqlIdentifier(idColumn, "lockScopedRecordId idColumn");
195
+ const scopeKey = requireSqlIdentifier(scopeColumn, "lockScopedRecordId scopeColumn");
196
+ const normalizedRecordId = normalizeDbRecordId(recordId);
197
+ const normalizedScopeId = normalizeDbRecordId(scopeId);
198
+ if (!normalizedRecordId || !normalizedScopeId) {
199
+ return null;
200
+ }
201
+ const row = await trx(table)
202
+ .select(recordKey)
203
+ .where(recordKey, normalizedRecordId)
204
+ .andWhere(scopeKey, normalizedScopeId)
205
+ .forUpdate()
206
+ .first();
207
+ return normalizeDbRecordId(row?.[recordKey]);
208
+ }
209
+
139
210
  export {
140
211
  resolveQueryOptions,
141
212
  resolveRepoClient,
@@ -145,7 +216,9 @@ export {
145
216
  normalizeNullableString,
146
217
  normalizeDbRecordId,
147
218
  resolveInsertedRecordId,
219
+ parseJsonValue,
148
220
  toDbJson,
149
221
  runInTransaction,
150
- createWithTransaction
222
+ createWithTransaction,
223
+ lockScopedRecordId
151
224
  };
@@ -1,10 +1,28 @@
1
1
  import test from "node:test";
2
2
  import assert from "node:assert/strict";
3
3
  import {
4
+ lockScopedRecordId,
4
5
  normalizeDbRecordId,
6
+ parseJsonValue,
5
7
  resolveInsertedRecordId
6
8
  } from "../src/shared/repositoryOptions.js";
7
9
 
10
+ function fakeLockTransaction(row = null) {
11
+ const calls = [];
12
+ const query = {
13
+ select(value) { calls.push(["select", value]); return this; },
14
+ where(key, value) { calls.push(["where", key, value]); return this; },
15
+ andWhere(key, value) { calls.push(["andWhere", key, value]); return this; },
16
+ forUpdate() { calls.push(["forUpdate"]); return this; },
17
+ async first() { calls.push(["first"]); return row; }
18
+ };
19
+ const trx = (table) => {
20
+ calls.push(["table", table]);
21
+ return query;
22
+ };
23
+ return { calls, trx };
24
+ }
25
+
8
26
  test("normalizeDbRecordId preserves canonical DB ids and rejects unsafe JS numbers", () => {
9
27
  const unsafeNumericId = Number(9007199254740993n);
10
28
  assert.equal(normalizeDbRecordId("9007199254740993"), "9007199254740993");
@@ -19,3 +37,43 @@ test("resolveInsertedRecordId normalizes insert ids without accepting unsafe JS
19
37
  assert.equal(resolveInsertedRecordId([42]), "42");
20
38
  assert.equal(resolveInsertedRecordId([unsafeNumericId]), null);
21
39
  });
40
+
41
+ test("parseJsonValue parses stored JSON and falls back for empty, null or invalid text", () => {
42
+ assert.deepEqual(parseJsonValue(" [1,2] "), [1, 2]);
43
+ assert.deepEqual(parseJsonValue({ source: "console" }), { source: "console" });
44
+ assert.equal(parseJsonValue("", "fallback"), "fallback");
45
+ assert.equal(parseJsonValue("{", "fallback"), "fallback");
46
+ assert.equal(parseJsonValue("null", "fallback"), "fallback");
47
+ assert.equal(parseJsonValue("null", "fallback", { allowNull: true }), null);
48
+ assert.deepEqual(parseJsonValue(undefined, null, { fallback: [] }), []);
49
+ });
50
+
51
+ test("lockScopedRecordId locks an exact record inside its owner scope", async () => {
52
+ const { calls, trx } = fakeLockTransaction({ id: "42" });
53
+ assert.equal(await lockScopedRecordId({
54
+ trx,
55
+ tableName: "daycare_events",
56
+ recordId: 42,
57
+ scopeId: 7
58
+ }), "42");
59
+ assert.deepEqual(calls, [
60
+ ["table", "daycare_events"],
61
+ ["select", "id"],
62
+ ["where", "id", "42"],
63
+ ["andWhere", "workspace_id", "7"],
64
+ ["forUpdate"],
65
+ ["first"]
66
+ ]);
67
+ });
68
+
69
+ test("lockScopedRecordId rejects missing transactions and unsafe identifiers", async () => {
70
+ await assert.rejects(
71
+ () => lockScopedRecordId({ tableName: "records", recordId: 1, scopeId: 1 }),
72
+ /requires a transaction/u
73
+ );
74
+ const { trx } = fakeLockTransaction();
75
+ await assert.rejects(
76
+ () => lockScopedRecordId({ trx, tableName: "records; drop table", recordId: 1, scopeId: 1 }),
77
+ /safe SQL identifier/u
78
+ );
79
+ });