Persistence

Named Queries

Defining and using custom named queries on entity services in Kinotic.

Named queries let you define custom query methods on your repositories. You declare methods with the @Query decorator using a SQL-like syntax, sync them with the server, and the CLI generates the implementation.

Currently named queries support aggregate queries (COUNT, SUM, AVG, MIN, MAX, etc.). Support for SELECT, UPDATE, and DELETE queries is coming soon.

Workflow

  1. Run kinotic sync to sync your entities and generate the base repository classes
  2. Add custom methods with the @Query decorator to the generated repository class
  3. Run kinotic generate to regenerate the repository with your query implementations (generate runs locally without syncing to the server)

Defining Named Queries

Add methods with the @Query decorator to your repository class. Use :paramName syntax for parameter binding.

import { Query } from '@kinotic-ai/persistence'

export class OrderRepository extends BaseOrderRepository {

    @Query('SELECT COUNT(*) FROM Order WHERE status = :status')
    countByStatus(status: string): Promise<number> {
        // Implementation is generated by the CLI
    }

    @Query('SELECT SUM(amount) FROM Order WHERE customerId = :customerId')
    totalSpentByCustomer(customerId: string): Promise<number> {
        // Implementation is generated by the CLI
    }
}

The method body is left empty because kinotic generate fills in the implementation.

Named queries must be synced to the server before they can be used. If a query has not been synced, calling it will fail at runtime.

Query Parameters

Each parameter in the query uses :paramName syntax. At runtime, the generated method maps its arguments to the corresponding placeholders.

Copyright © 2026