@rnaga/wp-node 1.3.3 → 1.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/database.d.ts CHANGED
@@ -2,17 +2,15 @@ import { Knex } from "knex";
2
2
  import { Config } from "./config";
3
3
  export type DbConnection = Knex | undefined;
4
4
  export default class Database {
5
- #private;
6
5
  private config;
7
- static connections: Knex[];
8
6
  static perSiteTables: (keyof import("./types").Tables)[];
7
+ private connectionKey;
9
8
  constructor(config: Config);
10
9
  get prefix(): string;
11
10
  hasTable(tableName: string): Promise<boolean>;
12
11
  get connection(): Knex<any, any[]>;
13
12
  get transaction(): Promise<Knex.Transaction<any, any[]>>;
14
13
  get schema(): Knex.SchemaBuilder;
15
- closeConnection(): void;
16
14
  static closeAll(): void;
17
15
  }
18
16
  //# sourceMappingURL=database.d.ts.map
package/database.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../src/database.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAKlC,MAAM,MAAM,YAAY,GAAG,IAAI,GAAG,SAAS,CAAC;AAG5C,MAAM,CAAC,OAAO,OAAO,QAAQ;;IAKf,OAAO,CAAC,MAAM;IAJ1B,MAAM,CAAC,WAAW,EAAE,IAAI,EAAE,CAAM;IAChC,MAAM,CAAC,aAAa,qCAAgC;gBAGhC,MAAM,EAAE,MAAM;IAmBlC,IAAI,MAAM,WAET;IAEK,QAAQ,CAAC,SAAS,EAAE,MAAM;IAIhC,IAAI,UAAU,qBAEb;IAED,IAAI,WAAW,0CAEd;IAED,IAAI,MAAM,uBAET;IAED,eAAe;IAUf,MAAM,CAAC,QAAQ;CAOhB"}
1
+ {"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../src/database.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAG5B,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAIlC,MAAM,MAAM,YAAY,GAAG,IAAI,GAAG,SAAS,CAAC;AAY5C,MAAM,CAAC,OAAO,OAAO,QAAQ;IAIf,OAAO,CAAC,MAAM;IAH1B,MAAM,CAAC,aAAa,qCAAgC;IACpD,OAAO,CAAC,aAAa,CAAS;gBAEV,MAAM,EAAE,MAAM;IAsBlC,IAAI,MAAM,WAET;IAEK,QAAQ,CAAC,SAAS,EAAE,MAAM;IAIhC,IAAI,UAAU,qBAEb;IAED,IAAI,WAAW,0CAEd;IAED,IAAI,MAAM,uBAET;IAED,MAAM,CAAC,QAAQ;CAMhB"}
package/database.js CHANGED
@@ -11,36 +11,43 @@ var __metadata = (this && this.__metadata) || function (k, v) {
11
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
- var Database_1;
15
14
  Object.defineProperty(exports, "__esModule", { value: true });
16
- const constants_1 = require("./constants");
15
+ const crypto_1 = require("crypto");
16
+ const moment_1 = __importDefault(require("moment"));
17
17
  const config_1 = require("./config");
18
+ const constants_1 = require("./constants");
18
19
  const component_1 = require("./decorators/component");
19
- const constants_2 = require("./constants");
20
- const moment_1 = __importDefault(require("moment"));
20
+ // Multiple Knex instances keyed by connection configuration
21
+ const knexInstances = new Map();
22
+ const generateConnectionKey = (config) => {
23
+ const connection = config.connection;
24
+ const connectionString = `${connection.host}:${connection.port}:${connection.database}:${connection.user}`;
25
+ return (0, crypto_1.createHash)("sha256").update(connectionString).digest("hex");
26
+ };
21
27
  let Database = class Database {
22
- static { Database_1 = this; }
23
28
  config;
24
- static connections = [];
25
- static perSiteTables = constants_2.DEFAULT_DATABASE_TABLES.blog;
26
- #knex;
29
+ static perSiteTables = constants_1.DEFAULT_DATABASE_TABLES.blog;
30
+ connectionKey;
27
31
  constructor(config) {
28
32
  this.config = config;
29
- // eslint-disable-next-line @typescript-eslint/no-var-requires
30
- this.#knex = require("knex")({
31
- ...this.config.config.database,
32
- connection: {
33
- ...this.config.config.database.connection,
34
- timezone: "+00:00", // UTC
35
- typeCast: (field, next) => {
36
- if (field.type == "DATETIME") {
37
- return (0, moment_1.default)(field.string()).format("YYYY-MM-DD HH:mm:ss");
38
- }
39
- return next();
33
+ this.connectionKey = generateConnectionKey(this.config.config.database);
34
+ if (!knexInstances.has(this.connectionKey)) {
35
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
36
+ const knex = require("knex")({
37
+ ...this.config.config.database,
38
+ connection: {
39
+ ...this.config.config.database.connection,
40
+ timezone: "+00:00", // UTC
41
+ typeCast: (field, next) => {
42
+ if (field.type == "DATETIME") {
43
+ return (0, moment_1.default)(field.string()).format("YYYY-MM-DD HH:mm:ss");
44
+ }
45
+ return next();
46
+ },
40
47
  },
41
- },
42
- });
43
- Database_1.connections.push(this.#knex);
48
+ });
49
+ knexInstances.set(this.connectionKey, knex);
50
+ }
44
51
  }
45
52
  get prefix() {
46
53
  return this.config.config.tablePrefix;
@@ -49,32 +56,23 @@ let Database = class Database {
49
56
  return await this.connection.schema.hasTable(tableName);
50
57
  }
51
58
  get connection() {
52
- return this.#knex;
59
+ return knexInstances.get(this.connectionKey);
53
60
  }
54
61
  get transaction() {
55
- return this.#knex.transaction();
62
+ return knexInstances.get(this.connectionKey).transaction();
56
63
  }
57
64
  get schema() {
58
- return this.#knex.schema;
59
- }
60
- closeConnection() {
61
- for (let i = 0; i < Database_1.connections.length; i++) {
62
- if (Database_1.connections[i] === this.#knex) {
63
- Database_1.connections.splice(i, 1);
64
- break;
65
- }
66
- }
67
- this.#knex.destroy();
65
+ return knexInstances.get(this.connectionKey).schema;
68
66
  }
69
67
  static closeAll() {
70
- for (const conn of Database_1.connections) {
71
- conn.destroy();
68
+ for (const knex of knexInstances.values()) {
69
+ knex.destroy();
72
70
  }
73
- Database_1.connections = [];
71
+ knexInstances.clear();
74
72
  }
75
73
  };
76
- Database = Database_1 = __decorate([
77
- (0, component_1.component)({ scope: constants_1.Scope.Singleton }),
74
+ Database = __decorate([
75
+ (0, component_1.component)(),
78
76
  __metadata("design:paramtypes", [config_1.Config])
79
77
  ], Database);
80
78
  exports.default = Database;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rnaga/wp-node",
3
- "version": "1.3.3",
3
+ "version": "1.3.4",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "rm -rf ./dist && tsc --project tsconfig.build.json && npm run copyfiles && cp package.json ./dist/",