Skip to content

Nested order documents

Orders written as nested JSON documents with inlined customers, products and tags.

related volume small DocumentsOrdersRelationships
Languages
eng
Formats
json

Recipe

recipe.gbddl
gbddl 1
language eng

table customers rows 6 (
  id    int64 autoincrement primarykey
  name  text same(person) fullName()
  email text same(person) email()
)

table products rows 5 (
  id    int64 autoincrement primarykey
  name  text words(maxWords: 2)
  price decimal(8, 2) decimal(min: 1, max: 500)
)

table orders rows 12 per(customers, 1, 3) (
  id          int64 autoincrement primarykey
  customer_id int64 fk(customers.id)
  status      text enum(values: ["draft", "confirmed", "shipped", "delivered"])
)

table order_items rows 24 per(orders, 1, 3) (
  id         int64 autoincrement primarykey
  order_id   int64 fk(orders.id)
  product_id int64 fk(products.id)
  quantity   int64 integer(min: 1, max: 5)
)

table labels rows 12 per(orders, 0, 3) (
  order_id int64 fk(orders.id)
  label    text words(maxWords: 1)
)

output json (
  pretty: true
  root orders omit(id) (
    object customer_id as customer omit(id)
    embed order_items as items omit(id) (
      object product_id as product
    )
    list labels.label as tags
  )
)

This is the exact recipe.gbddl committed to the repository. Copy it, or download the raw source.

About this recipe

Nested order documents

A multi-table recipe that writes orders as nested JSON documents instead of flat files.

What it demonstrates

  • Root projection — output json ( root orders ... ) builds one document per order.
  • Object — the customer_id foreign key is inlined as a customer object.
  • Embed — order_items is embedded as an items array, with each item's product_id inlined as a product object.
  • List — labels.label becomes a flat tags array of scalars.
  • Omit — id columns are omitted from the nested levels.

Run it

Submit the recipe body to POST /api/v1/jobs?input_format=gbddl, or paste it into Chef, to generate the data. The repository's local runner (run by task test) also generates it and verifies its output.

The recipe outputs one JSON document file, orders.json, with one nested document per order. The source tables are customers, products, orders, order_items and labels.

Guarantees

Each document nests its order's customer, items and products, every foreign key names an existing parent row, and per(...) grouping holds. Values are synthetic.

Tested features

Smart types, physical types, rules and formats this recipe exercises and verifies.

fullNameemailwordsdecimalenumintegerfkpersame:personautoincrementprimary-keyprojection:rootprojection:embedprojection:objectprojection:listprojection:omit