Skip to content

cramt/facet-owned-shape

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

facet-psql-schema

Bridge library for generating SeaQuery table definitions from Facet shapes.

This crate allows you to automatically generate PostgreSQL database schemas (via sea-query::Table) directly from your Rust struct definitions, leveraging facet's compile-time reflection capabilities.

Usage

Basic Conversion

Derive Facet on your structs, then convert the shape to a sea_query::Table.

use facet::Facet;
use facet_psql_schema::PartialSchema;
// Table is still available if you need to manipulate it manually
// use facet_psql_schema::Table;

#[derive(Facet)]
struct User {
    id: u64,
    username: String,
    is_active: bool,
}

fn main() {
    let shape = User::SHAPE;
    // Step 1: Convert Shape to PartialSchema
    let schema = PartialSchema::try_from(shape).expect("Schema conversion failed");
    
    // Step 2: Access the generated table(s)
    let table = schema.tables.first().expect("Expected at least one table");
    
    // table.name == "user"
    // table.columns include "id" (BigInt), "username" (Text), "is_active" (Boolean)
}

Primary Keys

Use the #[facet(...)] attribute to mark fields as primary keys.

#[derive(Facet)]
struct BlogPost {
    #[facet(psql::primary_key)]
    id: String, // Maps to Text Primary Key
    title: String,
}

Supported Types

Rust Type PostgreSQL Type (SeaQuery) Notes
bool Boolean
i8, u8, i16, u16 SmallInt
i32, u32 Integer
i64, u64, usize BigInt
f32 Real
f64 DoublePrecision
String, &str Text References are supported
char Char(1)
Vec<T> Jsonb Currently maps to JSONB, not Array
HashMap<K,V> Jsonb
Nested Structs Jsonb
Enums Integer Field-level enums maps to Integer
Option<T> Nullable Wraps the inner type

Limitations

  • Generics: Generic structs (struct Foo<T>) work only when monomorphized (e.g., Foo::<u64>::SHAPE).
  • Fixed-Size Arrays: [T; N] are currently not supported/mapped.
  • Enums: Top-level enums cannot be converted to Tables. Field-level enums map to Integer.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages