@devbro/sql-generator
Version:
generic sql generator
163 lines • 4.65 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Query_exports = {};
__export(Query_exports, {
Query: () => Query
});
module.exports = __toCommonJS(Query_exports);
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;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Query
});
//# sourceMappingURL=Query.js.map