Skip to main content

Crate product_os_store_macros

Crate product_os_store_macros 

Source
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_std environments with alloc

§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 → Boolean
  • String, &str → Text
  • i8 → Char (single byte)
  • i16, u8 → SmallInt
  • i32, u16, isize → Int
  • i64, u32 → BigInt
  • f32 → Real
  • f64 → Precision
§Special Types
  • Vec<u8> → ByteA (binary data)
  • Vec of any type → JsonB (serialized as JSON)
  • chrono::DateTime with Utc → TimestampTZ
  • uuid::Uuid → Uuid
  • serde_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 instruction
  • relational_alter() - Generate ALTER TABLE instruction
  • relational_drop() - Generate DROP TABLE instruction

§DML Operations

  • relational_insert() - Generate INSERT instruction
  • relational_update() - Generate UPDATE instruction
  • relational_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 row
  • relational_test() - Debug helper for generated field information

§Compatibility

This crate requires:

  • Rust 1.69 or later
  • Works with no_std (requires alloc)
  • Designed for use with product-os-store

Attribute Macros§

do_not_pluralize
Prevents automatic pluralization of the table name.

Derive Macros§

ProductOSRelational
Derives the ProductOSRelationalObject trait for a struct.