@jskit-ai/crud-core 0.1.193 → 0.1.195
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/crud-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.195",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test"
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
"./server/routeContracts": "./src/server/routeContracts.js"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@jskit-ai/database-runtime": "0.1.
|
|
27
|
-
"@jskit-ai/json-rest-api-core": "0.1.
|
|
28
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
26
|
+
"@jskit-ai/database-runtime": "0.1.184",
|
|
27
|
+
"@jskit-ai/json-rest-api-core": "0.1.128",
|
|
28
|
+
"@jskit-ai/resource-crud-core": "0.1.126",
|
|
29
29
|
"json-rest-schema": "^1.0.17"
|
|
30
30
|
},
|
|
31
31
|
"description": "Shared server-side CRUD service, repository, route, and query helpers.",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
}
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"@jskit-ai/http-runtime": "0.1.
|
|
49
|
-
"@jskit-ai/kernel": "0.1.
|
|
48
|
+
"@jskit-ai/http-runtime": "0.1.182",
|
|
49
|
+
"@jskit-ai/kernel": "0.1.184"
|
|
50
50
|
}
|
|
51
51
|
}
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
"./shared": "./src/shared/index.js"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@jskit-ai/crud-core": "0.1.
|
|
12
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
11
|
+
"@jskit-ai/crud-core": "0.1.195",
|
|
12
|
+
"@jskit-ai/resource-crud-core": "0.1.126"
|
|
13
13
|
},
|
|
14
14
|
"jskit": {
|
|
15
15
|
"kind": "runtime",
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import knexLib from "knex";
|
|
4
|
+
import { createServiceToolCatalog } from "@jskit-ai/assistant-core/server";
|
|
5
|
+
import { createActionCatalogue } from "@jskit-ai/kernel/server/actions";
|
|
6
|
+
import {
|
|
7
|
+
addResourceIfMissing,
|
|
8
|
+
createJsonRestApiHost,
|
|
9
|
+
createJsonRestContext,
|
|
10
|
+
createJsonRestResourceScopeOptions
|
|
11
|
+
} from "@jskit-ai/json-rest-api-core/server/jsonRestApiHost";
|
|
12
|
+
import { defineCrudResource } from "@jskit-ai/resource-crud-core/shared/crudResource";
|
|
13
|
+
import { createCrudJsonApiActions } from "../src/server/jsonApiModule/actions.js";
|
|
14
|
+
import { createCrudJsonApiRepository } from "../src/server/jsonApiModule/repository.js";
|
|
15
|
+
import { createCrudJsonApiService } from "../src/server/jsonApiModule/service.js";
|
|
16
|
+
|
|
17
|
+
test("assistant list traverses real SQL pages with sparse fields and capped limits", async (t) => {
|
|
18
|
+
const knex = knexLib({
|
|
19
|
+
client: "better-sqlite3",
|
|
20
|
+
connection: { filename: ":memory:" },
|
|
21
|
+
useNullAsDefault: true
|
|
22
|
+
});
|
|
23
|
+
t.after(() => knex.destroy());
|
|
24
|
+
await knex.schema.createTable("books", (table) => {
|
|
25
|
+
table.integer("id").primary();
|
|
26
|
+
table.integer("workspace_id").notNullable();
|
|
27
|
+
table.string("name").notNullable();
|
|
28
|
+
table.dateTime("created_at").notNullable();
|
|
29
|
+
});
|
|
30
|
+
const rows = Array.from({ length: 410 }, (_, index) => ({
|
|
31
|
+
id: index + 1,
|
|
32
|
+
workspace_id: index % 2 + 1,
|
|
33
|
+
name: `Book ${index + 1}`,
|
|
34
|
+
created_at: new Date(Date.UTC(2026, 8, 1, 0, 0, Math.floor(index / 6)))
|
|
35
|
+
}));
|
|
36
|
+
await knex.batchInsert("books", rows, 100);
|
|
37
|
+
|
|
38
|
+
const resource = defineCrudResource({
|
|
39
|
+
namespace: "books",
|
|
40
|
+
tableName: "books",
|
|
41
|
+
autofilter: "workspace",
|
|
42
|
+
defaultSort: ["-createdAt"],
|
|
43
|
+
crudOperations: ["list"],
|
|
44
|
+
schema: {
|
|
45
|
+
id: { type: "id", primary: true },
|
|
46
|
+
workspaceId: { type: "id", column: "workspace_id" },
|
|
47
|
+
name: {
|
|
48
|
+
type: "string",
|
|
49
|
+
operations: { output: { required: true } }
|
|
50
|
+
},
|
|
51
|
+
createdAt: { type: "dateTime", temporalPrecision: 3, column: "created_at" }
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
const api = await createJsonRestApiHost({ knex });
|
|
55
|
+
await addResourceIfMissing(api, "books", createJsonRestResourceScopeOptions(resource));
|
|
56
|
+
const repository = createCrudJsonApiRepository({ api, knex, resource, resourceScopeName: "books" });
|
|
57
|
+
const service = createCrudJsonApiService({ repository });
|
|
58
|
+
const actions = createActionCatalogue();
|
|
59
|
+
actions.register({
|
|
60
|
+
contributorId: "test.books",
|
|
61
|
+
domain: "books",
|
|
62
|
+
actions: createCrudJsonApiActions({
|
|
63
|
+
namespace: "books",
|
|
64
|
+
resource,
|
|
65
|
+
service,
|
|
66
|
+
surface: "admin",
|
|
67
|
+
operations: ["list"],
|
|
68
|
+
permissionForOperation: () => ({ require: "all", permissions: ["books.list"] })
|
|
69
|
+
})
|
|
70
|
+
});
|
|
71
|
+
const catalog = createServiceToolCatalog(actions, { maxDirectTools: 0 });
|
|
72
|
+
const context = {
|
|
73
|
+
actor: { id: "7" },
|
|
74
|
+
permissions: ["books.list"],
|
|
75
|
+
surface: "admin",
|
|
76
|
+
scopeValues: { workspaceId: "1" }
|
|
77
|
+
};
|
|
78
|
+
const toolSet = catalog.resolveToolSet(context);
|
|
79
|
+
const search = await catalog.executeToolCall({
|
|
80
|
+
toolName: "assistant_action_search",
|
|
81
|
+
argumentsText: JSON.stringify({ query: "books list" }),
|
|
82
|
+
context,
|
|
83
|
+
toolSet
|
|
84
|
+
});
|
|
85
|
+
assert.equal(search.ok, true);
|
|
86
|
+
assert.deepEqual(search.result.items.map((item) => item.actionId), ["crud.books.list"]);
|
|
87
|
+
const contract = await catalog.executeToolCall({
|
|
88
|
+
toolName: "assistant_action_contract",
|
|
89
|
+
argumentsText: JSON.stringify({ actionId: "crud.books.list", version: 1 }),
|
|
90
|
+
context,
|
|
91
|
+
toolSet
|
|
92
|
+
});
|
|
93
|
+
assert.equal(contract.ok, true);
|
|
94
|
+
const expectedIds = await knex("books")
|
|
95
|
+
.where("workspace_id", 1)
|
|
96
|
+
.orderBy("created_at", "desc")
|
|
97
|
+
.orderBy("id", "asc")
|
|
98
|
+
.pluck("id");
|
|
99
|
+
|
|
100
|
+
for (const limit of [100, 200]) {
|
|
101
|
+
await t.test(`requested limit ${limit}`, async () => {
|
|
102
|
+
const ids = [];
|
|
103
|
+
const cursors = new Set();
|
|
104
|
+
const pageSizes = [];
|
|
105
|
+
let cursor = null;
|
|
106
|
+
for (let page = 0; page < 4; page++) {
|
|
107
|
+
const response = await catalog.executeToolCall({
|
|
108
|
+
toolName: "assistant_action_execute",
|
|
109
|
+
argumentsText: JSON.stringify({
|
|
110
|
+
actionId: "crud.books.list",
|
|
111
|
+
version: 1,
|
|
112
|
+
input: {
|
|
113
|
+
fields: { books: ["id", "name"] },
|
|
114
|
+
limit,
|
|
115
|
+
...(cursor ? { cursor } : {})
|
|
116
|
+
}
|
|
117
|
+
}),
|
|
118
|
+
context,
|
|
119
|
+
toolSet
|
|
120
|
+
});
|
|
121
|
+
assert.equal(response.ok, true, JSON.stringify(response));
|
|
122
|
+
const result = response.result.result;
|
|
123
|
+
pageSizes.push(result.items.length);
|
|
124
|
+
for (const item of result.items) {
|
|
125
|
+
assert.deepEqual(Object.keys(item).sort(), ["id", "name"]);
|
|
126
|
+
assert.equal(item.name, `Book ${item.id}`);
|
|
127
|
+
ids.push(item.id);
|
|
128
|
+
}
|
|
129
|
+
cursor = result.nextCursor;
|
|
130
|
+
if (cursor === null) break;
|
|
131
|
+
assert.equal(typeof cursor, "string");
|
|
132
|
+
assert.equal(cursors.has(cursor), false, "Each page must advance its cursor");
|
|
133
|
+
cursors.add(cursor);
|
|
134
|
+
}
|
|
135
|
+
assert.equal(cursor, null, "Traversal must reach the end");
|
|
136
|
+
assert.deepEqual(pageSizes, [100, 100, 5]);
|
|
137
|
+
assert.deepEqual(ids, expectedIds.map(String));
|
|
138
|
+
assert.equal(new Set(ids).size, 205);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
await t.test("plain writes and JSON:API repository writes preserve full records", async () => {
|
|
143
|
+
const created = await api.resources.books.post({
|
|
144
|
+
inputRecord: { id: "501", workspaceId: "1", name: "Plain record", createdAt: "2026-09-09T00:00:00.000Z" },
|
|
145
|
+
format: "plain"
|
|
146
|
+
}, createJsonRestContext(context));
|
|
147
|
+
assert.equal(created.id, "501");
|
|
148
|
+
assert.equal(created.name, "Plain record");
|
|
149
|
+
const patched = await repository.patchDocumentById("501", { name: "JSON:API update" }, { context });
|
|
150
|
+
assert.equal(patched.data.id, "501");
|
|
151
|
+
assert.equal(patched.data.attributes.name, "JSON:API update");
|
|
152
|
+
const document = await repository.createDocument({
|
|
153
|
+
id: "502", workspaceId: "1", name: "JSON:API record", createdAt: "2026-09-09T00:00:00.000Z"
|
|
154
|
+
}, { context });
|
|
155
|
+
assert.equal(document.data.id, "502");
|
|
156
|
+
assert.equal(document.data.attributes.name, "JSON:API record");
|
|
157
|
+
assert.equal((await api.resources.books.get({ id: "502", format: "plain" }, createJsonRestContext(context))).name, "JSON:API record");
|
|
158
|
+
await assert.rejects(knex.transaction(async (trx) => {
|
|
159
|
+
await repository.patchDocumentById("502", { name: "Rolled back" }, { trx, context });
|
|
160
|
+
throw new Error("Rollback proof");
|
|
161
|
+
}), /Rollback proof/);
|
|
162
|
+
assert.equal((await repository.getDocumentById("502", {}, { context })).data.attributes.name, "JSON:API record");
|
|
163
|
+
await repository.deleteDocumentById("501", { context });
|
|
164
|
+
await repository.deleteDocumentById("502", { context });
|
|
165
|
+
assert.equal(await repository.getDocumentById("501", {}, { context }), null);
|
|
166
|
+
assert.equal(await knex("books").whereIn("id", [501, 502]).first(), undefined);
|
|
167
|
+
});
|
|
168
|
+
});
|