A shop in three tables
Fifty customers, their orders and the lines of each order. The relations decide how many rows there are, and every child carries the key of its parent.
en-US 3 tables 478 rows JSON
belongs_to: { customer: 0-5 } gives every customer up to five orders, some none at all, and belongs_to: { order: 1-4 } gives every order one to four lines. In JSON every order stands inside its customer and every line inside its order, and each still names its parent in a field of its own.
Statuses, plans and quantities come from lists in the recipe, so they are values your application knows. Totals and prices are decimal numbers, and about seven coupons in ten are empty, to show how your code treats a missing value. Change format: json to csv for a file per table.
The recipe
# A shop in three related tables: customers, their orders and the lines of each
# order, written as one JSON file with every order inside its customer and every
# line inside its order.
#
# Every customer has zero to five orders and every order one to four lines, so
# the number of rows comes from the relations rather than from a count. Each
# child also carries the key of its parent. Plans, statuses and quantities come
# from lists, totals and prices are decimal numbers, and about seven coupons in
# ten are left empty.
#
# qadg generate orders.yaml
#
# writes dataset.json and run-note.json. The same seed gives the same rows on
# every machine.
version: "0.1"
seed: "orders"
locale: en-US
entities:
customer:
count: 50
primary_key: [customer_id]
fields:
customer_id: { type: integer, unique: true }
first_name: { generator: first_name, required: true, max_length: 40 }
last_name: { generator: last_name, required: true, max_length: 40 }
gender: { generator: gender }
plan: { values: [free, pro, team] }
city: { generator: city }
state: { generator: region }
zip: { generator: postal_code }
consistency:
- first_name matches: [gender]
order:
belongs_to: { customer: 0-5 }
primary_key: [order_id]
fields:
order_id: { type: integer, unique: true }
status: { values: [new, paid, shipped, cancelled] }
total: { type: decimal }
card: { generator: card }
coupon: { values: [SPRING10, WELCOME, FREESHIP], empty: 70% }
order_line:
belongs_to: { order: 1-4 }
primary_key: [line_id]
fields:
line_id: { type: integer, unique: true }
isbn: { generator: isbn }
quantity: { values: ["1", "2", "3", "4", "5"] }
price: { type: decimal }
output:
format: json
Run it
Save the recipe, then run this in the same folder. This is what the program printed for it.
$ qadg generate orders.yaml customer: 50 rows, 8 columns order: 120 rows, 5 columns order_line: 308 rows, 4 columns customer: state follows city, though no rule says so customer: zip follows city and state, though no rule says so 3 tables, 478 rows written
What it writes
dataset.json
The first 40 of its 3,960 lines.
{
"customer": [
{
"customer_id": "351940",
"first_name": "Claudia",
"last_name": "Russell",
"gender": "female",
"plan": "pro",
"city": "Winston-Salem",
"state": "NC",
"zip": "27103",
"order": [
]
},
{
"customer_id": "743231",
"first_name": "Taylor",
"last_name": "Mejia",
"gender": "female",
"plan": "team",
"city": "Plains",
"state": "KS",
"zip": "67869",
"order": [
]
},
{
"customer_id": "386830",
"first_name": "Desiree",
"last_name": "Orr",
"gender": "female",
"plan": "team",
"city": "Miami",
"state": "FL",
"zip": "33136",
"order": [
{
"order_id": "35172",
"status": "new",
"total": "359.60",
run-note.json
The seed, the locale and the versions behind the run, and every table it wrote with its rows and columns.