Skip to content

Commit

Permalink
Enable passing application context in non-public schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Oct 25, 2024
1 parent 0f4e667 commit 02dd508
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

#### [v0.7.0](https://github.com/BemiHQ/bemi-prisma/compare/v0.6.0...v0.7.0) - 2024-10-24

- Enable passing application context in non-public schemas.

#### [v0.6.0](https://github.com/BemiHQ/bemi-prisma/compare/v0.5.0...v0.6.0) - 2024-10-14

- Fix compatibility with Prisma v5.20+
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bemi-db/prisma",
"version": "0.6.0",
"version": "0.7.0",
"description": "Automatic data change tracking for Prisma",
"main": "dist/index.js",
"module": "./dist/index.mjs",
Expand Down
15 changes: 8 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ $$ LANGUAGE plpgsql;
CREATE OR REPLACE PROCEDURE _bemi_create_triggers()
AS $$
DECLARE
current_schemaname TEXT;
current_tablename TEXT;
BEGIN
FOR current_tablename IN
SELECT tablename FROM pg_tables
FOR current_schemaname, current_tablename IN
SELECT schemaname, tablename FROM pg_tables
LEFT JOIN information_schema.triggers ON tablename = event_object_table AND schemaname = trigger_schema AND trigger_name LIKE '_bemi_row_trigger_%'
WHERE schemaname = 'public' AND trigger_name IS NULL
GROUP BY tablename
WHERE schemaname NOT IN ('information_schema', 'pg_catalog') AND trigger_name IS NULL
GROUP BY schemaname, tablename
LOOP
EXECUTE format(
'CREATE OR REPLACE TRIGGER _bemi_row_trigger_%s
BEFORE INSERT OR UPDATE OR DELETE ON %I FOR EACH ROW
'CREATE OR REPLACE TRIGGER _bemi_row_trigger_%s_%s
BEFORE INSERT OR UPDATE OR DELETE ON %I.%I FOR EACH ROW
EXECUTE FUNCTION _bemi_row_trigger_func()',
current_tablename, current_tablename
current_schemaname, current_tablename, current_schemaname, current_tablename
);
END LOOP;
END;
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { isContextComment, isWriteQuery, contextToSqlComment } from './pg-utils'
import { logger } from './logger'

const WRITE_OPERATIONS = ["create", "update", "upsert", "delete", "createMany", "updateMany", "deleteMany"]
const EXECUTE_OPERATIONS = ["$executeRaw", "$executeRawUnsafe"]
const EXECUTE_RAW_UNSAFE_OPERATION = ["$executeRawUnsafe"]
const EXECUTE_OPERATIONS = ["$executeRaw", EXECUTE_RAW_UNSAFE_OPERATION]
const ASYNC_LOCAL_STORAGE = new AsyncLocalStorage();
const MAX_CONTEXT_SIZE = 1000000 // ~ 1MB

Expand All @@ -21,6 +22,7 @@ export const withPgAdapter = <PrismaClientType>(
const prisma = (originalPrisma as any).$extends({
query: {
async $allOperations({ args, query, operation, model }: any) {

// Not included model
if (model && includeModels && !includeModels.includes(model)) {
return query(args)
Expand All @@ -38,7 +40,7 @@ export const withPgAdapter = <PrismaClientType>(
}

// Injected context query
if (operation === '$executeRawUnsafe' && args[0] && isContextComment(args[0])) {
if (operation === EXECUTE_RAW_UNSAFE_OPERATION && args[0] && isContextComment(args[0])) {
return query(args)
}

Expand Down

0 comments on commit 02dd508

Please sign in to comment.