epic-sql
Version:
A Simple But Powerful SQL ORM!!!
55 lines (54 loc) • 2.19 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Connection = void 0;
const promise_1 = require("mysql2/promise");
const exceptions_1 = require("./exceptions");
class Connection {
constructor(type, options, logs = false) {
this.Configuration = {
type,
options,
logs,
};
}
connect() {
return __awaiter(this, void 0, void 0, function* () {
if (this.Configuration.type === "mysql")
return (yield promise_1.createConnection(Object.assign({ multipleStatements: true }, this.Configuration.options)));
else
throw new exceptions_1.ConnectionException(`Invalid connection type '${this.Configuration.type}' has been provided!`);
});
}
get() {
return __awaiter(this, void 0, void 0, function* () {
if (this.Connection)
return this.Connection;
else
return (this.Connection = yield this.connect());
});
}
end() {
if (this.Connection) {
// End Current Connection
this.Connection.end().catch(() => { });
// Destroy Connection
this.Connection.destroy();
// Delete Connection
delete this.Connection;
}
return this;
}
getConfig() {
return this.Configuration;
}
}
exports.Connection = Connection;
;