interface QueryConfig {
table: string;
select: string[];
where?: Record<string, unknown>;
orderBy?: string;
limit?: number;
}
class QueryBuilder {
private config: Partial<QueryConfig> = {};
from(table: string): this {
this.config.table = table;
return this;
}
select(...fields: string[]): this {
this.config.select = fields;
return this;
}
where(conditions: Record<string, unknown>): this {
this.config.where = conditions;
return this;
}
orderBy(field: string): this {
this.config.orderBy = field;
return this;
}
limit(count: number): this {
this.config.limit = count;
return this;
}
build(): QueryConfig {
if (!this.config.table || !this.config.select) {
throw new Error('table and select are required');
}
return this.config as QueryConfig;
}
}Usage
const query = new QueryBuilder()
.from('users')
.select('id', 'name', 'email')
.where({ active: true })
.orderBy('createdAt')
.limit(10)
.build();



