Skip to content

Commit

Permalink
update packages. use read only queries for watched queries
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensJourney committed Nov 6, 2023
1 parent 37612c1 commit 1391a69
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
15 changes: 6 additions & 9 deletions packages/powersync-react/src/hooks/usePowerSyncQuery.ts
Original file line number Diff line number Diff line change
@@ -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 = <T = any>(
sqlStatement: string,
parameters: any[] = []
): T[] => {
export const usePowerSyncQuery = <T = any>(sqlStatement: string, parameters: any[] = []): T[] => {
const powerSync = usePowerSync();
if (!powerSync) {
return [];
Expand All @@ -19,10 +16,10 @@ export const usePowerSyncQuery = <T = any>(
const [data, setData] = React.useState<T[]>([]);

React.useEffect(() => {
powerSync.execute(sqlStatement, parameters).then((result) => {
setData(result.rows?._array ?? []);
powerSync.readLock(async (tx) => {
const result = await tx.getAll<T>(sqlStatement, parameters);
setData(result);
});
//
}, [powerSync, sqlStatement, memoizedParams]);

return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB

async *watch(sql: string, parameters?: any[], options?: SQLWatchOptions): AsyncIterable<QueryResult> {
//Fetch initial data
yield await this.execute(sql, parameters);
yield await this.executeReadOnly(sql, parameters);

const resolvedTables = options?.tables ?? [];
if (!options?.tables) {
Expand All @@ -425,7 +425,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
...(options ?? {}),
tables: resolvedTables
})) {
yield await this.execute(sql, parameters);
yield await this.executeReadOnly(sql, parameters);
}
}

Expand Down Expand Up @@ -476,4 +476,9 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
return () => dispose();
});
}

private async executeReadOnly(sql: string, params: any[]) {
await this.initialized;
return this.database.readLock((tx) => tx.execute(sql, params));
}
}
4 changes: 2 additions & 2 deletions packages/powersync-sdk-react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand All @@ -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",
Expand Down
15 changes: 11 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2066,10 +2066,17 @@
"@types/yargs" "^17.0.8"
chalk "^4.0.0"

"@journeyapps/[email protected]":
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/[email protected]":
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/[email protected]":
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"
Expand Down

0 comments on commit 1391a69

Please sign in to comment.