@@ -11,15 +11,17 @@ import {
1111 createTableRelationsHelpers ,
1212 extractTablesRelationalConfig ,
1313 ExtractTablesWithRelations ,
14+ TableRelationalConfig ,
1415 type RelationalSchemaConfig ,
1516 type TablesRelationalConfig
1617} from 'drizzle-orm/relations' ;
17- import { SQLiteTransaction } from 'drizzle-orm/sqlite-core' ;
18+ import { SQLiteSession , SQLiteTable , SQLiteTransaction } from 'drizzle-orm/sqlite-core' ;
1819import { BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core/db' ;
1920import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core/dialect' ;
21+ import { RelationalQueryBuilder } from 'drizzle-orm/sqlite-core/query-builders/query' ;
2022import type { DrizzleConfig } from 'drizzle-orm/utils' ;
2123import { toCompilableQuery } from './../utils/compilableQuery.js' ;
22- import { PowerSyncSQLiteTransactionConfig } from './PowerSyncSQLiteBaseSession.js' ;
24+ import { PowerSyncSQLiteBaseSession , PowerSyncSQLiteTransactionConfig } from './PowerSyncSQLiteBaseSession.js' ;
2325import { PowerSyncSQLiteSession } from './PowerSyncSQLiteSession.js' ;
2426
2527export type DrizzleQuery < T > = { toSQL ( ) : Query ; execute ( ) : Promise < T | T [ ] > } ;
@@ -54,6 +56,37 @@ export class PowerSyncSQLiteDatabase<
5456
5557 super ( 'async' , dialect , session as any , schema as any ) ;
5658 this . db = db ;
59+
60+ /**
61+ * A hack in order to use read locks for `db.query.users.findMany()` etc queries.
62+ * We don't currently get queryMetadata for these queries, so we can't use the regular session.
63+ * This session always uses read locks.
64+ */
65+ const querySession = new PowerSyncSQLiteBaseSession (
66+ {
67+ useReadContext : ( callback ) => db . readLock ( callback ) ,
68+ useWriteContext : ( callback ) => db . readLock ( callback )
69+ } ,
70+ dialect ,
71+ schema ,
72+ {
73+ logger
74+ }
75+ ) ;
76+ if ( this . _ . schema ) {
77+ for ( const [ tableName , columns ] of Object . entries ( this . _ . schema ) ) {
78+ this . query [ tableName as keyof typeof this . query ] = new RelationalQueryBuilder (
79+ 'async' ,
80+ schema ! . fullSchema ,
81+ this . _ . schema ,
82+ this . _ . tableNamesMap ,
83+ schema ! . fullSchema [ tableName ] as SQLiteTable ,
84+ columns as TableRelationalConfig ,
85+ dialect ,
86+ querySession as SQLiteSession < any , any , any , any > as any
87+ ) as any ;
88+ }
89+ }
5790 }
5891
5992 transaction < T > (
0 commit comments