Skip to content

Commit 02dd508

Browse files
committed
Enable passing application context in non-public schemas
1 parent 0f4e667 commit 02dd508

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
#### [v0.7.0](https://github.com/BemiHQ/bemi-prisma/compare/v0.6.0...v0.7.0) - 2024-10-24
4+
5+
- Enable passing application context in non-public schemas.
6+
37
#### [v0.6.0](https://github.com/BemiHQ/bemi-prisma/compare/v0.5.0...v0.6.0) - 2024-10-14
48

59
- Fix compatibility with Prisma v5.20+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bemi-db/prisma",
3-
"version": "0.6.0",
3+
"version": "0.7.0",
44
"description": "Automatic data change tracking for Prisma",
55
"main": "dist/index.js",
66
"module": "./dist/index.mjs",

src/cli.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,20 @@ $$ LANGUAGE plpgsql;
2626
CREATE OR REPLACE PROCEDURE _bemi_create_triggers()
2727
AS $$
2828
DECLARE
29+
current_schemaname TEXT;
2930
current_tablename TEXT;
3031
BEGIN
31-
FOR current_tablename IN
32-
SELECT tablename FROM pg_tables
32+
FOR current_schemaname, current_tablename IN
33+
SELECT schemaname, tablename FROM pg_tables
3334
LEFT JOIN information_schema.triggers ON tablename = event_object_table AND schemaname = trigger_schema AND trigger_name LIKE '_bemi_row_trigger_%'
34-
WHERE schemaname = 'public' AND trigger_name IS NULL
35-
GROUP BY tablename
35+
WHERE schemaname NOT IN ('information_schema', 'pg_catalog') AND trigger_name IS NULL
36+
GROUP BY schemaname, tablename
3637
LOOP
3738
EXECUTE format(
38-
'CREATE OR REPLACE TRIGGER _bemi_row_trigger_%s
39-
BEFORE INSERT OR UPDATE OR DELETE ON %I FOR EACH ROW
39+
'CREATE OR REPLACE TRIGGER _bemi_row_trigger_%s_%s
40+
BEFORE INSERT OR UPDATE OR DELETE ON %I.%I FOR EACH ROW
4041
EXECUTE FUNCTION _bemi_row_trigger_func()',
41-
current_tablename, current_tablename
42+
current_schemaname, current_tablename, current_schemaname, current_tablename
4243
);
4344
END LOOP;
4445
END;

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { isContextComment, isWriteQuery, contextToSqlComment } from './pg-utils'
88
import { logger } from './logger'
99

1010
const WRITE_OPERATIONS = ["create", "update", "upsert", "delete", "createMany", "updateMany", "deleteMany"]
11-
const EXECUTE_OPERATIONS = ["$executeRaw", "$executeRawUnsafe"]
11+
const EXECUTE_RAW_UNSAFE_OPERATION = ["$executeRawUnsafe"]
12+
const EXECUTE_OPERATIONS = ["$executeRaw", EXECUTE_RAW_UNSAFE_OPERATION]
1213
const ASYNC_LOCAL_STORAGE = new AsyncLocalStorage();
1314
const MAX_CONTEXT_SIZE = 1000000 // ~ 1MB
1415

@@ -21,6 +22,7 @@ export const withPgAdapter = <PrismaClientType>(
2122
const prisma = (originalPrisma as any).$extends({
2223
query: {
2324
async $allOperations({ args, query, operation, model }: any) {
25+
2426
// Not included model
2527
if (model && includeModels && !includeModels.includes(model)) {
2628
return query(args)
@@ -38,7 +40,7 @@ export const withPgAdapter = <PrismaClientType>(
3840
}
3941

4042
// Injected context query
41-
if (operation === '$executeRawUnsafe' && args[0] && isContextComment(args[0])) {
43+
if (operation === EXECUTE_RAW_UNSAFE_OPERATION && args[0] && isContextComment(args[0])) {
4244
return query(args)
4345
}
4446

0 commit comments

Comments
 (0)