@entifix/sql 0.23.14 → 0.23.16
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.
|
@@ -5,7 +5,9 @@ import { type StoreFailure } from '@entifix/core';
|
|
|
5
5
|
* read from the Postgres SQLSTATE wherever it sits in the cause chain —
|
|
6
6
|
* `@effect/sql` wraps the driver's error, and the code lives on the driver's.
|
|
7
7
|
*
|
|
8
|
-
* - `23505` unique violation → `DuplicateKey
|
|
8
|
+
* - `23505` unique violation → `DuplicateKey`, naming the column Postgres
|
|
9
|
+
* reports in its `detail` (`Key (sku)=(A-1) already exists.`), so a member
|
|
10
|
+
* declared `unique` (ADR 0034) is refused on its own field
|
|
9
11
|
* - `40001` serialization failure, `40P01` deadlock → `Contention`
|
|
10
12
|
* - `57014` statement cancelled by timeout → `Timeout`
|
|
11
13
|
* - class `08` connection exceptions, and anything unrecognised → `Unavailable`
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"translate-sql-error.d.ts","sourceRoot":"","sources":["../../src/errors/translate-sql-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,YAAY,EAGlB,MAAM,eAAe,CAAC;AAEvB
|
|
1
|
+
{"version":3,"file":"translate-sql-error.d.ts","sourceRoot":"","sources":["../../src/errors/translate-sql-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,YAAY,EAGlB,MAAM,eAAe,CAAC;AAEvB;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,iBAAiB,GAC5B,OAAO,OAAO,EACd,mBAAkB,KACjB,YAUF,CAAC"}
|
|
@@ -5,14 +5,21 @@ import { Contention, DuplicateKey, isFailure, Timeout, Unavailable } from '@enti
|
|
|
5
5
|
* read from the Postgres SQLSTATE wherever it sits in the cause chain —
|
|
6
6
|
* `@effect/sql` wraps the driver's error, and the code lives on the driver's.
|
|
7
7
|
*
|
|
8
|
-
* - `23505` unique violation → `DuplicateKey
|
|
8
|
+
* - `23505` unique violation → `DuplicateKey`, naming the column Postgres
|
|
9
|
+
* reports in its `detail` (`Key (sku)=(A-1) already exists.`), so a member
|
|
10
|
+
* declared `unique` (ADR 0034) is refused on its own field
|
|
9
11
|
* - `40001` serialization failure, `40P01` deadlock → `Contention`
|
|
10
12
|
* - `57014` statement cancelled by timeout → `Timeout`
|
|
11
13
|
* - class `08` connection exceptions, and anything unrecognised → `Unavailable`
|
|
12
14
|
*/ export const translateSqlError = (error, dependency = 'sql')=>{
|
|
13
15
|
if (isFailure(error)) return error;
|
|
14
16
|
const code = sqlState(error);
|
|
15
|
-
if (code === '23505')
|
|
17
|
+
if (code === '23505') {
|
|
18
|
+
const key = duplicatedColumn(error);
|
|
19
|
+
return new DuplicateKey(key === undefined ? {} : {
|
|
20
|
+
key
|
|
21
|
+
});
|
|
22
|
+
}
|
|
16
23
|
if (code === '40001' || code === '40P01') return new Contention();
|
|
17
24
|
if (code === '57014') return new Timeout({
|
|
18
25
|
dependency
|
|
@@ -21,6 +28,18 @@ import { Contention, DuplicateKey, isFailure, Timeout, Unavailable } from '@enti
|
|
|
21
28
|
dependency
|
|
22
29
|
});
|
|
23
30
|
};
|
|
31
|
+
/** The column(s) a unique violation names in its `detail`, comma-joined. */ const duplicatedColumn = (error)=>{
|
|
32
|
+
let current = error;
|
|
33
|
+
for(let depth = 0; current != null && depth < 5; depth += 1){
|
|
34
|
+
const detail = current.detail;
|
|
35
|
+
if (typeof detail === 'string') {
|
|
36
|
+
const match = /^Key \((.+?)\)=/.exec(detail);
|
|
37
|
+
if ((match == null ? void 0 : match[1]) !== undefined) return match[1].split(',').map((column)=>column.trim().replace(/^"(.*)"$/, '$1')).join(',');
|
|
38
|
+
}
|
|
39
|
+
current = current.cause;
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
};
|
|
24
43
|
const sqlState = (error)=>{
|
|
25
44
|
let current = error;
|
|
26
45
|
for(let depth = 0; current != null && depth < 5; depth += 1){
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/errors/translate-sql-error.ts"],"sourcesContent":["import {\n Contention,\n DuplicateKey,\n isFailure,\n type StoreFailure,\n Timeout,\n Unavailable,\n} from '@entifix/core';\n\n/**\n * What a SQL error means, as a categorised failure\n * ([ADR 0013](../../../../../docs/adr/0013-failures-are-typed-values-decided-at-the-shell.md)),\n * read from the Postgres SQLSTATE wherever it sits in the cause chain —\n * `@effect/sql` wraps the driver's error, and the code lives on the driver's.\n *\n * - `23505` unique violation → `DuplicateKey
|
|
1
|
+
{"version":3,"sources":["../../src/errors/translate-sql-error.ts"],"sourcesContent":["import {\n Contention,\n DuplicateKey,\n isFailure,\n type StoreFailure,\n Timeout,\n Unavailable,\n} from '@entifix/core';\n\n/**\n * What a SQL error means, as a categorised failure\n * ([ADR 0013](../../../../../docs/adr/0013-failures-are-typed-values-decided-at-the-shell.md)),\n * read from the Postgres SQLSTATE wherever it sits in the cause chain —\n * `@effect/sql` wraps the driver's error, and the code lives on the driver's.\n *\n * - `23505` unique violation → `DuplicateKey`, naming the column Postgres\n * reports in its `detail` (`Key (sku)=(A-1) already exists.`), so a member\n * declared `unique` (ADR 0034) is refused on its own field\n * - `40001` serialization failure, `40P01` deadlock → `Contention`\n * - `57014` statement cancelled by timeout → `Timeout`\n * - class `08` connection exceptions, and anything unrecognised → `Unavailable`\n */\nexport const translateSqlError = (\n error: unknown,\n dependency = 'sql',\n): StoreFailure => {\n if (isFailure(error)) return error as StoreFailure;\n const code = sqlState(error);\n if (code === '23505') {\n const key = duplicatedColumn(error);\n return new DuplicateKey(key === undefined ? {} : { key });\n }\n if (code === '40001' || code === '40P01') return new Contention();\n if (code === '57014') return new Timeout({ dependency });\n return new Unavailable({ dependency });\n};\n\n/** The column(s) a unique violation names in its `detail`, comma-joined. */\nconst duplicatedColumn = (error: unknown): string | undefined => {\n let current: unknown = error;\n for (let depth = 0; current != null && depth < 5; depth += 1) {\n const detail = (current as { detail?: unknown }).detail;\n if (typeof detail === 'string') {\n const match = /^Key \\((.+?)\\)=/.exec(detail);\n if (match?.[1] !== undefined)\n return match[1]\n .split(',')\n .map(column => column.trim().replace(/^\"(.*)\"$/, '$1'))\n .join(',');\n }\n current = (current as { cause?: unknown }).cause;\n }\n return undefined;\n};\n\nconst sqlState = (error: unknown): string | undefined => {\n let current: unknown = error;\n for (let depth = 0; current != null && depth < 5; depth += 1) {\n const code = (current as { code?: unknown }).code;\n if (typeof code === 'string' && /^[0-9A-Z]{5}$/.test(code)) return code;\n current = (current as { cause?: unknown }).cause;\n }\n return undefined;\n};\n"],"names":["Contention","DuplicateKey","isFailure","Timeout","Unavailable","translateSqlError","error","dependency","code","sqlState","key","duplicatedColumn","undefined","current","depth","detail","match","exec","split","map","column","trim","replace","join","cause","test"],"mappings":"AAAA,SACEA,UAAU,EACVC,YAAY,EACZC,SAAS,EAETC,OAAO,EACPC,WAAW,QACN,gBAAgB;AAEvB;;;;;;;;;;;;CAYC,GACD,OAAO,MAAMC,oBAAoB,CAC/BC,OACAC,aAAa,KAAK;IAElB,IAAIL,UAAUI,QAAQ,OAAOA;IAC7B,MAAME,OAAOC,SAASH;IACtB,IAAIE,SAAS,SAAS;QACpB,MAAME,MAAMC,iBAAiBL;QAC7B,OAAO,IAAIL,aAAaS,QAAQE,YAAY,CAAC,IAAI;YAAEF;QAAI;IACzD;IACA,IAAIF,SAAS,WAAWA,SAAS,SAAS,OAAO,IAAIR;IACrD,IAAIQ,SAAS,SAAS,OAAO,IAAIL,QAAQ;QAAEI;IAAW;IACtD,OAAO,IAAIH,YAAY;QAAEG;IAAW;AACtC,EAAE;AAEF,0EAA0E,GAC1E,MAAMI,mBAAmB,CAACL;IACxB,IAAIO,UAAmBP;IACvB,IAAK,IAAIQ,QAAQ,GAAGD,WAAW,QAAQC,QAAQ,GAAGA,SAAS,EAAG;QAC5D,MAAMC,SAAS,AAACF,QAAiCE,MAAM;QACvD,IAAI,OAAOA,WAAW,UAAU;YAC9B,MAAMC,QAAQ,kBAAkBC,IAAI,CAACF;YACrC,IAAIC,CAAAA,yBAAAA,KAAO,CAAC,EAAE,MAAKJ,WACjB,OAAOI,KAAK,CAAC,EAAE,CACZE,KAAK,CAAC,KACNC,GAAG,CAACC,CAAAA,SAAUA,OAAOC,IAAI,GAAGC,OAAO,CAAC,YAAY,OAChDC,IAAI,CAAC;QACZ;QACAV,UAAU,AAACA,QAAgCW,KAAK;IAClD;IACA,OAAOZ;AACT;AAEA,MAAMH,WAAW,CAACH;IAChB,IAAIO,UAAmBP;IACvB,IAAK,IAAIQ,QAAQ,GAAGD,WAAW,QAAQC,QAAQ,GAAGA,SAAS,EAAG;QAC5D,MAAMN,OAAO,AAACK,QAA+BL,IAAI;QACjD,IAAI,OAAOA,SAAS,YAAY,gBAAgBiB,IAAI,CAACjB,OAAO,OAAOA;QACnEK,UAAU,AAACA,QAAgCW,KAAK;IAClD;IACA,OAAOZ;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@entifix/sql",
|
|
3
|
-
"version": "0.23.
|
|
3
|
+
"version": "0.23.16",
|
|
4
4
|
"description": "SQL adapter over @effect/sql: an entity repository with RSQL filter and sort translation, and a health probe.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -63,18 +63,18 @@
|
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"@swc/helpers": "0.5.23",
|
|
66
|
-
"@entifix/business": "0.23.
|
|
67
|
-
"@entifix/core": "0.23.
|
|
66
|
+
"@entifix/business": "0.23.16",
|
|
67
|
+
"@entifix/core": "0.23.16"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
70
|
"@effect/sql-pg": "4.0.7",
|
|
71
71
|
"effect": "4.0.7",
|
|
72
|
-
"@entifix/testing-unit": "0.23.
|
|
73
|
-
"@entifix/transactions": "0.23.
|
|
72
|
+
"@entifix/testing-unit": "0.23.16",
|
|
73
|
+
"@entifix/transactions": "0.23.16"
|
|
74
74
|
},
|
|
75
75
|
"peerDependencies": {
|
|
76
76
|
"effect": "^4.0.7",
|
|
77
|
-
"@entifix/transactions": "0.23.
|
|
77
|
+
"@entifix/transactions": "0.23.16"
|
|
78
78
|
},
|
|
79
79
|
"peerDependenciesMeta": {
|
|
80
80
|
"@entifix/transactions": {
|