diff --git a/packages/powersync-react/src/hooks/usePowerSyncQuery.ts b/packages/powersync-react/src/hooks/usePowerSyncQuery.ts index b8520e09..0fed38d0 100644 --- a/packages/powersync-react/src/hooks/usePowerSyncQuery.ts +++ b/packages/powersync-react/src/hooks/usePowerSyncQuery.ts @@ -1,14 +1,11 @@ -import React from "react"; -import { usePowerSync } from "./PowerSyncContext"; +import React from 'react'; +import { usePowerSync } from './PowerSyncContext'; /** * A hook to access a single static query. * For an updated result, use usePowerSyncWatchedQuery instead */ -export const usePowerSyncQuery = ( - sqlStatement: string, - parameters: any[] = [] -): T[] => { +export const usePowerSyncQuery = (sqlStatement: string, parameters: any[] = []): T[] => { const powerSync = usePowerSync(); if (!powerSync) { return []; @@ -19,10 +16,10 @@ export const usePowerSyncQuery = ( const [data, setData] = React.useState([]); React.useEffect(() => { - powerSync.execute(sqlStatement, parameters).then((result) => { - setData(result.rows?._array ?? []); + powerSync.readLock(async (tx) => { + const result = await tx.getAll(sqlStatement, parameters); + setData(result); }); - // }, [powerSync, sqlStatement, memoizedParams]); return data; diff --git a/packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts b/packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts index 026d69f5..af13afe2 100644 --- a/packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts +++ b/packages/powersync-sdk-common/src/client/AbstractPowerSyncDatabase.ts @@ -406,7 +406,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver { //Fetch initial data - yield await this.execute(sql, parameters); + yield await this.executeReadOnly(sql, parameters); const resolvedTables = options?.tables ?? []; if (!options?.tables) { @@ -425,7 +425,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver dispose(); }); } + + private async executeReadOnly(sql: string, params: any[]) { + await this.initialized; + return this.database.readLock((tx) => tx.execute(sql, params)); + } } diff --git a/packages/powersync-sdk-react-native/package.json b/packages/powersync-sdk-react-native/package.json index 796d69ba..4365904a 100644 --- a/packages/powersync-sdk-react-native/package.json +++ b/packages/powersync-sdk-react-native/package.json @@ -27,7 +27,7 @@ }, "homepage": "https://docs.powersync.co/", "peerDependencies": { - "@journeyapps/react-native-quick-sqlite": "0.0.0-dev-20231103124824", + "@journeyapps/react-native-quick-sqlite": "0.1.1", "base-64": "^1.0.0", "react": "*", "react-native": "*", @@ -44,7 +44,7 @@ "async-lock": "^1.4.0" }, "devDependencies": { - "@journeyapps/react-native-quick-sqlite": "0.0.0-dev-20231103124824", + "@journeyapps/react-native-quick-sqlite": "0.1.1", "@types/async-lock": "^1.4.0", "react-native": "0.72.4", "react": "18.2.0", diff --git a/yarn.lock b/yarn.lock index 8316ed1b..2cab165c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2066,10 +2066,17 @@ "@types/yargs" "^17.0.8" chalk "^4.0.0" -"@journeyapps/react-native-quick-sqlite@0.0.0-dev-20231103124824": - version "0.0.0-dev-20231103124824" - resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-0.0.0-dev-20231103124824.tgz#c6e3eaa8a568471735a68e0459fb5e6b3541a7cc" - integrity sha512-wBXC0ti3byegZUnCiA4B9Y7GAfLLdyCwoxmPcHQm0cdg3puicZSQNFcZQ+8uVuQUUDhTZpFjXqPaO/AgHwdzNQ== +"@journeyapps/react-native-quick-sqlite@0.1.0": + version "0.1.0" + resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-0.1.0.tgz#51f38f04c477cd8f457465aec48d097d7df85011" + integrity sha512-uF1R2RGFXhuY1vvjABAR47kuUSATJmf4RWLmTBHtBa8pLkPh/DKqbvCGhO9lsCC8JDzUfY0+xhsCmnQ4t5trow== + dependencies: + lodash "^4.17.21" + +"@journeyapps/react-native-quick-sqlite@0.1.1": + version "0.1.1" + resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-0.1.1.tgz#94145dba13b177f6aa42552754e56ecc3b2e7f17" + integrity sha512-UNoN6aoYOGYtZ+44S/gwP6OVgwEqY4OdVKDugZpYkPeyXi6DVdjWnZ61XlmxY/j2uDMLzvoqpSHjlmrSukpl2Q== dependencies: lodash "^4.17.21" uuid "3.4.0"