@prisma-next/sql-errors 0.13.0 → 0.14.0

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/dist/index.d.mts CHANGED
@@ -26,6 +26,21 @@ declare class SqlQueryError extends Error implements SqlDriverError<'sql_query'>
26
26
  */
27
27
  static is(error: unknown): error is SqlQueryError;
28
28
  }
29
+ /**
30
+ * SQLSTATE for a unique-constraint / primary-key violation — the SQL-standard
31
+ * `unique_violation` class. Drivers normalize their target-specific codes (e.g.
32
+ * Postgres `23505`, SQLite `SQLITE_CONSTRAINT_UNIQUE`/`PRIMARYKEY`) onto this
33
+ * value on {@link SqlQueryError.sqlState}, so consumers classify violations
34
+ * without knowing any target-specific error shape.
35
+ */
36
+ declare const UNIQUE_VIOLATION_SQLSTATE = "23505";
37
+ /**
38
+ * Whether an error is a unique-constraint (or primary-key) violation, decided
39
+ * solely from the driver-normalized {@link SqlQueryError.sqlState}. Raw driver
40
+ * errors that were never normalized are not classified — callers run behind the
41
+ * driver, which always normalizes before the error escapes.
42
+ */
43
+ declare function isUniqueConstraintViolation(error: unknown): boolean;
29
44
  /**
30
45
  * SQL connection error (timeouts, connection resets, etc.).
31
46
  */
@@ -43,5 +58,5 @@ declare class SqlConnectionError extends Error implements SqlDriverError<'sql_co
43
58
  static is(error: unknown): error is SqlConnectionError;
44
59
  }
45
60
  //#endregion
46
- export { SqlConnectionError, SqlQueryError };
61
+ export { SqlConnectionError, SqlQueryError, UNIQUE_VIOLATION_SQLSTATE, isUniqueConstraintViolation };
47
62
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/errors.ts"],"mappings":";UAAiB,cAAA;EAAA,SACN,IAAA,EAAM,IAAI;AAAA;;;;cAKR,aAAA,SAAsB,KAAA,YAAiB,cAAA;EAAA,gBAClC,UAAA;EAAA,SACP,IAAA;EAAA,SACA,QAAA;EAAA,SACA,UAAA;EAAA,S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AhB2C;AAO1D;;EAP0D,OA2BjD,EAAA,CAAG,KAAA,YAAiB,KAAA,IAAS,kBAAA;AAAA"}
package/dist/index.mjs CHANGED
@@ -27,6 +27,23 @@ var SqlQueryError = class SqlQueryError extends Error {
27
27
  }
28
28
  };
29
29
  /**
30
+ * SQLSTATE for a unique-constraint / primary-key violation — the SQL-standard
31
+ * `unique_violation` class. Drivers normalize their target-specific codes (e.g.
32
+ * Postgres `23505`, SQLite `SQLITE_CONSTRAINT_UNIQUE`/`PRIMARYKEY`) onto this
33
+ * value on {@link SqlQueryError.sqlState}, so consumers classify violations
34
+ * without knowing any target-specific error shape.
35
+ */
36
+ const UNIQUE_VIOLATION_SQLSTATE = "23505";
37
+ /**
38
+ * Whether an error is a unique-constraint (or primary-key) violation, decided
39
+ * solely from the driver-normalized {@link SqlQueryError.sqlState}. Raw driver
40
+ * errors that were never normalized are not classified — callers run behind the
41
+ * driver, which always normalizes before the error escapes.
42
+ */
43
+ function isUniqueConstraintViolation(error) {
44
+ return SqlQueryError.is(error) && error.sqlState === "23505";
45
+ }
46
+ /**
30
47
  * SQL connection error (timeouts, connection resets, etc.).
31
48
  */
32
49
  var SqlConnectionError = class SqlConnectionError extends Error {
@@ -46,6 +63,6 @@ var SqlConnectionError = class SqlConnectionError extends Error {
46
63
  }
47
64
  };
48
65
  //#endregion
49
- export { SqlConnectionError, SqlQueryError };
66
+ export { SqlConnectionError, SqlQueryError, UNIQUE_VIOLATION_SQLSTATE, isUniqueConstraintViolation };
50
67
 
51
68
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["export interface SqlDriverError<Kind extends string> {\n readonly kind: Kind;\n}\n/**\n * SQL query error for query-related failures (syntax errors, constraint violations, permissions).\n */\nexport class SqlQueryError extends Error implements SqlDriverError<'sql_query'> {\n static readonly ERROR_NAME = 'SqlQueryError' as const;\n readonly kind = 'sql_query' as const;\n readonly sqlState: string | undefined;\n readonly constraint: string | undefined;\n readonly table: string | undefined;\n readonly column: string | undefined;\n readonly detail: string | undefined;\n\n constructor(\n message: string,\n options?: {\n readonly cause?: Error;\n readonly sqlState?: string;\n readonly constraint?: string;\n readonly table?: string;\n readonly column?: string;\n readonly detail?: string;\n },\n ) {\n super(message, { cause: options?.cause });\n this.name = SqlQueryError.ERROR_NAME;\n this.sqlState = options?.sqlState;\n this.constraint = options?.constraint;\n this.table = options?.table;\n this.column = options?.column;\n this.detail = options?.detail;\n }\n\n /**\n * Type predicate to check if an error is a SqlQueryError.\n */\n static is(error: unknown): error is SqlQueryError {\n return (\n typeof error === 'object' &&\n error !== null &&\n Object.hasOwn(error, 'kind') &&\n (error as { kind: unknown }).kind === 'sql_query'\n );\n }\n}\n\n/**\n * SQL connection error (timeouts, connection resets, etc.).\n */\nexport class SqlConnectionError extends Error implements SqlDriverError<'sql_connection'> {\n static readonly ERROR_NAME = 'SqlConnectionError' as const;\n readonly kind = 'sql_connection' as const;\n readonly transient: boolean | undefined;\n\n constructor(\n message: string,\n options?: {\n readonly cause?: Error;\n readonly transient?: boolean;\n },\n ) {\n super(message, { cause: options?.cause });\n this.name = SqlConnectionError.ERROR_NAME;\n this.transient = options?.transient;\n }\n\n /**\n * Type predicate to check if an error is a SqlConnectionError.\n */\n static is(error: unknown): error is SqlConnectionError {\n return (\n typeof error === 'object' &&\n error !== null &&\n Object.hasOwn(error, 'kind') &&\n (error as { kind: unknown }).kind === 'sql_connection'\n );\n }\n}\n"],"mappings":";;;;AAMA,IAAa,gBAAb,MAAa,sBAAsB,MAA6C;CAC9E,OAAgB,aAAa;CAC7B,OAAgB;CAChB;CACA;CACA;CACA;CACA;CAEA,YACE,SACA,SAQA;EACA,MAAM,SAAS,EAAE,OAAO,SAAS,MAAM,CAAC;EACxC,KAAK,OAAO,cAAc;EAC1B,KAAK,WAAW,SAAS;EACzB,KAAK,aAAa,SAAS;EAC3B,KAAK,QAAQ,SAAS;EACtB,KAAK,SAAS,SAAS;EACvB,KAAK,SAAS,SAAS;CACzB;;;;CAKA,OAAO,GAAG,OAAwC;EAChD,OACE,OAAO,UAAU,YACjB,UAAU,QACV,OAAO,OAAO,OAAO,MAAM,KAC1B,MAA4B,SAAS;CAE1C;AACF;;;;AAKA,IAAa,qBAAb,MAAa,2BAA2B,MAAkD;CACxF,OAAgB,aAAa;CAC7B,OAAgB;CAChB;CAEA,YACE,SACA,SAIA;EACA,MAAM,SAAS,EAAE,OAAO,SAAS,MAAM,CAAC;EACxC,KAAK,OAAO,mBAAmB;EAC/B,KAAK,YAAY,SAAS;CAC5B;;;;CAKA,OAAO,GAAG,OAA6C;EACrD,OACE,OAAO,UAAU,YACjB,UAAU,QACV,OAAO,OAAO,OAAO,MAAM,KAC1B,MAA4B,SAAS;CAE1C;AACF"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["export interface SqlDriverError<Kind extends string> {\n readonly kind: Kind;\n}\n/**\n * SQL query error for query-related failures (syntax errors, constraint violations, permissions).\n */\nexport class SqlQueryError extends Error implements SqlDriverError<'sql_query'> {\n static readonly ERROR_NAME = 'SqlQueryError' as const;\n readonly kind = 'sql_query' as const;\n readonly sqlState: string | undefined;\n readonly constraint: string | undefined;\n readonly table: string | undefined;\n readonly column: string | undefined;\n readonly detail: string | undefined;\n\n constructor(\n message: string,\n options?: {\n readonly cause?: Error;\n readonly sqlState?: string;\n readonly constraint?: string;\n readonly table?: string;\n readonly column?: string;\n readonly detail?: string;\n },\n ) {\n super(message, { cause: options?.cause });\n this.name = SqlQueryError.ERROR_NAME;\n this.sqlState = options?.sqlState;\n this.constraint = options?.constraint;\n this.table = options?.table;\n this.column = options?.column;\n this.detail = options?.detail;\n }\n\n /**\n * Type predicate to check if an error is a SqlQueryError.\n */\n static is(error: unknown): error is SqlQueryError {\n return (\n typeof error === 'object' &&\n error !== null &&\n Object.hasOwn(error, 'kind') &&\n (error as { kind: unknown }).kind === 'sql_query'\n );\n }\n}\n\n/**\n * SQLSTATE for a unique-constraint / primary-key violation — the SQL-standard\n * `unique_violation` class. Drivers normalize their target-specific codes (e.g.\n * Postgres `23505`, SQLite `SQLITE_CONSTRAINT_UNIQUE`/`PRIMARYKEY`) onto this\n * value on {@link SqlQueryError.sqlState}, so consumers classify violations\n * without knowing any target-specific error shape.\n */\nexport const UNIQUE_VIOLATION_SQLSTATE = '23505';\n\n/**\n * Whether an error is a unique-constraint (or primary-key) violation, decided\n * solely from the driver-normalized {@link SqlQueryError.sqlState}. Raw driver\n * errors that were never normalized are not classified — callers run behind the\n * driver, which always normalizes before the error escapes.\n */\nexport function isUniqueConstraintViolation(error: unknown): boolean {\n return SqlQueryError.is(error) && error.sqlState === UNIQUE_VIOLATION_SQLSTATE;\n}\n\n/**\n * SQL connection error (timeouts, connection resets, etc.).\n */\nexport class SqlConnectionError extends Error implements SqlDriverError<'sql_connection'> {\n static readonly ERROR_NAME = 'SqlConnectionError' as const;\n readonly kind = 'sql_connection' as const;\n readonly transient: boolean | undefined;\n\n constructor(\n message: string,\n options?: {\n readonly cause?: Error;\n readonly transient?: boolean;\n },\n ) {\n super(message, { cause: options?.cause });\n this.name = SqlConnectionError.ERROR_NAME;\n this.transient = options?.transient;\n }\n\n /**\n * Type predicate to check if an error is a SqlConnectionError.\n */\n static is(error: unknown): error is SqlConnectionError {\n return (\n typeof error === 'object' &&\n error !== null &&\n Object.hasOwn(error, 'kind') &&\n (error as { kind: unknown }).kind === 'sql_connection'\n );\n }\n}\n"],"mappings":";;;;AAMA,IAAa,gBAAb,MAAa,sBAAsB,MAA6C;CAC9E,OAAgB,aAAa;CAC7B,OAAgB;CAChB;CACA;CACA;CACA;CACA;CAEA,YACE,SACA,SAQA;EACA,MAAM,SAAS,EAAE,OAAO,SAAS,MAAM,CAAC;EACxC,KAAK,OAAO,cAAc;EAC1B,KAAK,WAAW,SAAS;EACzB,KAAK,aAAa,SAAS;EAC3B,KAAK,QAAQ,SAAS;EACtB,KAAK,SAAS,SAAS;EACvB,KAAK,SAAS,SAAS;CACzB;;;;CAKA,OAAO,GAAG,OAAwC;EAChD,OACE,OAAO,UAAU,YACjB,UAAU,QACV,OAAO,OAAO,OAAO,MAAM,KAC1B,MAA4B,SAAS;CAE1C;AACF;;;;;;;;AASA,MAAa,4BAA4B;;;;;;;AAQzC,SAAgB,4BAA4B,OAAyB;CACnE,OAAO,cAAc,GAAG,KAAK,KAAK,MAAM,aAAA;AAC1C;;;;AAKA,IAAa,qBAAb,MAAa,2BAA2B,MAAkD;CACxF,OAAgB,aAAa;CAC7B,OAAgB;CAChB;CAEA,YACE,SACA,SAIA;EACA,MAAM,SAAS,EAAE,OAAO,SAAS,MAAM,CAAC;EACxC,KAAK,OAAO,mBAAmB;EAC/B,KAAK,YAAY,SAAS;CAC5B;;;;CAKA,OAAO,GAAG,OAA6C;EACrD,OACE,OAAO,UAAU,YACjB,UAAU,QACV,OAAO,OAAO,OAAO,MAAM,KAC1B,MAA4B,SAAS;CAE1C;AACF"}
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@prisma-next/sql-errors",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "description": "Normalized SQL driver error types for Prisma Next",
8
8
  "devDependencies": {
9
- "@prisma-next/test-utils": "0.13.0",
10
- "@prisma-next/tsconfig": "0.13.0",
11
- "@prisma-next/tsdown": "0.13.0",
9
+ "@prisma-next/test-utils": "0.14.0",
10
+ "@prisma-next/tsconfig": "0.14.0",
11
+ "@prisma-next/tsdown": "0.14.0",
12
12
  "tsdown": "0.22.1",
13
13
  "typescript": "5.9.3",
14
14
  "vitest": "4.1.8"
package/src/errors.ts CHANGED
@@ -46,6 +46,25 @@ export class SqlQueryError extends Error implements SqlDriverError<'sql_query'>
46
46
  }
47
47
  }
48
48
 
49
+ /**
50
+ * SQLSTATE for a unique-constraint / primary-key violation — the SQL-standard
51
+ * `unique_violation` class. Drivers normalize their target-specific codes (e.g.
52
+ * Postgres `23505`, SQLite `SQLITE_CONSTRAINT_UNIQUE`/`PRIMARYKEY`) onto this
53
+ * value on {@link SqlQueryError.sqlState}, so consumers classify violations
54
+ * without knowing any target-specific error shape.
55
+ */
56
+ export const UNIQUE_VIOLATION_SQLSTATE = '23505';
57
+
58
+ /**
59
+ * Whether an error is a unique-constraint (or primary-key) violation, decided
60
+ * solely from the driver-normalized {@link SqlQueryError.sqlState}. Raw driver
61
+ * errors that were never normalized are not classified — callers run behind the
62
+ * driver, which always normalizes before the error escapes.
63
+ */
64
+ export function isUniqueConstraintViolation(error: unknown): boolean {
65
+ return SqlQueryError.is(error) && error.sqlState === UNIQUE_VIOLATION_SQLSTATE;
66
+ }
67
+
49
68
  /**
50
69
  * SQL connection error (timeouts, connection resets, etc.).
51
70
  */
@@ -1 +1,6 @@
1
- export { SqlConnectionError, SqlQueryError } from '../errors';
1
+ export {
2
+ isUniqueConstraintViolation,
3
+ SqlConnectionError,
4
+ SqlQueryError,
5
+ UNIQUE_VIOLATION_SQLSTATE,
6
+ } from '../errors';