mysql-rewrapped
Version:
A wrapper for MySQL on node to make querying a database as easy as possible
101 lines (100 loc) • 3.72 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Query_1 = require("../Query");
var InvalidJoinTypeError_1 = require("../Errors/InvalidJoinTypeError");
var Select = /** @class */ (function (_super) {
__extends(Select, _super);
function Select(items) {
var _this = _super.call(this, "SELECT") || this;
_this.joinStatement = "";
_this.isDistinct = false;
_this.groupItems = "";
_this.orderItems = "";
_this.orderAsc = false;
var itemsParsed = [];
var itemsJoined = "";
if (!items) {
itemsParsed.push("*");
}
else {
for (var i = 0; i < items.length; i++) {
itemsParsed.push(items[i]);
if (typeof items[i] === "object") {
itemsParsed.push(items[i].name + " AS " + items[i].as);
}
}
itemsJoined = items.join(", ");
}
_this.items = itemsJoined;
return _this;
}
Select.prototype.distinct = function () {
this.isDistinct = true;
return this;
};
/**
*
* @param params join formatted as [{target: target table, from: field on main table, to: field on joining table, type: type of join}]
* @returns {Select}
*/
Select.prototype.join = function (params) {
var jointypes = ["INNER", "FULL OUTER", "LEFT", "RIGHT"];
var statement = "";
params.forEach(function (item) {
if (item.type) {
if (jointypes.includes(item.type.toUpperCase())) {
statement += item.type.toUpperCase();
}
else {
throw new InvalidJoinTypeError_1.default(item.type + " Is not a valid type of join");
}
}
else {
statement += "INNER";
}
statement += " JOIN " + item.target + " ON " + item.from + " = " + item.to + " ";
});
this.joinStatement = statement;
return this;
};
Select.prototype.groupBy = function (items) {
this.groupItems = items.join(", ");
return this;
};
Select.prototype.orderBy = function (items, asc) {
this.orderItems = items.join(", ");
this.orderAsc = asc;
return this;
};
Select.prototype.toString = function () {
return (this.type +
(this.isDistinct ? " DISTINCT " : "") +
" " +
this.items +
(this.tableName ? " FROM " + this.tableName : "") +
" " +
(this.joinStatement ? this.joinStatement + " " : "") +
(this.whereStatement ? this.whereStatement : "") +
(this.groupItems ? " GROUP BY " + this.groupItems : "") +
(this.orderItems ? " ORDER BY " + this.orderItems + (this.orderAsc ? " DESC" : " ASC") : ""));
};
Select.prototype.exec = function (cback) {
this.queryParams = this.whereParams;
_super.prototype.exec.call(this, cback);
};
return Select;
}(Query_1.default));
exports.default = Select;