Skip to main content
BaseModelClass is the base class that all Models inherit from, providing core functionality for database access, query execution, and transaction management.

Database Access Methods

getDB(preset)

Returns a Knex database connection.
Parameters:
  • preset: "r" (read) or "w" (write)
Usage examples:
DBPreset types:
  • "r": Read - Read-only (SELECT)
  • "w": Write - Writable (INSERT, UPDATE, DELETE)
Separating read/write allows distributing read load in database replication.
Prefer getPuri() over getDB() for business logic. getPuri() automatically participates in transactions created by @transactional and includes a built-in UpsertBuilder. Direct use of getDB() is only recommended for migration files, test code, and infrastructure setups that require a raw Knex instance.

getPuri(preset)

Returns a Puri query builder. Automatically uses transaction connection when a transaction is active.
Parameters:
  • preset: "r" (read) or "w" (write)
Usage examples:
getPuri() automatically detects transaction context. When called within @transactional() decorator, it returns the transaction connection.

Subset Query Methods

getSubsetQueries(subset)

Returns a query builder for a specific Subset.
Return values:
  • qb: Query builder (for adding conditions)
  • onSubset: Subset-specific type casting function
Usage examples:
onSubset() is for type checking. It actually returns the same qb object, so it doesn’t affect performance.

executeSubsetQuery(params)

Executes a Subset query and returns results. Automatically handles pagination, Loader, Hydration, and Enhancer.
Parameters: queryMode options: Usage examples:
Execution order: 1. Execute COUNT query (calculate total) 2. Execute LIST query (apply pagination) 3. Execute Loader (load HasMany, ManyToMany data) 4. Hydrate (flat object → nested object conversion) 5. Apply Enhancer (calculate virtual fields) 6. Remove Internal fields

createEnhancers(enhancers)

Helper function to create Enhancer objects. Provides type validation and inference.
What is an Enhancer? An Enhancer is a function that adds virtual fields to query results or transforms data. Usage examples:
Enhancers are called for each row, so avoid heavy operations. If needed, use Loaders or separate APIs.

Utility Methods

getInsertedIds(wdb, rows, tableName, unqKeyFields, chunkSize)

Retrieves IDs of inserted records. Useful after upsert since it queries based on Unique keys.
Parameters: Usage examples:

hydrate(rows)

Converts flat records to nested objects. Transforms table__field format from JOIN results to objects.
Conversion rules:
  • user__name{ user: { name } }
  • user__profile__bio{ user: { profile: { bio } } }
  • If nullable relation’s id is null, the entire object becomes null
Usage examples:
hydrate() is automatically called inside executeSubsetQuery(), so you rarely need to call it directly.

omitInternalFields(row, fields)

Removes Internal fields from an object. Also handles nested fields and arrays.
Usage example:
Fields specified with the internal option in Subset are automatically removed in executeSubsetQuery().

destroy()

Closes database connections. Usually called when the application terminates.
Usage example:
Calling destroy() closes DB connections for all Models. Use only in test environments.

Practical Usage Patterns

Standard findMany Pattern

Complex Filtering Pattern

Custom Aggregation Query

Next Steps

Puri Query Builder

Type-safe query builder usage

Subset

Data retrieval using Subsets

Transactions

Detailed transaction management guide

Testing

Writing Model tests