test-entity-library-asm 2.8.12 → 2.8.14

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