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
ThegetPuri method works internally as follows:
- Check for an active transaction via
DB.getTransactionContext() - If a transaction exists, reuse it
- 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. AllgetPuri 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.Practical Examples
- Aggregate Queries
- Raw SQL
- Batch Operations
- Complex Transactions
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
"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