UNPKG

@smallprod/models

Version:
127 lines (126 loc) 4.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Having = exports.Join = void 0; const dbmanager_1 = __importDefault(require("../../dbs/dbmanager")); const where_query_1 = __importDefault(require("./where.query")); class FindQuery extends where_query_1.default { constructor(tableName, afterExec, dbName) { super(tableName, dbName); this.attributes = []; this.sorts = []; this.isDistinct = false; this.lim = -1; this.offset = -1; this.tableAlias = 'default_table'; this.joins = []; this.groups = []; this.havings = null; this.where = (column, operator, value) => { this.wheres.push({ column, value, operator }); return this; }; this.limit = (limit, offset = 0) => { this.lim = limit; this.offset = offset; return this; }; this.join = (table, alias) => { const join = new Join(table, alias, this); this.joins.push(join); return join; }; this.distinct = () => { this.isDistinct = true; return this; }; this.alias = (alias) => { this.tableAlias = alias; return this; }; this.addAttribute = (attr, alias = '', func = null) => { this.attributes.push({ alias, attribute: attr, function: func }); return this; }; this.addAttributes = (attr) => { this.attributes = this.attributes.concat(attr.map((a) => ({ attribute: a, alias: '', function: null }))); return this; }; this.sort = (attr, method = 'ASC') => { this.sorts.push({ attribute: attr, mode: method }); return this; }; this.groupBy = (column) => { this.groups.push(column); return this; }; this.having = (column, operator, value) => { const having = new Having(this); this.havings = having; return having.having(column, operator, value); }; this.exec = async (dbName = null) => { let dbConnName = dbName; if (!dbConnName && this.dbName) dbConnName = this.dbName; const db = dbmanager_1.default.getInstance().get(dbConnName); if (!db) throw Error('Database not found'); const res = await db.select(this.tableName, this.isDistinct, this.attributes, this.wheres, this.sorts, this.tableAlias, this.lim, this.offset, this.joins.map((j) => j.getInterface()), this.groups, this.havings ? this.havings.getWheres() : []); if (this.afterExec) { return this.afterExec(res); } return res; }; this.afterExec = afterExec; this.dbName = dbName; } } exports.default = FindQuery; class Join extends where_query_1.default { constructor(tableName, alias, query) { super(tableName); this.method = 'inner'; this.on = (column, operator, value) => { this.wheres.push({ column, value, operator }); return this; }; this.endJoin = () => { return this.query; }; this.left = () => { this.method = 'left'; return this; }; this.right = () => { this.method = 'right'; return this; }; this.getInterface = () => ({ alias: this.alias, tableName: this.tableName, method: this.method, wheres: this.wheres, }); this.alias = alias; this.query = query; } } exports.Join = Join; class Having extends where_query_1.default { constructor(query) { super(''); this.having = (column, operator, value) => { this.wheres.push({ column, value, operator }); return this; }; this.endHaving = () => { return this.query; }; this.getWheres = () => this.wheres; this.query = query; } } exports.Having = Having;