UNPKG

pg-query-config

Version:
143 lines (142 loc) 4.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.QueryConfig = void 0; const types_1 = require("./types"); const conditions_1 = require("./conditions"); const utils_1 = require("./utils"); const wrappers_1 = require("./wrappers"); class QueryConfig { constructor({ schema, table, columns = ['*'], limit, offset }) { this.schema = schema; this.table = table; this.sqlCommand = 'SELECT'; this.columnSet = new Set(columns); this.joinSet = new Set(); this.whereSet = new Set(); this.orderSet = new Set(); this.limit = limit; this.offset = offset; this.valueRefSet = new Set(); } select(column) { if (typeof column === 'object') { this.columnSet = new Set(column); } else if (typeof column === 'string') { this.columnSet = new Set([column]); } return this; } join(table, condition) { this.joinSet.add(`JOIN ${table} ON ${condition}`); return this; } leftJoin(table, condition) { this.joinSet.add(`LEFT JOIN ${table} ON ${condition}`); return this; } rightJoin(table, condition) { this.joinSet.add(`RIGHT JOIN ${table} ON ${condition}`); return this; } where(whereCondition) { const conditions = this.buildWhereConditions(whereCondition); for (const condition of conditions) { this.whereSet.add(condition); } return this; } orWhere(whereConditions) { const orWhereSets = new Set(whereConditions.map((condition) => new Set(this.buildWhereConditions(condition)))); const orWhereSql = [...orWhereSets].map((set) => [...set].join(' AND ')).join(' OR '); this.whereSet.add(`(${orWhereSql})`); return this; } order(orderCondition) { const orderConditions = this.buildOrderConditions(orderCondition); for (const condition of orderConditions) { this.orderSet.add(condition); } return this; } buildWhereConditions(whereCondition) { const whereConditions = []; const whereConditionMap = conditions_1.createCondition(whereCondition); for (const [column, value] of whereConditionMap) { const col = wrappers_1.wrap(column); if (typeof value === 'function') { const fnValue = value(this.valueRefSet); if (types_1.isNullableCondition(value)) { if (fnValue) { whereConditions.push(`(${col} ${fnValue} OR ${col} ${value.nullableCondition})`); } else { whereConditions.push(`${col} ${value.nullableCondition}`); } } else { whereConditions.push(`${col} ${fnValue}`); } } else if (value === null) { whereConditions.push(`${col} IS NULL`); } else { whereConditions.push(`${col} = $${utils_1.addValueToReferenceSet(value, this.valueRefSet)}`); } } return whereConditions; } get text() { return [ this.sqlCommand, this.sqlColumns, this.sqlFrom, this.sqlJoin, this.sqlWhere, this.sqlOrder, this.sqlLimit, this.sqlOffset, ] .filter((sql) => sql) .join(' '); } get values() { return [...this.valueRefSet]; } buildOrderConditions(orderCondition) { const orderConditions = []; for (const column in orderCondition) { const value = orderCondition[column]; if (typeof value === 'object') { this.buildOrderConditions(value); } else { orderConditions.push(`${wrappers_1.wrap(column)} ${value}`); } } return orderConditions; } get sqlColumns() { return [...this.columnSet].map(wrappers_1.wrap).join(','); } get sqlFrom() { return this.schema ? `FROM ${this.schema}.${this.table}` : `FROM ${this.table}`; } get sqlJoin() { return this.joinSet.size ? [...this.joinSet].join(' ') : undefined; } get sqlWhere() { return this.whereSet.size ? `WHERE ${[...this.whereSet].join(' AND ')}` : undefined; } get sqlOrder() { return this.orderSet.size ? `ORDER BY ${[...this.orderSet].join(',')}` : undefined; } get sqlLimit() { return this.limit ? `LIMIT ${this.limit}` : undefined; } get sqlOffset() { return this.offset ? `OFFSET ${this.offset}` : undefined; } } exports.QueryConfig = QueryConfig;