Skip to content

Commit db37449

Browse files
authored
Merge pull request #8 from supabase-community/bo/feat/local-dev
feat: add script for local dev and updated README
2 parents 2167faf + 85336cb commit db37449

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

README.md

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@ This project demostrates how to create a Postgres Foreign Data Wrapper with Wasm
44

55
This example reads the [realtime GitHub events](https://api.github.com/events) into a Postgres database.
66

7+
## Table of contents
8+
9+
- [Project Structure](#project-structure)
10+
- [Getting started](#getting-started)
11+
- [Create project](#create-project)
12+
- [Install prerequisites](#install-prerequisites)
13+
- [Implement the foreign data wrapper logic](#implement-the-foreign-data-wrapper-logic)
14+
- [Build the Wasm FDW package](#build-the-wasm-fdw-package)
15+
- [Release the Wasm FDW package](#release-the-wasm-fdw-package)
16+
- [Use with Supabase](#use-with-supabase)
17+
- [Checking Wrappers version](#checking-wrappers-version)
18+
- [Installing your Wasm FDW](#installing-your-wasm-fdw)
19+
- [Local development](#local-development)
20+
- [Set up](#set-up)
21+
- [Use Wasm FDW on local Supabase](#use-wasm-fdw-on-local-supabase)
22+
- [Considerations](#considerations)
23+
- [Version compatibility](#version-compatibility)
24+
- [Security](#security)
25+
- [Performance](#performance)
26+
- [Automation](#automation)
27+
- [Limitations](#limitations)
28+
- [Other examples](#other-examples)
729

830
## Project Structure
931

@@ -94,7 +116,7 @@ This will build the Wasm file in `target/wasm32-unknown-unknown/release/wasm_fdw
94116

95117
### Release the Wasm FDW package
96118

97-
To create a release of the Wasm FDW package, create a version tag and then push it. This will trigger a workflow to build the package and create a release on GitHub.
119+
To create a release of the Wasm FDW package, create a version tag and then push it. This will trigger a workflow to build the package and create a release on your repo.
98120

99121
```bash
100122
git tag v0.1.0
@@ -133,8 +155,7 @@ Create foreign server and foreign table like below,
133155
create server example_server
134156
foreign data wrapper wasm_wrapper
135157
options (
136-
-- change below fdw_pacakge_* options accordingly
137-
-- check available releases at https://github.com/supabase-community/wasm-fdw-example/releases
158+
-- change below fdw_package_* options accordingly, find examples in the README.txt in your releases
138159
fdw_package_url 'https://github.com/supabase-community/wasm-fdw-example/releases/download/v0.1.0/wasm_fdw_example.wasm',
139160
fdw_package_name 'my-company:example-fdw',
140161
fdw_package_version '0.1.0',
@@ -178,6 +199,75 @@ limit 5;
178199

179200
:clap: :clap: Congratulations! You have built your first Wasm FDW.
180201

202+
## Local development
203+
204+
### Set up
205+
206+
To develop Wasm FDW locally with Supabase, [Supabase CLI](https://supabase.com/docs/guides/cli/getting-started) is needed, check its docs for more installation details. After the CLI is installed, start the Supabase services:
207+
208+
```bash
209+
supabase start
210+
```
211+
212+
And then run the script to build the Wasm FDW package and copy it to Supabase database container:
213+
214+
```bash
215+
./local-dev.sh
216+
```
217+
218+
> [!TIP]
219+
> You can also use it with [cargo watch](https://crates.io/crates/cargo-watch): `cargo watch -s ./local-dev.sh`
220+
221+
### Use Wasm FDW on local Supabase
222+
223+
Visit SQL Editor at http://127.0.0.1:54323/project/default/sql/1, create foreign server and foreign table like below,
224+
225+
```sql
226+
create server example_server
227+
foreign data wrapper wasm_wrapper
228+
options (
229+
-- use 'file://' schema to reference the local wasm file in container
230+
fdw_package_url 'file:///wasm_fdw_example.wasm',
231+
fdw_package_name 'my-company:example-fdw',
232+
fdw_package_version '0.1.0',
233+
api_url 'https://api.github.com'
234+
);
235+
236+
create schema github;
237+
238+
create foreign table github.events (
239+
id text,
240+
type text,
241+
actor jsonb,
242+
repo jsonb,
243+
payload jsonb,
244+
public boolean,
245+
created_at timestamp
246+
)
247+
server example_server
248+
options (
249+
object 'events',
250+
rowid_column 'id'
251+
);
252+
```
253+
254+
> [!NOTE]
255+
> The foreign server option `fdw_package_checksum` is not needed for local development.
256+
257+
Now you can edit and save `src/lib.rs` file, then query the foreign table like below to see result:
258+
259+
```sql
260+
select
261+
id,
262+
type,
263+
actor->>'login' as login,
264+
repo->>'name' as repo,
265+
created_at
266+
from
267+
github.events
268+
limit 5;
269+
```
270+
181271
## Considerations
182272

183273
### Version compatibility

local-dev.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
set -euxo pipefail
4+
5+
# build wasm fdw package
6+
cargo component build --release --target wasm32-unknown-unknown
7+
8+
# set wasm file permission and copy it into supabase db container
9+
chmod +r target/wasm32-unknown-unknown/release/*.wasm
10+
docker cp target/wasm32-unknown-unknown/release/*.wasm supabase_db_supabase:/

0 commit comments

Comments
 (0)