Home / Function/ transaction() — drizzle-orm Function Reference

transaction() — drizzle-orm Function Reference

Architecture documentation for the transaction() function in index.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  88a4cfc7_6a4b_ec71_e63a_82d38bac0d2d["transaction()"]
  34f01008_7c45_3804_1fad_b8b59cd8a413["MyDurableObject"]
  88a4cfc7_6a4b_ec71_e63a_82d38bac0d2d -->|defined in| 34f01008_7c45_3804_1fad_b8b59cd8a413
  7007928d_3a80_8682_4dbb_42bd693db19a["nestedTransaction()"]
  7007928d_3a80_8682_4dbb_42bd693db19a -->|calls| 88a4cfc7_6a4b_ec71_e63a_82d38bac0d2d
  7c7acbfe_6328_c8df_feba_552ee62a84d9["default.fetch()"]
  7c7acbfe_6328_c8df_feba_552ee62a84d9 -->|calls| 88a4cfc7_6a4b_ec71_e63a_82d38bac0d2d
  b19b108f_def7_3a32_99a3_c8be51ce2fbf["beforeEach()"]
  88a4cfc7_6a4b_ec71_e63a_82d38bac0d2d -->|calls| b19b108f_def7_3a32_99a3_c8be51ce2fbf
  style 88a4cfc7_6a4b_ec71_e63a_82d38bac0d2d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

integration-tests/tests/sqlite/durable-objects/index.ts lines 2066–2111

	async transaction(): Promise<void> {
		try {
			await this.beforeEach();
			const users = sqliteTable('users_transactions', {
				id: integer('id').primaryKey(),
				balance: integer('balance').notNull(),
			});
			const products = sqliteTable('products_transactions', {
				id: integer('id').primaryKey(),
				price: integer('price').notNull(),
				stock: integer('stock').notNull(),
			});

			this.db.run(sql`drop table if exists ${users}`);
			this.db.run(sql`drop table if exists ${products}`);

			this.db.run(sql`create table users_transactions (id integer not null primary key, balance integer not null)`);
			this.db.run(
				sql`create table products_transactions (id integer not null primary key, price integer not null, stock integer not null)`,
			);

			const user = this.db.insert(users).values({ balance: 100 }).returning().get();
			const product = this.db.insert(products).values({ price: 10, stock: 10 }).returning().get();

			this.db.transaction(async (tx) => {
				tx.update(users)
					.set({ balance: user.balance - product.price })
					.where(eq(users.id, user.id))
					.run();
				tx.update(products)
					.set({ stock: product.stock - 1 })
					.where(eq(products.id, product.id))
					.run();
			});

			const result = this.db.select().from(users).all();

			expect(result).deep.equal([{ id: 1, balance: 90 }]);

			this.db.run(sql`drop table ${users}`);
			this.db.run(sql`drop table ${products}`);
		} catch (error: any) {
			console.error(error);
			throw new Error(`transaction error`);
		}
	}

Domain

Subdomains

Calls

Frequently Asked Questions

What does transaction() do?
transaction() is a function in the drizzle-orm codebase, defined in integration-tests/tests/sqlite/durable-objects/index.ts.
Where is transaction() defined?
transaction() is defined in integration-tests/tests/sqlite/durable-objects/index.ts at line 2066.
What does transaction() call?
transaction() calls 1 function(s): beforeEach.
What calls transaction()?
transaction() is called by 2 function(s): default.fetch, nestedTransaction.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free