@veloxts/orm 0.7.5 → 0.7.7
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/CHANGELOG.md +16 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -0
- package/dist/prisma-errors.d.ts +40 -0
- package/dist/prisma-errors.js +79 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @veloxts/orm
|
|
2
2
|
|
|
3
|
+
## 0.7.7
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- refactor(router): rename swaggerUIPlugin → swaggerPlugin, remove redundant exports
|
|
8
|
+
- Updated dependencies
|
|
9
|
+
- @veloxts/core@0.7.7
|
|
10
|
+
|
|
11
|
+
## 0.7.6
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- feat(router): custom access levels for the Resource API + advanced Architectural Patterns
|
|
16
|
+
- Updated dependencies
|
|
17
|
+
- @veloxts/core@0.7.6
|
|
18
|
+
|
|
3
19
|
## 0.7.5
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -38,4 +38,5 @@ export type { ConnectionState, ConnectionStatus, DatabaseClient, DatabaseWrapper
|
|
|
38
38
|
export { isDatabaseClient } from './types.js';
|
|
39
39
|
export type { Database } from './client.js';
|
|
40
40
|
export { createDatabase } from './client.js';
|
|
41
|
+
export { db, handlePrismaError } from './prisma-errors.js';
|
|
41
42
|
export { databasePlugin } from './plugin.js';
|
package/dist/index.js
CHANGED
|
@@ -44,6 +44,10 @@ export const ORM_VERSION = packageJson.version ?? '0.0.0-unknown';
|
|
|
44
44
|
export { isDatabaseClient } from './types.js';
|
|
45
45
|
export { createDatabase } from './client.js';
|
|
46
46
|
// ============================================================================
|
|
47
|
+
// Prisma Error Handling
|
|
48
|
+
// ============================================================================
|
|
49
|
+
export { db, handlePrismaError } from './prisma-errors.js';
|
|
50
|
+
// ============================================================================
|
|
47
51
|
// Plugin
|
|
48
52
|
// ============================================================================
|
|
49
53
|
export { databasePlugin } from './plugin.js';
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prisma Error Wrapper
|
|
3
|
+
*
|
|
4
|
+
* Converts Prisma-specific errors to VeloxTS error classes,
|
|
5
|
+
* providing consistent, domain-specific error messages.
|
|
6
|
+
*
|
|
7
|
+
* @module prisma-errors
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Convert a Prisma error to a VeloxTS error.
|
|
11
|
+
*
|
|
12
|
+
* Handles:
|
|
13
|
+
* - P2002 (Unique constraint) → ConflictError
|
|
14
|
+
* - P2001 / P2018 / P2025 (Record not found) → NotFoundError
|
|
15
|
+
* - P2003 (Foreign key constraint) → ValidationError
|
|
16
|
+
*
|
|
17
|
+
* Throws the original error if it is not a known Prisma error code.
|
|
18
|
+
*
|
|
19
|
+
* @param error - The caught error
|
|
20
|
+
* @throws A VeloxTS error or the original error if not a Prisma error
|
|
21
|
+
*/
|
|
22
|
+
export declare function handlePrismaError(error: unknown): never;
|
|
23
|
+
/**
|
|
24
|
+
* Wraps a Prisma operation, converting known errors to VeloxTS errors.
|
|
25
|
+
*
|
|
26
|
+
* @param operation - An async function performing a Prisma query
|
|
27
|
+
* @returns The result of the operation
|
|
28
|
+
* @throws ConflictError, NotFoundError, ValidationError, or the original error
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { db } from '@veloxts/orm';
|
|
33
|
+
*
|
|
34
|
+
* // Instead of manual try/catch for P2002, P2025, etc.:
|
|
35
|
+
* const user = await db(() => prisma.user.create({ data: input }));
|
|
36
|
+
* // P2002 → ConflictError("A record with this email already exists", ["email"])
|
|
37
|
+
* // P2025 → NotFoundError("User")
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function db<T>(operation: () => Promise<T>): Promise<T>;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prisma Error Wrapper
|
|
3
|
+
*
|
|
4
|
+
* Converts Prisma-specific errors to VeloxTS error classes,
|
|
5
|
+
* providing consistent, domain-specific error messages.
|
|
6
|
+
*
|
|
7
|
+
* @module prisma-errors
|
|
8
|
+
*/
|
|
9
|
+
import { ConflictError, NotFoundError, ValidationError } from '@veloxts/core';
|
|
10
|
+
function isPrismaKnownRequestError(error) {
|
|
11
|
+
return (error instanceof Error &&
|
|
12
|
+
error.name === 'PrismaClientKnownRequestError' &&
|
|
13
|
+
'code' in error &&
|
|
14
|
+
typeof error.code === 'string');
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Convert a Prisma error to a VeloxTS error.
|
|
18
|
+
*
|
|
19
|
+
* Handles:
|
|
20
|
+
* - P2002 (Unique constraint) → ConflictError
|
|
21
|
+
* - P2001 / P2018 / P2025 (Record not found) → NotFoundError
|
|
22
|
+
* - P2003 (Foreign key constraint) → ValidationError
|
|
23
|
+
*
|
|
24
|
+
* Throws the original error if it is not a known Prisma error code.
|
|
25
|
+
*
|
|
26
|
+
* @param error - The caught error
|
|
27
|
+
* @throws A VeloxTS error or the original error if not a Prisma error
|
|
28
|
+
*/
|
|
29
|
+
export function handlePrismaError(error) {
|
|
30
|
+
if (!isPrismaKnownRequestError(error)) {
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
switch (error.code) {
|
|
34
|
+
case 'P2002': {
|
|
35
|
+
const fields = error.meta?.target ?? [];
|
|
36
|
+
const fieldNames = fields.length > 0 ? fields.join(', ') : 'field';
|
|
37
|
+
throw new ConflictError(`A record with this ${fieldNames} already exists`, fields.length > 0 ? fields : undefined);
|
|
38
|
+
}
|
|
39
|
+
case 'P2001':
|
|
40
|
+
case 'P2018':
|
|
41
|
+
case 'P2025': {
|
|
42
|
+
const model = error.meta?.modelName ?? error.meta?.cause ?? 'Record';
|
|
43
|
+
throw new NotFoundError(model);
|
|
44
|
+
}
|
|
45
|
+
case 'P2003': {
|
|
46
|
+
const field = error.meta?.field_name ?? 'field';
|
|
47
|
+
throw new ValidationError(`Related record not found for ${field}`, {
|
|
48
|
+
[field]: 'Related record not found',
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
default:
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Wraps a Prisma operation, converting known errors to VeloxTS errors.
|
|
57
|
+
*
|
|
58
|
+
* @param operation - An async function performing a Prisma query
|
|
59
|
+
* @returns The result of the operation
|
|
60
|
+
* @throws ConflictError, NotFoundError, ValidationError, or the original error
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* import { db } from '@veloxts/orm';
|
|
65
|
+
*
|
|
66
|
+
* // Instead of manual try/catch for P2002, P2025, etc.:
|
|
67
|
+
* const user = await db(() => prisma.user.create({ data: input }));
|
|
68
|
+
* // P2002 → ConflictError("A record with this email already exists", ["email"])
|
|
69
|
+
* // P2025 → NotFoundError("User")
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export async function db(operation) {
|
|
73
|
+
try {
|
|
74
|
+
return await operation();
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
return handlePrismaError(error);
|
|
78
|
+
}
|
|
79
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veloxts/orm",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.7",
|
|
4
4
|
"description": "Prisma wrapper with enhanced DX for VeloxTS framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"fastify": "5.7.4",
|
|
20
20
|
"pg": "8.18.0",
|
|
21
21
|
"pg-format": "1.0.4",
|
|
22
|
-
"@veloxts/core": "0.7.
|
|
22
|
+
"@veloxts/core": "0.7.7"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@types/pg": "8.16.0",
|