Supabase PostgREST Client for Rust
A Rust client library for Supabase PostgreSQL REST API access.
Features
- Basic CRUD operations (
select,insert,update,delete) - Filtering (
eq,gt,lt,like,ilike,in_list,not,contains,contained_by,text_search, etc.) - Ordering (
order) and pagination (limit,offset) - Joins (
inner_join,left_join,include,referenced_by) - Transactions (
begin_transaction,commit,rollback,savepoint) - RPC function calls (
rpc) - CSV export (
export_csv) - TypeScript to Rust type conversion infrastructure (via
schema-convertfeature, conversion logic is currently a placeholder) - Basic type-safe operation helpers (requires
schema-convertfeature, experimental)
Project Status & Roadmap
Current Status: Alpha (v0.1.3) - Core API Implemented, Type Safety Experimental
This crate provides core PostgREST functionality, transaction support, and initial infrastructure for type generation. It is under active development.
Roadmap:
- Basic CRUD, Filtering, Ordering, Pagination
- RPC Function Calls
- Transactions & Savepoints
- CSV Export
- Full-text search (
text_search) - Basic JSONB operations (
contains,contained_by) - Support for more complex
selectqueries (e.g., advanced nested resources/embedding) - Complete the
schema-convertfeature for robust TypeScript -> Rust type generation. - Enhance type-safe operation helpers based on generated types.
- Improved error reporting and handling details.
- Comprehensive integration tests against a live Supabase instance.
- Explore potential state machine usage for request building/transaction management.
Installation
Add the dependency to your Cargo.toml:
[]
= "0.1.3"
To use the TypeScript to Rust type conversion feature:
[]
= { = "0.1.3", = ["schema-convert"] }
Basic Usage
use PostgrestClient;
use Client;
async
TypeScript to Rust Type Conversion
This crate provides functionality to convert TypeScript type definitions generated by Supabase's supabase gen types typescript command into Rust types. To use this feature, you must enable the schema-convert feature.
Note: While the infrastructure and CLI tools for schema conversion are present, the core logic (convert_typescript_to_rust function) that performs the detailed type mapping from TypeScript interfaces/types to Rust structs/enums is currently a placeholder and requires further implementation based on specific project needs or more sophisticated parsing. The current implementation primarily sets up the file structure and basic generation.
Converting Using Make (Recommended)
The simplest way to generate Rust types from your Supabase schema is using the provided Makefile:
# Copy the Makefile from this repository to your project root
# Then run:
This will:
- Generate TypeScript types using
supabase gen types typescript - Convert them to Rust types
- Place the generated Rust file in
src/generated/schema.rs
You can customize the output location and module name:
For more options, run:
Converting from Command Line
# Run in your repository root directory
# Generate Rust types from TypeScript definitions
Converting Programmatically
// NOTE: Requires the 'schema-convert' feature to be enabled.
// The core conversion logic is currently a placeholder.
#
#
Type-Safe Database Operations
Note: The type-safe operations shown below rely on the schema-convert feature and the associated generated types. This feature is currently experimental, and the underlying schema conversion logic is incomplete. Use with caution and expect potential limitations or required manual adjustments.
// NOTE: Requires the 'schema-convert' feature and generated types.
#
#
Testing
To run the tests for this crate, including feature-specific tests:
We aim for high test coverage, particularly for core CRUD and filtering operations. Integration tests using wiremock simulate responses from the PostgREST API.
Security Considerations
- API Keys: Ensure your Supabase URL and
anonkey (orservice_rolekey if used) are stored and handled securely. Avoid hardcoding them directly in your source code. Consider using environment variables or a secrets management solution. - Input Validation: While this library promotes type safety, always validate user-provided input before constructing database queries, especially for filter values, to prevent potential injection issues or unintended data access.
Contributing
Contributions are welcome! Please feel free to open an issue or submit a pull request. For major changes, please open an issue first to discuss what you would like to change.
Ensure that your contributions pass all tests (cargo test --all-features) and adhere to the project's coding style (run cargo fmt).
License
MIT