Cloudflare D1 Wrapper
Overview
Cloudflare D1 Wrapper
Cloudflare D1 is Cloudflare's managed, serverless database built on SQLite's SQL semantics, with built-in disaster recovery and access via Workers and the HTTP API.
The Cloudflare D1 Wrapper brings your D1 databases into Postgres as queryable foreign tables. Read and write D1 data with plain SQL, join it against your application data, and use subqueries in table definitions for more flexible data access. The wrapper works with Supabase Vault for secure API token management and is built on WebAssembly (Wasm) for lightweight, sandboxed execution.
Supported Operations
| Object | Select | Insert | Update | Delete |
|---|---|---|---|---|
| D1 Databases | ✅ | |||
| D1 Tables | ✅ | ✅ | ✅ | ✅ |
Preparation
Before you get started, make sure the wrappers extension is installed on your database:
_10create extension if not exists wrappers with schema extensions;
and then enable the Wasm foreign data wrapper:
_10create foreign data wrapper wasm_wrapper_10 handler wasm_fdw_handler_10 validator wasm_fdw_validator;
Secure your credentials (optional)
By default, Postgres stores FDW credentials inside pg_catalog.pg_foreign_server in plain text. Anyone with access to this table will be able to view these credentials. Wrappers is designed to work with Vault, which provides an additional level of security for storing credentials. We recommend using Vault to store your credentials.
_10-- Save your D1 API token in Vault and retrieve the created `key_id`_10select vault.create_secret(_10 '<D1 API token>',_10 'cfd1',_10 'Cloudflare D1 API key for Wrappers'_10);
Connecting to D1
We need to provide Postgres with the credentials to access D1, and any additional options. We can do this using the create server command:
With Vault:
_12create server cfd1_server_12 foreign data wrapper wasm_wrapper_12 options (_12 fdw_package_url 'https://github.com/supabase/wrappers/releases/download/wasm_cfd1_fdw_v0.2.0/cfd1_fdw.wasm',_12 fdw_package_name 'supabase:cfd1-fdw',_12 fdw_package_version '0.2.0',_12 fdw_package_checksum '0f1d022d9733b5dd0c39d65f35ffcfd86102e7ee53e72d8cf94749500c224c0d',_12 api_url 'https://api.cloudflare.com/client/v4/accounts/<account_id>/d1/database',_12 account_id '<Account ID>',_12 database_id '<Database ID>',_12 api_token_id '<key_ID>'_12 );
Without Vault:
_12create server cfd1_server_12 foreign data wrapper wasm_wrapper_12 options (_12 fdw_package_url 'https://github.com/supabase/wrappers/releases/download/wasm_cfd1_fdw_v0.2.0/cfd1_fdw.wasm',_12 fdw_package_name 'supabase:cfd1-fdw',_12 fdw_package_version '0.2.0',_12 fdw_package_checksum '0f1d022d9733b5dd0c39d65f35ffcfd86102e7ee53e72d8cf94749500c224c0d',_12 api_url 'https://api.cloudflare.com/client/v4/accounts/<account_id>/d1/database',_12 account_id '<Account ID>',_12 database_id '<Database ID>',_12 api_token '<D1 API token>'_12 );
Note: the fdw_package_* options are required and specify the Wasm package metadata. See the Available Versions table above for the full list.
Resources
Details
Third-party integrations and docs are managed by Supabase partners.