@asumano/worklip-typeorm-db 1.0.0

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/README.md ADDED
File without changes
@@ -0,0 +1,10 @@
1
+ export declare class Country {
2
+ id: number;
3
+ code: string;
4
+ name: string;
5
+ currency: string;
6
+ prefix: string;
7
+ structure_phone: string;
8
+ details: any | null;
9
+ status: number;
10
+ }
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Country = void 0;
13
+ const typeorm_1 = require("typeorm");
14
+ let Country = class Country {
15
+ };
16
+ exports.Country = Country;
17
+ __decorate([
18
+ (0, typeorm_1.PrimaryGeneratedColumn)({
19
+ type: "int",
20
+ comment: "Número de identificación (ID) único de cada registro.",
21
+ }),
22
+ __metadata("design:type", Number)
23
+ ], Country.prototype, "id", void 0);
24
+ __decorate([
25
+ (0, typeorm_1.Column)({ length: 10, type: "varchar", comment: "Código del país." }),
26
+ __metadata("design:type", String)
27
+ ], Country.prototype, "code", void 0);
28
+ __decorate([
29
+ (0, typeorm_1.Column)({ length: 50, type: "varchar", comment: "Nombre del país." }),
30
+ __metadata("design:type", String)
31
+ ], Country.prototype, "name", void 0);
32
+ __decorate([
33
+ (0, typeorm_1.Column)({
34
+ length: 10,
35
+ type: "varchar",
36
+ comment: "Usamos esta columna para saber la moneda del país y sugerírsela al usuario o al local.",
37
+ }),
38
+ __metadata("design:type", String)
39
+ ], Country.prototype, "currency", void 0);
40
+ __decorate([
41
+ (0, typeorm_1.Column)({
42
+ length: 10,
43
+ type: "varchar",
44
+ comment: "Prefijo para los números de teléfono.",
45
+ }),
46
+ __metadata("design:type", String)
47
+ ], Country.prototype, "prefix", void 0);
48
+ __decorate([
49
+ (0, typeorm_1.Column)({
50
+ type: "varchar",
51
+ length: 15,
52
+ comment: "Estructura que tiene el teléfono en ese país.",
53
+ }),
54
+ __metadata("design:type", String)
55
+ ], Country.prototype, "structure_phone", void 0);
56
+ __decorate([
57
+ (0, typeorm_1.Column)({
58
+ type: "json",
59
+ nullable: true,
60
+ default: null,
61
+ comment: "Columna de tipo JSON para almacenar información adicional sobre el país.",
62
+ }),
63
+ __metadata("design:type", Object)
64
+ ], Country.prototype, "details", void 0);
65
+ __decorate([
66
+ (0, typeorm_1.Column)({
67
+ default: 1,
68
+ type: "int",
69
+ width: 1,
70
+ comment: "Estado del registro, es decir:\r\n1. Activo: Es visible en la plataforma.\r\n0. Inactivo: No será visible en la plataforma.",
71
+ }),
72
+ __metadata("design:type", Number)
73
+ ], Country.prototype, "status", void 0);
74
+ exports.Country = Country = __decorate([
75
+ (0, typeorm_1.Entity)({ comment: "Países donde está disponible la plataforma." })
76
+ ], Country);
@@ -0,0 +1 @@
1
+ export { Country } from "./entities/Country";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Country = void 0;
4
+ var Country_1 = require("./entities/Country");
5
+ Object.defineProperty(exports, "Country", { enumerable: true, get: function () { return Country_1.Country; } });
@@ -0,0 +1,4 @@
1
+ import { DataSource, EntityTarget, ObjectLiteral, Repository } from "typeorm";
2
+ export declare function createDataBaseSource(): DataSource;
3
+ export declare function showEntityName<T extends ObjectLiteral>(entity: EntityTarget<T>): Promise<Repository<T>>;
4
+ export declare function callStoredProcedureName(storedProcedure: any, elements: any): Promise<any>;
package/dist/index.js ADDED
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.createDataBaseSource = createDataBaseSource;
37
+ exports.showEntityName = showEntityName;
38
+ exports.callStoredProcedureName = callStoredProcedureName;
39
+ const dotenv_1 = require("dotenv");
40
+ const path_1 = require("path");
41
+ const typeorm_1 = require("typeorm");
42
+ const EntitiesViewsRoutes = __importStar(require("./entities.index"));
43
+ let connection = null;
44
+ // DOC: Función para crear la conexión de la base de datos.
45
+ function createDataBaseSource() {
46
+ if (!connection) {
47
+ (0, dotenv_1.config)({ path: (0, path_1.resolve)(process.cwd(), ".env") });
48
+ connection = new typeorm_1.DataSource({
49
+ type: process.env.DB_TYPE,
50
+ // dateStrings: true,
51
+ dateStrings: ["DATE"],
52
+ host: process.env.DB_HOST,
53
+ port: parseInt(process.env.DB_PORT ?? ""),
54
+ username: process.env.DB_USERNAME,
55
+ password: process.env.DB_PASSWORD ?? "",
56
+ database: process.env.DB_DATABASE,
57
+ synchronize: process.env.DB_SYNCHRONIZE === "true",
58
+ timezone: "Z", // Configura la zona horaria a UTC
59
+ entities: Object.values(EntitiesViewsRoutes),
60
+ // entities: [
61
+ // __dirname +
62
+ // `/entities${
63
+ // process.env.DB_ENTITIES_ROUTE
64
+ // ? `/${process.env.DB_ENTITIES_ROUTE}`
65
+ // : ""
66
+ // }/*{.js,.ts}`,
67
+ // __dirname +
68
+ // `/views${
69
+ // process.env.DB_ENTITIES_ROUTE
70
+ // ? `/${process.env.DB_ENTITIES_ROUTE}`
71
+ // : ""
72
+ // }/*{.js,.ts}`,
73
+ // ],
74
+ subscribers: [],
75
+ migrations: [],
76
+ });
77
+ }
78
+ return connection;
79
+ }
80
+ async function showEntityName(entity) {
81
+ if (!connection || !connection.isInitialized) {
82
+ connection = createDataBaseSource();
83
+ await connection.initialize();
84
+ }
85
+ return connection.getRepository(entity);
86
+ }
87
+ async function callStoredProcedureName(storedProcedure, elements) {
88
+ try {
89
+ if (!connection) {
90
+ connection = createDataBaseSource();
91
+ await connection.initialize();
92
+ }
93
+ const result = await connection.query(`CALL ${storedProcedure}`, elements);
94
+ return result;
95
+ }
96
+ catch (error) {
97
+ throw error;
98
+ }
99
+ }
@@ -0,0 +1 @@
1
+ export type IType = "mysql" | "mariadb";
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/ormconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "type": "mysql",
3
+ "migrations": [
4
+ "src/migrations/*.ts"
5
+ ],
6
+ "cli": {
7
+ "migrationsDir": "src/migrations"
8
+ }
9
+ }
10
+
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@asumano/worklip-typeorm-db",
3
+ "version": "1.0.0",
4
+ "publishConfig": {
5
+ "access": "restricted"
6
+ },
7
+ "description": "Librería npm para la gestión de bases de datos utilizando TypeORM en el proyecto Worklip.",
8
+ "main": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "scripts": {
11
+ "build": "npx rimraf dist && tsc --build"
12
+ },
13
+ "keywords": [],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "type": "commonjs",
17
+ "dependencies": {
18
+ "crypto": "^1.0.1",
19
+ "dotenv": "^17.4.1",
20
+ "express": "^5.2.1",
21
+ "mysql2": "^3.21.1",
22
+ "typeorm": "^0.3.28"
23
+ },
24
+ "devDependencies": {
25
+ "@types/express": "^5.0.6",
26
+ "@types/node": "^25.5.2",
27
+ "rimraf": "^6.1.3",
28
+ "typescript": "^6.0.2"
29
+ }
30
+ }
@@ -0,0 +1,56 @@
1
+ import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from "typeorm";
2
+
3
+ @Entity({ comment: "Países donde está disponible la plataforma." })
4
+ export class Country {
5
+ @PrimaryGeneratedColumn({
6
+ type: "int",
7
+ comment: "Número de identificación (ID) único de cada registro.",
8
+ })
9
+ id: number;
10
+
11
+ @Column({ length: 10, type: "varchar", comment: "Código del país." })
12
+ code: string;
13
+
14
+ @Column({ length: 50, type: "varchar", comment: "Nombre del país." })
15
+ name: string;
16
+
17
+ @Column({
18
+ length: 10,
19
+ type: "varchar",
20
+ comment:
21
+ "Usamos esta columna para saber la moneda del país y sugerírsela al usuario o al local.",
22
+ })
23
+ currency: string;
24
+
25
+ @Column({
26
+ length: 10,
27
+ type: "varchar",
28
+ comment: "Prefijo para los números de teléfono.",
29
+ })
30
+ prefix: string;
31
+
32
+ @Column({
33
+ type: "varchar",
34
+ length: 15,
35
+ comment: "Estructura que tiene el teléfono en ese país.",
36
+ })
37
+ structure_phone: string;
38
+
39
+ @Column({
40
+ type: "json",
41
+ nullable: true,
42
+ default: null,
43
+ comment:
44
+ "Columna de tipo JSON para almacenar información adicional sobre el país.",
45
+ })
46
+ details: any | null;
47
+
48
+ @Column({
49
+ default: 1,
50
+ type: "int",
51
+ width: 1,
52
+ comment:
53
+ "Estado del registro, es decir:\r\n1. Activo: Es visible en la plataforma.\r\n0. Inactivo: No será visible en la plataforma.",
54
+ })
55
+ status: number;
56
+ }
@@ -0,0 +1 @@
1
+ export { Country } from "./entities/Country";
package/src/index.ts ADDED
@@ -0,0 +1,72 @@
1
+ import { config } from "dotenv";
2
+ import { resolve } from "path";
3
+ import { DataSource, EntityTarget, ObjectLiteral, Repository } from "typeorm";
4
+ import { IType } from "./types";
5
+ import * as EntitiesViewsRoutes from "./entities.index";
6
+
7
+ let connection: DataSource | null = null;
8
+
9
+ // DOC: Función para crear la conexión de la base de datos.
10
+ export function createDataBaseSource(): DataSource {
11
+ if (!connection) {
12
+ config({ path: resolve(process.cwd(), ".env") });
13
+
14
+ connection = new DataSource({
15
+ type: process.env.DB_TYPE as unknown as IType,
16
+ // dateStrings: true,
17
+ dateStrings: ["DATE"],
18
+ host: process.env.DB_HOST,
19
+ port: parseInt(process.env.DB_PORT ?? ""),
20
+ username: process.env.DB_USERNAME,
21
+ password: process.env.DB_PASSWORD ?? "",
22
+ database: process.env.DB_DATABASE,
23
+ synchronize: process.env.DB_SYNCHRONIZE === "true",
24
+ timezone: "Z", // Configura la zona horaria a UTC
25
+ entities: Object.values(EntitiesViewsRoutes),
26
+ // entities: [
27
+ // __dirname +
28
+ // `/entities${
29
+ // process.env.DB_ENTITIES_ROUTE
30
+ // ? `/${process.env.DB_ENTITIES_ROUTE}`
31
+ // : ""
32
+ // }/*{.js,.ts}`,
33
+ // __dirname +
34
+ // `/views${
35
+ // process.env.DB_ENTITIES_ROUTE
36
+ // ? `/${process.env.DB_ENTITIES_ROUTE}`
37
+ // : ""
38
+ // }/*{.js,.ts}`,
39
+ // ],
40
+ subscribers: [],
41
+ migrations: [],
42
+ });
43
+ }
44
+ return connection;
45
+ }
46
+
47
+ export async function showEntityName<T extends ObjectLiteral>(
48
+ entity: EntityTarget<T>,
49
+ ): Promise<Repository<T>> {
50
+ if (!connection || !connection.isInitialized) {
51
+ connection = createDataBaseSource();
52
+ await connection.initialize();
53
+ }
54
+ return connection.getRepository(entity);
55
+ }
56
+
57
+ export async function callStoredProcedureName(
58
+ storedProcedure: any,
59
+ elements: any,
60
+ ) {
61
+ try {
62
+ if (!connection) {
63
+ connection = createDataBaseSource();
64
+ await connection.initialize();
65
+ }
66
+
67
+ const result = await connection.query(`CALL ${storedProcedure}`, elements);
68
+ return result;
69
+ } catch (error) {
70
+ throw error;
71
+ }
72
+ }
package/src/types.ts ADDED
@@ -0,0 +1 @@
1
+ export type IType = "mysql" | "mariadb";
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "Node16",
5
+ "moduleResolution": "Node16",
6
+ "lib": ["es2020"],
7
+ "types": ["node"],
8
+ "rootDir": "./src",
9
+ "outDir": "./dist",
10
+ "declaration": true,
11
+ "strict": true,
12
+ "emitDecoratorMetadata": true,
13
+ "experimentalDecorators": true,
14
+ "strictPropertyInitialization": false,
15
+ "skipLibCheck": true,
16
+ "esModuleInterop": true,
17
+ "allowSyntheticDefaultImports": true
18
+ },
19
+ "include": ["src"]
20
+ }
@@ -0,0 +1 @@
1
+ {"root":["./src/entities.index.ts","./src/index.ts","./src/types.ts","./src/entities/country.ts"],"version":"6.0.2"}