Skip to content

API user documents

Users as nested JSON documents with their addresses embedded, shaped like a REST response.

related volume small CustomersPeopleLocationsDocuments
Languages
eng
Formats
json

Recipe

recipe.gbddl
gbddl 1
language eng

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

table addresses rows 9 per(users, 1, 2) (
  id       int64 autoincrement primarykey
  user_id  int64 fk(users.id)
  kind     text enum(values: ["home", "work", "billing"])
  street   text street()
  city     text same(geo) city()
  postcode text same(geo) postcode()
  country  text same(geo) countryCode()
)

output json (
  pretty: true
  root users (
    embed addresses as addresses omit(id, user_id)
  )
)

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

About this recipe

API user documents

A recipe that writes users as nested JSON documents, each with its addresses embedded, the shape a REST endpoint such as GET /users returns.

What it demonstrates

  • Embedding: embed addresses as addresses places each user's addresses inside the user document, without their own keys.
  • Coherent people: same(person) keeps a user's name, e-mail and username about the same person.
  • Coherent places: same(geo) keeps each address's city, postcode and country together.
  • Row distribution: per(users, 1, 2) gives each user one or two addresses.

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 reads the users and addresses tables and outputs one JSON file with a document per user.

Guarantees

Every primary key is unique, every embedded address belongs to its user. Values are synthetic.

Tested features

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

fullNameemailusernameenumstreetcitypostcodecountryCodesame:personsame:geographyfkperprojection:rootprojection:embedprojection:omitautoincrementprimary-key