Drizzle transactions

From Intense-Visions/harness-engineering · View interactive version

Drizzle transactions is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Execute atomic Drizzle operations with db.transaction(), nested transactions, and rollback semantics

How to use this skill

Skill file

Drizzle Transactions

Execute atomic Drizzle operations with db.transaction(), nested transactions, and rollback semantics

When to Use

Instructions

  1. Basic transaction — pass an async callback:
const result = await db.transaction(async (tx) => {
  const [sender] = await tx
    .update(accounts)
    .set({ balance: sql`${accounts.balance} - ${amount}` })
    .where(eq(accounts.id, senderId))
    .returning();

  if (sender.balance < 0) {
    tx.rollback(); // Rolls back the entire transaction
  }

  const [receiver] = await tx
    .update(accounts)
    .set({ balance: sql`${accounts.balance} + ${amount}` })
    .where(eq(accounts.id, receiverId))
    .returning();

  return { sender, receiver };
});
  1. Use tx for all queries inside the callback. Using db instead of tx runs queries outside the transaction.
  1. Rollback — call tx.rollback() or throw an error:
await db.transaction(async (tx) => {
  await tx.insert(orders).values({ userId, total });
  const inventory = await tx.query.products.findFirst({
    where: eq(products.id, productId),
  });

  if (!inventory || inventory.stock < quantity) {
    throw new Error('Insufficient stock'); // Triggers rollback
  }

  await tx
    .update(products)
    .set({ stock: sql`${products.stock} - ${quantity}` })
    .where(eq(products.id, productId));
});
  1. Nested transactions (savepoints):
await db.transaction(async (tx) => {
  await tx.insert(orders).values(orderData);

  try {
    await tx.transaction(async (tx2) => {
      await tx2.insert(notifications).values(notifData);
      // If this fails, only the inner savepoint rolls back
    });
  } catch {
    // Order still committed; notification failed silently
  }
});
  1. Transaction configuration (PostgreSQL):
await db.transaction(
  async (tx) => {
    // ...
  },
  {
    isolationLevel: 'serializable',
    accessMode: 'read write',
    deferrable: true,
  }
);
  1. Batch operations in a transaction:
await db.transaction(async (tx) => {
  for (const item of items) {
    await tx
      .update(inventory)
      .set({ stock: sql`${inventory.stock} - ${item.qty}` })
      .where(eq(inventory.sku, item.sku));
  }
});

Details

Drizzle transactions use the underlying database driver's transaction support. The tx object is a transaction-scoped Drizzle client with the same query API as db.

tx.rollback(): This throws a special error that Drizzle catches to trigger rollback. Do not catch this error inside the transaction callback. The transaction function returns never after tx.rollback().

Nested transactions use savepoints. On PostgreSQL and MySQL, nested tx.transaction() calls translate to SAVEPOINT / ROLLBACK TO SAVEPOINT. SQLite does not support savepoints in all drivers.

Isolation levels (PostgreSQL):

Connection handling: A transaction holds one database connection for its entire duration. Keep transactions short to avoid connection pool exhaustion. Avoid external HTTP calls, file I/O, or long computations inside transactions.

Trade-offs:

Source

https://orm.drizzle.team/docs/transactions

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.