extend("flow")
extend("postgres")
extend("string")
conn = pg:connect("postgres://tom:12345678@localhost/tom")
// Ensure table exists
pg:query(conn, `
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username TEXT NOT NULL,
age INT NOT NULL,
balance FLOAT
)
`)
// Insert a user using named parameters
pg:query(
conn,
"INSERT INTO users (username, age, balance) VALUES ($username, $age, $balance)",
[username: "tom", age: 42, balance: 123.45]
)
// Select all users, print username, age, balnce using flow:compose and slog
stream = pg:query(
conn,
"SELECT username, age, balance FROM users"
)
flow:compose(
slog,
flow:throttle(200),
flow:trans(row => string:multi(row["username"], " ", row["age"], " ", row["balance"]))
)(stream)