queryforge
Version:
A powerful, type-safe SQL query builder for Node.js with support for MySQL, PostgreSQL, and SQLite. Features fluent API, transaction management, and connection pooling.
164 lines • 4.43 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryBuilder = void 0;
class QueryBuilder {
constructor(options = {}) {
this.options = options;
this.state = {
type: 'SELECT',
table: '',
columns: ['*'],
values: [],
where: [],
joins: [],
groupBy: [],
having: [],
orderBy: [],
};
}
reset(keepTable = false) {
const oldTable = keepTable ? this.state.table : '';
this.state = {
type: 'SELECT',
table: oldTable,
columns: ['*'],
values: [],
where: [],
joins: [],
groupBy: [],
having: [],
orderBy: [],
};
}
table(tableName) {
this.reset(false);
this.state.table = tableName;
return this;
}
select(...columns) {
this.reset(true);
this.state.type = 'SELECT';
this.state.columns = columns.length ? columns : ['*'];
return this;
}
where(column, operator, value) {
this.state.where.push({
column,
operator: operator,
value,
logic: 'AND',
});
return this;
}
orWhere(column, operator, value) {
this.state.where.push({
column,
operator: operator,
value,
logic: 'OR',
});
return this;
}
join(table, leftColumn, operator, rightColumn) {
this.state.joins.push({
table,
type: 'INNER',
on: {
leftColumn,
operator: operator,
rightColumn,
},
});
return this;
}
leftJoin(table, leftColumn, operator, rightColumn) {
this.state.joins.push({
table,
type: 'LEFT',
on: {
leftColumn,
operator: operator,
rightColumn,
},
});
return this;
}
orderBy(column, direction = 'ASC') {
this.state.orderBy = [{ column, direction }];
return this;
}
groupBy(...columns) {
this.state.groupBy.push(...columns);
return this;
}
having(column, operator, value) {
this.state.having.push({
column,
operator: operator,
value,
logic: 'AND',
});
return this;
}
limit(limit) {
this.state.limit = limit;
return this;
}
offset(offset) {
this.state.offset = offset;
return this;
}
insert(data) {
this.reset(true);
this.state.type = 'INSERT';
this.state.columns = Object.keys(data);
this.state.values = Object.values(data);
return this;
}
insertMany(records) {
if (!records.length) {
throw new Error('No records provided for bulk insert');
}
this.reset(true);
this.state.type = 'INSERT';
this.state.columns = Object.keys(records[0]);
this.state.values = records
.map(record => {
const values = this.state.columns.map(column => record[column]);
if (values.length !== this.state.columns.length) {
throw new Error('All records must have the same columns');
}
return values;
})
.flat();
return this;
}
update(data) {
this.reset(true);
this.state.type = 'UPDATE';
this.state.updateValues = data;
return this;
}
delete() {
this.reset(true);
this.state.type = 'DELETE';
return this;
}
getState() {
return { ...this.state };
}
buildQuery() {
// Bu metod alt sınıflarda implement edilecek
throw new Error('buildQuery method must be implemented in derived class');
}
buildParameters() {
// Bu metod alt sınıflarda implement edilecek
throw new Error('buildParameters method must be implemented in derived class');
}
async execute() {
// Bu metod alt sınıflarda implement edilecek
throw new Error('execute method must be implemented in derived class');
}
}
exports.QueryBuilder = QueryBuilder;
//# sourceMappingURL=QueryBuilder.js.map