Named Queries
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
- Run
kinotic syncto sync your entities and generate the base repository classes - Add custom methods with the
@Querydecorator to the generated repository class - Run
kinotic generateto regenerate the repository with your query implementations (generateruns 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.