Skip to content

Add optional initialization hook for static extensions - #96

Open
akpor-kofi wants to merge 1 commit into
powersync-ja:masterfrom
akpor-kofi:feat/powersync-static-extension-init
Open

akpor-kofi wants to merge 1 commit into
powersync-ja:masterfrom
akpor-kofi:feat/powersync-static-extension-init

Conversation

@akpor-kofi

Copy link
Copy Markdown

Summary

Add an optional SQLITE_WASM_EXTRA_INIT callback after successful sqlite3_initialize(). This lets custom builds register statically linked extensions before JavaScript opens databases, using the existing CFILES_EXTRA and WASQLITE_EXTRA_DEFINES options. The default build has no extra callback.

Document a complete decimal.c auto-registration example and the need to deploy matching generated JavaScript/WASM pairs.

Validation

Built the same hook in the PowerSync fork with Emscripten 4.0.9 and SQLite 3.53.0. Through PowerSync's real browser worker, Chrome and WebKit executed 10,000-row exact decimal sums, scalar/window probes, persisted reopen, and offline reads. Chrome IndexedDB/OPFS also passed freeze/wake. The generic hook itself uses only SQLite initialization and auto-extension APIs; no PowerSync dependency is added.

Scratch checks are not committed. Reproduction recipe: https://github.com/akpor-kofi/sqlite-decimal-rn

Changelog / Operational Notes

One optional compile-time callback and documentation. Custom builders add their own C source files and define the callback symbol. No change to default exports, adapters, package dependencies, database schemas, environment variables, permissions, background jobs, or provider integrations.

@simolus3 simolus3 left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the contribution!

I'd prefer a setup where loading statically linked extensions would be similar to what we do for the existing PowerSync extension, which is exposed through a powersync_init_static function exported from the WebAssembly module.

I'm not sure why we're not just calling powersync_init_static() in main (that would probably work too), but it should be consistent. So given that you need to fork these build scripts either way, how about you statically link the extensions you need and add them to powersync_exported_functions.json?

I would be happy with a PR for @powersync/web that could take an array of additional initialization functions to call when opening connections, which would then load them in addition to the core extension. That shouldn't need any wa-sqlite changes.

@akpor-kofi

Copy link
Copy Markdown
Author

Thanks, that makes sense. I'll follow the existing powersync_init_static approach: statically link the extensions and export their initialization functions through powersync_exported_functions.json.

I'll open a PR for @powersync/web to accept an array of additional initialization functions and call them when opening connections, alongside the core extension. I'll link it here once it's ready.

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