new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Drizzle ORM · all subjects

transactions

8 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Transaction basic usage

Drizzle ORM provides a transaction API by calling db.transaction() with an async callback. The callback receives a tx object that has the same query methods as the main db object. All statements within the transaction are executed as a single logical unit and either all commit or all rollback together.

Nested transactions with savepoints

Drizzle ORM supports savepoints through nested transactions. You can call tx.transaction() inside the callback of db.transaction() to create nested transaction blocks. Each nested level creates a savepoint.

Transactions with relational queries

Transactions work with relational query builders. You can use tx._query to perform relational queries within a transaction, accessing the same methods and features as the main relational query builder.

CockroachDB transaction configuration

CockroachDB transactions accept a configuration object with three optional properties: isolationLevel (string: "read uncommitted" | "read committed" | "repeatable read" | "serializable"), accessMode (string: "read only" | "read write"), and deferrable (boolean).

Transaction example with query builder

Example: const db = drizzle(...); await db.transaction(async (tx) => { await tx.update(accounts).set({ balance: sql`${accounts.balance} - 100.00` }).where(eq(users.name, 'Dan')); await tx.update(accounts).set({ balance: sql`${accounts.balance} + 100.00` }).where(eq(users.name, 'Andrew')); });

Nested transaction example

Example: await db.transaction(async (tx) => { await tx.update(accounts).set({ balance: sql`${accounts.balance} - 100.00` }).where(eq(users.name, 'Dan')); await tx.update(accounts).set({ balance: sql`${accounts.balance} + 100.00` }).where(eq(users.name, 'Andrew')); await tx.transaction(async (tx2) => { await tx2.update(users).set({ name: "Mr. Dan" }).where(eq(users.name, "Dan")); }); });

Transaction with CockroachDB configuration example

Example: await db.transaction(async (tx) => { await tx.update(accounts).set({ balance: sql`${accounts.balance} - 100.00` }).where(eq(users.name, "Dan")); await tx.update(accounts).set({ balance: sql`${accounts.balance} + 100.00` }).where(eq(users.name, "Andrew")); }, { isolationLevel: "read committed", accessMode: "read write", deferrable: true, });

Set transactions in MySQL bug fixed in v0.32.2

Version 0.32.2 fixed a bug with set transactions in MySQL.

Give your agent this brain