test-entity-library-asm 2.8.12 → 2.8.13
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/dist/index.d.ts +4 -4
- package/dist/index.js +17 -14
- package/package.json +1 -1
- package/src/index.ts +51 -49
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { NextFunction, Request, Response } from
|
|
2
|
-
import { DataSource, EntityTarget, ObjectLiteral, Repository } from
|
|
3
|
-
import { CustomRepository } from
|
|
4
|
-
export * from
|
|
1
|
+
import { NextFunction, Request, Response } from "express";
|
|
2
|
+
import { DataSource, EntityTarget, ObjectLiteral, Repository } from "typeorm";
|
|
3
|
+
import { CustomRepository } from ".";
|
|
4
|
+
export * from "./entities.views.routes";
|
|
5
5
|
export declare function createDataBaseSource(): DataSource;
|
|
6
6
|
export declare function showEntityNameEntity<T extends ObjectLiteral>(entity: EntityTarget<T>): Promise<Repository<T>>;
|
|
7
7
|
export declare function showEntity(entityName: string): Promise<any>;
|
package/dist/index.js
CHANGED
|
@@ -57,6 +57,7 @@ var path_1 = require("path");
|
|
|
57
57
|
var typeorm_1 = require("typeorm");
|
|
58
58
|
var _1 = require(".");
|
|
59
59
|
var moment = require("moment-timezone");
|
|
60
|
+
var entities_views_routes_1 = require("./entities.views.routes");
|
|
60
61
|
__exportStar(require("./entities.views.routes"), exports);
|
|
61
62
|
var asyncLocalStorage = new async_hooks_1.AsyncLocalStorage();
|
|
62
63
|
var connection = null;
|
|
@@ -65,25 +66,26 @@ var connection = null;
|
|
|
65
66
|
function createDataBaseSource() {
|
|
66
67
|
var _a, _b;
|
|
67
68
|
if (!connection) {
|
|
68
|
-
(0, dotenv_1.config)({ path: (0, path_1.resolve)(process.cwd(),
|
|
69
|
+
(0, dotenv_1.config)({ path: (0, path_1.resolve)(process.cwd(), ".env") });
|
|
69
70
|
connection = new typeorm_1.DataSource({
|
|
70
71
|
type: process.env.DB_TYPE,
|
|
71
72
|
host: process.env.DB_HOST,
|
|
72
|
-
port: parseInt((_a = process.env.DB_PORT) !== null && _a !== void 0 ? _a :
|
|
73
|
+
port: parseInt((_a = process.env.DB_PORT) !== null && _a !== void 0 ? _a : ""),
|
|
73
74
|
username: process.env.DB_USERNAME,
|
|
74
|
-
password: (_b = process.env.DB_PASSWORD) !== null && _b !== void 0 ? _b :
|
|
75
|
+
password: (_b = process.env.DB_PASSWORD) !== null && _b !== void 0 ? _b : "",
|
|
75
76
|
database: process.env.DB_DATABASE,
|
|
76
|
-
synchronize: process.env.DB_SYNCHRONIZE ===
|
|
77
|
-
timezone:
|
|
77
|
+
synchronize: process.env.DB_SYNCHRONIZE === "true",
|
|
78
|
+
timezone: "Z",
|
|
78
79
|
entities: [
|
|
79
80
|
__dirname +
|
|
80
81
|
"/entities".concat(process.env.DB_ENTITIES_ROUTE
|
|
81
82
|
? "/".concat(process.env.DB_ENTITIES_ROUTE)
|
|
82
|
-
:
|
|
83
|
+
: "", "/*{.js,.ts}"),
|
|
83
84
|
__dirname +
|
|
84
85
|
"/views".concat(process.env.DB_ENTITIES_ROUTE
|
|
85
86
|
? "/".concat(process.env.DB_ENTITIES_ROUTE)
|
|
86
|
-
:
|
|
87
|
+
: "", "/*{.js,.ts}"),
|
|
88
|
+
entities_views_routes_1.PartnerNotifications,
|
|
87
89
|
],
|
|
88
90
|
subscribers: [],
|
|
89
91
|
migrations: [],
|
|
@@ -99,7 +101,8 @@ function showEntityNameEntity(entity) {
|
|
|
99
101
|
return __generator(this, function (_a) {
|
|
100
102
|
switch (_a.label) {
|
|
101
103
|
case 0:
|
|
102
|
-
if (
|
|
104
|
+
if (!(!connection || !connection.isInitialized)) return [3 /*break*/, 2];
|
|
105
|
+
console.log("Esperando inicialización del DataSource...");
|
|
103
106
|
connection = createDataBaseSource();
|
|
104
107
|
return [4 /*yield*/, connection.initialize()];
|
|
105
108
|
case 1:
|
|
@@ -147,7 +150,7 @@ function callStoredProcedure(storedProcedure, elements) {
|
|
|
147
150
|
return [2 /*return*/, result];
|
|
148
151
|
case 4:
|
|
149
152
|
error_1 = _a.sent();
|
|
150
|
-
console.error(
|
|
153
|
+
console.error("Error calling stored procedure:", error_1);
|
|
151
154
|
throw error_1;
|
|
152
155
|
case 5: return [2 /*return*/];
|
|
153
156
|
}
|
|
@@ -156,12 +159,12 @@ function callStoredProcedure(storedProcedure, elements) {
|
|
|
156
159
|
}
|
|
157
160
|
exports.callStoredProcedure = callStoredProcedure;
|
|
158
161
|
function timezoneMiddleware(req, res, next) {
|
|
159
|
-
var userTimeZone = req.headers[
|
|
162
|
+
var userTimeZone = req.headers["user-timezone"];
|
|
160
163
|
if (Array.isArray(userTimeZone)) {
|
|
161
164
|
userTimeZone = userTimeZone[0];
|
|
162
165
|
}
|
|
163
|
-
userTimeZone = userTimeZone ||
|
|
164
|
-
asyncLocalStorage.run(new Map([[
|
|
166
|
+
userTimeZone = userTimeZone || "UTC";
|
|
167
|
+
asyncLocalStorage.run(new Map([["timezone", userTimeZone]]), function () {
|
|
165
168
|
next();
|
|
166
169
|
});
|
|
167
170
|
}
|
|
@@ -169,12 +172,12 @@ exports.timezoneMiddleware = timezoneMiddleware;
|
|
|
169
172
|
function getTimeZone() {
|
|
170
173
|
var _a;
|
|
171
174
|
var store = asyncLocalStorage.getStore();
|
|
172
|
-
return (_a = store === null || store === void 0 ? void 0 : store.get(
|
|
175
|
+
return (_a = store === null || store === void 0 ? void 0 : store.get("timezone")) !== null && _a !== void 0 ? _a : "UTC";
|
|
173
176
|
}
|
|
174
177
|
exports.getTimeZone = getTimeZone;
|
|
175
178
|
function getTimezoneOffset(timezone) {
|
|
176
179
|
var now = moment.tz(timezone);
|
|
177
|
-
var offset = now.format(
|
|
180
|
+
var offset = now.format("Z");
|
|
178
181
|
return offset;
|
|
179
182
|
}
|
|
180
183
|
exports.getTimezoneOffset = getTimezoneOffset;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,50 +1,52 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from
|
|
2
|
-
import { config } from
|
|
3
|
-
import { NextFunction, Request, Response } from
|
|
4
|
-
import { resolve } from
|
|
5
|
-
import { DataSource, EntityTarget, ObjectLiteral, Repository } from
|
|
6
|
-
import { IType } from
|
|
7
|
-
import { CustomRepository } from
|
|
8
|
-
import moment = require(
|
|
9
|
-
|
|
1
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
2
|
+
import { config } from "dotenv";
|
|
3
|
+
import { NextFunction, Request, Response } from "express";
|
|
4
|
+
import { resolve } from "path";
|
|
5
|
+
import { DataSource, EntityTarget, ObjectLiteral, Repository } from "typeorm";
|
|
6
|
+
import { IType } from "./interfaces";
|
|
7
|
+
import { CustomRepository } from ".";
|
|
8
|
+
import moment = require("moment-timezone");
|
|
9
|
+
import { PartnerNotifications } from "./entities.views.routes";
|
|
10
|
+
export * from "./entities.views.routes";
|
|
10
11
|
|
|
11
|
-
const asyncLocalStorage = new AsyncLocalStorage<Map<string, string>>()
|
|
12
|
+
const asyncLocalStorage = new AsyncLocalStorage<Map<string, string>>();
|
|
12
13
|
|
|
13
|
-
let connection: DataSource | null = null
|
|
14
|
+
let connection: DataSource | null = null;
|
|
14
15
|
|
|
15
16
|
// DOCUMENTATION: Función para crear la conexión de la base de datos
|
|
16
17
|
// AUTHOR: jgomezp97@gmail.com
|
|
17
18
|
export function createDataBaseSource(): DataSource {
|
|
18
19
|
if (!connection) {
|
|
19
|
-
config({ path: resolve(process.cwd(),
|
|
20
|
+
config({ path: resolve(process.cwd(), ".env") });
|
|
20
21
|
connection = new DataSource({
|
|
21
22
|
type: process.env.DB_TYPE as unknown as IType,
|
|
22
23
|
host: process.env.DB_HOST,
|
|
23
|
-
port: parseInt(process.env.DB_PORT ??
|
|
24
|
+
port: parseInt(process.env.DB_PORT ?? ""),
|
|
24
25
|
username: process.env.DB_USERNAME,
|
|
25
|
-
password: process.env.DB_PASSWORD ??
|
|
26
|
+
password: process.env.DB_PASSWORD ?? "",
|
|
26
27
|
database: process.env.DB_DATABASE,
|
|
27
|
-
synchronize: process.env.DB_SYNCHRONIZE ===
|
|
28
|
-
timezone:
|
|
28
|
+
synchronize: process.env.DB_SYNCHRONIZE === "true",
|
|
29
|
+
timezone: "Z",
|
|
29
30
|
entities: [
|
|
30
31
|
__dirname +
|
|
31
32
|
`/entities${
|
|
32
33
|
process.env.DB_ENTITIES_ROUTE
|
|
33
34
|
? `/${process.env.DB_ENTITIES_ROUTE}`
|
|
34
|
-
:
|
|
35
|
+
: ""
|
|
35
36
|
}/*{.js,.ts}`,
|
|
36
37
|
__dirname +
|
|
37
38
|
`/views${
|
|
38
39
|
process.env.DB_ENTITIES_ROUTE
|
|
39
40
|
? `/${process.env.DB_ENTITIES_ROUTE}`
|
|
40
|
-
:
|
|
41
|
+
: ""
|
|
41
42
|
}/*{.js,.ts}`,
|
|
43
|
+
PartnerNotifications,
|
|
42
44
|
],
|
|
43
45
|
subscribers: [],
|
|
44
46
|
migrations: [],
|
|
45
|
-
})
|
|
47
|
+
});
|
|
46
48
|
}
|
|
47
|
-
return connection
|
|
49
|
+
return connection;
|
|
48
50
|
}
|
|
49
51
|
|
|
50
52
|
// DOCUMENTATION: Función para retornar la entidad de una base de datos en específico por medio de un DataSource
|
|
@@ -52,35 +54,35 @@ export function createDataBaseSource(): DataSource {
|
|
|
52
54
|
export async function showEntityNameEntity<T extends ObjectLiteral>(
|
|
53
55
|
entity: EntityTarget<T>
|
|
54
56
|
): Promise<Repository<T>> {
|
|
55
|
-
if (!connection) {
|
|
56
|
-
|
|
57
|
-
|
|
57
|
+
if (!connection || !connection.isInitialized) {
|
|
58
|
+
console.log("Esperando inicialización del DataSource...");
|
|
59
|
+
connection = createDataBaseSource();
|
|
60
|
+
await connection.initialize();
|
|
58
61
|
}
|
|
59
|
-
|
|
60
|
-
return connection.getRepository(entity)
|
|
62
|
+
return connection.getRepository(entity);
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
export async function showEntity(entityName: string): Promise<any> {
|
|
64
66
|
if (!connection) {
|
|
65
|
-
connection = createDataBaseSource()
|
|
66
|
-
await connection.initialize()
|
|
67
|
+
connection = createDataBaseSource();
|
|
68
|
+
await connection.initialize();
|
|
67
69
|
}
|
|
68
70
|
|
|
69
|
-
return connection.getRepository(entityName)
|
|
71
|
+
return connection.getRepository(entityName);
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
export async function callStoredProcedure(storedProcedure: any, elements: any) {
|
|
73
75
|
try {
|
|
74
76
|
if (!connection) {
|
|
75
|
-
connection = createDataBaseSource()
|
|
76
|
-
await connection.initialize()
|
|
77
|
+
connection = createDataBaseSource();
|
|
78
|
+
await connection.initialize();
|
|
77
79
|
}
|
|
78
80
|
|
|
79
|
-
const result = await connection.query(`CALL ${storedProcedure}`, elements)
|
|
80
|
-
return result
|
|
81
|
+
const result = await connection.query(`CALL ${storedProcedure}`, elements);
|
|
82
|
+
return result;
|
|
81
83
|
} catch (error) {
|
|
82
|
-
console.error(
|
|
83
|
-
throw error
|
|
84
|
+
console.error("Error calling stored procedure:", error);
|
|
85
|
+
throw error;
|
|
84
86
|
}
|
|
85
87
|
}
|
|
86
88
|
|
|
@@ -89,38 +91,38 @@ export function timezoneMiddleware(
|
|
|
89
91
|
res: Response,
|
|
90
92
|
next: NextFunction
|
|
91
93
|
) {
|
|
92
|
-
let userTimeZone = req.headers[
|
|
94
|
+
let userTimeZone = req.headers["user-timezone"];
|
|
93
95
|
|
|
94
96
|
if (Array.isArray(userTimeZone)) {
|
|
95
|
-
userTimeZone = userTimeZone[0]
|
|
97
|
+
userTimeZone = userTimeZone[0];
|
|
96
98
|
}
|
|
97
99
|
|
|
98
|
-
userTimeZone = userTimeZone ||
|
|
100
|
+
userTimeZone = userTimeZone || "UTC";
|
|
99
101
|
|
|
100
|
-
asyncLocalStorage.run(new Map([[
|
|
101
|
-
next()
|
|
102
|
-
})
|
|
102
|
+
asyncLocalStorage.run(new Map([["timezone", userTimeZone]]), () => {
|
|
103
|
+
next();
|
|
104
|
+
});
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
export function getTimeZone(): string {
|
|
106
|
-
const store = asyncLocalStorage.getStore()
|
|
107
|
-
return store?.get(
|
|
108
|
+
const store = asyncLocalStorage.getStore();
|
|
109
|
+
return store?.get("timezone") ?? "UTC";
|
|
108
110
|
}
|
|
109
111
|
|
|
110
112
|
export function getTimezoneOffset(timezone: string) {
|
|
111
|
-
const now = moment.tz(timezone)
|
|
112
|
-
const offset = now.format(
|
|
113
|
-
return offset
|
|
113
|
+
const now = moment.tz(timezone);
|
|
114
|
+
const offset = now.format("Z");
|
|
115
|
+
return offset;
|
|
114
116
|
}
|
|
115
117
|
|
|
116
118
|
export const getRepositoryByEntity = async <T extends ObjectLiteral>(
|
|
117
119
|
entity: EntityTarget<T>
|
|
118
120
|
): Promise<CustomRepository<T>> => {
|
|
119
121
|
if (!connection) {
|
|
120
|
-
connection = createDataBaseSource()
|
|
121
|
-
await connection.initialize()
|
|
122
|
+
connection = createDataBaseSource();
|
|
123
|
+
await connection.initialize();
|
|
122
124
|
}
|
|
123
125
|
return connection
|
|
124
126
|
.getRepository(entity)
|
|
125
|
-
.extend(CustomRepository.prototype) as CustomRepository<T
|
|
126
|
-
}
|
|
127
|
+
.extend(CustomRepository.prototype) as CustomRepository<T>;
|
|
128
|
+
};
|