Skip to main content

Transactions

Transactions 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.

Configure which ORM to use with the userDbclient field in your DBOS configuration:

DBOS.setConfig({
name: 'my-app',
databaseUrl: process.env.DBOS_DATABASE_URL,
userDbclient: 'knex',
});
await DBOS.launch();

Here are some example transactions:

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

export class Greetings {

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

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

Templates

You can initialize a template app for each ORM with the following command:

npx -y @dbos-inc/create@latest -t dbos-knex -n <app-name>

Then, build it, run schema migrations, and start the app:

npm run build
npx dbos migrate
npx dbos start

Visit http://localhost:3000/greeting/dbos to see your app!