UNPKG

@avonjs/avonjs

Version:

A fluent Node.js API generator.

112 lines (111 loc) 3.03 kB
import { type AnyRecord, type Model, type Optional, type Order, type PrimaryKey, type QueryModifierCallback, type SearchCollection, type Transaction, type TransactionCallback, type Where } from '../Contracts'; export default abstract class Repository<TModel extends Model = Model> { /** * List of applied conditions. */ wheres: Where[]; /** * List of applied orderings. */ orders: Order[]; /** * List of query modifier callbacks. */ modifiers: QueryModifierCallback[]; /** * The transaction instance. */ _transaction?: Transaction; /** * Run a transaction on the storage. */ transaction<V>(callback: TransactionCallback<V, this>): Promise<V>; /** * Set the transaction instance. */ setTransaction(transaction?: Transaction): this; /** * Get the transaction instance. */ getTransaction(): Transaction | undefined; /** * Indicates whether the transaction is running or not. */ runningInTransaction(): boolean; /** * Start new transaction. */ prepareTransaction(): Promise<Transaction>; /** * Apply condition('s) to the repository. */ where(where: Where | Where[]): this; /** * Set conditions on the repository. */ setWheres(wheres: Where[]): this; /** * Get all conditions from the repository. */ getWheres(): Where[]; /** * Apply condition to repository. */ order(order: Order | Order[]): this; /** * Set orders on the repository. */ setOrders(orders: Order[]): this; /** * Get all orders from the repository. */ getOrders(): Order[]; /** * Modify underlying query before execute. */ modify<T>(modifier: QueryModifierCallback<T>): this; /** * Find model for the given key. */ find(key: PrimaryKey): Promise<Optional<TModel>>; /** * Apply primary key condition */ whereKey(key: PrimaryKey): this; /** * Apply primary key condition */ whereKeys(keys: Array<PrimaryKey>): this; /** * Fill data into model. */ fillModel(result: AnyRecord): TModel; /** * Search storage for given query string. */ abstract search(search: string, page?: number, perPage?: number): Promise<SearchCollection>; /** * Find all model's for the given conditions. */ abstract all(wheres?: Where[]): Promise<TModel[]>; /** * Find first model for the given conditions. */ abstract first(wheres?: Where[]): Promise<Optional<TModel>>; /** * Store given model into the storage. */ abstract store(model: TModel): Promise<TModel>; /** * Update the given model in storage. */ abstract update(model: TModel): Promise<TModel>; /** * Delete model for the given key. */ abstract delete(key: PrimaryKey): Promise<void>; /** * Create new instance of model. */ abstract model(): TModel; }