Architecture

Capstone implementation and tests

Implementation turns the capstone contract into enforceable behavior. A transaction protects stock, order and outbox together; a uniqueness constraint plus request comparison protects duplicate requests. Use a composite tenant-and-book key, parameterized SQL and a bounded quantity. A retry with the same tenant and idempotency key returns the existing result only if the original request matches; otherwise report conflict. An order for a different tenant must not reveal or mutate its stock. Place the stock update, order insert and event insert in one transaction, roll back on failure, and test actual state. The separate capstone fixture includes native HTTP and real SQLite; it is an isolated teaching application, not a production-ready database architecture. Do not check stock in one unprotected operation and decrement it later; do not accept duplicate keys with different payloads.

Formal Syntax & Structure

nodejs syntaxnodejs
Conceptual contract; see example

Working Example

Example Snippetnodejs
import {DatabaseSync} from 'node:sqlite';const db=new DatabaseSync(':memory:');db.exec('CREATE TABLE stock(q INTEGER CHECK(q>=0)); INSERT INTO stock VALUES(3)');db.exec('BEGIN');try{db.exec('UPDATE stock SET q=1; INSERT INTO stock VALUES(-1)');db.exec('COMMIT')}catch{db.exec('ROLLBACK')}console.log(db.prepare('SELECT q FROM stock').get().q);db.close();
Node.js Reference