Skip to content

Conversation

evelant
Copy link

@evelant evelant commented Aug 27, 2025

(very) WIP react native support.

Warning: this is definitely not complete or entirely correct. It is proof-of-concept level. There is a lot of leftover verbose logging and incorrect/unused code from iterating upon this. I don't have much C/C++ experience so I used claude code to help write and debug this.

This adds a pglite-react-native package and at least the beginnings of the changes needed for pglite to run on react native. See react-native.md in the corresponding postgres-pglite PR for an overview.

So far the following works:

  1. Builds pglite as a shared library
  2. Adds a pglite-react-native package that uses nitro modules to interface with the shared lib
  3. Adds mobile specific config and conditionals attempting to mirror wasm as much as possible
  4. Adds an example react-native app that does some basic queries
  5. Compiles and works on an android emulator
  6. Some documentation
  7. A script to handle recompiling and reinstalling the example app for testing
  8. A flake.nix for a consistent env with the android ndk and other necessary tools

The following needs works:

  1. iOS not finished or tested
  2. Lots of leftover debug logging
  3. Leftover probably unnecessary code from iterating on this.
  4. Probably lots of crashes and corner cases because the mobile build does not include all of the same conditionals as the wasm build. These likely need to be investigated by someone more knowledgeable than me.

To try it out

  • checkout this branch with submodules so you get postgres-pglite
  • pnpm install and pnpm build (first time only)
  • npm install in packages/pglite-react-native/example
  • npx expo run:android in packages/pglite-react-native/example to start an android emulator, then ctrl+c to stop the rest
  • ./rebuild-mobile.sh in packages/pglite-react-native/example to compile everything, build the mobile app, and install it on the simulator
  • npx expo start -c to start dev server
  • open app in simulator
The rebuild-mobile.sh script automates a lot of the following steps, but you can do these manually if you have problems
  • in postgres-pglite run PLATFORM=android ABI=arm64-v8a PG_BRANCH=REL_17_5_WASM ./mobile-build/build-mobile.sh to build pglite static libs for android. You need nix installed.
  • build-mobile.sh copies static libs into pglite-react-native project
  • in packages/pglite-react-native do pnpm install
  • in packages/pglite-react-native/example
    • npm install to install deps (first run only)
    • npx expo run:android to start android sim (first run only)
    • rm -rf android to clean generated android project (if needed)
    • npx expo prebuild -p android --clean to generate native android project
    • cd android
    • ./gradlew assembleDebug to build the apk
    • adb install app/build/outputs/apk/debug/app-debug.apk to install on device
    • cd ..
    • npx expo start -c to start the react native dev server
    • Use adb logcat to view android system logs for more detail.
    • Open app on the emulator to test.
    • To view more logs
      • adb root to get root adb
      • adb shell to open a shell on device
      • run-as com.evelant.example to run as app user
      • cat files/pglite/runtime/initdb.stderr.log to see backend logs
      • You may need to rm -rf files/pglite/pgdata to clear data for another run after a crash

I hope this serves as a good starting point for robust react-native support!

@evelant
Copy link
Author

evelant commented Aug 30, 2025

Just added initial iOS build proof of concept. PGLite runs on android and ios as a native C library!

@rikur
Copy link

rikur commented Sep 1, 2025

🚀 subscribed

@evelant
Copy link
Author

evelant commented Sep 3, 2025

Fixed a bunch of bugs with the shared memory communication between react native and pglite. The basic tests in the example app seem to work properly now on both iOS and Android when setting up a new db or loading an existing one.

@evelant
Copy link
Author

evelant commented Sep 3, 2025

The basics seem to work now, I think this is proof of concept achieved. To make it production usable there's still some todos:

  • Clean up leftover code from iterating and excessive debug logging
  • Extract base pglite TS package without web dependencies to handle serialization and protocol
  • Get extensions working
  • Fix whatever I touched that has probably broken the wasm build
  • Figure out how to ensure DB doesn't get corrupted by mobile app lifecycle (didn't see it happen in basic tests so far but seems likely to be a problem)
  • Examine the overall approach, look for improvements and simplifications
    • This is where help is needed most from @pmp-p @tdrz @samwillis and others who are far more familiar with the postgres and pglite codebase. I likely didn't implement this in an optimal fashion.

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