Skip to content

Commit f9ca628

Browse files
authored
Merge branch 'main' into bo/feat/ci
2 parents 83929b3 + 2f50b8b commit f9ca628

File tree

1 file changed

+55
-34
lines changed

1 file changed

+55
-34
lines changed

README.md

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
1-
# Wasm Foreign Data Wrapper Example
1+
# Postgres Wasm FDW [Template]
22

3-
This repo is an example WebAssembly Foreign Data Wrapper (FDW) project. This project demostrates how to use [Wrappers Wasm FDW framework](https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/wasm_fdw) to develop a foreign data wrapper which reads the [realtime GitHub events](https://api.github.com/events) into Postgres database.
3+
This project demostrates how to create a Postgres Foreign Data Wrapper with Wasm, using the [Wasm FDW framework](https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/wasm_fdw).
44

5-
## How to use this project
5+
This example reads the [realtime GitHub events](https://api.github.com/events) into a Postgres database.
66

7-
You can use this example as the start point to develop your own foreign data wrapper. Firstly, fork this project to your own GitHub account by clicking the `Fork` button on top of this page, and then clone your forked project to local.
7+
8+
## Project Structure
9+
10+
```bash
11+
├── src
12+
│   └── lib.rs # The package source code. We will implement the FDW logic, in this file.
13+
├── supabase-wrappers-wit # The Wasm Interface Type provided by Supabase. See below for a detailed description.
14+
│   ├── http.wit
15+
│   ├── jwt.wit
16+
│   ├── routines.wit
17+
│   ├── stats.wit
18+
│   ├── time.wit
19+
│   ├── types.wit
20+
│   ├── utils.wit
21+
│   └── world.wit
22+
└── wit # The WIT interface this project will use to build the Wasm package.
23+
└── world.wit
24+
```
25+
26+
A [Wasm Interface Type](https://github.com/bytecodealliance/wit-bindgen) (WIT) defines the interfaces between the Wasm FDW (guest) and the Wasm runtime (host). For example, the `http.wit` defines the HTTP related types and functions can be used in the guest, and the `routines.wit` defines the functions the guest needs to implement.
27+
28+
## Getting started
29+
30+
You can use this example as the start point to develop your own FDW.
31+
32+
### Fork
33+
34+
Fork this project to your own GitHub account by clicking the `Fork` button on top of this page, and then clone your forked project to your local machine
835

936
### Install prerequisites
1037

@@ -22,20 +49,6 @@ This project is a normal Rust library project, so we will assume you have instal
2249
cargo install cargo-component
2350
```
2451

25-
### Project folder structure
26-
27-
- src/lib.rs
28-
29-
The package source code, we will implement the `Guest` trait, which contains the FDW logic, in this file.
30-
31-
- supabase-wrappers-wit/
32-
33-
The [Wasm Interface Type](https://github.com/bytecodealliance/wit-bindgen) (WIT) provided by Supabase, it defines the interfaces between the Wasm FDW (guest) and the Wasm runtime (host). For example, the `http.wit` defines the HTTP related types and functions can be used in the guest, and the `routines.wit` defines the functions the guest needs to implement.
34-
35-
- wit/
36-
37-
The WIT interface this project will use to build the Wasm package.
38-
3952
### Implement the foreign data wrapper logic
4053

4154
To implement the foreign data wrapper logic, we need to implement the `Guest` trait:
@@ -66,9 +79,8 @@ pub trait Guest {
6679
}
6780
```
6881

69-
Implement those functions is smiliar to implement a native Wrappers FDW, check out [Supabase Wrappers Framework](https://docs.rs/supabase-wrappers/0.1.18/supabase_wrappers/) for more details.
7082

71-
For this example project, we can check out the details in the `src/lib.rs` file to see how to read GitHub Events endpoint. Note this package will be built to `wasm32-unknown-unknown` target, which is a limited execution environment and lack of most stdlib functionalities such as file and socket IO. Also note that many 3rd parties libs on crates.io are not supporting the `wasm32-unknown-unknown` target, so choose dependencies carefully.
83+
Check out the details in the `src/lib.rs` file to see how to read GitHub Events endpoint. This package will be built to `wasm32-unknown-unknown` target, which is a limited execution environment and lack of most stdlib functionalities such as file and socket IO. Also note that many 3rd parties libs on crates.io are not supporting the `wasm32-unknown-unknown` target, so choose your dependencies carefully.
7284

7385
### Build the Wasm FDW package
7486

@@ -80,15 +92,23 @@ cargo component build --release --target wasm32-unknown-unknown
8092

8193
This will build the Wasm file in `target/wasm32-unknown-unknown/release/wasm_fdw_example.wasm`. This is the Wasm FDW package can be used on Supabase platform.
8294

83-
## Use this example foreign data wrapper on Supabase platform
95+
## Use with Supabase
96+
97+
You can use your Wasm FDW on the Supabase platform as long as the Wrappers extension version is `>=0.4.1`.
98+
99+
### Checking Wrappers version
84100

85-
To use our own Wasm FDW on Supabase platform, make sure the Wrappers extension version is `>=0.4.1`. Go to `SQL Editor` in Supabase Studio and run below query to check its version:
101+
Go to `SQL Editor` in Supabase Studio and run below query to check its version:
86102

87103
```sql
88-
SELECT * FROM pg_available_extension_versions WHERE name = 'wrappers';
104+
select *
105+
from pg_available_extension_versions
106+
where name = 'wrappers';
89107
```
90108

91-
And then install the Wrappers extension and create Wasm foreign data wrapper:
109+
### Installing your Wasm FDW
110+
111+
Install the Wrappers extension and initialize the Wasm FDW:
92112

93113
```sql
94114
create extension if not exists wrappers with schema extensions;
@@ -147,7 +167,7 @@ limit 5;
147167

148168
:clap: :clap: Congratulations! You have built your first Wasm FDW.
149169

150-
## Other considerations
170+
## Considerations
151171

152172
### Version compatibility
153173

@@ -163,18 +183,18 @@ impl Guest for ExampleFdw {
163183

164184
Both guest and host are using [Semantic Versioning](https://docs.rs/semver/latest/semver/enum.Op.html). 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, this Wasm FDW cannot run on that version of host.
165185

166-
All the available host versions are list in here: https://github.com/supabase/wrappers/blob/main/wrappers/src/fdw/wasm_fdw/README.md. When you develop your own Wasm FDW, always choose compatible host version properly.
186+
All the available host versions are listed here: https://github.com/supabase/wrappers/blob/main/wrappers/src/fdw/wasm_fdw/README.md. When you develop your own Wasm FDW, always choose compatible host version properly.
167187

168188
### Security
169189

170190
> [!WARNING]
171-
> Never use untrusted Wasm FDW on your database, as malicious Wasm FDW may cause foreign table data breach.
191+
> Never use untrusted Wasm FDW on your database.
172192
173-
Although we have implemented intensive security approaches and limited the Wasm runtime environment to minimal, the data breach can still happen if you used Wasm FDW from untrusted source. Always use official sources, like [Supabase Wasm FDW](https://github.com/supabase/wrappers/tree/main/wasm-wrappers), or the sources you have full visibility and control.
193+
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](https://github.com/supabase/wrappers/tree/main/wasm-wrappers), or sources which you have full visibility and control.
174194

175195
### Performance
176196

177-
Because the Wasm package will be dynamically downloaded and loaded to run on Postgres, we need to make sure the Wasm FDW small and performant. We will always build this project into `release` mode with profile specified in the `Cargo.toml` file:
197+
The Wasm package will be dynamically downloaded and loaded to run on Postgres, and 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:
178198

179199
```toml
180200
[profile.release]
@@ -189,11 +209,12 @@ cargo component build --release --target wasm32-unknown-unknown
189209

190210
### Automation
191211

192-
If you host the project source code on GitHub, the building and release process can be automated, take a look at the `.github/workflow/release_wasm_fdw.yml` file to see an example.
212+
If you host source code on GitHub, the building and release process can be automated, take a look at the `.github/workflow/release_wasm_fdw.yml` file to see an example of CI workflow.
213+
193214

194-
## More Wasm foreign data wrapper projects
215+
## Other examples
195216

196-
There are some other Wasm foreign data wrapper projects developed by Supabase team, you can check them out to see more examples.
217+
Some other Wasm foreign data wrapper projects developed by Supabase team:
197218

198-
- [Snowflake Wasm foreign data wrapper](https://github.com/supabase/wrappers/tree/main/wasm-wrappers/fdw/snowflake_fdw)
199-
- [Paddle Wasm foreign data wrapper](https://github.com/supabase/wrappers/tree/main/wasm-wrappers/fdw/paddle_fdw)
219+
- [Snowflake Wasm FDW](https://github.com/supabase/wrappers/tree/main/wasm-wrappers/fdw/snowflake_fdw)
220+
- [Paddle Wasm FDW](https://github.com/supabase/wrappers/tree/main/wasm-wrappers/fdw/paddle_fdw)

0 commit comments

Comments
 (0)