Orders DB (v0.0.1)
What is this?
Orders DB is the primary database for the Orders domain. It is a PostgreSQL 14 database and acts as the system of record for orders and their line items. If you are looking for where an order lives, how to query it, or how order state changes are persisted, you are in the right place.
What does it store?
- Orders: One row per customer order. Includes status, totals, currency, timestamps.
- Order Lines: One row per item inside an order. Includes product, quantities, and pricing.
- Relationships:
order_lines
are linked toorders
byorder_id
(cascading deletes are enabled).
Schema and test data for Orders DB
-- Orders Database - Simple orders table for documentation-- This is fake data for documentation purposes
-- Drop table if it exists (for clean re-runs)DROP TABLE IF EXISTS orders;
-- Create orders tableCREATE TABLE orders ( order_id SERIAL PRIMARY KEY, order_number VARCHAR(50) UNIQUE NOT NULL, customer_name VARCHAR(100) NOT NULL, customer_email VARCHAR(100) NOT NULL, order_status VARCHAR(20) DEFAULT 'pending', order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, product_name VARCHAR(200) NOT NULL, quantity INTEGER NOT NULL, unit_price DECIMAL(10,2) NOT NULL, total_amount DECIMAL(10,2) NOT NULL, payment_method VARCHAR(50), payment_status VARCHAR(20) DEFAULT 'pending', shipping_address VARCHAR(200), notes TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
-- Insert sample ordersINSERT INTO orders (order_number, customer_name, customer_email, order_status, product_name, quantity, unit_price, total_amount, payment_method, payment_status, shipping_address) VALUES('ORD-2024-001', 'John Doe', 'john.doe@email.com', 'delivered', 'MacBook Pro 14"', 1, 1999.99, 1999.99, 'credit_card', 'completed', '123 Main St, New York, NY 10001'),('ORD-2024-002', 'Jane Smith', 'jane.smith@email.com', 'shipped', 'iPhone 15 Pro', 1, 999.99, 999.99, 'paypal', 'completed', '456 Oak Ave, Los Angeles, CA 90210'),('ORD-2024-003', 'Michael Johnson', 'michael.johnson@email.com', 'processing', 'AirPods Pro', 2, 249.99, 499.98, 'credit_card', 'completed', '789 Pine Rd, Chicago, IL 60601'),('ORD-2024-004', 'Emily Davis', 'emily.davis@email.com', 'pending', 'Cotton T-Shirt', 3, 19.99, 59.97, 'debit_card', 'pending', '321 Elm St, Houston, TX 77001'),('ORD-2024-005', 'David Wilson', 'david.wilson@email.com', 'delivered', 'Smart Watch', 1, 299.99, 299.99, 'credit_card', 'completed', '654 Maple Dr, Phoenix, AZ 85001'),('ORD-2024-006', 'Sarah Brown', 'sarah.brown@email.com', 'cancelled', 'Travel Backpack', 1, 79.99, 79.99, 'paypal', 'refunded', '987 Cedar Ln, Philadelphia, PA 19101'),('ORD-2024-007', 'James Miller', 'james.miller@email.com', 'shipped', 'Premium Coffee Beans', 5, 24.99, 124.95, 'credit_card', 'completed', '147 Birch Way, San Antonio, TX 78201'),('ORD-2024-008', 'Lisa Garcia', 'lisa.garcia@email.com', 'delivered', 'Running Sneakers', 1, 89.99, 89.99, 'apple_pay', 'completed', '258 Spruce St, San Diego, CA 92101'),('ORD-2024-009', 'Robert Martinez', 'robert.martinez@email.com', 'processing', 'iPad Air', 1, 599.99, 599.99, 'google_pay', 'completed', '369 Willow Ave, Dallas, TX 75201'),('ORD-2024-010', 'Jennifer Anderson', 'jennifer.anderson@email.com', 'pending', 'The Great Gatsby', 2, 12.99, 25.98, 'credit_card', 'pending', '741 Poplar Rd, San Jose, CA 95101'),('ORD-2024-011', 'Mark Thompson', 'mark.thompson@email.com', 'delivered', 'Wireless Headphones', 1, 149.99, 149.99, 'paypal', 'completed', '852 Oak Street, Seattle, WA 98101'),('ORD-2024-012', 'Amanda White', 'amanda.white@email.com', 'shipped', 'Gaming Mouse', 2, 79.99, 159.98, 'credit_card', 'completed', '963 Pine Avenue, Denver, CO 80201');
-- Create indexes for better performanceCREATE INDEX idx_orders_status ON orders(order_status);CREATE INDEX idx_orders_date ON orders(order_date);CREATE INDEX idx_orders_customer_email ON orders(customer_email);
-- Insert sample ordersINSERT INTO orders (order_number, customer_name, customer_email, order_status, product_name, quantity, unit_price, total_amount, payment_method, payment_status, shipping_address) VALUES('ORD-2024-001', 'John Doe', 'john.doe@email.com', 'delivered', 'MacBook Pro 14"', 1, 1999.99, 1999.99, 'credit_card', 'completed', '123 Main St, New York, NY 10001'),('ORD-2024-002', 'Jane Smith', 'jane.smith@email.com', 'shipped', 'iPhone 15 Pro', 1, 999.99, 999.99, 'paypal', 'completed', '456 Oak Ave, Los Angeles, CA 90210'),('ORD-2024-003', 'Michael Johnson', 'michael.johnson@email.com', 'processing', 'AirPods Pro', 2, 249.99, 499.98, 'credit_card', 'completed', '789 Pine Rd, Chicago, IL 60601'),('ORD-2024-004', 'Emily Davis', 'emily.davis@email.com', 'pending', 'Cotton T-Shirt', 3, 19.99, 59.97, 'debit_card', 'pending', '321 Elm St, Houston, TX 77001'),('ORD-2024-005', 'David Wilson', 'david.wilson@email.com', 'delivered', 'Smart Watch', 1, 299.99, 299.99, 'credit_card', 'completed', '654 Maple Dr, Phoenix, AZ 85001'),('ORD-2024-006', 'Sarah Brown', 'sarah.brown@email.com', 'cancelled', 'Travel Backpack', 1, 79.99, 79.99, 'paypal', 'refunded', '987 Cedar Ln, Philadelphia, PA 19101'),('ORD-2024-007', 'James Miller', 'james.miller@email.com', 'shipped', 'Premium Coffee Beans', 5, 24.99, 124.95, 'credit_card', 'completed', '147 Birch Way, San Antonio, TX 78201'),('ORD-2024-008', 'Lisa Garcia', 'lisa.garcia@email.com', 'delivered', 'Running Sneakers', 1, 89.99, 89.99, 'apple_pay', 'completed', '258 Spruce St, San Diego, CA 92101'),('ORD-2024-009', 'Robert Martinez', 'robert.martinez@email.com', 'processing', 'iPad Air', 1, 599.99, 599.99, 'google_pay', 'completed', '369 Willow Ave, Dallas, TX 75201'),('ORD-2024-010', 'Jennifer Anderson', 'jennifer.anderson@email.com', 'pending', 'The Great Gatsby', 2, 12.99, 25.98, 'credit_card', 'pending', '741 Poplar Rd, San Jose, CA 95101'),('ORD-2024-011', 'Mark Thompson', 'mark.thompson@email.com', 'delivered', 'Wireless Headphones', 1, 149.99, 149.99, 'paypal', 'completed', '852 Oak Street, Seattle, WA 98101'),('ORD-2024-012', 'Amanda White', 'amanda.white@email.com', 'shipped', 'Gaming Mouse', 2, 79.99, 159.98, 'credit_card', 'completed', '963 Pine Avenue, Denver, CO 80201');
High-level data model
- An
order
has manyorder_lines
. - Order lifecycle is tracked via the
status
column. Common statuses:PENDING
,PAID
,CANCELLED
,FULFILLED
,REFUNDED
. - Monetary values are stored as integer cents; currency is 3-letter ISO (e.g.
USD
).
Common queries
-- Fetch a single order with its linesSELECT o.*, l.*FROM orders oLEFT JOIN order_lines l USING (order_id)WHERE o.order_id = $1;
-- List recent orders for a customer (paged)SELECT *FROM ordersWHERE customer_id = $1ORDER BY created_at DESCLIMIT $2 OFFSET $3;
-- Daily revenue (rounded to dollars) for the last 30 daysSELECT date_trunc('day', created_at) AS day, SUM(total_amount_cents)/100.0 AS revenueFROM ordersWHERE created_at >= now() - interval '30 days' AND status IN ('PAID','FULFILLED','REFUNDED')GROUP BY 1ORDER BY 1;
How data gets here (lifecycle)
- A user places an order →
OrdersService
validates input and writes toorders
andorder_lines
in a single transaction. - Payment success updates the order
status
toPAID
. - Fulfillment updates the order
status
toFULFILLED
. - Cancellations and refunds update
status
accordingly. - Each change emits a domain event that other services consume.
Access patterns and guidance
- Use the
order_id
for point lookups; most queries should be indexed by this orcustomer_id
. - Prefer reading from read replicas for analytics/reporting workloads when available.
- Treat
status
as the source of truth for order lifecycle; avoid inferring state from timestamps alone. - Do not store PII beyond
customer_id
here; card data never resides in this database.
Retention and residency
- Retention: 7 years (see frontmatter). Archival or partitioning may be used to keep hot data smaller.
- Residency:
eu-west-1
(see frontmatter). Ensure cross-region access complies with data policies.
Backups and recovery
- Automated daily snapshots, plus point-in-time recovery retained per platform policy.
- Test restores should be performed regularly to validate RPO/RTO.
Security
- Access is role-based.
OrdersService
has read/write. Downstream services typically have read-only. - Enforce least privilege at the database role level. Avoid ad-hoc superuser connections.
Operational notes
- Consider time-based partitioning on
orders.created_at
for very large datasets. - Monitor bloat and autovacuum; ensure
vacuum_analyze
runs regularly. - Keep indexes targeted; avoid over-indexing write-heavy columns.
Local development
- Connection string: provided via
ORDERS_DB_URL
environment variable. - Seed data: use project seed scripts to create sample orders and lines.
Gotchas
- Deleting an order cascades to
order_lines
due toON DELETE CASCADE
. Prefer status changes over deletes in most cases. - Monetary math should be done in integer cents to avoid float rounding issues.
If you’re unsure which table to use or how to fetch something, start with the orders
table by order_id
, then join order_lines
as needed. For more, see the OrdersService
documentation and related domain events.