UNPKG

silvie

Version:

Typescript Back-end Framework

155 lines (154 loc) 5.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } class WhereConditionBuilder { constructor() { _defineProperty(this, "conditions", void 0); this.conditions = []; } baseWhere(type, relation, lhs, operator, rhs) { const condition = { type, relation }; if (lhs instanceof Function) { const conditionBuilder = new WhereConditionBuilder(); lhs(conditionBuilder); condition.type = 'group'; condition.conditions = conditionBuilder.conditions; } else { condition.leftHandSide = lhs; if (rhs === undefined) { if (operator === undefined) { if (!type.endsWith('null')) { throw new Error(`Invalid usage of ${type} where`); } } else { if (!['between', 'not between', 'in', 'not in', 'like', 'not like'].includes(type)) { condition.operator = '='; } condition.rightHandSide = operator; } } else { if (operator) condition.operator = operator; condition.rightHandSide = rhs; } } this.conditions.push(condition); return this; } where(column, operator, value) { return this.baseWhere('value', 'and', column, operator, value); } orWhere(column, operator, value) { return this.baseWhere('value', 'or', column, operator, value); } whereNull(column) { return this.baseWhere('null', 'and', column); } orWhereNull(column) { return this.baseWhere('null', 'or', column); } whereNotNull(column) { return this.baseWhere('not null', 'and', column); } orWhereNotNull(column) { return this.baseWhere('not null', 'or', column); } whereBetween(column, values) { return this.baseWhere('between', 'and', column, values); } orWhereBetween(column, values) { return this.baseWhere('between', 'and', column, values); } whereNotBetween(column, values) { return this.baseWhere('not between', 'and', column, values); } orWhereNotBetween(column, values) { return this.baseWhere('not between', 'and', column, values); } whereIn(column, values) { return this.baseWhere('in', 'and', column, values); } orWhereIn(column, values) { return this.baseWhere('in', 'or', column, values); } whereNotIn(column, values) { return this.baseWhere('not in', 'and', column, values); } orWhereNotIn(column, values) { return this.baseWhere('not in', 'or', column, values); } whereLike(column, value) { return this.baseWhere('like', 'and', column, value); } orWhereLike(column, value) { return this.baseWhere('like', 'or', column, value); } whereNotLike(column, value) { return this.baseWhere('not like', 'and', column, value); } orWhereNotLike(column, value) { return this.baseWhere('not like', 'or', column, value); } whereColumn(firstColumn, operator, secondColumn) { return this.baseWhere('column', 'and', firstColumn, operator, secondColumn); } orWhereColumn(firstColumn, operator, secondColumn) { return this.baseWhere('column', 'or', firstColumn, operator, secondColumn); } whereDate(column, operator, value) { return this.baseWhere('date', 'and', column, operator, value); } orWhereDate(column, operator, value) { return this.baseWhere('date', 'or', column, operator, value); } whereYear(column, operator, value) { return this.baseWhere('year', 'and', column, operator, value); } orWhereYear(column, operator, value) { return this.baseWhere('year', 'or', column, operator, value); } whereMonth(column, operator, value) { return this.baseWhere('month', 'and', column, operator, value); } orWhereMonth(column, operator, value) { return this.baseWhere('month', 'or', column, operator, value); } whereDay(column, operator, value) { return this.baseWhere('day', 'and', column, operator, value); } orWhereDay(column, operator, value) { return this.baseWhere('day', 'or', column, operator, value); } whereTime(column, operator, value) { return this.baseWhere('time', 'and', column, operator, value); } orWhereTime(column, operator, value) { return this.baseWhere('time', 'or', column, operator, value); } whereRaw(query, params) { this.conditions.push({ type: 'raw', relation: 'and', query, params: params || [] }); return this; } orWhereRaw(query, params) { this.conditions.push({ type: 'raw', relation: 'or', query, params: params || [] }); return this; } } exports.default = WhereConditionBuilder;