Your first scenario#
This page walks through writing and running a small scenario that inserts a merchant row and a product row into Postgres, wiring the merchant’s generated ID into the product insert.
Prerequisites#
You’ll need a PostgreSQL database you can write to and two tables:
CREATE TABLE merchants (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
region TEXT NOT NULL
);
CREATE TABLE products (
id SERIAL PRIMARY KEY,
merchant_id INT NOT NULL REFERENCES merchants(id),
name TEXT NOT NULL,
price NUMERIC NOT NULL
);Write the scenario#
Create scenarios/hello.hcl:
scenario "hello" {
required_providers {
postgres = {
source = "builtin/postgres"
}
}
variable "dsn" {
default = "postgres://localhost:5432/orchard_dev?sslmode=disable"
}
variable "merchant_name" {
default = "Acme Corp"
}
provider "postgres" {
dsn = var.dsn
}
action "postgres_query" "create_merchant" {
query = "INSERT INTO merchants (name, region) VALUES ('${var.merchant_name}', 'us') RETURNING id, name"
}
action "postgres_query" "create_product" {
query = "INSERT INTO products (merchant_id, name, price) VALUES (${action.postgres_query.create_merchant.id}, 'Widget', 9.99) RETURNING id, name"
}
output "merchant_id" {
value = action.postgres_query.create_merchant.id
}
output "product_id" {
value = action.postgres_query.create_product.id
}
}A few things to notice:
required_providersdeclares which providers the scenario uses and where they come from.builtin/postgresis compiled into theorchardbinary.action "postgres_query" "create_merchant"is an action of typepostgres_query(supplied by the postgres provider) namedcreate_merchant. Thequeryattribute is passed to the provider; the first returned row becomes the action’s outputs.action.postgres_query.create_merchant.idis how the second action references the first action’s output. Orchard parses this reference, adds an edge to the dependency graph, and runscreate_merchantbeforecreate_product.outputblocks expose values from the scenario when it finishes.
Preview with plan#
Before running, check the execution plan:
orchard plan scenarios/hello.hclplan validates the scenario, type-checks all references, and prints the graph.
It does not open a database connection or run any actions.
Run it#
orchard run scenarios/hello.hclOrchard configures the postgres provider, runs the two actions in dependency order, and prints the declared outputs.
To override a variable:
orchard run scenarios/hello.hcl --var merchant_name="Globex"Next steps#
- Read Concepts to understand scenarios, components, providers, and wiring.
- Browse the provider reference to see what each built-in provider can do.
- Check out guides for worked examples: demo data, local dev, integration testing.