@bendyline/gezel-script-stdlib 1.1.0 → 1.1.2
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 +2 -2
- package/scripts/storeRecords.ts +27 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bendyline/gezel-script-stdlib",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Standard gate-script library packed into the app. Plain defineScript sources resolved in place by the script runner (scope: 'standard'); read-only, trusted, versioned with the app.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"!scripts/**/*.test.ts"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@bendyline/gezel-sdk": "1.1.
|
|
44
|
+
"@bendyline/gezel-sdk": "1.1.2"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/node": "^24.13.3",
|
package/scripts/storeRecords.ts
CHANGED
|
@@ -73,6 +73,12 @@ export const meta = defineScript({
|
|
|
73
73
|
itemType: 'object',
|
|
74
74
|
},
|
|
75
75
|
total: { type: 'number', description: 'Record count (list).', nullable: true },
|
|
76
|
+
file: {
|
|
77
|
+
type: 'string',
|
|
78
|
+
description:
|
|
79
|
+
"Workspace file the action read or wrote: <root>.json in single-file mode; with one folder per record, that record's record.json (null for delete and list).",
|
|
80
|
+
nullable: true,
|
|
81
|
+
},
|
|
76
82
|
},
|
|
77
83
|
requires: ['workspace.read', 'workspace.write'],
|
|
78
84
|
} as const);
|
|
@@ -87,15 +93,24 @@ function fail(message: string): never {
|
|
|
87
93
|
throw new Error(message);
|
|
88
94
|
}
|
|
89
95
|
|
|
90
|
-
|
|
96
|
+
// A single-file store named by its file (`repairs.json`) is the same store as
|
|
97
|
+
// its root (`repairs`); appending the extension again made `repairs.json.json`
|
|
98
|
+
// and sent a model hunting for the record it had just written.
|
|
99
|
+
const givenRoot = String(input.root ?? '')
|
|
91
100
|
.replace(/\\+/g, '/')
|
|
92
101
|
.replace(/\/+$/, '');
|
|
102
|
+
const root = input.mode === 'single-file' ? givenRoot.replace(/\.json$/i, '') : givenRoot;
|
|
93
103
|
if (!root) fail('root must be a non-empty workspace-relative path.');
|
|
94
104
|
|
|
95
105
|
const mode = input.mode;
|
|
96
106
|
const storeFile = `${root}.json`;
|
|
97
107
|
const indexFile = `${root}/index.json`;
|
|
98
108
|
|
|
109
|
+
/** Where one record lives, so a caller never has to guess the layout. */
|
|
110
|
+
function fileFor(id: string): string {
|
|
111
|
+
return mode === 'single-file' ? storeFile : recordFile(id);
|
|
112
|
+
}
|
|
113
|
+
|
|
99
114
|
function recordFile(id: string): string {
|
|
100
115
|
return `${root}/${id}/record.json`;
|
|
101
116
|
}
|
|
@@ -278,17 +293,18 @@ async function create(): Promise<Fields> {
|
|
|
278
293
|
const id = input.id !== undefined ? requireSuppliedId(input.id) : generatedId(fields);
|
|
279
294
|
if (mode === 'folder-per-record') {
|
|
280
295
|
if ((await readJsonIfPresent(recordFile(id))) !== null) {
|
|
281
|
-
fail(`Record '${id}' already exists in ${root}.`);
|
|
296
|
+
fail(`Record '${id}' already exists in ${root}. Use action "update" to change it.`);
|
|
282
297
|
}
|
|
283
298
|
await writeJson(recordFile(id), { version: 1, id, ...fields });
|
|
284
299
|
await regenerateFolderIndex();
|
|
285
300
|
} else {
|
|
286
301
|
const records = await loadSingleFile();
|
|
287
|
-
if (Object.hasOwn(records, id))
|
|
302
|
+
if (Object.hasOwn(records, id))
|
|
303
|
+
fail(`Record '${id}' already exists in ${storeFile}. Use action "update" to change it.`);
|
|
288
304
|
records[id] = fields;
|
|
289
305
|
await saveSingleFile(records);
|
|
290
306
|
}
|
|
291
|
-
return { ok: true, action: 'create', id, record: { id, ...fields } };
|
|
307
|
+
return { ok: true, action: 'create', id, record: { id, ...fields }, file: fileFor(id) };
|
|
292
308
|
}
|
|
293
309
|
|
|
294
310
|
async function get(): Promise<Fields> {
|
|
@@ -296,11 +312,11 @@ async function get(): Promise<Fields> {
|
|
|
296
312
|
if (mode === 'folder-per-record') {
|
|
297
313
|
const file = await readJsonIfPresent(recordFile(id));
|
|
298
314
|
if (file === null) noRecord(id, await folderIds());
|
|
299
|
-
return { ok: true, action: 'get', id, record: recordFromFile(id, file) };
|
|
315
|
+
return { ok: true, action: 'get', id, record: recordFromFile(id, file), file: fileFor(id) };
|
|
300
316
|
}
|
|
301
317
|
const records = await loadSingleFile();
|
|
302
318
|
if (!Object.hasOwn(records, id)) noRecord(id, Object.keys(records).sort());
|
|
303
|
-
return { ok: true, action: 'get', id, record: { id, ...bodyOf(records, id) } };
|
|
319
|
+
return { ok: true, action: 'get', id, record: { id, ...bodyOf(records, id) }, file: storeFile };
|
|
304
320
|
}
|
|
305
321
|
|
|
306
322
|
async function update(): Promise<Fields> {
|
|
@@ -312,14 +328,14 @@ async function update(): Promise<Fields> {
|
|
|
312
328
|
const merged = { ...recordFromFile(id, file), ...patch, id };
|
|
313
329
|
await writeJson(recordFile(id), { version: 1, ...merged });
|
|
314
330
|
await regenerateFolderIndex();
|
|
315
|
-
return { ok: true, action: 'update', id, record: merged };
|
|
331
|
+
return { ok: true, action: 'update', id, record: merged, file: fileFor(id) };
|
|
316
332
|
}
|
|
317
333
|
const records = await loadSingleFile();
|
|
318
334
|
if (!Object.hasOwn(records, id)) noRecord(id, Object.keys(records).sort());
|
|
319
335
|
const merged = { ...bodyOf(records, id), ...patch };
|
|
320
336
|
records[id] = merged;
|
|
321
337
|
await saveSingleFile(records);
|
|
322
|
-
return { ok: true, action: 'update', id, record: { id, ...merged } };
|
|
338
|
+
return { ok: true, action: 'update', id, record: { id, ...merged }, file: storeFile };
|
|
323
339
|
}
|
|
324
340
|
|
|
325
341
|
async function remove(): Promise<Fields> {
|
|
@@ -351,7 +367,7 @@ async function remove(): Promise<Fields> {
|
|
|
351
367
|
if (!Object.hasOwn(records, id)) noRecord(id, Object.keys(records).sort());
|
|
352
368
|
delete records[id];
|
|
353
369
|
await saveSingleFile(records);
|
|
354
|
-
return { ok: true, action: 'delete', id };
|
|
370
|
+
return { ok: true, action: 'delete', id, file: storeFile };
|
|
355
371
|
}
|
|
356
372
|
|
|
357
373
|
async function list(): Promise<Fields> {
|
|
@@ -363,7 +379,7 @@ async function list(): Promise<Fields> {
|
|
|
363
379
|
const out = Object.keys(records)
|
|
364
380
|
.sort()
|
|
365
381
|
.map((id) => ({ id, ...bodyOf(records, id) }));
|
|
366
|
-
return { ok: true, action: 'list', records: out, total: out.length };
|
|
382
|
+
return { ok: true, action: 'list', records: out, total: out.length, file: storeFile };
|
|
367
383
|
}
|
|
368
384
|
|
|
369
385
|
async function run(): Promise<Fields> {
|
|
@@ -385,4 +401,4 @@ async function run(): Promise<Fields> {
|
|
|
385
401
|
|
|
386
402
|
// Every declared output is stamped on every action, including inapplicable fields.
|
|
387
403
|
// The shared runner validates the complete contract before accepting a run.
|
|
388
|
-
gezel.output({ id: null, record: null, records: [], total: null, ...(await run()) });
|
|
404
|
+
gezel.output({ id: null, record: null, records: [], total: null, file: null, ...(await run()) });
|