@@ -23,6 +23,7 @@ import {
2323 OperatorNode ,
2424 ParensNode ,
2525 PrimitiveValueListNode ,
26+ RawNode ,
2627 ReferenceNode ,
2728 ReturningNode ,
2829 SelectAllNode ,
@@ -63,11 +64,23 @@ export type MutationQueryNode = InsertQueryNode | UpdateQueryNode | DeleteQueryN
6364
6465type FieldLevelPolicyOperations = Exclude < CRUD_EXT , 'create' | 'delete' > ;
6566
67+ export type PolicyHandlerOptions = {
68+ /**
69+ * Dangerously bypasses access-policy enforcement for raw SQL queries.
70+ * Raw queries remain in the current transaction, but the policy plugin will
71+ * not inspect or reject them.
72+ */
73+ dangerouslyAllowRawSql ?: boolean ;
74+ } ;
75+
6676export class PolicyHandler < Schema extends SchemaDef > extends OperationNodeTransformer {
6777 private readonly dialect : BaseCrudDialect < Schema > ;
6878 private readonly eb = expressionBuilder < any , any > ( ) ;
6979
70- constructor ( private readonly client : ClientContract < Schema > ) {
80+ constructor (
81+ private readonly client : ClientContract < Schema > ,
82+ private readonly options : PolicyHandlerOptions = { } ,
83+ ) {
7184 super ( ) ;
7285 this . dialect = getCrudDialect ( this . client . $schema , this . client . $options ) ;
7386 }
@@ -76,6 +89,9 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
7689
7790 async handle ( node : RootOperationNode , proceed : ProceedKyselyQueryFunction ) {
7891 if ( ! this . isCrudQueryNode ( node ) ) {
92+ if ( this . options . dangerouslyAllowRawSql && RawNode . is ( node as never ) ) {
93+ return proceed ( node ) ;
94+ }
7995 // non-CRUD queries are not allowed
8096 throw createRejectedByPolicyError (
8197 undefined ,
0 commit comments