@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
81 lines (80 loc) • 3.11 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var Database_1;
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("./constants");
const config_1 = require("./config");
const component_1 = require("./decorators/component");
const constants_2 = require("./constants");
const moment_1 = __importDefault(require("moment"));
let Database = class Database {
static { Database_1 = this; }
config;
static connections = [];
static perSiteTables = constants_2.DEFAULT_DATABASE_TABLES.blog;
#knex;
constructor(config) {
this.config = config;
// eslint-disable-next-line @typescript-eslint/no-var-requires
this.#knex = require("knex")({
...this.config.config.database,
connection: {
...this.config.config.database.connection,
timezone: "+00:00", // UTC
typeCast: (field, next) => {
if (field.type == "DATETIME") {
return (0, moment_1.default)(field.string()).format("YYYY-MM-DD HH:mm:ss");
}
return next();
},
},
});
Database_1.connections.push(this.#knex);
}
get prefix() {
return this.config.config.tablePrefix;
}
async hasTable(tableName) {
return await this.connection.schema.hasTable(tableName);
}
get connection() {
return this.#knex;
}
get transaction() {
return this.#knex.transaction();
}
get schema() {
return this.#knex.schema;
}
closeConnection() {
for (let i = 0; i < Database_1.connections.length; i++) {
if (Database_1.connections[i] === this.#knex) {
Database_1.connections.splice(i, 1);
break;
}
}
this.#knex.destroy();
}
static closeAll() {
for (const conn of Database_1.connections) {
conn.destroy();
}
Database_1.connections = [];
}
};
Database = Database_1 = __decorate([
(0, component_1.component)({ scope: constants_1.Scope.Singleton }),
__metadata("design:paramtypes", [config_1.Config])
], Database);
exports.default = Database;