diff --git a/packages/powersync-sdk-react-native/package.json b/packages/powersync-sdk-react-native/package.json index 02abac1c..61ffe8bf 100644 --- a/packages/powersync-sdk-react-native/package.json +++ b/packages/powersync-sdk-react-native/package.json @@ -24,7 +24,7 @@ }, "homepage": "https://docs.powersync.co/", "peerDependencies": { - "@journeyapps/react-native-quick-sqlite": "^0.0.1", + "@journeyapps/react-native-quick-sqlite": "0.0.0-dev-20231023173642", "base-64": "^1.0.0", "react": "*", "react-native-fetch-api": "^3.0.0", @@ -40,7 +40,7 @@ "async-lock": "^1.4.0" }, "devDependencies": { - "@journeyapps/react-native-quick-sqlite": "^0.0.1", + "@journeyapps/react-native-quick-sqlite": "0.0.0-dev-20231023173642", "@types/async-lock": "^1.4.0", "react": "18.2.0", "typescript": "^4.1.3" diff --git a/packages/powersync-sdk-react-native/src/db/adapters/react-native-quick-sqlite/RNQSDBOpenFactory.ts b/packages/powersync-sdk-react-native/src/db/adapters/react-native-quick-sqlite/RNQSDBOpenFactory.ts index bb8112da..c44530be 100644 --- a/packages/powersync-sdk-react-native/src/db/adapters/react-native-quick-sqlite/RNQSDBOpenFactory.ts +++ b/packages/powersync-sdk-react-native/src/db/adapters/react-native-quick-sqlite/RNQSDBOpenFactory.ts @@ -1,4 +1,4 @@ -import { open } from '@journeyapps/react-native-quick-sqlite'; +import { open, QuickSQLite, QuickSQLiteConnection } from '@journeyapps/react-native-quick-sqlite'; import { AbstractPowerSyncDatabase, @@ -19,7 +19,22 @@ export class RNQSPowerSyncDatabaseOpenFactory extends AbstractPowerSyncDatabaseO * in the options (if provided) * https://github.com/margelo/react-native-quick-sqlite/blob/main/README.md#loading-existing-dbs */ - return new RNQSDBAdapter(open(this.options.dbFilename, { location: this.options.dbLocation })); + const { dbFilename } = this.options; + const openOptions = { location: this.options.dbLocation }; + let DB: QuickSQLiteConnection; + try { + // Hot reloads can sometimes clear global JS state, but not close DB on native side + DB = open(dbFilename, openOptions); + } catch (ex) { + if (ex.message.includes('already open')) { + QuickSQLite.close(dbFilename); + DB = open(dbFilename, openOptions); + } else { + throw ex; + } + } + + return new RNQSDBAdapter(DB); } generateInstance(options: PowerSyncDatabaseOptions): AbstractPowerSyncDatabase { diff --git a/yarn.lock b/yarn.lock index d552a964..1ece8051 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2134,10 +2134,12 @@ "@types/yargs" "^17.0.8" chalk "^4.0.0" -"@journeyapps/react-native-quick-sqlite@0.0.1", "@journeyapps/react-native-quick-sqlite@^0.0.1": - version "0.0.1" - resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-0.0.1.tgz#a5b731bfc658d6c8257ef3edebf243941a69a764" - integrity sha512-vUXWngjpTFX6xCsy6Y8jd9zcI1eajIw/rWfdkpJOdnazR0SzAmrMc0ViRB6K84fWq9CmAV3DZw+GCVEHubqhSw== +"@journeyapps/react-native-quick-sqlite@0.0.0-dev-20231023173642": + version "0.0.0-dev-20231023173642" + resolved "https://registry.npmjs.org/@journeyapps/react-native-quick-sqlite/-/react-native-quick-sqlite-0.0.0-dev-20231023173642.tgz#00cfd6aec9674519ffbbc06245e7ce4ce42a0bcb" + integrity sha512-Na4XDi2u5AUZw+/mflV6LLIHJJvfvJxp5uYMDZtG+4WZ4dw0NDq+sZk+U26T/IU9xgdDWlcmwVKku9dDHLt7eg== + dependencies: + lodash "^4.17.21" "@jridgewell/gen-mapping@^0.3.0", "@jridgewell/gen-mapping@^0.3.2": version "0.3.3"