You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+55-34Lines changed: 55 additions & 34 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,37 @@
1
-
# Wasm Foreign Data Wrapper Example
1
+
# Postgres Wasm FDW [Template]
2
2
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).
4
4
5
-
## How to use this project
5
+
This example reads the [realtime GitHub events](https://api.github.com/events) into a Postgres database.
6
6
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
8
35
9
36
### Install prerequisites
10
37
@@ -22,20 +49,6 @@ This project is a normal Rust library project, so we will assume you have instal
22
49
cargo install cargo-component
23
50
```
24
51
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
-
39
52
### Implement the foreign data wrapper logic
40
53
41
54
To implement the foreign data wrapper logic, we need to implement the `Guest` trait:
@@ -66,9 +79,8 @@ pub trait Guest {
66
79
}
67
80
```
68
81
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.
70
82
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.
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.
82
94
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
84
100
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:
86
102
87
103
```sql
88
-
SELECT*FROM pg_available_extension_versions WHERE name ='wrappers';
104
+
select*
105
+
from pg_available_extension_versions
106
+
where name ='wrappers';
89
107
```
90
108
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:
92
112
93
113
```sql
94
114
create extension if not exists wrappers with schema extensions;
@@ -147,7 +167,7 @@ limit 5;
147
167
148
168
:clap::clap: Congratulations! You have built your first Wasm FDW.
149
169
150
-
## Other considerations
170
+
## Considerations
151
171
152
172
### Version compatibility
153
173
@@ -163,18 +183,18 @@ impl Guest for ExampleFdw {
163
183
164
184
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.
165
185
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.
167
187
168
188
### Security
169
189
170
190
> [!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.
172
192
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.
174
194
175
195
### Performance
176
196
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:
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
+
193
214
194
-
## More Wasm foreign data wrapper projects
215
+
## Other examples
195
216
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:
197
218
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)
0 commit comments