@decaf-ts/for-postgres
Version:
template for ts projects
131 lines (130 loc) • 5.29 kB
TypeScript
import { Condition, Paginator, Statement } from "@decaf-ts/core";
import { Model } from "@decaf-ts/decorator-validation";
import { PostgresQuery } from "../types";
import { PostgresAdapter } from "../adapter";
/**
* @description Statement builder for PostgreSQL queries
* @summary Provides a fluent interface for building PostgreSQL queries with type safety
* @template M - The model type that extends Model
* @template R - The result type
* @param adapter - The PostgreSQL adapter
* @class PostgresStatement
* @example
* // Example of using PostgreSQLStatement
* const adapter = new MyPostgreSQLAdapter(pool);
* const statement = new PostgreSQLStatement<User, User[]>(adapter);
*
* // Build a query
* const users = await statement
* .from(User)
* .where(Condition.attribute<User>('age').gt(18))
* .orderBy('lastName', 'asc')
* .limit(10)
* .execute();
*/
export declare class PostgresStatement<M extends Model, R> extends Statement<PostgresQuery, M, R> {
constructor(adapter: PostgresAdapter);
/**
* @description Builds a PostgreSQL query from the statement
* @summary Converts the statement's conditions, selectors, and options into a PostgreSQL query
* @return {PostgresQuery} The built PostgreSQL query
* @throws {Error} If there are invalid query conditions
* @mermaid
* sequenceDiagram
* participant Statement
* participant Repository
* participant parseCondition
*
* Statement->>Statement: build()
* Note over Statement: Initialize query
* Statement->>Repository: Get table name
* Repository-->>Statement: Return table name
* Statement->>Statement: Create base query
*
* alt Has selectSelector
* Statement->>Statement: Add columns to query
* end
*
* alt Has whereCondition
* Statement->>Statement: Create combined condition with table
* Statement->>parseCondition: Parse condition
* parseCondition-->>Statement: Return parsed conditions
* Statement->>Statement: Add conditions to query
* end
*
* alt Has orderBySelector
* Statement->>Statement: Add orderBy to query
* end
*
* alt Has limitSelector
* Statement->>Statement: Set limit
* else
* Statement->>Statement: Use default limit
* end
*
* alt Has offsetSelector
* Statement->>Statement: Set offset
* end
*
* Statement-->>Statement: Return query
*/
protected build(): PostgresQuery;
/**
* @description Creates a paginator for the statement
* @summary Builds the query and returns a PostgreSQLPaginator for paginated results
* @template R - The result type
* @param {number} size - The page size
* @return {Promise<Paginator<M, R, PostgreSQLQuery>>} A promise that resolves to a paginator
* @throws {InternalError} If there's an error building the query
*/
paginate<R>(size: number): Promise<Paginator<M, R, PostgresQuery>>;
/**
* @description Processes a record from PostgreSQL
* @summary Converts a raw PostgreSQL record to a model instance
* @param {any} r - The raw record from PostgreSQL
* @param pkAttr - The primary key attribute of the model
* @param {"Number" | "BigInt" | undefined} sequenceType - The type of the sequence
* @return {any} The processed record
*/
private processRecord;
/**
* @description Executes a raw PostgreSQL query
* @summary Sends a raw PostgreSQL query to the database and processes the results
* @template R - The result type
* @param {PostgresQuery} rawInput - The raw PostgreSQL query to execute
* @return {Promise<R>} A promise that resolves to the query results
*/
raw<R>(rawInput: PostgresQuery): Promise<R>;
/**
* @description Parses a condition into PostgreSQL conditions
* @summary Converts a Condition object into PostgreSQL condition structures
* @param {Condition<M>} condition - The condition to parse
* @param {string} [tableName] - the positional index of the arguments
* @return {PostgresQuery} The PostgresSQL condition
* @mermaid
* sequenceDiagram
* participant Statement
* participant translateOperators
* participant parseCondition
*
* Statement->>Statement: parseCondition(condition)
*
* Note over Statement: Extract condition parts
*
* alt Simple comparison operator
* Statement->>translateOperators: translateOperators(operator)
* translateOperators-->>Statement: Return PostgreSQL operator
* Statement->>Statement: Create condition with column, operator, and value
* else NOT operator
* Statement->>Statement: parseCondition(attr1)
* Statement->>Statement: Add NOT to conditions
* else AND/OR operator
* Statement->>Statement: parseCondition(attr1)
* Statement->>Statement: parseCondition(comparison)
* Statement->>Statement: Combine conditions with AND/OR
* end
*
* Statement-->>Statement: Return conditions array
*/
protected parseCondition(condition: Condition<M>, tableName: string): PostgresQuery;
}