← Writing

Own-store architecture, part 1: Prototype with one app and PostgreSQL

On this page

A prototype should show whether a shopper can place and review an order and an operator can find and process it. This fictional store has one seller, physical goods, one warehouse, and one currency. One app serves the storefront and admin interface and writes to one managed PostgreSQL instance. Use a test database isolated from production and simulate payment approval, decline, and timeout inside the app.

Data and build sequence

Product 10, Canvas Tote, has two sellable variants: SKU 101 TOTE-NVY and SKU 102 TOTE-NAT. Each has one unit in stock and costs 18,000 KRW. Cart 301 holds one unit of SKU 101. Store money as an integer in won, the minor unit of KRW. Accept 1–99 units per SKU and unit prices from 0 to 100000000 won. The total sums snapshot unit price × quantity across lines. Shipping, taxes, and discounts are outside the example.

Step Build and check
Catalog → cart An operator adds the product, prices, and stock per SKU. A shopper picks a variant and quantity. Reject unknown SKUs and out-of-range quantities.
Quote → order The server rereads listing status, price, and stock before copying the SKU code, unit price, and quantity to the order. On a price change, show the new quote and ask the shopper to confirm it.
Fake payment → lookup Exercise approval, decline, and timeout in the simulator. The shopper sees the result, and the operator can find order 9001 and verify the agreed amount.

Run both SQL blocks in sequence in an empty PostgreSQL 17 test database. Primary keys identify rows, and foreign keys link existing products, SKUs, carts, and orders. Composite primary keys prevent duplicate SKU lines. The schema permits one order per cart. The app closes the cart after creating its order.

CREATE TABLE product (
  id bigint PRIMARY KEY,
  title text NOT NULL
);
CREATE TABLE sku (
  id bigint PRIMARY KEY,
  product_id bigint NOT NULL REFERENCES product(id),
  code text NOT NULL UNIQUE,
  price_minor bigint NOT NULL CHECK (price_minor BETWEEN 0 AND 100000000),
  listed boolean NOT NULL DEFAULT true,
  published_at timestamptz NOT NULL
);
CREATE TABLE inventory (
  sku_id bigint PRIMARY KEY REFERENCES sku(id),
  on_hand integer NOT NULL CHECK (on_hand >= 0)
);
CREATE TABLE cart (
  id bigint PRIMARY KEY
);
CREATE TABLE cart_item (
  cart_id bigint REFERENCES cart(id),
  sku_id bigint REFERENCES sku(id),
  quantity integer NOT NULL CHECK (quantity BETWEEN 1 AND 99),
  PRIMARY KEY (cart_id, sku_id)
);
CREATE TABLE shop_order (
  id bigint PRIMARY KEY,
  cart_id bigint NOT NULL UNIQUE REFERENCES cart(id),
  currency text NOT NULL CHECK (currency = 'KRW'),
  status text NOT NULL CHECK (status IN ('pending', 'reserved', 'confirmed', 'cancelled'))
);
CREATE TABLE order_item (
  order_id bigint REFERENCES shop_order(id),
  sku_id bigint REFERENCES sku(id),
  sku_code text NOT NULL,
  unit_minor bigint NOT NULL CHECK (unit_minor BETWEEN 0 AND 100000000),
  quantity integer NOT NULL CHECK (quantity BETWEEN 1 AND 99),
  PRIMARY KEY (order_id, sku_id)
);

INSERT INTO product VALUES (10, 'Canvas Tote');
INSERT INTO sku VALUES
  (101, 10, 'TOTE-NVY', 18000, true, '2026-11-01 00:00:00+00'),
  (102, 10, 'TOTE-NAT', 18000, true, '2026-11-01 00:00:00+00');
INSERT INTO inventory VALUES (101, 1), (102, 1);
INSERT INTO cart VALUES (301);
INSERT INTO cart_item VALUES (301, 101, 1);

Copy the cart into an order

Cart 301 · 1 × SKU 101 → Check listing, price and stock → Save order 9001 / Refresh quote

The diagram follows the single-SKU checkout of cart 301. In this SQL, 301, 101, 9001, and 18000 are fixture inputs. The app authenticates the cart owner and generates the order ID on the server. It compares the shopper’s accepted price with the locked SKU row and saves the price from that row. Here pending means awaiting payment. It leaves stock unreserved and undeducted.

BEGIN;
DO $$
DECLARE
  line record;
  expected_price constant bigint := 18000;
BEGIN
  SELECT s.id, s.code, s.price_minor, c.quantity, i.on_hand
    INTO line
    FROM cart_item c
    JOIN sku s ON s.id = c.sku_id
    JOIN inventory i ON i.sku_id = s.id
   WHERE c.cart_id = 301 AND s.id = 101 AND s.listed
     FOR UPDATE OF c, s, i;

  IF NOT FOUND OR line.price_minor <> expected_price
               OR line.on_hand < line.quantity THEN
    RAISE EXCEPTION 'refresh_quote';
  END IF;

  INSERT INTO shop_order VALUES (9001, 301, 'KRW', 'pending');
  INSERT INTO order_item VALUES
    (9001, line.id, line.code, line.price_minor, line.quantity);
END $$;
COMMIT;

SELECT o.id, o.currency, sum(i.unit_minor * i.quantity) AS total_minor
FROM shop_order o JOIN order_item i ON i.order_id = o.id
WHERE o.id = 9001
GROUP BY o.id, o.currency;

Expect 9001 | KRW | 18000. An unlisted SKU, price change, or stock shortage raises refresh_quote and rolls back the order and its line. A unique constraint rejects reuse of a cart. In local PostgreSQL 17.12, the query produced the expected output. Tests covered price changes, insufficient stock, cart reuse, foreign-key, uniqueness, range and currency constraints, and snapshot retention after a price change.

When to move to first production

Advance when shoppers and operators can distinguish approval, decline, and pending outcomes and find the same order. Keep test orders, customers, payment keys, and webhook URLs in their own environment and leave them out of the production import. Before accepting money, verify stock contention, retries, payment reconciliation, operator permissions, and backup restoration. The next part takes the same test order 9001 and reserves its stock.

Official documentation

Next — Own-store architecture, part 2: Stock reservations and payments in first production

Discussion

View on GitHub ↗