UNPKG

@decaf-ts/core

Version:

Core persistence module for the decaf framework

130 lines (129 loc) 6.91 kB
import { AttributeOption, ConditionBuilderOption } from "./options"; import { ConditionalAsync, Model, ModelErrorDefinition } from "@decaf-ts/decorator-validation"; import { GroupOperator, Operator } from "./constants"; type InferAsync<M> = M extends Model<infer A> ? A : false; /** * @description Represents a logical condition for database queries * @summary A class that encapsulates query conditions with support for complex logical operations. * This class allows for building and combining query conditions using logical operators (AND, OR, NOT) * and comparison operators (equals, not equals, greater than, etc.). * @template M - The model type this condition operates on * @param {string | Condition<M>} attr1 - The attribute name or a nested condition * @param {Operator | GroupOperator} operator - The operator to use for the condition * @param {any} comparison - The value to compare against or another condition * @class Condition * @example * // Create a simple condition * const nameCondition = Condition.attribute("name").eq("John"); * * // Create a complex condition * const complexCondition = Condition.attribute("age").gt(18) * .and(Condition.attribute("status").eq("active")); * * // Use the builder pattern * const userQuery = Condition.builder() * .attribute("email").regexp(".*@example.com") * .and(Condition.attribute("lastLogin").gt(new Date("2023-01-01"))); * @mermaid * sequenceDiagram * participant Dev * participant Condition * Dev->>Condition: builder().attribute("age").gt(18) * Condition-->>Dev: Condition(age > 18) * Dev->>Condition: .and(attribute("status").eq("active")) * Condition-->>Dev: Condition((age > 18) AND (status = "active")) */ export declare class Condition<M extends Model<any>> extends Model<InferAsync<M>> { protected attr1?: string | Condition<M>; protected operator?: Operator | GroupOperator; protected comparison?: any; private constructor(); /** * @description Combines this condition with another using logical AND * @summary Joins two conditions with an AND operator, requiring both to be true * @param {Condition<M>} condition - The condition to combine with this one * @return {Condition<M>} A new condition representing the AND operation */ and(condition: Condition<M>): Condition<M>; /** * @description Combines this condition with another using logical OR * @summary Joins two conditions with an OR operator, requiring at least one to be true * @param {Condition<M>} condition - The condition to combine with this one * @return {Condition<M>} A new condition representing the OR operation */ or(condition: Condition<M>): Condition<M>; /** * @description Creates a negation condition * @summary Excludes a value from the result by applying a NOT operator * @param {any} val - The value to negate * @return {Condition<M>} A new condition representing the NOT operation */ not(val: any): Condition<M>; /** * @description Validates the condition and checks for errors * @summary Extends the base validation to ensure the condition is properly formed * @param {...string[]} exceptions - Fields to exclude from validation * @return {ModelErrorDefinition | undefined} Error definition if validation fails, undefined otherwise */ hasErrors(...exceptions: string[]): ConditionalAsync<InferAsync<M>, ModelErrorDefinition | undefined>; /** * @description Creates a new condition that combines two conditions with logical AND * @summary Static method that joins two conditions with an AND operator, requiring both to be true * @template M - The model type this condition operates on * @param {Condition<M>} condition1 - The first condition * @param {Condition<M>} condition2 - The second condition * @return {Condition<M>} A new condition representing the AND operation */ static and<M extends Model>(condition1: Condition<M>, condition2: Condition<M>): Condition<M>; /** * @description Creates a new condition that combines two conditions with logical OR * @summary Static method that joins two conditions with an OR operator, requiring at least one to be true * @template M - The model type this condition operates on * @param {Condition<M>} condition1 - The first condition * @param {Condition<M>} condition2 - The second condition * @return {Condition<M>} A new condition representing the OR operation */ static or<M extends Model>(condition1: Condition<M>, condition2: Condition<M>): Condition<M>; /** * @description Creates a new condition that groups two conditions with a specified operator * @summary Private static method that combines two conditions using the specified group operator * @template M - The model type this condition operates on * @param {Condition<M>} condition1 - The first condition * @param {GroupOperator} operator - The group operator to use (AND, OR) * @param {Condition<M>} condition2 - The second condition * @return {Condition<M>} A new condition representing the grouped operation */ private static group; /** * @description Creates a condition builder for a specific model attribute * @summary Static method that initializes a condition builder with the specified attribute * @template M - The model type this condition operates on * @param attr - The model attribute to build a condition for * @return {AttributeOption<M>} A condition builder initialized with the attribute */ static attribute<M extends Model>(attr: keyof M): AttributeOption<M>; /** * @description Alias for the attribute method * @summary Shorthand method that initializes a condition builder with the specified attribute * @template M - The model type this condition operates on * @param attr - The model attribute to build a condition for * @return {AttributeOption<M>} A condition builder initialized with the attribute */ static attr<M extends Model>(attr: keyof M): AttributeOption<M>; /** * @description Provides a fluent API to build query conditions * @summary A builder class that simplifies the creation of database query conditions * with a chainable interface for setting attributes and operators * @template M - The model type this condition builder operates on * @class ConditionBuilder */ private static Builder; /** * @description Creates a new condition builder * @summary Factory method that returns a new instance of the condition builder * @template M - The model type this condition builder will operate on * @return {ConditionBuilderOption<M>} A new condition builder instance */ static builder<M extends Model>(): ConditionBuilderOption<M>; } export {};