Skip to main content

Transactions

We recommend performing database operations in transactions. These are a special type of step that are optimized for database accesses. They execute as a single database transaction.

To make a TypeScript function a transaction, annotate it with the DBOS.transaction decorator. Then, access the database using raw SQL or one of several supported ORMs, including Knex.js, Drizzle, TypeORM, and Prisma. You can configure which ORM to use in your dbos-config.yaml file. Knex is the default.

Here are some examples:

interface GreetingRecord {
name: string;
note: string;
}

export class Greetings {
//...
@DBOS.transaction()
static async insertGreeting(gr: GreetingRecord) {
await DBOS.knexClient('greetings').insert(gr);
}

@DBOS.transaction({readOnly: true})
static async getGreetings(): Promise<GreetingRecord[]> {
return await DBOS.knexClient<GreetingRecord>('greetings').select('*');
}
}
note

As shown above, we suggest decorating read-only transactions with @DBOS.transaction({readOnly: true}) for better performance.

Schema Management

We strongly recommend you manage your database schema using migrations.

Migration commands are configured in your dbos-config.yaml file. At migration time, DBOS runs all migration commands. Please see these guides for details on how to configure migrations with each supported ORM: