Skip to main content
getPuri is a method that retrieves the Puri query builder. It is necessary when writing direct SQL queries or using UpsertBuilder.

Type Signature

Parameters

which

Specifies the database preset. Type: DBPreset ("r" | "w")
  • "r": Read-only (Replica DB, for queries)
  • "w": Write-capable (Primary DB, for writes)
"r" and "w" may point to the same DB depending on your configuration.

Return Value

Type: PuriWrapper Returns a Puri query builder wrapper object.

PuriWrapper Methods

table / from

Starts a Puri query builder by specifying a table.

transaction

Starts a transaction.

UpsertBuilder Methods

ubRegister

Registers a record to UpsertBuilder.

ubUpsert

Upserts the registered records.

ubInsertOnly

Inserts the registered records only (no UPDATE).

ubUpdateBatch

Batch updates the registered records.

raw

Executes raw SQL.

Basic Usage

Writing Direct Queries

Complex Queries

Using Transactions

Automatic Transaction Context Detection

getPuri automatically detects and reuses transaction contexts. This is an important feature when used with the @transactional decorator.

How It Works

The getPuri method works internally as follows:
  1. Check for an active transaction via DB.getTransactionContext()
  2. If a transaction exists, reuse it
  3. If no transaction exists, create a new PuriWrapper

Using Within @transactional

Nested Method Call Safety

Thanks to this feature, it’s safe to call other methods within a transaction. All getPuri calls use the same transaction.
The transaction context set by the @transactional decorator is automatically shared across all sub-method calls. This is implemented via AsyncLocalStorage.
A PuriWrapper obtained via getPuri within a transaction and a new transaction started with wdb.transaction() are different transactions. If you need nested transactions, you must manage them explicitly.

Practical Examples

getPuri vs getDB

getPuri

Provides Puri query builder + UpsertBuilder.

getDB

Provides a pure Knex instance.
In most cases, we recommend using getPuri. It provides type safety and UpsertBuilder functionality.

Transaction Isolation Levels

Isolation levels:
  • "read uncommitted"
  • "read committed" (PostgreSQL default)
  • "repeatable read"
  • "serializable"

Type Safety

Puri provides complete type safety.

Caveats

1. Read/Write Separation

Use "r" for read operations and "w" for write operations.

2. Transaction Context

Within @transactional, transactions are automatically managed.

3. UpsertBuilder Requires Transaction

Performance Optimization

1. Use Indexes

2. Limit SELECT Fields

3. Batch Processing

Next Steps

Puri Query Builder

How to write Puri queries

UpsertBuilder

UpsertBuilder usage guide

@transactional

Transaction decorator

Transactions

Manual transaction management