@jskit-ai/crud-core 0.2.0 → 0.2.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 +6 -6
- package/patterns/json-api-resource-package/PATTERN.md +7 -0
- package/patterns/json-api-resource-package/example/packages/books/package.json +2 -2
- package/src/server/defineCrudJsonApiFeature.js +4 -2
- package/src/server/jsonApiModule/actions.js +3 -2
- package/src/server/jsonApiModule/repository.js +13 -7
- package/test/defineCrudJsonApiFeature.test.js +39 -0
- package/test/jsonApiRepository.test.js +47 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/crud-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24.0.0"
|
|
@@ -27,9 +27,9 @@
|
|
|
27
27
|
"./locales/en.json": "./locales/en.json"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@jskit-ai/database-runtime": "0.1.
|
|
31
|
-
"@jskit-ai/json-rest-api-core": "0.2.
|
|
32
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
30
|
+
"@jskit-ai/database-runtime": "0.1.251",
|
|
31
|
+
"@jskit-ai/json-rest-api-core": "0.2.2",
|
|
32
|
+
"@jskit-ai/resource-crud-core": "0.1.193",
|
|
33
33
|
"json-rest-schema": "^1.0.17",
|
|
34
34
|
"json-rest-api": "2.1.0"
|
|
35
35
|
},
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
}
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"@jskit-ai/http-runtime": "0.2.
|
|
54
|
-
"@jskit-ai/kernel": "0.2.
|
|
53
|
+
"@jskit-ai/http-runtime": "0.2.2",
|
|
54
|
+
"@jskit-ai/kernel": "0.2.2"
|
|
55
55
|
}
|
|
56
56
|
}
|
|
@@ -96,6 +96,13 @@ surface further; do not register unused mutation actions and routes. Use
|
|
|
96
96
|
`routes: false` only for an internal
|
|
97
97
|
capability/action surface that deliberately has no HTTP API.
|
|
98
98
|
|
|
99
|
+
Create, update, and delete publish `<namespace>.record.changed` to the
|
|
100
|
+
`event_scope` realtime audience, which reaches only the room of the action's
|
|
101
|
+
user, workspace, or workspace-user scope. Set `realtimeAudience` to another
|
|
102
|
+
realtime audience when the product needs one, for example `"all_users"` so
|
|
103
|
+
every signed-in user's view of a public resource refreshes. It also accepts a
|
|
104
|
+
function of the action execution.
|
|
105
|
+
|
|
99
106
|
When otherwise-standard CRUD earns product behavior, name the exact
|
|
100
107
|
capabilities in `requires` and provide `decorateService({ service, repository,
|
|
101
108
|
resource, ...dependencies })`. Retain the standard methods, override only the
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
"./shared": "./src/shared/index.js"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@jskit-ai/crud-core": "0.2.
|
|
12
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
11
|
+
"@jskit-ai/crud-core": "0.2.2",
|
|
12
|
+
"@jskit-ai/resource-crud-core": "0.1.193"
|
|
13
13
|
},
|
|
14
14
|
"jskit": {
|
|
15
15
|
"kind": "runtime",
|
|
@@ -163,7 +163,8 @@ function defineCrudJsonApiFeature({
|
|
|
163
163
|
operationLifecycle = {},
|
|
164
164
|
operationInputs = {},
|
|
165
165
|
actions = {},
|
|
166
|
-
operations = undefined
|
|
166
|
+
operations = undefined,
|
|
167
|
+
realtimeAudience = "event_scope"
|
|
167
168
|
} = {}) {
|
|
168
169
|
if (!resource || typeof resource !== "object" || Array.isArray(resource)) {
|
|
169
170
|
throw new TypeError("defineCrudJsonApiFeature requires resource.");
|
|
@@ -301,7 +302,8 @@ function defineCrudJsonApiFeature({
|
|
|
301
302
|
permissions: permissionOptions
|
|
302
303
|
}),
|
|
303
304
|
scopeInputValidator: normalizedScope.actionInputValidator,
|
|
304
|
-
scopeInputKeys: normalizedScope.inputKeys
|
|
305
|
+
scopeInputKeys: normalizedScope.inputKeys,
|
|
306
|
+
realtimeAudience
|
|
305
307
|
});
|
|
306
308
|
}
|
|
307
309
|
});
|
|
@@ -558,7 +558,8 @@ function createCrudJsonApiActions({
|
|
|
558
558
|
operationLifecycle = {},
|
|
559
559
|
operationInputs = {},
|
|
560
560
|
actions: additionalActionDefinitions = {},
|
|
561
|
-
dependencies = {}
|
|
561
|
+
dependencies = {},
|
|
562
|
+
realtimeAudience = "event_scope"
|
|
562
563
|
} = {}) {
|
|
563
564
|
const actionId = (operation) => `crud.${namespace}.${operation}`;
|
|
564
565
|
if (!service || typeof service !== "object") {
|
|
@@ -580,7 +581,7 @@ function createCrudJsonApiActions({
|
|
|
580
581
|
entityId,
|
|
581
582
|
realtime: {
|
|
582
583
|
event: recordChangedEvent,
|
|
583
|
-
audience:
|
|
584
|
+
audience: realtimeAudience
|
|
584
585
|
}
|
|
585
586
|
});
|
|
586
587
|
const lifecycle = normalizeCrudOperationLifecycle(operationLifecycle);
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
returnBadRequestWhenJsonRestFieldsetInvalid,
|
|
5
5
|
returnNullWhenJsonRestResourceMissing
|
|
6
6
|
} from "@jskit-ai/json-rest-api-core/server/jsonRestApiHost";
|
|
7
|
+
import { retryDeadlocks } from "@jskit-ai/database-runtime/shared";
|
|
7
8
|
|
|
8
9
|
function createCrudJsonApiRepository({ api, resource, resourceScopeName } = {}) {
|
|
9
10
|
if (!api) {
|
|
@@ -18,7 +19,12 @@ function createCrudJsonApiRepository({ api, resource, resourceScopeName } = {})
|
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
function withTransaction(work) {
|
|
21
|
-
return api.transaction(work);
|
|
22
|
+
return retryDeadlocks(() => api.transaction(work));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// A write given no transaction owns one, so it is outermost and may re-run.
|
|
26
|
+
function write(options, run) {
|
|
27
|
+
return options.trx ? run() : retryDeadlocks(run);
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
function inputData(payload) {
|
|
@@ -80,7 +86,7 @@ function createCrudJsonApiRepository({ api, resource, resourceScopeName } = {})
|
|
|
80
86
|
}
|
|
81
87
|
|
|
82
88
|
async function createDocument(payload = {}, options = {}) {
|
|
83
|
-
return requireApiResource().post(
|
|
89
|
+
return write(options, () => requireApiResource().post(
|
|
84
90
|
{
|
|
85
91
|
data: inputData(payload),
|
|
86
92
|
returning: "full",
|
|
@@ -88,7 +94,7 @@ function createCrudJsonApiRepository({ api, resource, resourceScopeName } = {})
|
|
|
88
94
|
format: "jsonapi"
|
|
89
95
|
},
|
|
90
96
|
createJsonRestContext(options.context || null)
|
|
91
|
-
);
|
|
97
|
+
));
|
|
92
98
|
}
|
|
93
99
|
|
|
94
100
|
async function patchDocumentById(recordId, patch = {}, options = {}) {
|
|
@@ -97,7 +103,7 @@ function createCrudJsonApiRepository({ api, resource, resourceScopeName } = {})
|
|
|
97
103
|
return getDocumentById(recordId, {}, options);
|
|
98
104
|
}
|
|
99
105
|
|
|
100
|
-
return returnNullWhenJsonRestResourceMissing(() =>
|
|
106
|
+
return returnNullWhenJsonRestResourceMissing(() => write(options, () =>
|
|
101
107
|
requireApiResource().patch(
|
|
102
108
|
{
|
|
103
109
|
id: recordId,
|
|
@@ -108,12 +114,12 @@ function createCrudJsonApiRepository({ api, resource, resourceScopeName } = {})
|
|
|
108
114
|
},
|
|
109
115
|
createJsonRestContext(options.context || null)
|
|
110
116
|
)
|
|
111
|
-
);
|
|
117
|
+
));
|
|
112
118
|
}
|
|
113
119
|
|
|
114
120
|
async function deleteDocumentById(recordId, options = {}) {
|
|
115
121
|
return returnNullWhenJsonRestResourceMissing(async () => {
|
|
116
|
-
await requireApiResource().delete(
|
|
122
|
+
await write(options, () => requireApiResource().delete(
|
|
117
123
|
{
|
|
118
124
|
id: recordId,
|
|
119
125
|
returning: "none",
|
|
@@ -121,7 +127,7 @@ function createCrudJsonApiRepository({ api, resource, resourceScopeName } = {})
|
|
|
121
127
|
format: "jsonapi"
|
|
122
128
|
},
|
|
123
129
|
createJsonRestContext(options.context || null)
|
|
124
|
-
);
|
|
130
|
+
));
|
|
125
131
|
return null;
|
|
126
132
|
});
|
|
127
133
|
}
|
|
@@ -150,6 +150,45 @@ test("defineCrudJsonApiFeature composes one resource without ceremonial token la
|
|
|
150
150
|
await runtime.shutdown();
|
|
151
151
|
});
|
|
152
152
|
|
|
153
|
+
test("defineCrudJsonApiFeature sends record.changed to its realtime audience", async () => {
|
|
154
|
+
const execution = { input: { recordId: "5" }, result: { data: { id: "5" } }, context: {} };
|
|
155
|
+
for (const [options, audience] of [[{}, "event_scope"], [{ realtimeAudience: "all_users" }, "all_users"]]) {
|
|
156
|
+
let actions;
|
|
157
|
+
const observer = defineProvider({
|
|
158
|
+
id: "test.observer",
|
|
159
|
+
requires: { actions: "runtime.actions", books: "crud.books" },
|
|
160
|
+
setup(dependencies) {
|
|
161
|
+
actions = dependencies.actions;
|
|
162
|
+
return {};
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
const runtime = createCapabilityRuntime({
|
|
166
|
+
providers: [
|
|
167
|
+
createActionProvider(),
|
|
168
|
+
...createFeatureDependencies([]),
|
|
169
|
+
defineCrudJsonApiFeature({
|
|
170
|
+
resource: createBookResource({ apiAccess: "public", autofilter: "public" }),
|
|
171
|
+
surface: "home",
|
|
172
|
+
...options
|
|
173
|
+
}),
|
|
174
|
+
observer
|
|
175
|
+
]
|
|
176
|
+
});
|
|
177
|
+
await runtime.start();
|
|
178
|
+
|
|
179
|
+
assert.deepEqual(
|
|
180
|
+
actions.listDefinitions()
|
|
181
|
+
.filter((entry) => entry.events.length > 0)
|
|
182
|
+
.map((entry) => [entry.id, entry.events[0](execution).realtime]),
|
|
183
|
+
["create", "update", "delete"].map((operation) => [
|
|
184
|
+
`crud.books.${operation}`,
|
|
185
|
+
{ event: "books.record.changed", audience }
|
|
186
|
+
])
|
|
187
|
+
);
|
|
188
|
+
await runtime.shutdown();
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
153
192
|
test("defineCrudJsonApiFeature makes workspace scope and permissions explicit", async () => {
|
|
154
193
|
const routes = [];
|
|
155
194
|
const feature = defineCrudJsonApiFeature({
|
|
@@ -10,6 +10,13 @@ import { createCrudJsonApiActions } from "../src/server/jsonApiModule/actions.js
|
|
|
10
10
|
import { createCrudJsonApiRepository } from "../src/server/jsonApiModule/repository.js";
|
|
11
11
|
import { createCrudJsonApiService } from "../src/server/jsonApiModule/service.js";
|
|
12
12
|
|
|
13
|
+
function deadlockVictim() {
|
|
14
|
+
return Object.assign(new Error("Deadlock found when trying to get lock; try restarting transaction"), {
|
|
15
|
+
code: "ER_LOCK_DEADLOCK",
|
|
16
|
+
errno: 1213
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
13
20
|
function createRepositoryRecorder() {
|
|
14
21
|
const calls = [];
|
|
15
22
|
const response = { data: { type: "books", id: "7", attributes: { title: "Dune" } } };
|
|
@@ -114,6 +121,46 @@ test("repository transactions commit resource writes and raw SQL before completi
|
|
|
114
121
|
assert.deepEqual(committed, ["post", "post"]);
|
|
115
122
|
});
|
|
116
123
|
|
|
124
|
+
test("a deadlock victim's repository transaction runs again and commits its writes once", async (t) => {
|
|
125
|
+
const { knex, repository } = await createDatabaseRepository(t);
|
|
126
|
+
let attempts = 0;
|
|
127
|
+
const document = await repository.withTransaction(async (trx) => {
|
|
128
|
+
attempts += 1;
|
|
129
|
+
const created = await repository.createDocument({ title: "Dune" }, { trx });
|
|
130
|
+
await trx("audit_entries").insert({ message: `Attempt ${attempts}` });
|
|
131
|
+
if (attempts === 1) {
|
|
132
|
+
throw deadlockVictim();
|
|
133
|
+
}
|
|
134
|
+
return created;
|
|
135
|
+
});
|
|
136
|
+
assert.equal(attempts, 2);
|
|
137
|
+
assert.equal(document.data.attributes.title, "Dune");
|
|
138
|
+
assert.deepEqual(await knex("books").pluck("title"), ["Dune"]);
|
|
139
|
+
assert.deepEqual(await knex("audit_entries").pluck("message"), ["Attempt 2"]);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("a write that owns its transaction runs again after a deadlock; one in a caller's transaction does not", async () => {
|
|
143
|
+
let posts = 0;
|
|
144
|
+
const api = {
|
|
145
|
+
resources: {
|
|
146
|
+
books: {
|
|
147
|
+
async post() {
|
|
148
|
+
posts += 1;
|
|
149
|
+
if (posts === 1) {
|
|
150
|
+
throw deadlockVictim();
|
|
151
|
+
}
|
|
152
|
+
return { data: { type: "books", id: String(posts) } };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
const repository = createCrudJsonApiRepository({ api, resource: { schema: {} }, resourceScopeName: "books" });
|
|
158
|
+
assert.equal((await repository.createDocument({ title: "Dune" })).data.id, "2");
|
|
159
|
+
posts = 0;
|
|
160
|
+
await assert.rejects(repository.createDocument({ title: "Dune" }, { trx: "managed" }), { code: "ER_LOCK_DEADLOCK" });
|
|
161
|
+
assert.equal(posts, 1);
|
|
162
|
+
});
|
|
163
|
+
|
|
117
164
|
test("repository transactions roll back resource writes and raw SQL after a rejected participant", async (t) => {
|
|
118
165
|
const { knex, repository } = await createDatabaseRepository(t);
|
|
119
166
|
await assert.rejects(
|