@jskit-ai/database-runtime 0.1.184 → 0.1.186
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 -3
- package/src/shared/duplicateEntry.js +25 -13
- package/src/shared/index.js +1 -1
- package/test/duplicateEntry.test.js +24 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/database-runtime",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.186",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test"
|
|
@@ -21,7 +21,6 @@
|
|
|
21
21
|
"./shared/repositoryOptions": "./src/shared/repositoryOptions.js",
|
|
22
22
|
"./shared/visibility": "./src/shared/visibility.js",
|
|
23
23
|
"./shared/retention": "./src/shared/retention.js",
|
|
24
|
-
"./shared/runtime": "./src/shared/runtime.js",
|
|
25
24
|
"./shared/runtimeErrors": "./src/shared/runtimeErrors.js",
|
|
26
25
|
"./shared/transactionManager": "./src/shared/transactionManager.js",
|
|
27
26
|
"./shared/transactions": "./src/shared/transactions.js"
|
|
@@ -80,6 +79,6 @@
|
|
|
80
79
|
}
|
|
81
80
|
},
|
|
82
81
|
"peerDependencies": {
|
|
83
|
-
"@jskit-ai/kernel": "0.1.
|
|
82
|
+
"@jskit-ai/kernel": "0.1.186"
|
|
84
83
|
}
|
|
85
84
|
}
|
|
@@ -15,23 +15,35 @@ function isPostgresDuplicateEntryError(error) {
|
|
|
15
15
|
return code === "23505";
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
function
|
|
19
|
-
if (!error) {
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
|
|
18
|
+
function findDuplicateEntryError(error, { dialect = "", client = null } = {}) {
|
|
23
19
|
const resolvedDialect =
|
|
24
20
|
normalizeDialect(dialect) || (client ? normalizeDialect(detectDialectFromClient(client)) : "");
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
const seen = new Set();
|
|
22
|
+
let duplicate = null;
|
|
23
|
+
|
|
24
|
+
while (error && typeof error === "object" && !seen.has(error)) {
|
|
25
|
+
seen.add(error);
|
|
26
|
+
let matches;
|
|
27
|
+
if (resolvedDialect === "postgres") {
|
|
28
|
+
matches = isPostgresDuplicateEntryError(error);
|
|
29
|
+
} else if (resolvedDialect === "mysql") {
|
|
30
|
+
matches = isMysqlDuplicateEntryError(error);
|
|
31
|
+
} else {
|
|
32
|
+
matches = isMysqlDuplicateEntryError(error) || isPostgresDuplicateEntryError(error);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (matches) {
|
|
36
|
+
// Wrappers may copy driver codes; retain the underlying constraint details.
|
|
37
|
+
duplicate = error;
|
|
38
|
+
}
|
|
39
|
+
error = error.cause;
|
|
28
40
|
}
|
|
29
41
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
42
|
+
return duplicate;
|
|
43
|
+
}
|
|
33
44
|
|
|
34
|
-
|
|
45
|
+
function isDuplicateEntryError(error, options) {
|
|
46
|
+
return findDuplicateEntryError(error, options) !== null;
|
|
35
47
|
}
|
|
36
48
|
|
|
37
|
-
export { isDuplicateEntryError };
|
|
49
|
+
export { findDuplicateEntryError, isDuplicateEntryError };
|
package/src/shared/index.js
CHANGED
|
@@ -23,7 +23,7 @@ export {
|
|
|
23
23
|
resolveDatabaseConnectionFromEnvironment,
|
|
24
24
|
resolveKnexConnectionFromEnvironment
|
|
25
25
|
} from "./databaseConnection.js";
|
|
26
|
-
export { isDuplicateEntryError } from "./duplicateEntry.js";
|
|
26
|
+
export { findDuplicateEntryError, isDuplicateEntryError } from "./duplicateEntry.js";
|
|
27
27
|
export { normalizePath, jsonTextExpression, whereJsonTextEquals } from "./json.js";
|
|
28
28
|
export { DEFAULT_VISIBILITY_COLUMNS, applyVisibility, applyVisibilityOwners } from "./visibility.js";
|
|
29
29
|
export { createRepositoryScope } from "./repositoryScope.js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import test from "node:test";
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
|
-
import { isDuplicateEntryError } from "../src/shared/duplicateEntry.js";
|
|
3
|
+
import { findDuplicateEntryError, isDuplicateEntryError } from "../src/shared/duplicateEntry.js";
|
|
4
4
|
|
|
5
5
|
test("isDuplicateEntryError matches mysql and postgres duplicate signatures", () => {
|
|
6
6
|
assert.equal(isDuplicateEntryError({ code: "ER_DUP_ENTRY" }), true);
|
|
@@ -8,3 +8,26 @@ test("isDuplicateEntryError matches mysql and postgres duplicate signatures", ()
|
|
|
8
8
|
assert.equal(isDuplicateEntryError({ code: "23505" }), true);
|
|
9
9
|
assert.equal(isDuplicateEntryError({ code: "ER_PARSE_ERROR", errno: 1064 }), false);
|
|
10
10
|
});
|
|
11
|
+
|
|
12
|
+
test("duplicate classification follows causes and retains the database error", () => {
|
|
13
|
+
const databaseError = Object.freeze({ code: "23505", message: "users_email_unique" });
|
|
14
|
+
const failure = new Error("Write failed", { cause: new Error("Storage failed", { cause: databaseError }) });
|
|
15
|
+
|
|
16
|
+
assert.equal(isDuplicateEntryError(failure, { dialect: "postgres" }), true);
|
|
17
|
+
assert.equal(findDuplicateEntryError(failure), databaseError);
|
|
18
|
+
failure.code = "23505";
|
|
19
|
+
assert.equal(findDuplicateEntryError(failure), databaseError);
|
|
20
|
+
assert.equal(findDuplicateEntryError(failure, { dialect: "mysql" }), null);
|
|
21
|
+
assert.equal(findDuplicateEntryError({ cause: { errno: 1062 } }, { dialect: "mysql" }).errno, 1062);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("duplicate classification terminates on missing, primitive and cyclic causes", () => {
|
|
25
|
+
const first = new Error("first");
|
|
26
|
+
const second = new Error("second", { cause: first });
|
|
27
|
+
first.cause = second;
|
|
28
|
+
|
|
29
|
+
for (const error of [null, undefined, false, "failure", { cause: 42 }, first]) {
|
|
30
|
+
assert.equal(findDuplicateEntryError(error), null);
|
|
31
|
+
assert.equal(isDuplicateEntryError(error), false);
|
|
32
|
+
}
|
|
33
|
+
});
|