Inventory Lookup
Queries a SQLite database to check product stock levels and reports whether inventory is sufficient or running low.
Full source
txt
config:
name: "Inventory Lookup"
version: 1
services:
DB is a database at "./inventory.sqlite"
workflow:
trigger: when a stock check is requested
step CheckProduct:
get product using DB at "products" with id request.product-id
save the result as product
if product is empty:
reject with "Product not found"
log "Found product: {product.name}"
step EvaluateStock:
if product.stock is below 10:
log "LOW STOCK WARNING: {product.name} has only {product.stock} units"
complete with status "low-stock" and product-name product.name and stock product.stock
otherwise:
log "Stock level OK: {product.name} has {product.stock} units"
complete with status "in-stock" and product-name product.name and stock product.stockWhat this does
- Declares a database service —
DB is a database at "./inventory.sqlite"connects to a local SQLite file - Queries a single row —
get product using DB at "products"auto-generates aSELECT * FROM products WHERE id = :id LIMIT 1 - Handles missing data — if the query returns no rows,
productis empty and the workflow rejects - Reads fields from the result —
product.nameandproduct.stockaccess columns from the returned row - Evaluates stock level — compares
product.stockagainst a threshold usingis below - Completes with status — returns either
"low-stock"or"in-stock"along with the product details
Concepts demonstrated
- Database service type —
is a database at "path"backed by SQLite viabetter-sqlite3 - Table mode queries —
at "tablename"withwithparams auto-generates SQL (no raw SQL needed) - Verb-to-operation mapping —
getmaps toSELECT ... LIMIT 1, returning a single record or empty - Empty checks —
if product is empty:handles the case where no database row matched - Dot access on query results —
product.name,product.stockread columns from the returned record - Multiple outputs —
complete with status ... and product-name ... and stock ...
Running it
bash
# Test with mock services (no database needed)
flow run examples/inventory-lookup.flow --mock \
--input '{"request": {"product-id": 1}}'
# Run with a real SQLite database
flow run examples/inventory-lookup.flow \
--input '{"request": {"product-id": 42}}'Database setup
For real execution, create the SQLite database first:
bash
sqlite3 inventory.sqlite <<'SQL'
CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
stock INTEGER DEFAULT 0
);
INSERT INTO products (name, stock) VALUES ('Widget', 100);
INSERT INTO products (name, stock) VALUES ('Gadget', 3);
INSERT INTO products (name, stock) VALUES ('Doohickey', 50);
SQLAs a webhook
bash
flow serve examples/inventory-lookup.flow --mock
curl -X POST http://localhost:3000 \
-H "Content-Type: application/json" \
-d '{"request": {"product-id": 1}}'