Developing a Wasm Wrapper (Advanced)
If you followed the Quick Start, you should have:
- Built a GitHub FDW
- Installed it on Postgres
- Used it to query GitHub events
This guide will show you how to develop the Wasm Wrapper locally to iterate faster. We will adapt GitHub wrapper and instead we will query a Google Sheet.
Install pre-requisites
If you haven't already completed the Quick Start, do that first. For local development:
- install the Supabase CLI (version
>= 1.187.10
is needed). - install Docker
Create a Google Sheet
In this example, we're going to create a Wrapper that can query a Google Sheet.
- Open this Google Sheet.
- Go to
File
>Make a copy
- In your new Sheet, click
Share
(top right), and then change General Access toAnyone with the link
.
You should have your own Google Sheet:
Rename your Wrapper
Open Cargo.toml
and update the Wrapper details:
1 2 3 4 5 6 |
|
Open wit/world.wit
and update the package name and version:
1 |
|
Modify the source code
Tip
You can refer to the Paddle source code to help with your development.
We need to query the Google Sheet using the JSON API:
1 2 |
|
To do this, we need our Wrapper to accept a sheet_id
instead of a GitHub api_url
. The sheet_id
can be found in above Google Sheet URL, in this example it is 1OWi0x39w9FhVFP0EmSRRWWKkzhVXpYeTZJLmvaSKy-o
.
Replace the init()
function with the following code:
src/lib.rs | |
---|---|
1 2 3 4 5 6 7 8 9 10 |
|
Replace the begin_scan()
function with the following code, this function will be called before the foreign table is queried.
src/lib.rs | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
Replace the iter_scan()
function with the following code, this function will be called for each source row.
src/lib.rs | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
Developing locally
We'll use the CLI to develop locally. This will be faster than the GitHub release workflow.
Start Supabase
After the CLI is installed, start the Supabase services. If this is your first time it might take some time to download all the services.
1 2 |
|
Build your wrapper
And then run the script to build the Wasm FDW package and copy it to Supabase database container:
1 |
|
Tip
You can also use it with cargo watch: cargo watch -s ./local-dev.sh
Installing the wrapper
Visit SQL Editor in your local browser, create foreign server and foreign table like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Note
The foreign server option fdw_package_checksum
is not needed for local development.
Now you can query the foreign table like below to see result:
1 2 3 4 |
|
Considerations
Version compatibility
The Wasm FDW (guest) runs inside a Wasm runtime (host) which is provided by the Wrappers Wasm FDW framework. The guest and host versions need to be compatible. We can define the required host version in the guest's host_version_requirement()
function like below:
1 2 3 4 5 |
|
Both guest and host are using Semantic Versioning. The above code means the guest is compatible with host version greater or equal 0.1.0
but less than 0.2.0
. If the version isn't comatible, the Wasm FDW cannot run on that version of host.
All the available host versions are listed here. When you develop your own Wasm FDW, always choose compatible host version properly.
Security
Warning
Never use untrusted Wasm FDW on your database.
Although we have implemented security measures and limited the Wasm runtime environment to a minimal interface, ultimately you are responsible for your data. Never install a Wasm FDW from untrusted source. Always use official sources, like Supabase Wasm FDW, or sources over which you have full visibility and control.
Performance
The Wasm package will be dynamically downloaded and loaded to run on Postgres, so you should make sure the Wasm FDW is small to improve performance. Always build your project in release
mode using the profile specified in the Cargo.toml
file:
1 2 3 |
|
1 2 |
|
Automation
If you host source code on GitHub, the building and release process can be automated, take a look at the example CI workflow file for more details.
Limitations
The Wasm FDW currently only supports data sources which have HTTP(s) based JSON API, other sources such like TCP based DBMS or local files are not supported.
Another limitation is that many 3rd-party Rust libraries don't support wasm32-unknown-unknown
target, we cannot use them in the Wasm FDW project.
Wrap up
When you're ready, you can follow the Release process in the quickstart guide to release a new version of your wrapper.
More examples
Some other Wasm foreign data wrapper projects developed by Supabase team: