Transactions
elements man database/transactionsRunning work in a transaction, how nesting behaves, and how the connection pool is sized.
Formatting
Write every insert with one column per line, ) values (, then one value per
line in the same order, so the nth value sits under the nth column whatever the
number of columns. Other queries are laid out by clause; see
elements man start/conventions.
One per linesql(` insert into signups ( name, email, fileName, contentType, size ) values ( ${name}, ${email}, ${file?.name}, ${file?.contentType}, ${file?.size} ) `);
Each value is on the same line of the value list as its column is in the column list, and adding a column adds one line to the column list and one line to the value list.
Transactions
tx(fn) runs fn inside a Postgres transaction. The transaction commits when
the callback returns and rolls back if the callback throws.
Transactionimport { tx, sql } from "@elements/app"; tx(() => { let user = sql<User>(` insert into users ( name ) values ( ${name} ) returning * `).firstOrThrow(); sql(` insert into auditLog ( event, userId ) values ( 'user_created', ${user.id} ) `); });
The connection that opens the transaction is held for the duration of the
callback. Every sql() call inside tx() runs on that same connection
automatically, so you do not pass a transaction object to the
functions you call.
A tx() called inside another tx() joins the transaction already in
progress. No new transaction opens, and the transaction commits when the
outermost tx() callback returns or rolls back when it throws. A helper that
uses tx() can be called inside another tx() without change.
Nested txtx(() => { let user = createUser(name); sql(` insert into orgs ( ownerId ) values ( ${user.id} ) `); }); function createUser(name: string) { return tx(() => { return sql<User>(` insert into users ( name ) values ( ${name} ) returning * `).firstOrThrow(); }); }
Because there is one transaction, an unhandled error in the outer callback or in any nested callback rolls back every statement the transaction ran.
txAsync(fn) is the explicit async variant. Use it only when you need explicit
promise control inside the callback.
A Dedicated Connection
sql() checks out a connection from the pool for each query, and every
sql() inside a tx() runs on that transaction's connection. When you need a
separate connection for work that must run or persist outside the surrounding
transaction, such as audit logging, check one out with checkoutDb().
Own connectionimport { checkoutDb } from "@elements/app"; using db = checkoutDb(); db.tx(() => { db.sql(` insert into audit ( event ) values ( 'exported' ) `); });
checkoutDb() returns a connection object, db above. It has db.sql() for
queries and db.tx() for transactions. For savepoints and other transaction
control, run the SQL statement with db.sql(), for example
db.sql("savepoint sp1"). Declare it with using so the connection returns to
the pool when the block ends.
Connection Pool
The app server maintains a Postgres connection pool. The pool eagerly opens its first connection at app startup so a connectivity problem fails fast instead of surfacing on the first request.
Every sql() call checks out a connection from the pool, runs the query, and
returns the connection. A tx() callback checks out a single connection at the
start of the block and holds it for the entire callback, so every sql() inside
reuses that one connection. The connection returns to the pool when the
transaction commits or rolls back.
Keep transactions to the database writes that need to be atomic. Do long-running
work like network calls or file processing outside the tx() block so the
connection returns to the pool quickly.