Skip to content

Database adapters: Share common logic#886

Merged
simolus3 merged 4 commits intomainfrom
db-adapters-shared-logic
Mar 11, 2026
Merged

Database adapters: Share common logic#886
simolus3 merged 4 commits intomainfrom
db-adapters-shared-logic

Conversation

@simolus3
Copy link
Contributor

@simolus3 simolus3 commented Mar 10, 2026

A lof of our DBAdapter implementations are very similar:

  • In all cases, the top-level get(), getOptional() and getAll() implementations delegate to readLock((ctx) => ctx.respectiveMethod(...)).
  • In all cases, transactions are implemented with an internalTransaction method on readLock and writeLock (except for RNQS, where the exact same transaction wrapper is instead defined in @journeyapps/react-native-quick-sqlite).
  • In all cases except Capacitor, get(), getOptional() and getAll() are implemented as wrappers over execute().

Given that the implementations are so similar, we should try to share them! Currently, RNQS, OP-SQLite, Capacitor, web, sql.js and node all use duplicate implementations that are exactly the same. If we add a Tauri SDK in the future, it would also have to duplicate this. This is also annoying for users: If an DBAdapter implementation didn't require around 100 lines of boilerplate, it would be much easier for users to write their own implementations (see e.g. #845 where that's relevant).

So, this:

  1. Adds the ConnectionPool interface which just defines the "intrinsic" top-level methods (name, close, readLock, writeLock and refreshSchema). It adds the DBAdapterDefaultMixin mixin which can turn a class implementing ConnectionPool into a class implementing DBAdapter by delegating to readLock or writeLock, respectively.
  2. Adds the SqlExecutor interface defining the execute, executeBatch and executeRaw methods. Similarly to the connection pool setup, DBGetUtilsDefaultMixin allows implementing get(), getAll and getOptional by wrapping execute.
  3. Uses those mixins to remove duplicate code from the RNQS, OP-SQLite, Capacitor, sql.js and Node implementations.

The web worker setup is left unchanged for now, I'll revisit that in a follow-up PR. The reason is that I believe the web worker setup could benefit from adopting an architecture similar to the one used in the Node SDK, where clients contain most of the logic and workers just execute raw statements. But that's a non-trivial refactoring, so I wanted to get this out first.

I'm relying on existing unit tests for web, sql.js, node and op-sqlite here. I've tested capacitor manually, unfortunately I can't seem to get any RN demos to even compile on my machine.

@changeset-bot
Copy link

changeset-bot bot commented Mar 10, 2026

🦋 Changeset detected

Latest commit: 8e1def5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@powersync/react-native Minor
@powersync/common Minor
@powersync/web Minor
@powersync/op-sqlite Patch
@powersync/capacitor Patch
@powersync/node Patch
@powersync/adapter-sql-js Patch
@powersync/tanstack-react-query Patch
@powersync/diagnostics-app Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@simolus3 simolus3 marked this pull request as ready for review March 10, 2026 17:09
@simolus3 simolus3 requested a review from stevensJourney March 10, 2026 17:09
stevensJourney
stevensJourney previously approved these changes Mar 11, 2026
Copy link
Collaborator

@stevensJourney stevensJourney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really really love these changes. These are a welcome addition. Thank you for implementing this.

I tested the React Native Supabase Todolist (iOS) on my machine. That compiled and ran without issues for me.
I also tested the React Native OP-SQLite barebones app. This had some iOS lockfile issues, but after fixing those - the demo worked normally for me.

@simolus3 simolus3 force-pushed the db-adapters-shared-logic branch from df227c3 to 8e1def5 Compare March 11, 2026 09:22
@simolus3 simolus3 merged commit 42afb0e into main Mar 11, 2026
9 of 10 checks passed
@simolus3 simolus3 deleted the db-adapters-shared-logic branch March 11, 2026 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants