POC react native support
(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:
- Builds pglite as a shared library
- Adds a pglite-react-native package that uses nitro modules to interface with the shared lib
- Adds mobile specific config and conditionals attempting to mirror wasm as much as possible
- Adds an example react-native app that does some basic queries
- Compiles and works on an android emulator
- Some documentation
- A script to handle recompiling and reinstalling the example app for testing
- A flake.nix for a consistent env with the android ndk and other necessary tools
The following needs works:
- iOS not finished or tested
- Lots of leftover debug logging
- Leftover probably unnecessary code from iterating on this.
- 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 installandpnpm build(first time only)npm installinpackages/pglite-react-native/examplenpx expo run:androidinpackages/pglite-react-native/exampleto start an android emulator, then ctrl+c to stop the rest./rebuild-mobile.shinpackages/pglite-react-native/exampleto compile everything, build the mobile app, and install it on the simulatornpx expo start -cto 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-pgliterunPLATFORM=android ABI=arm64-v8a PG_BRANCH=REL_17_5_WASM ./mobile-build/build-mobile.shto 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-nativedopnpm install - in
packages/pglite-react-native/examplenpm installto install deps (first run only)npx expo run:androidto start android sim (first run only)rm -rf androidto clean generated android project (if needed)npx expo prebuild -p android --cleanto generate native android projectcd android./gradlew assembleDebugto build the apkadb install app/build/outputs/apk/debug/app-debug.apkto install on devicecd ..npx expo start -cto start the react native dev server- Use
adb logcatto view android system logs for more detail. - Open app on the emulator to test.
- To view more logs
adb rootto get root adbadb shellto open a shell on devicerun-as com.evelant.exampleto run as app usercat files/pglite/runtime/initdb.stderr.logto see backend logs- You may need to
rm -rf files/pglite/pgdatato clear data for another run after a crash
I hope this serves as a good starting point for robust react-native support!
Just added initial iOS build proof of concept. PGLite runs on android and ios as a native C library!
🚀 subscribed
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.
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.
Can we have progress for pglite-react-native?