Skip to content

[POC] SQL.js #647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft

[POC] SQL.js #647

wants to merge 6 commits into from

Conversation

stevensJourney
Copy link
Collaborator

@stevensJourney stevensJourney commented Jun 30, 2025

Overview

Certain environments have trouble loading PowerSync due to it requiring a SQLite implementation which supports loading our Rust core extension. One of these environments is Expo Go which is a prebuilt app container for Expo apps.

This POC uses a pure JS implementation of SQLite via SQL.js. SQLite and our Rust core extension are compiled to JavaScript via Emscripen. This pure JS payload can be used in environments such as Expo Go.

SQL.js already has a pure JS build, this POC locally links PowerSync into that build process. This POC applies Git patches to the SQL.js repo and inits a build.

A small DBAdapter is included for SQL.js. This POC currently uses this adapter in the React Native Supabase Todolist Demo. This demo can now be started in Expo GO.

Outstanding work

  • There were some weird issues with using SQL.js update hooks. I've added our own implementation as a workaround.
  • SQL.js operates in memory. We can persist the entire DB to disk on commit though. This has not yet been implemented for RN.
  • This currently builds SQL.js in the monorepo. This requires toolchains which we should not expect all users of the monorepo to have present. We could fallback to prebuilt assets in such cases.
  • Topology: The packages added here were just to get something working as fast as possible. We might consider adding this pure JS driver to the RN package.

Demo

ExpoGo2.mp4

Copy link

changeset-bot bot commented Jun 30, 2025

⚠️ No Changeset found

Latest commit: c52828e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@@ -14,6 +14,7 @@
"@powersync/attachments": "workspace:*",
"@powersync/common": "workspace:*",
"@powersync/react": "workspace:*",
"@powersync/dev-adapter": "workspace:*",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've put this driver in it's own package for now.

SQL.js is relatively large. About 2Mb. Alternatively we could publish this under a different export e.g. @powersync/common/driver

commonjs({}),
alias({
entries: [
{ find: 'fs', replacement: path.resolve(__dirname, 'vendored/empty.js') },
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The default Emscripten output contains code like require("fs"). This seems to be unreachable, but Metro complains when it detects it. This replaces the requires to return empty modules.

}
}

(globalThis as any).onSqliteUpdate = (
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The standard update hook function did not trigger for me. I've implemented this as a workaround.

+ /** TODO
+ @return {number} the last insert id
+ */
+ Database.prototype["lastInsertId"] = function lastInsertId() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was not exposed by SQL.js, but we need it in order to validate the response from checkpoint validations.

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.

1 participant