Skip to content

Commit 987d4c1

Browse files
committed
fix: made finance client readonly
1 parent 8d17205 commit 987d4c1

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ main
2626

2727
.env
2828
node_modules
29-
dist
29+
dist
30+
31+
packages/finance-prisma-client/prisma/generated

packages/finance-prisma-client/src/index.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
11
import { PrismaClient, Prisma } from '../prisma/generated/client';
22

33
/**
4-
* Creates a Prisma client instance for the finance database
4+
* Write operations that should be blocked on the readonly client
5+
*/
6+
const BLOCKED_WRITE_OPERATIONS = [
7+
'create',
8+
'createMany',
9+
'update',
10+
'updateMany',
11+
'upsert',
12+
'delete',
13+
'deleteMany',
14+
] as const;
15+
16+
/**
17+
* Creates a read-only Prisma client instance for the finance database.
18+
* This client only allows read operations (findMany, findFirst, findUnique, count, aggregate, etc.).
19+
* Write operations (create, update, delete, etc.) will throw an error.
20+
*
521
* @param connectionString - Database connection string
622
* @param options - Optional Prisma client options (logging, transaction timeout, etc.)
7-
* @returns Configured PrismaClient instance
23+
* @returns Configured read-only PrismaClient instance
824
*/
925
export function createFinancePrismaClient(
1026
connectionString: string,
1127
options?: Omit<Prisma.PrismaClientOptions, 'datasources'>
1228
): PrismaClient {
13-
return new PrismaClient({
29+
const client = new PrismaClient({
1430
...options,
1531
datasources: {
1632
db: {
1733
url: connectionString,
1834
},
1935
},
2036
});
37+
38+
// Use Prisma middleware to block write operations
39+
// Type assertion needed because $use may not be in the type definition
40+
(client as any).$use(async (params: any, next: any) => {
41+
// Check if this is a write operation
42+
if (BLOCKED_WRITE_OPERATIONS.includes(params.action)) {
43+
throw new Error(
44+
`Write operation '${params.model}.${params.action}' is not allowed on read-only finance Prisma client. ` +
45+
'This client only supports read operations (findMany, findFirst, findUnique, count, aggregate, groupBy, etc.).'
46+
);
47+
}
48+
49+
// Allow read operations and other allowed operations
50+
return next(params);
51+
});
52+
53+
return client;
2154
}
2255

2356
// Re-export Prisma types and enums for use in consuming applications

0 commit comments

Comments
 (0)