File tree Expand file tree Collapse file tree 4 files changed +26
-17
lines changed
powersync-react/src/hooks
powersync-sdk-common/src/client
powersync-sdk-react-native Expand file tree Collapse file tree 4 files changed +26
-17
lines changed Original file line number Diff line number Diff line change 1- import React from " react" ;
2- import { usePowerSync } from " ./PowerSyncContext" ;
1+ import React from ' react' ;
2+ import { usePowerSync } from ' ./PowerSyncContext' ;
33
44/**
55 * A hook to access a single static query.
66 * For an updated result, use usePowerSyncWatchedQuery instead
77 */
8- export const usePowerSyncQuery = < T = any > (
9- sqlStatement : string ,
10- parameters : any [ ] = [ ]
11- ) : T [ ] => {
8+ export const usePowerSyncQuery = < T = any > ( sqlStatement : string , parameters : any [ ] = [ ] ) : T [ ] => {
129 const powerSync = usePowerSync ( ) ;
1310 if ( ! powerSync ) {
1411 return [ ] ;
@@ -19,10 +16,10 @@ export const usePowerSyncQuery = <T = any>(
1916 const [ data , setData ] = React . useState < T [ ] > ( [ ] ) ;
2017
2118 React . useEffect ( ( ) => {
22- powerSync . execute ( sqlStatement , parameters ) . then ( ( result ) => {
23- setData ( result . rows ?. _array ?? [ ] ) ;
19+ powerSync . readLock ( async ( tx ) => {
20+ const result = await tx . getAll < T > ( sqlStatement , parameters ) ;
21+ setData ( result ) ;
2422 } ) ;
25- //
2623 } , [ powerSync , sqlStatement , memoizedParams ] ) ;
2724
2825 return data ;
Original file line number Diff line number Diff line change @@ -406,7 +406,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
406406
407407 async * watch ( sql : string , parameters ?: any [ ] , options ?: SQLWatchOptions ) : AsyncIterable < QueryResult > {
408408 //Fetch initial data
409- yield await this . execute ( sql , parameters ) ;
409+ yield await this . executeReadOnly ( sql , parameters ) ;
410410
411411 const resolvedTables = options ?. tables ?? [ ] ;
412412 if ( ! options ?. tables ) {
@@ -425,7 +425,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
425425 ...( options ?? { } ) ,
426426 tables : resolvedTables
427427 } ) ) {
428- yield await this . execute ( sql , parameters ) ;
428+ yield await this . executeReadOnly ( sql , parameters ) ;
429429 }
430430 }
431431
@@ -476,4 +476,9 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
476476 return ( ) => dispose ( ) ;
477477 } ) ;
478478 }
479+
480+ private async executeReadOnly ( sql : string , params : any [ ] ) {
481+ await this . initialized ;
482+ return this . database . readLock ( ( tx ) => tx . execute ( sql , params ) ) ;
483+ }
479484}
Original file line number Diff line number Diff line change 2727 },
2828 "homepage" : " https://docs.powersync.co/" ,
2929 "peerDependencies" : {
30- "@journeyapps/react-native-quick-sqlite" : " 0.0.0-dev-20231103124824 " ,
30+ "@journeyapps/react-native-quick-sqlite" : " 0.1.1 " ,
3131 "base-64" : " ^1.0.0" ,
3232 "react" : " *" ,
3333 "react-native" : " *" ,
4444 "async-lock" : " ^1.4.0"
4545 },
4646 "devDependencies" : {
47- "@journeyapps/react-native-quick-sqlite" : " 0.0.0-dev-20231103124824 " ,
47+ "@journeyapps/react-native-quick-sqlite" : " 0.1.1 " ,
4848 "@types/async-lock" : " ^1.4.0" ,
4949 "react-native" : " 0.72.4" ,
5050 "react" : " 18.2.0" ,
Original file line number Diff line number Diff line change 20662066 "@types/yargs" "^17.0.8"
20672067 chalk "^4.0.0"
20682068
2069- "@journeyapps/react-native-quick-sqlite@0.0.0-dev-20231103124824":
2070- version "0.0.0-dev-20231103124824"
2071- resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-0.0.0-dev-20231103124824.tgz#c6e3eaa8a568471735a68e0459fb5e6b3541a7cc"
2072- integrity sha512-wBXC0ti3byegZUnCiA4B9Y7GAfLLdyCwoxmPcHQm0cdg3puicZSQNFcZQ+8uVuQUUDhTZpFjXqPaO/AgHwdzNQ==
2069+ "@journeyapps/react-native-quick-sqlite@0.1.0":
2070+ version "0.1.0"
2071+ resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-0.1.0.tgz#51f38f04c477cd8f457465aec48d097d7df85011"
2072+ integrity sha512-uF1R2RGFXhuY1vvjABAR47kuUSATJmf4RWLmTBHtBa8pLkPh/DKqbvCGhO9lsCC8JDzUfY0+xhsCmnQ4t5trow==
2073+ dependencies:
2074+ lodash "^4.17.21"
2075+
2076+ "@journeyapps/react-native-quick-sqlite@0.1.1":
2077+ version "0.1.1"
2078+ resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-0.1.1.tgz#94145dba13b177f6aa42552754e56ecc3b2e7f17"
2079+ integrity sha512-UNoN6aoYOGYtZ+44S/gwP6OVgwEqY4OdVKDugZpYkPeyXi6DVdjWnZ61XlmxY/j2uDMLzvoqpSHjlmrSukpl2Q==
20732080 dependencies:
20742081 lodash "^4.17.21"
20752082 uuid "3.4.0"
You can’t perform that action at this time.
0 commit comments