@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 +1 -3
- package/database.d.ts.map +1 -1
- package/database.js +37 -39
- package/package.json +1 -1
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":"
|
|
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
|
|
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
|
-
|
|
20
|
-
const
|
|
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
|
|
25
|
-
|
|
26
|
-
#knex;
|
|
29
|
+
static perSiteTables = constants_1.DEFAULT_DATABASE_TABLES.blog;
|
|
30
|
+
connectionKey;
|
|
27
31
|
constructor(config) {
|
|
28
32
|
this.config = config;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
...this.config.config.database
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
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
|
|
59
|
+
return knexInstances.get(this.connectionKey);
|
|
53
60
|
}
|
|
54
61
|
get transaction() {
|
|
55
|
-
return this
|
|
62
|
+
return knexInstances.get(this.connectionKey).transaction();
|
|
56
63
|
}
|
|
57
64
|
get schema() {
|
|
58
|
-
return this
|
|
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
|
|
71
|
-
|
|
68
|
+
for (const knex of knexInstances.values()) {
|
|
69
|
+
knex.destroy();
|
|
72
70
|
}
|
|
73
|
-
|
|
71
|
+
knexInstances.clear();
|
|
74
72
|
}
|
|
75
73
|
};
|
|
76
|
-
Database =
|
|
77
|
-
(0, component_1.component)(
|
|
74
|
+
Database = __decorate([
|
|
75
|
+
(0, component_1.component)(),
|
|
78
76
|
__metadata("design:paramtypes", [config_1.Config])
|
|
79
77
|
], Database);
|
|
80
78
|
exports.default = Database;
|