@jskit-ai/crud-core 0.2.1 → 0.2.3

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.2.1",
3
+ "version": "0.2.3",
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.250",
31
- "@jskit-ai/json-rest-api-core": "0.2.1",
32
- "@jskit-ai/resource-crud-core": "0.1.192",
30
+ "@jskit-ai/database-runtime": "0.1.252",
31
+ "@jskit-ai/json-rest-api-core": "0.2.3",
32
+ "@jskit-ai/resource-crud-core": "0.1.194",
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.1",
54
- "@jskit-ai/kernel": "0.2.1"
53
+ "@jskit-ai/http-runtime": "0.2.3",
54
+ "@jskit-ai/kernel": "0.2.3"
55
55
  }
56
56
  }
@@ -8,8 +8,8 @@
8
8
  "./shared": "./src/shared/index.js"
9
9
  },
10
10
  "dependencies": {
11
- "@jskit-ai/crud-core": "0.2.1",
12
- "@jskit-ai/resource-crud-core": "0.1.192"
11
+ "@jskit-ai/crud-core": "0.2.3",
12
+ "@jskit-ai/resource-crud-core": "0.1.194"
13
13
  },
14
14
  "jskit": {
15
15
  "kind": "runtime",
@@ -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
  }
@@ -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(