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

Transaction Integration

When a transaction is already active for the requested preset, getPuri participates in that DB transaction. Otherwise, it accesses the DB configured for the preset.

Using Within @transactional

Nested Method Call Safety

Methods called inside a transaction participate in the same DB transaction when they request the same preset.
Within @transactional, calls to getPuri with the same dbPreset participate in the same DB transaction. The outermost decorated call owns commit and rollback.
When a transaction for the same dbPreset is already active, nested .transaction() creates a savepoint. Await nested calls sequentially; do not run sibling savepoints in parallel with Promise.all. Use a distinct dbPreset consistently for each configured DB; do not reuse one preset name for different DB connections. This partial-rollback guidance is for direct DB queries. For a shared UpsertBuilder batch or graph, propagate failures to the outermost transaction and roll back the whole unit instead of catching and continuing.

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. Using @transactional

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