@devbro/sql-generator
Version:
generic sql generator
139 lines • 3.65 kB
JavaScript
class Query {
constructor(connection, grammar) {
this.connection = connection;
this.grammar = grammar;
}
allowedOperations = ["=", ">", "<", "!=", "like", "ilike"];
parts = {
select: ["*"],
table: "",
join: [],
where: [],
groupBy: [],
having: [],
orderBy: [],
limit: null,
offset: null
};
table(tableName) {
this.parts.table = tableName;
return this;
}
whereOp(column, operation, value, joinCondition = "and", negateCondition = false) {
this.parts.where.push({
type: "operation",
column,
operation,
value,
joinCondition,
negateCondition
});
return this;
}
whereColumn(column1, operation, column2, joinCondition = "and", negateCondition = false) {
this.parts.where.push({
type: "operationColumn",
column1,
operation,
column2,
joinCondition,
negateCondition
});
return this;
}
whereNull(column, joinCondition = "and", negateCondition = false) {
this.parts.where.push({ type: "null", column, joinCondition, negateCondition });
return this;
}
clearWhere() {
this.parts.where = [];
return this;
}
select(selects) {
this.parts.select = [...selects];
return this;
}
groupBy(columns) {
this.parts.groupBy = [...columns];
return this;
}
havingOp(column, operation, value, joinCondition = "and", negateCondition = false) {
this.parts.having.push({
type: "operation",
column,
operation,
value,
joinCondition,
negateCondition
});
return this;
}
havingRaw(sql, bindings, joinCondition = "and", negateCondition = false) {
this.parts.having.push({ type: "raw", sql, bindings, joinCondition, negateCondition });
return this;
}
orderBy(column, direction = "asc") {
this.parts.orderBy.push(`${column} ${direction}`);
return this;
}
limit(limit) {
this.parts.limit = limit;
return this;
}
offset(offset) {
this.parts.offset = offset;
return this;
}
toSql() {
return this.grammar.toSql(this);
}
async get() {
return await this.connection?.runQuery(this.toSql());
}
async getCursor() {
return await this.connection?.runCursor(this.toSql());
}
getConnection() {
return this.connection;
}
async insert(data) {
const csql = this.grammar.compileInsert(this, data);
return await this.connection?.runQuery(csql);
}
async insertGetId(data, options = { primaryKey: ["id"] }) {
const csql = this.grammar.compileInsertGetId(this, data, options);
return await this.connection?.runQuery(csql);
}
async update(data) {
const csql = this.grammar.compileUpdate(this, data);
return await this.connection?.runQuery(csql);
}
async upsert(data, uniqueColumns, updateColumns) {
const csql = this.grammar.compileUpsert(this, data, uniqueColumns, updateColumns);
return await this.connection?.runQuery(csql);
}
async delete() {
const csql = this.grammar.compileDelete(this);
return await this.connection?.runQuery(csql);
}
innerJoin(table, condtions) {
this.parts.join.push({ type: "inner", table, conditions: condtions });
return this;
}
leftJoin(table, condtions) {
this.parts.join.push({ type: "left", table, conditions: condtions });
return this;
}
rightJoin(table, condtions) {
this.parts.join.push({ type: "right", table, conditions: condtions });
return this;
}
fullJoin(table, condtions) {
this.parts.join.push({ type: "full", table, conditions: condtions });
return this;
}
}
export {
Query
};
//# sourceMappingURL=Query.mjs.map