Skip to main content

Storage Backends

ducto abstracts storage behind the CreditStore interface. Three built-in implementations plus custom adapters.

Comparison

StoreDependenciesUse Case
MemoryStoreNoneTesting, development
HttpxSupabaseStorehttpx (Python) / native fetch (JS)Production (Supabase)
PostgresStorepsycopg (Python) / pg (JS)Production (direct PostgreSQL)

CreditStore Interface

The CreditStore ABC/interface defines all operations. Implementations exist for Memory, Postgres, and Supabase.

CategoryMethods
Balancesget_balance, add_credits, reserve_credits, deduct_credits
Pricingget_active_pricing, set_active_pricing
Plansget_user_plan, set_user_plan, check_allowance, increment_usage_window
Spend capscheck_spend_cap
Refundsrefund_credits
Expirysweep_expired_credits
Analyticsspend_by_user, spend_by_model, top_users, daily_spend, aggregate_stats
Teamscreate_team, get_team_balance, add_team_member, get_team_members, deduct_team
Setupsetup

SQL Migrations

Ten bundled SQL files create the required schema:

FileCreates
001_credit_tables.sqluser_credits, credit_transactions, credit_reservations tables, RLS policies, signup bonus trigger
002_credit_rpcs.sqlCore RPCs: credits_add, reserve_credits, deduct_credits, get_credits_balance
003_pricing_config.sqlcredit_pricing_config table, get/set RPCs
004_user_plans.sqlcredit_plans, credit_usage_window tables, plan RPCs
005_credit_refunds.sqlrefund_credits RPC with duplicate detection and partial refunds
006_credit_expiry.sqlexpire_credits RPC with dry-run support
007_usage_analytics.sqlspend_by_user, spend_by_model, top_users, daily_spend RPCs
008_team_balances.sqlcredit_teams, credit_team_members tables, team RPCs
009_spend_caps.sqlcredit_spend_caps table, check_spend_cap RPC
010_aggregate_stats.sqlaggregate_stats RPC returning JSON

All DDL is idempotent (IF NOT EXISTS / CREATE OR REPLACE).

Run via CLI:

ducto migrate "postgresql://user:pass@host:5432/db"

Or from code:

from ducto.interface.supabase import run_migrations
result = run_migrations("postgresql://user:pass@host:5432/db")