UNPKG

@htdangkhoa/google-ads

Version:
73 lines (72 loc) 2.32 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.QueryBuilder = void 0; const deepmerge_1 = __importDefault(require("deepmerge")); const types_js_1 = require("./types.js"); class QueryBuilder { constructor() { this._attributes = []; this._table = ''; this._conditions = []; this._orders = []; this._limit = 0; this._parameters = {}; } select(...args) { this._attributes.push(...args); return this; } from(table) { this._table = table; return this; } where(...args) { this._conditions.push(...args); return this; } orderBy(...args) { this._orders.push(...args); return this; } limit(limit) { this._limit = limit; return this; } parameters(parameters) { this._parameters = (deepmerge_1.default.all([this._parameters, parameters])); return this; } build() { const query = []; if (this._attributes.length) { query.push(types_js_1.QUERY.SELECT, this._attributes.join(', ')); } if (this._table) { query.push(types_js_1.QUERY.FROM, this._table); } if (this._conditions.length) { query.push(types_js_1.QUERY.WHERE, this._conditions .map((condition) => `${condition.attribute} ${condition.operator} ${condition.value}`) .join(' AND ')); } if (this._orders.length) { query.push(types_js_1.QUERY.ORDER_BY, this._orders .map((order) => `${order.attribute} ${order.direction || types_js_1.OrderDirection.ASC}`) .join(', ')); } if (this._limit) { query.push(types_js_1.QUERY.LIMIT, this._limit); } const parameterKeys = Object.keys(this._parameters); if (parameterKeys.length) { query.push(types_js_1.QUERY.PARAMETERS, parameterKeys .map((key) => `${key} = ${this._parameters[key]}`) .join(', ')); } return query.join(' '); } } exports.QueryBuilder = QueryBuilder;