Skip to content

DSM — Data Storage Model

This guide is split into three parts, and you are on Short info. Continue with the tutorial or browse the samples.

DSM answers: how is information stored, constrained and retrieved?

Use it when persistent data needs explicit types, keys, relationships, constraints or indexes. Start with one table, a primary key and a constraint that rejects a bad row. Reverse-engineering an existing schema is often a better first step than drawing it again. Storage decisions should support domain rules and access patterns without pretending to describe the whole domain.

  • Expecting a row-level constraint to solve a cross-row concurrency rule.
  • Adding indexes without naming the query they support.
  • Changing nullability without reviewing data already stored.

Relational modeling has a foundational reference in E. F. Codd’s 1970 paper. ContextBrew’s DSM represents storage choices such as tables, columns, constraints, indexes and migrations. A broader explanation of relational concepts and hands-on SQL appears in the PostgreSQL tutorial.

Separate logical integrity from performance. A foreign key prevents a missing parent; an index may accelerate a query but does not establish a business invariant. Some invariants span rows or transactions and cannot be proven by a single column check. Record those in DDD/CBRL and use an appropriate implementation test. A schema diff shows a structural change; it does not prove a production migration preserves data or meets a latency target.

An ORM schema keeps storage close to code; SQL DDL provides database-native precision; an ER diagram emphasizes entities and relationships. Use a document or other storage model when its access and consistency trade-offs fit the problem. Choosing a storage pattern compares those choices.

  • Tables – database tables mapped from DDD aggregates
  • Columns – typed columns with nullability, defaults, and length constraints
  • Constraints – primary keys, foreign keys, unique constraints, CHECK constraints
  • Indexes – performance indexes on frequently queried columns
  • Migrations – versioned schema changes with up/down paths
  • DDD – Tables carry an aggregate field naming the owning aggregate; invariants map to CHECK constraints when schemas are derived from code (ctxbrew init --from-code)
  • CBRL – Requirements can reference table constraints they depend on
  • SCM – Screens reference tables whose data they display
  • USM – Stories involving data persistence link to the relevant tables

The serialized DSM model (models/dsm.json inside a .ctxbrew archive) is validated by a normative JSON Schema generated from ContextBrew’s source code. See the DSM file schema page — or print the schema anywhere with ctxbrew schema dsm.