/** * @db4/core - Unified Predicate Types * * This module provides unified predicate/constraint types for SQL query operations * across all @db4 packages. It defines a single operator union type that can be * used by virtual-tables, dosql-backend, and other query-related modules. * * @packageDocumentation */ /** * SQL value types supported by db4 query operations. * These represent the primitive types that can be stored and queried in SQL. */ export type SqlValue = string | number | boolean | null; /** * Comparison operators for predicates. * * These operators handle basic value comparisons: * - `eq`: Equal to (=) * - `ne`: Not equal to (!=, <>) * - `lt`: Less than (<) * - `le`: Less than or equal to (<=) * - `gt`: Greater than (>) * - `ge`: Greater than or equal to (>=) */ export type ComparisonOp = 'eq' | 'ne' | 'lt' | 'le' | 'gt' | 'ge'; /** * Pattern matching operators for predicates. * * - `like`: SQL LIKE pattern matching (supports % and _ wildcards) */ export type PatternOp = 'like'; /** * Set membership operators for predicates. * * - `in`: Value is in a set of values (IN clause) */ export type SetOp = 'in'; /** * NULL check operators for predicates. * * - `is_null`: Value is NULL (IS NULL) * - `is_not_null`: Value is not NULL (IS NOT NULL) */ export type NullOp = 'is_null' | 'is_not_null'; /** * Complete union of all predicate operators. * * This type represents all supported SQL filter operations across db4: * - Comparisons: eq, ne, lt, le, gt, ge * - Pattern matching: like * - Set membership: in * - NULL checks: is_null, is_not_null * * @example * ```typescript * const predicate: Predicate = { * column: 'name', * op: 'like', * value: 'John%' * }; * ``` */ export type PredicateOp = ComparisonOp | PatternOp | SetOp | NullOp; /** * A query predicate (constraint/filter condition). * * Predicates represent individual conditions in a WHERE clause. * They can be combined using AND logic, with support for OR conditions * via the optional `or` array. * * @example * ```typescript * // Simple equality * const eq: Predicate = { column: 'status', op: 'eq', value: 'active' }; * * // Range query * const range: Predicate = { column: 'age', op: 'gte', value: 18 }; * * // NULL check * const nullCheck: Predicate = { column: 'deletedAt', op: 'is_null', value: null }; * * // IN clause * const inSet: Predicate = { column: 'role', op: 'in', value: ['admin', 'moderator'] }; * * // OR condition * const orCondition: Predicate = { * column: 'type', * op: 'eq', * value: 'A', * or: [{ column: 'type', op: 'eq', value: 'B' }] * }; * ``` */ export interface Predicate { /** * The column name to apply the predicate to. * May include table prefix for joins (e.g., "users.id"). */ column: string; /** * The predicate operator. */ op: PredicateOp; /** * The value to compare against. * * - For comparison operators: a single SqlValue * - For `in` operator: an array of SqlValues * - For `is_null`/`is_not_null`: typically null (value is ignored) * - For `like`: a string pattern with SQL wildcards (% and _) */ value: SqlValue | SqlValue[]; /** * Alternative predicates for OR conditions. * * When present, the predicate matches if either the main condition * OR any of the alternative conditions match. * * @example * ```typescript * // WHERE (status = 'active' OR status = 'pending') * const predicate: Predicate = { * column: 'status', * op: 'eq', * value: 'active', * or: [{ column: 'status', op: 'eq', value: 'pending' }] * }; * ``` */ or?: Predicate[]; } /** * Alias for Predicate - used by virtual-tables module. * * @deprecated Use `Predicate` instead. This alias is provided for backward * compatibility with existing code that uses the `Constraint` name. */ export type Constraint = Predicate; /** * Alias for PredicateOp - used by dosql-backend module. * * @deprecated Use `PredicateOp` instead. This alias is provided for backward * compatibility with existing code that uses the `FilterOp['op']` pattern. */ export type FilterOpType = PredicateOp; /** * All comparison operators as a readonly array. * Useful for validation and iteration. */ export declare const COMPARISON_OPS: readonly ComparisonOp[]; /** * All pattern operators as a readonly array. */ export declare const PATTERN_OPS: readonly PatternOp[]; /** * All set operators as a readonly array. */ export declare const SET_OPS: readonly SetOp[]; /** * All NULL operators as a readonly array. */ export declare const NULL_OPS: readonly NullOp[]; /** * All predicate operators as a readonly array. * Useful for validation and iteration. */ export declare const PREDICATE_OPS: readonly PredicateOp[]; /** * Type guard for comparison operators. * * @param value - The value to check * @returns true if the value is a comparison operator */ export declare function isComparisonOp(value: unknown): value is ComparisonOp; /** * Type guard for pattern operators. * * @param value - The value to check * @returns true if the value is a pattern operator */ export declare function isPatternOp(value: unknown): value is PatternOp; /** * Type guard for set operators. * * @param value - The value to check * @returns true if the value is a set operator */ export declare function isSetOp(value: unknown): value is SetOp; /** * Type guard for NULL operators. * * @param value - The value to check * @returns true if the value is a NULL operator */ export declare function isNullOp(value: unknown): value is NullOp; /** * Type guard for predicate operators. * * @param value - The value to check * @returns true if the value is a valid predicate operator */ export declare function isPredicateOp(value: unknown): value is PredicateOp; /** * Type guard for SqlValue. * * @param value - The value to check * @returns true if the value is a valid SQL value */ export declare function isSqlValue(value: unknown): value is SqlValue; /** * Type guard for Predicate. * * @param value - The value to check * @returns true if the value is a valid Predicate */ export declare function isPredicate(value: unknown): value is Predicate; /** * Alias for isPredicate - for backward compatibility with Constraint usage. * * @deprecated Use `isPredicate` instead. */ export declare const isConstraint: typeof isPredicate; /** * Maps a predicate operator to its SQL representation. * * @param op - The predicate operator * @returns The SQL operator string * * @example * ```typescript * predicateOpToSQL('eq') // '=' * predicateOpToSQL('ne') // '!=' * predicateOpToSQL('lt') // '<' * predicateOpToSQL('like') // 'LIKE' * predicateOpToSQL('in') // 'IN' * predicateOpToSQL('is_null') // 'IS NULL' * ``` */ export declare function predicateOpToSQL(op: PredicateOp): string; /** * Creates a simple equality predicate. * * @param column - The column name * @param value - The value to compare against * @returns A Predicate with op='eq' */ export declare function eq(column: string, value: SqlValue): Predicate; /** * Creates a simple not-equal predicate. * * @param column - The column name * @param value - The value to compare against * @returns A Predicate with op='ne' */ export declare function ne(column: string, value: SqlValue): Predicate; /** * Creates a less-than predicate. * * @param column - The column name * @param value - The value to compare against * @returns A Predicate with op='lt' */ export declare function lt(column: string, value: SqlValue): Predicate; /** * Creates a less-than-or-equal predicate. * * @param column - The column name * @param value - The value to compare against * @returns A Predicate with op='le' */ export declare function le(column: string, value: SqlValue): Predicate; /** * Creates a greater-than predicate. * * @param column - The column name * @param value - The value to compare against * @returns A Predicate with op='gt' */ export declare function gt(column: string, value: SqlValue): Predicate; /** * Creates a greater-than-or-equal predicate. * * @param column - The column name * @param value - The value to compare against * @returns A Predicate with op='ge' */ export declare function ge(column: string, value: SqlValue): Predicate; /** * Creates a LIKE pattern predicate. * * @param column - The column name * @param pattern - The LIKE pattern (use % for any characters, _ for single character) * @returns A Predicate with op='like' */ export declare function like(column: string, pattern: string): Predicate; /** * Creates an IN predicate. * * @param column - The column name * @param values - The array of values to check against * @returns A Predicate with op='in' */ export declare function inSet(column: string, values: SqlValue[]): Predicate; /** * Creates an IS NULL predicate. * * @param column - The column name * @returns A Predicate with op='is_null' */ export declare function nullPredicate(column: string): Predicate; /** * Creates an IS NOT NULL predicate. * * @param column - The column name * @returns A Predicate with op='is_not_null' */ export declare function notNullPredicate(column: string): Predicate; /** * Combines predicates with OR logic. * * @param first - The first predicate * @param rest - Additional predicates to OR together * @returns A predicate with the others as OR alternatives * * @example * ```typescript * // WHERE (status = 'active' OR status = 'pending' OR status = 'review') * const pred = or( * eq('status', 'active'), * eq('status', 'pending'), * eq('status', 'review') * ); * ``` */ export declare function or(first: Predicate, ...rest: Predicate[]): Predicate; //# sourceMappingURL=predicate.d.ts.map