Skip to content

Commit

Permalink
Release v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Aug 8, 2024
1 parent 733b381 commit bc64069
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

#### [v0.5.0](https://github.com/BemiHQ/bemi-prisma/compare/v0.4.1...v0.5.0) - 2024-08-08

- Allow context passing only for specific models with `includeModels`
- Enable SQL comment affix customization

#### [v0.4.1](https://github.com/BemiHQ/bemi-prisma/compare/v0.4.0...v0.4.1) - 2024-08-02

- Fix compatibility with Prisma v5.15+
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.4.1",
"version": "0.5.0",
"description": "Automatic data change tracking for Prisma",
"main": "dist/index.js",
"module": "./dist/index.mjs",
Expand Down
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ const EXECUTE_OPERATIONS = ["$executeRaw", "$executeRawUnsafe"]
const ASYNC_LOCAL_STORAGE = new AsyncLocalStorage();
const MAX_CONTEXT_SIZE = 1000000 // ~ 1MB

export const withPgAdapter = <PrismaClientType>(originalPrisma: PrismaClientType): PrismaClientType => {
export const withPgAdapter = <PrismaClientType>(
originalPrisma: PrismaClientType,
{ includeModels }: { includeModels?: string[] } = {},
): PrismaClientType => {
const { logQueries } = (originalPrisma as any)._engineConfig

const prisma = (originalPrisma as any).$extends({
query: {
async $allOperations({ args, query, operation }: any) {
async $allOperations({ args, query, operation, model }: any) {
// Not included model
if (model && includeModels && !includeModels.includes(model)) {
return query(args)
}

// Not contextualizable query
if (
!WRITE_OPERATIONS.includes(operation) &&
Expand Down
8 changes: 5 additions & 3 deletions src/pg-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Query } from '@prisma/driver-adapter-utils'
import pg from 'pg'

const SQL_COMMENT_AFFIX = process.env.BEMI_SQL_COMMENT_AFFIX || 'Bemi'

export interface StdClient extends pg.Pool {
logQueries: boolean
}
Expand All @@ -14,15 +16,15 @@ export interface TransactionClient extends pg.PoolClient {
export const EMPTY_RESULT = { rowCount: null, fields: [], command: '', oid: 0, rows: [] } as pg.QueryResult

export const contextToSqlComment = (context: any): string => {
return `/*Bemi ${JSON.stringify(context)} Bemi*/`
return `/*${SQL_COMMENT_AFFIX} ${JSON.stringify(context)} ${SQL_COMMENT_AFFIX}*/`
}

export const sqlCommentToContext = (sql: string): any => {
return JSON.parse(sql.replace('/*Bemi ', '').replace(' Bemi*/', ''))
return JSON.parse(sql.replace(`/*${SQL_COMMENT_AFFIX} `, '').replace(` ${SQL_COMMENT_AFFIX}*/`, ''))
}

export const isContextComment = (sql: string): boolean => {
return sql.startsWith('/*Bemi') && sql.endsWith('Bemi*/')
return sql.startsWith(`/*${SQL_COMMENT_AFFIX}`) && sql.endsWith(`${SQL_COMMENT_AFFIX}*/`)
}

export const isWriteQuery = (sql: string): boolean => {
Expand Down

0 comments on commit bc64069

Please sign in to comment.