@@ -524,6 +524,8 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
524
524
throw prismaClientValidationError ( this . prisma , 'data field is required in query argument' ) ;
525
525
}
526
526
527
+ args = this . utils . clone ( args ) ;
528
+
527
529
const { result, error } = await this . transaction ( async ( tx ) => {
528
530
// proceed with nested writes and collect post-write checks
529
531
const { result, postWriteChecks } = await this . doUpdate ( args , tx ) ;
@@ -543,8 +545,6 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
543
545
}
544
546
545
547
private async doUpdate ( args : any , db : Record < string , DbOperations > ) {
546
- args = this . utils . clone ( args ) ;
547
-
548
548
// collected post-update checks
549
549
const postWriteChecks : PostWriteCheckRecord [ ] = [ ] ;
550
550
@@ -903,6 +903,8 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
903
903
await this . utils . tryReject ( this . prisma , this . model , 'create' ) ;
904
904
await this . utils . tryReject ( this . prisma , this . model , 'update' ) ;
905
905
906
+ args = this . utils . clone ( args ) ;
907
+
906
908
// We can call the native "upsert" because we can't tell if an entity was created or updated
907
909
// for doing post-write check accordingly. Instead, decompose it into create or update.
908
910
@@ -998,6 +1000,8 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
998
1000
throw prismaClientValidationError ( this . prisma , 'query argument is required' ) ;
999
1001
}
1000
1002
1003
+ args = this . utils . clone ( args ) ;
1004
+
1001
1005
// inject policy conditions
1002
1006
await this . utils . injectAuthGuard ( this . prisma , args , this . model , 'read' ) ;
1003
1007
@@ -1012,6 +1016,8 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
1012
1016
throw prismaClientValidationError ( this . prisma , 'query argument is required' ) ;
1013
1017
}
1014
1018
1019
+ args = this . utils . clone ( args ) ;
1020
+
1015
1021
// inject policy conditions
1016
1022
await this . utils . injectAuthGuard ( this . prisma , args , this . model , 'read' ) ;
1017
1023
@@ -1023,7 +1029,7 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
1023
1029
1024
1030
async count ( args : any ) {
1025
1031
// inject policy conditions
1026
- args = args ?? { } ;
1032
+ args = args ? this . utils . clone ( args ) : { } ;
1027
1033
await this . utils . injectAuthGuard ( this . prisma , args , this . model , 'read' ) ;
1028
1034
1029
1035
if ( this . shouldLogQuery ) {
@@ -1034,6 +1040,55 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
1034
1040
1035
1041
//#endregion
1036
1042
1043
+ //#region Subscribe (Prisma Pulse)
1044
+
1045
+ async subscribe ( args : any ) {
1046
+ const readGuard = this . utils . getAuthGuard ( this . prisma , this . model , 'read' ) ;
1047
+ if ( this . utils . isTrue ( readGuard ) ) {
1048
+ // no need to inject
1049
+ if ( this . shouldLogQuery ) {
1050
+ this . logger . info ( `[policy] \`subscribe\` ${ this . model } :\n${ formatObject ( args ) } ` ) ;
1051
+ }
1052
+ return this . modelClient . subscribe ( args ) ;
1053
+ }
1054
+
1055
+ if ( ! args ) {
1056
+ // include all
1057
+ args = { create : { } , update : { } , delete : { } } ;
1058
+ } else {
1059
+ if ( typeof args !== 'object' ) {
1060
+ throw prismaClientValidationError ( this . prisma , 'argument must be an object' ) ;
1061
+ }
1062
+ if ( Object . keys ( args ) . length === 0 ) {
1063
+ // include all
1064
+ args = { create : { } , update : { } , delete : { } } ;
1065
+ } else {
1066
+ args = this . utils . clone ( args ) ;
1067
+ }
1068
+ }
1069
+
1070
+ // inject into subscribe conditions
1071
+
1072
+ if ( args . create ) {
1073
+ args . create . after = this . utils . and ( args . create . after , readGuard ) ;
1074
+ }
1075
+
1076
+ if ( args . update ) {
1077
+ args . update . after = this . utils . and ( args . update . after , readGuard ) ;
1078
+ }
1079
+
1080
+ if ( args . delete ) {
1081
+ args . delete . before = this . utils . and ( args . delete . before , readGuard ) ;
1082
+ }
1083
+
1084
+ if ( this . shouldLogQuery ) {
1085
+ this . logger . info ( `[policy] \`subscribe\` ${ this . model } :\n${ formatObject ( args ) } ` ) ;
1086
+ }
1087
+ return this . modelClient . subscribe ( args ) ;
1088
+ }
1089
+
1090
+ //#endregion
1091
+
1037
1092
//#region Utils
1038
1093
1039
1094
private get shouldLogQuery ( ) {
0 commit comments