UNPKG

@decaf-ts/core

Version:

Core persistence module for the decaf framework

84 lines (83 loc) 3.89 kB
import { Condition } from "../query"; import { RamContext, RawRamQuery } from "./types"; import { Model } from "@decaf-ts/decorator-validation"; import { Statement } from "../query/Statement"; import { Adapter, AdapterFlags } from "../persistence/index"; /** * @description RAM-specific query statement builder * @summary Extends the base Statement class to provide query building functionality for the RAM adapter. * This class translates high-level query operations into predicates that can filter and sort * in-memory data structures. * @template M - The model type being queried * @template R - The result type returned by the query * @param {RamAdapter} adapter - The RAM adapter instance to use for executing queries * @class RamStatement * @category Ram * @example * ```typescript * // Create a statement for querying User models * const statement = new RamStatement<User, User>(ramAdapter); * * // Build a query to find active users with age > 18 * const results = await statement * .from(User) * .where(Condition.and( * Condition.eq('active', true), * Condition.gt('age', 18) * )) * .orderBy('lastName', 'asc') * .limit(10) * .execute(); * ``` */ export declare class RamStatement<M extends Model, R, A extends Adapter<M, any, RawRamQuery<any>, RamContext>> extends Statement<M, A, R, RawRamQuery<any>> { constructor(adapter: A, overrides?: Partial<AdapterFlags>); /** * @description Creates a sort comparator function * @summary Generates a function that compares two model instances based on the orderBy criteria. * This method handles different data types (string, number, date) and sort directions (asc, desc). * @return {function(Model, Model): number} A comparator function for sorting model instances */ private getSort; private compareBooleans; private compareNumbers; private compareBigInts; private compareStrings; private compareDates; /** * @description Builds a RAM query from the statement * @summary Converts the statement's selectors and conditions into a RawRamQuery object * that can be executed by the RAM adapter. This method assembles all query components * (select, from, where, limit, offset, sort) into the final query structure. * @return {RawRamQuery<M>} The constructed RAM query object */ protected build(): RawRamQuery<any>; /** * @description Parses a condition into a RAM query predicate * @summary Converts a Condition object into a predicate function that can be used * to filter model instances in memory. This method handles both simple conditions * (equals, greater than, etc.) and complex conditions with logical operators (AND, OR). * @template M - The model type for the condition * @param {Condition<M>} condition - The condition to parse * @return {RawRamQuery<M>} A RAM query object with a where predicate function * @mermaid * sequenceDiagram * participant Caller * participant RamStatement * participant SimpleCondition * participant ComplexCondition * * Caller->>RamStatement: parseCondition(condition) * alt Simple condition (eq, gt, lt, etc.) * RamStatement->>SimpleCondition: Extract attr1, operator, comparison * SimpleCondition-->>RamStatement: Return predicate function * else Logical operator (AND, OR) * RamStatement->>ComplexCondition: Extract nested conditions * RamStatement->>RamStatement: parseCondition(leftCondition) * RamStatement->>RamStatement: parseCondition(rightCondition) * ComplexCondition-->>RamStatement: Combine predicates with logical operator * end * RamStatement-->>Caller: Return query with where predicate */ protected parseCondition(condition: Condition<M>): RawRamQuery<any>; }