import { AST } from 'node-sql-parser'; import { Document, FindCursor, AggregationCursor } from 'mongodb'; /** * Represents a parsed SQL statement */ export interface SqlStatement { ast: AST; text: string; metadata?: { nestedFieldReplacements?: [string, string][]; }; } /** * Command types supported by the MongoDB executor */ export type CommandType = 'FIND' | 'INSERT' | 'UPDATE' | 'DELETE' | 'AGGREGATE'; /** * Base interface for all MongoDB commands */ export interface BaseCommand { type: CommandType; collection: string; } /** * Find command for MongoDB */ export interface FindCommand extends BaseCommand { type: 'FIND'; filter?: Record; projection?: Record; sort?: Record; limit?: number; skip?: number; group?: { _id: any; [key: string]: any; }; pipeline?: Record[]; lookup?: { from: string; localField: string; foreignField: string; as: string; }[]; } /** * Insert command for MongoDB */ export interface InsertCommand extends BaseCommand { type: 'INSERT'; documents: Record[]; } /** * Update command for MongoDB */ export interface UpdateCommand extends BaseCommand { type: 'UPDATE'; filter?: Record; update: Record; upsert?: boolean; } /** * Delete command for MongoDB */ export interface DeleteCommand extends BaseCommand { type: 'DELETE'; filter?: Record; } /** * Aggregate command for MongoDB */ export interface AggregateCommand extends BaseCommand { type: 'AGGREGATE'; pipeline: Record[]; } /** * Union type of all MongoDB commands */ export type Command = FindCommand | InsertCommand | UpdateCommand | DeleteCommand | AggregateCommand; /** * SQL parser interface */ export interface SqlParser { parse(sql: string): SqlStatement; } /** * SQL to MongoDB compiler interface */ export interface SqlCompiler { compile(statement: SqlStatement): Command[]; } /** * Represents result types that can be returned by the executor */ export type ExecutionResult = Document[] | Document | null; /** * Represents cursor types that can be returned by the cursor executor */ export type CursorResult = FindCursor | AggregationCursor | null; /** * Options for cursor execution */ export interface CursorOptions { /** * Number of documents to fetch per batch * This will be set directly on the query command, not on the cursor after creation */ batchSize?: number; } /** * MongoDB command executor interface */ export interface CommandExecutor { connect(): Promise; close(): Promise; /** * Execute MongoDB commands and return documents * @param commands Array of commands to execute * @returns Document results (no cursors) */ execute(commands: Command[]): Promise>; /** * Execute MongoDB commands and return cursors for FIND and AGGREGATE commands * @param commands Array of commands to execute * @param options Options for cursor execution * @returns Cursor for FIND and AGGREGATE commands, null for other commands */ executeCursor(commands: Command[], options?: CursorOptions): Promise>; } /** * Main QueryLeaf interface */ export interface QueryLeaf { /** * Execute a SQL query and return documents * @param sql SQL query string * @returns Document results (no cursors) */ execute(sql: string): Promise>; /** * Execute a SQL query and return a cursor for SELECT queries * @param sql SQL query string * @param options Options for cursor execution * @returns Cursor for SELECT queries, null for other queries */ executeCursor(sql: string, options?: CursorOptions): Promise>; parse(sql: string): SqlStatement; compile(statement: SqlStatement): Command[]; getExecutor(): CommandExecutor; close(): Promise; } export interface Squongo extends QueryLeaf { execute(sql: string): Promise>; executeCursor(sql: string, options?: CursorOptions): Promise>; parse(sql: string): SqlStatement; compile(statement: SqlStatement): Command[]; getExecutor(): CommandExecutor; close(): Promise; }