UNPKG

@rnaga/wp-node

Version:

👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**

79 lines (78 loc) • 3.36 kB
"use strict"; 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 }; }; Object.defineProperty(exports, "__esModule", { value: true }); const crypto_1 = require("crypto"); const moment_1 = __importDefault(require("moment")); const config_1 = require("./config"); const constants_1 = require("./constants"); const component_1 = require("./decorators/component"); // Multiple Knex instances keyed by connection configuration const knexInstances = new Map(); const generateConnectionKey = (config) => { const connection = config.connection; const connectionString = `${connection.host}:${connection.port}:${connection.database}:${connection.user}`; return (0, crypto_1.createHash)("sha256").update(connectionString).digest("hex"); }; let Database = class Database { config; static perSiteTables = constants_1.DEFAULT_DATABASE_TABLES.blog; connectionKey; constructor(config) { this.config = config; this.connectionKey = generateConnectionKey(this.config.config.database); if (!knexInstances.has(this.connectionKey)) { // eslint-disable-next-line @typescript-eslint/no-var-requires const 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(); }, }, }); knexInstances.set(this.connectionKey, knex); } } get prefix() { return this.config.config.tablePrefix; } async hasTable(tableName) { return await this.connection.schema.hasTable(tableName); } get connection() { return knexInstances.get(this.connectionKey); } get transaction() { return knexInstances.get(this.connectionKey).transaction(); } get schema() { return knexInstances.get(this.connectionKey).schema; } static closeAll() { for (const knex of knexInstances.values()) { knex.destroy(); } knexInstances.clear(); } }; Database = __decorate([ (0, component_1.component)(), __metadata("design:paramtypes", [config_1.Config]) ], Database); exports.default = Database;