UNPKG

mysql-rewrapped

Version:

A wrapper for MySQL on node to make querying a database as easy as possible

82 lines (81 loc) 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Delete_1 = require("./Queries/Delete"); var Insert_1 = require("./Queries/Insert"); var Update_1 = require("./Queries/Update"); var Select_1 = require("./Queries/Select"); var InvalidFieldError_1 = require("./Errors/InvalidFieldError"); var Table = /** @class */ (function () { function Table(name, fields) { this.name = name; this.fields = fields; } Table.prototype.Select = function (fields) { var _this = this; var missingFields = []; if (fields && fields !== ["*"]) { fields.forEach(function (field) { if (field instanceof Object) { field = field.name; } if (!_this.fields.includes(field) && !field.includes(".")) missingFields.push(field); else if (field.includes(".")) { var table = field.split(".")[0]; var attr = field.split(".")[1]; if (table !== _this.name) { if (module.exports[table] && !module.exports[table].fields.includes(attr)) { missingFields.push(field); } } else { if (!_this.fields.includes(attr)) missingFields.push(attr); } } }); } else { fields = ["*"]; } if (missingFields.length > 0) { throw new InvalidFieldError_1.default("Field(s) not found in table: " + missingFields); } else { return new Select_1.default(fields).table(this.name); } }; Table.prototype.Update = function (items) { var _this = this; var missingFields = []; Object.keys(items).forEach(function (field) { if (!_this.fields.includes(field)) missingFields.push(field); }); if (missingFields.length > 0) { throw new InvalidFieldError_1.default("Field(s) not found in table: " + missingFields); } else { return new Update_1.default(items).table(this.name); } }; Table.prototype.Insert = function (items) { var _this = this; var missingFields = []; Object.keys(items).forEach(function (field) { if (!_this.fields.includes(field)) missingFields.push(field); }); if (missingFields.length > 0) { throw new InvalidFieldError_1.default("Field(s) not found in table: " + missingFields); } else { return new Insert_1.default(items).table(this.name); } }; Table.prototype.Delete = function () { return new Delete_1.default().table(this.name); }; return Table; }()); exports.default = Table;