Expand description
§Product OS Store Macros
This crate provides procedural macros for generating SQL query builders from Rust structs.
It works in conjunction with the product-os-store crate to provide a type-safe, ergonomic
interface for database operations.
§Features
- Automatic SQL Generation: Derive SQL operations from struct definitions
- Type Safety: Compile-time verification of field types and constraints
- Flexible Naming: Automatic table name pluralization or custom names
- Constraint Support: Primary keys, foreign keys, unique constraints, and more
- No-std Compatible: Works in
no_stdenvironments withalloc
§Usage
§Basic Example
ⓘ
use product_os_store_macros::ProductOSRelational;
use product_os_store::ProductOSRelationalObject;
#[derive(ProductOSRelational, Debug, Default)]
struct User {
#[primary_key]
id: i32,
name: String,
email: String,
active: bool,
}
let user = User::default();
let insert_instruction = user.relational_insert();§Table Naming
By default, table names are automatically pluralized:
ⓘ
#[derive(ProductOSRelational)]
struct User { /* ... */ } // Table name: "Users"
#[derive(ProductOSRelational)]
struct Category { /* ... */ } // Table name: "Categories"To disable pluralization:
ⓘ
#[derive(ProductOSRelational)]
#[do_not_pluralize]
struct Data { /* ... */ } // Table name: "Data"§Field Attributes
#[primary_key]- Marks a field as the primary key#[foreign_key]- Marks a field as a foreign key#[not_null]- Ensures the field cannot be null (implied for non-Option types)#[unique]- Adds a unique constraint#[always_identity]- Marks the field as always generated (e.g., auto-increment)#[default_identity]- Marks the field as generated by default
§Supported Types
§Primitive Types
bool→ BooleanString,&str→ Texti8→ Char (single byte)i16,u8→ SmallInti32,u16,isize→ Inti64,u32→ BigIntf32→ Realf64→ Precision
§Special Types
Vec<u8>→ ByteA (binary data)Vecof any type → JsonB (serialized as JSON)chrono::DateTimewithUtc→ TimestampTZuuid::Uuid→ Uuidserde_json::Value→ JsonB- Custom types implementing
Serialize/Deserialize→ JsonB
§Optional Types
Wrap any type in Option to make it nullable:
ⓘ
#[derive(ProductOSRelational)]
struct User {
id: i32, // NOT NULL
name: String, // NOT NULL
email: Option<String>, // Nullable
age: Option<i32>, // Nullable
}§Generated Methods
The ProductOSRelational derive macro generates implementations for the
ProductOSRelationalObject trait, including:
§DDL Operations
relational_create()- Generate CREATE TABLE instructionrelational_alter()- Generate ALTER TABLE instructionrelational_drop()- Generate DROP TABLE instruction
§DML Operations
relational_insert()- Generate INSERT instructionrelational_update()- Generate UPDATE instructionrelational_delete()- Generate DELETE instruction (static)relational_upsert()- Generate UPSERT instruction
§Query Operations
relational_query_all()- Generate SELECT * query (static)relational_query_basic()- Generate basic SELECT query (static)relational_query()- Generate SELECT with conditions (static)relational_query_advanced()- Generate SELECT with joins (static)
§Utility Methods
relational_from_row()- Populate struct from database rowrelational_test()- Debug helper for generated field information
§Compatibility
This crate requires:
- Rust 1.69 or later
- Works with
no_std(requiresalloc) - Designed for use with
product-os-store
Attribute Macros§
- do_
not_ pluralize - Prevents automatic pluralization of the table name.
Derive Macros§
- ProductOS
Relational - Derives the
ProductOSRelationalObjecttrait for a struct.