1- import type { QueryResult } from '@powersync/common' ;
2- import { Column , DriverValueDecoder , getTableName , SQL } from 'drizzle-orm' ;
1+ import type { LockContext , QueryResult } from '@powersync/common' ;
2+ import { Column , DriverValueDecoder , SQL , getTableName } from 'drizzle-orm' ;
33import type { Cache } from 'drizzle-orm/cache/core' ;
44import type { WithCacheConfig } from 'drizzle-orm/cache/core/types' ;
55import { entityKind , is } from 'drizzle-orm/entity' ;
@@ -70,10 +70,7 @@ export class PowerSyncSQLitePreparedQuery<
7070 async run ( placeholderValues ?: Record < string , unknown > ) : Promise < QueryResult > {
7171 const params = fillPlaceholders ( this . query . params , placeholderValues ?? { } ) ;
7272 this . logger . logQuery ( this . query . sql , params ) ;
73- /**
74- * Run operations are teated as potential mutations, so they use the write context.
75- */
76- return this . contextProvider . useWriteContext ( async ( ctx ) => {
73+ return this . useContext ( async ( ctx ) => {
7774 const rs = await ctx . execute ( this . query . sql , params ) ;
7875 return rs ;
7976 } ) ;
@@ -90,7 +87,6 @@ export class PowerSyncSQLitePreparedQuery<
9087 }
9188
9289 const rows = ( await this . values ( placeholderValues ) ) as unknown [ ] [ ] ;
93-
9490 if ( customResultMapper ) {
9591 const mapped = customResultMapper ( rows ) as T [ 'all' ] ;
9692 return mapped ;
@@ -128,20 +124,22 @@ export class PowerSyncSQLitePreparedQuery<
128124 const params = fillPlaceholders ( this . query . params , placeholderValues ?? { } ) ;
129125 this . logger . logQuery ( this . query . sql , params ) ;
130126
131- if ( this . readOnly ) {
132- return await this . contextProvider . useReadContext ( async ( ctx ) => {
133- return await ctx . executeRaw ( this . query . sql , params ) ;
134- } ) ;
135- } else {
136- return await this . contextProvider . useWriteContext ( async ( ctx ) => {
137- return await ctx . executeRaw ( this . query . sql , params ) ;
138- } ) ;
139- }
127+ return await this . useContext ( async ( ctx ) => {
128+ return await ctx . executeRaw ( this . query . sql , params ) ;
129+ } ) ;
140130 }
141131
142132 isResponseInArrayMode ( ) : boolean {
143133 return this . _isResponseInArrayMode ;
144134 }
135+
136+ protected useContext < T > ( callback : LockCallback < T > ) : Promise < T > {
137+ if ( this . readOnly ) {
138+ return this . contextProvider . useReadContext ( callback ) ;
139+ } else {
140+ return this . contextProvider . useWriteContext ( callback ) ;
141+ }
142+ }
145143}
146144
147145/**
0 commit comments