stateset-embedded 0.2.3

Embeddable commerce library - the SQLite of commerce operations
Documentation

StateSet iCommerce

The SQLite of commerce operations. An embeddable commerce library that runs anywhere with zero external dependencies.

Quick Start

use stateset_embedded::{Commerce, CreateCustomer, CreateOrder, CreateOrderItem, CreateInventoryItem};
use rust_decimal_macros::dec;

// Initialize with a database file (creates if not exists)
let commerce = Commerce::new("./store.db")?;

// Create a customer
let customer = commerce.customers().create(CreateCustomer {
    email: "alice@example.com".into(),
    first_name: "Alice".into(),
    last_name: "Smith".into(),
    ..Default::default()
})?;

// Create inventory
commerce.inventory().create_item(CreateInventoryItem {
    sku: "SKU-001".into(),
    name: "Widget".into(),
    initial_quantity: Some(dec!(100)),
    ..Default::default()
})?;

// Create an order
let order = commerce.orders().create(CreateOrder {
    customer_id: customer.id,
    items: vec![CreateOrderItem {
        sku: "SKU-001".into(),
        name: "Widget".into(),
        quantity: 2,
        unit_price: dec!(29.99),
        ..Default::default()
    }],
    ..Default::default()
})?;

// Adjust inventory
commerce.inventory().adjust("SKU-001", dec!(-2), "Order fulfillment")?;
# Ok::<(), stateset_embedded::CommerceError>(())

Features

  • Zero configuration - Just point to a file and go
  • Embedded SQLite - No external database server needed (default)
  • PostgreSQL support - Scale to production with postgres feature
  • Full commerce stack - Orders, inventory, customers, products, returns
  • Sync API - Simple blocking operations
  • Async API - True async for PostgreSQL with AsyncCommerce
  • Event-driven - Subscribe to commerce events for side effects

Database Backends

SQLite (default)

let commerce = Commerce::new("./store.db")?;
// or in-memory for testing
let commerce = Commerce::new(":memory:")?;

PostgreSQL (requires postgres feature)

let commerce = Commerce::with_postgres("postgres://user:pass@localhost/db")?;
// or via builder
let commerce = Commerce::builder()
    .postgres("postgres://localhost/stateset")
    .max_connections(20)
    .build()?;

Async PostgreSQL API

For true async operations with PostgreSQL, use AsyncCommerce:

use stateset_embedded::{AsyncCommerce, CreateOrder, CreateOrderItem};
use rust_decimal_macros::dec;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let commerce = AsyncCommerce::connect("postgres://localhost/stateset").await?;

    // All operations are truly async
    let order = commerce.orders().create(CreateOrder {
        customer_id: uuid::Uuid::new_v4(),
        items: vec![CreateOrderItem {
            sku: "SKU-001".into(),
            name: "Widget".into(),
            quantity: 2,
            unit_price: dec!(29.99),
            ..Default::default()
        }],
        ..Default::default()
    }).await?;

    let customers = commerce.customers().list(Default::default()).await?;
    let inventory = commerce.inventory().get_stock("SKU-001").await?;

    Ok(())
}

Architecture

┌─────────────────────────────────────────┐
│           Your Application              │
│  ┌───────────────────────────────────┐  │
│  │     Commerce (this crate)         │  │
│  │  ┌─────────────────────────────┐  │  │
│  │  │  SQLite or PostgreSQL       │  │  │
│  │  └─────────────────────────────┘  │  │
│  └───────────────────────────────────┘  │
└─────────────────────────────────────────┘