@javalabs/prisma-client 1.0.27 → 1.0.29

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.
Files changed (50) hide show
  1. package/.github/CODEOWNERS +1 -1
  2. package/README.md +269 -269
  3. package/migration-config.json +63 -63
  4. package/migration-config.json.bk +95 -95
  5. package/migrations/add_reserved_amount.sql +7 -7
  6. package/package.json +44 -44
  7. package/prisma/migrations/add_uuid_to_transactions.sql +13 -13
  8. package/prisma/schema.prisma +609 -601
  9. package/src/index.ts +23 -23
  10. package/src/prisma-factory.service.ts +40 -40
  11. package/src/prisma.module.ts +9 -9
  12. package/src/prisma.service.ts +16 -16
  13. package/src/scripts/add-uuid-to-table.ts +138 -138
  14. package/src/scripts/create-tenant-schemas.ts +145 -145
  15. package/src/scripts/data-migration/batch-migrator.ts +248 -248
  16. package/src/scripts/data-migration/data-transformer.ts +426 -426
  17. package/src/scripts/data-migration/db-connector.ts +120 -120
  18. package/src/scripts/data-migration/dependency-resolver.ts +174 -174
  19. package/src/scripts/data-migration/entity-discovery.ts +196 -196
  20. package/src/scripts/data-migration/foreign-key-manager.ts +277 -277
  21. package/src/scripts/data-migration/migration-config.json +63 -63
  22. package/src/scripts/data-migration/migration-tool.ts +509 -509
  23. package/src/scripts/data-migration/schema-utils.ts +248 -248
  24. package/src/scripts/data-migration/tenant-migrator.ts +201 -201
  25. package/src/scripts/data-migration/typecast-manager.ts +193 -193
  26. package/src/scripts/data-migration/types.ts +113 -113
  27. package/src/scripts/database-initializer.ts +49 -49
  28. package/src/scripts/drop-database.ts +104 -104
  29. package/src/scripts/dump-source-db.sh +61 -61
  30. package/src/scripts/encrypt-user-passwords.ts +36 -36
  31. package/src/scripts/error-handler.ts +117 -117
  32. package/src/scripts/fix-data-types.ts +241 -241
  33. package/src/scripts/fix-enum-values.ts +357 -357
  34. package/src/scripts/fix-schema-discrepancies.ts +317 -317
  35. package/src/scripts/fix-table-indexes.ts +601 -601
  36. package/src/scripts/migrate-schema-structure.ts +90 -90
  37. package/src/scripts/migrate-uuid.ts +76 -76
  38. package/src/scripts/post-migration-validator.ts +526 -526
  39. package/src/scripts/pre-migration-validator.ts +610 -610
  40. package/src/scripts/reset-database.ts +263 -263
  41. package/src/scripts/retry-failed-migrations.ts +416 -416
  42. package/src/scripts/run-migration.ts +707 -707
  43. package/src/scripts/schema-sync.ts +128 -128
  44. package/src/scripts/sequence-sync-cli.ts +416 -416
  45. package/src/scripts/sequence-synchronizer.ts +127 -127
  46. package/src/scripts/sync-enum-types.ts +170 -170
  47. package/src/scripts/sync-enum-values.ts +563 -563
  48. package/src/scripts/truncate-database.ts +123 -123
  49. package/src/scripts/verify-migration-setup.ts +135 -135
  50. package/tsconfig.json +17 -17
@@ -1,193 +1,193 @@
1
- import { PrismaClient } from "@prisma/client";
2
-
3
- export class TypecastManager {
4
- private readonly prisma = new PrismaClient();
5
- private readonly cache = new Map<string, Map<string, string>>(); // cache tabla -> columna -> typecast
6
-
7
- private readonly compatibilityMap: Record<string, string[]> = {
8
- integer: ["bigint", "smallint", "numeric", "decimal"],
9
- bigint: ["integer", "numeric", "decimal"],
10
- smallint: ["integer", "bigint", "numeric", "decimal"],
11
- numeric: [
12
- "integer",
13
- "bigint",
14
- "smallint",
15
- "decimal",
16
- "real",
17
- "double precision",
18
- ],
19
- decimal: [
20
- "numeric",
21
- "integer",
22
- "bigint",
23
- "smallint",
24
- "real",
25
- "double precision",
26
- ],
27
- real: ["numeric", "decimal", "double precision"],
28
- "double precision": ["numeric", "decimal", "real"],
29
- "character varying": ["text", "char", "varchar"],
30
- varchar: ["text", "character varying", "char"],
31
- char: ["text", "character varying", "varchar"],
32
- text: ["character varying", "varchar", "char"],
33
- timestamp: [
34
- "timestamptz",
35
- "timestamp without time zone",
36
- "timestamp with time zone",
37
- ],
38
- timestamptz: [
39
- "timestamp",
40
- "timestamp without time zone",
41
- "timestamp with time zone",
42
- ],
43
- date: ["timestamp", "timestamptz"],
44
- boolean: ["bool"],
45
- json: ["jsonb"],
46
- jsonb: ["json"],
47
- };
48
-
49
- async getTypecastForColumn(
50
- tableName: string,
51
- columnName: string
52
- ): Promise<string | undefined> {
53
- if (!this.cache.has(tableName)) {
54
- await this.loadTableSchema(tableName);
55
- }
56
- const tableSchema = this.cache.get(tableName);
57
- return tableSchema?.get(columnName);
58
- }
59
-
60
- private async loadTableSchema(tableName: string) {
61
- const columns = await this.prisma.$queryRawUnsafe<any[]>(`
62
- SELECT column_name, data_type, udt_name, is_nullable
63
- FROM information_schema.columns
64
- WHERE table_name = '${tableName}'
65
- `);
66
-
67
- const tableSchema = new Map<string, string>();
68
-
69
- for (const column of columns) {
70
- const { column_name, data_type, udt_name, is_nullable } = column;
71
-
72
- let typecast: string | undefined;
73
-
74
- switch (data_type.toLowerCase()) {
75
- case "timestamp with time zone":
76
- case "timestamp without time zone":
77
- case "timestamptz":
78
- typecast = "::timestamp with time zone";
79
- break;
80
- case "timestamp":
81
- typecast = "::timestamp";
82
- break;
83
- case "integer":
84
- typecast = "::integer";
85
- break;
86
- case "bigint":
87
- typecast = "::bigint";
88
- break;
89
- case "boolean":
90
- typecast = "::boolean";
91
- break;
92
- case "numeric":
93
- case "decimal":
94
- typecast = "::numeric";
95
- break;
96
- case "double precision":
97
- typecast = "::double precision";
98
- break;
99
- case "text":
100
- case "character varying":
101
- case "varchar":
102
- typecast = "::text";
103
- break;
104
- case "date":
105
- typecast = "::date";
106
- break;
107
- case "time":
108
- case "time without time zone":
109
- typecast = "::time";
110
- break;
111
- case "time with time zone":
112
- typecast = "::timetz";
113
- break;
114
- case "json":
115
- typecast = "::json";
116
- break;
117
- case "jsonb":
118
- typecast = "::jsonb";
119
- break;
120
- case "uuid":
121
- typecast = "::uuid";
122
- break;
123
- default:
124
- if (udt_name.startsWith("_")) {
125
- // Arrays
126
- typecast = `::${udt_name.substring(1)}[]`;
127
- } else if (udt_name.startsWith("enum_")) {
128
- // Enums
129
- typecast = `::${udt_name}`;
130
- } else {
131
- typecast = undefined;
132
- }
133
- }
134
-
135
- tableSchema.set(column_name, typecast);
136
- }
137
-
138
- this.cache.set(tableName, tableSchema);
139
- }
140
-
141
- areTypesCompatible(sourceType: string, targetType: string): boolean {
142
- // Si los tipos son iguales, son compatibles
143
- if (sourceType.toLowerCase() === targetType.toLowerCase()) {
144
- return true;
145
- }
146
-
147
- // Verificar si el tipo fuente puede convertirse al tipo destino
148
- const compatibleTypes =
149
- this.compatibilityMap[sourceType.toLowerCase()] || [];
150
- return compatibleTypes.includes(targetType.toLowerCase());
151
- }
152
-
153
- castValue(value: any, sourceType: string, targetType: string): any {
154
- if (value === null || value === undefined) {
155
- return null;
156
- }
157
-
158
- try {
159
- switch (targetType.toLowerCase()) {
160
- case "integer":
161
- case "bigint":
162
- case "smallint":
163
- return parseInt(value, 10);
164
- case "numeric":
165
- case "decimal":
166
- case "real":
167
- case "double precision":
168
- return parseFloat(value);
169
- case "boolean":
170
- return Boolean(value);
171
- case "json":
172
- case "jsonb":
173
- return typeof value === "string" ? JSON.parse(value) : value;
174
- case "timestamp":
175
- case "timestamptz":
176
- return new Date(value).toISOString();
177
- case "date":
178
- return new Date(value).toISOString().split("T")[0];
179
- case "text":
180
- case "character varying":
181
- case "varchar":
182
- case "char":
183
- return String(value);
184
- default:
185
- return value;
186
- }
187
- } catch (error) {
188
- throw new Error(
189
- `Error al convertir valor "${value}" de ${sourceType} a ${targetType}: ${error.message}`
190
- );
191
- }
192
- }
193
- }
1
+ import { PrismaClient } from "@prisma/client";
2
+
3
+ export class TypecastManager {
4
+ private readonly prisma = new PrismaClient();
5
+ private readonly cache = new Map<string, Map<string, string>>(); // cache tabla -> columna -> typecast
6
+
7
+ private readonly compatibilityMap: Record<string, string[]> = {
8
+ integer: ["bigint", "smallint", "numeric", "decimal"],
9
+ bigint: ["integer", "numeric", "decimal"],
10
+ smallint: ["integer", "bigint", "numeric", "decimal"],
11
+ numeric: [
12
+ "integer",
13
+ "bigint",
14
+ "smallint",
15
+ "decimal",
16
+ "real",
17
+ "double precision",
18
+ ],
19
+ decimal: [
20
+ "numeric",
21
+ "integer",
22
+ "bigint",
23
+ "smallint",
24
+ "real",
25
+ "double precision",
26
+ ],
27
+ real: ["numeric", "decimal", "double precision"],
28
+ "double precision": ["numeric", "decimal", "real"],
29
+ "character varying": ["text", "char", "varchar"],
30
+ varchar: ["text", "character varying", "char"],
31
+ char: ["text", "character varying", "varchar"],
32
+ text: ["character varying", "varchar", "char"],
33
+ timestamp: [
34
+ "timestamptz",
35
+ "timestamp without time zone",
36
+ "timestamp with time zone",
37
+ ],
38
+ timestamptz: [
39
+ "timestamp",
40
+ "timestamp without time zone",
41
+ "timestamp with time zone",
42
+ ],
43
+ date: ["timestamp", "timestamptz"],
44
+ boolean: ["bool"],
45
+ json: ["jsonb"],
46
+ jsonb: ["json"],
47
+ };
48
+
49
+ async getTypecastForColumn(
50
+ tableName: string,
51
+ columnName: string
52
+ ): Promise<string | undefined> {
53
+ if (!this.cache.has(tableName)) {
54
+ await this.loadTableSchema(tableName);
55
+ }
56
+ const tableSchema = this.cache.get(tableName);
57
+ return tableSchema?.get(columnName);
58
+ }
59
+
60
+ private async loadTableSchema(tableName: string) {
61
+ const columns = await this.prisma.$queryRawUnsafe<any[]>(`
62
+ SELECT column_name, data_type, udt_name, is_nullable
63
+ FROM information_schema.columns
64
+ WHERE table_name = '${tableName}'
65
+ `);
66
+
67
+ const tableSchema = new Map<string, string>();
68
+
69
+ for (const column of columns) {
70
+ const { column_name, data_type, udt_name, is_nullable } = column;
71
+
72
+ let typecast: string | undefined;
73
+
74
+ switch (data_type.toLowerCase()) {
75
+ case "timestamp with time zone":
76
+ case "timestamp without time zone":
77
+ case "timestamptz":
78
+ typecast = "::timestamp with time zone";
79
+ break;
80
+ case "timestamp":
81
+ typecast = "::timestamp";
82
+ break;
83
+ case "integer":
84
+ typecast = "::integer";
85
+ break;
86
+ case "bigint":
87
+ typecast = "::bigint";
88
+ break;
89
+ case "boolean":
90
+ typecast = "::boolean";
91
+ break;
92
+ case "numeric":
93
+ case "decimal":
94
+ typecast = "::numeric";
95
+ break;
96
+ case "double precision":
97
+ typecast = "::double precision";
98
+ break;
99
+ case "text":
100
+ case "character varying":
101
+ case "varchar":
102
+ typecast = "::text";
103
+ break;
104
+ case "date":
105
+ typecast = "::date";
106
+ break;
107
+ case "time":
108
+ case "time without time zone":
109
+ typecast = "::time";
110
+ break;
111
+ case "time with time zone":
112
+ typecast = "::timetz";
113
+ break;
114
+ case "json":
115
+ typecast = "::json";
116
+ break;
117
+ case "jsonb":
118
+ typecast = "::jsonb";
119
+ break;
120
+ case "uuid":
121
+ typecast = "::uuid";
122
+ break;
123
+ default:
124
+ if (udt_name.startsWith("_")) {
125
+ // Arrays
126
+ typecast = `::${udt_name.substring(1)}[]`;
127
+ } else if (udt_name.startsWith("enum_")) {
128
+ // Enums
129
+ typecast = `::${udt_name}`;
130
+ } else {
131
+ typecast = undefined;
132
+ }
133
+ }
134
+
135
+ tableSchema.set(column_name, typecast);
136
+ }
137
+
138
+ this.cache.set(tableName, tableSchema);
139
+ }
140
+
141
+ areTypesCompatible(sourceType: string, targetType: string): boolean {
142
+ // Si los tipos son iguales, son compatibles
143
+ if (sourceType.toLowerCase() === targetType.toLowerCase()) {
144
+ return true;
145
+ }
146
+
147
+ // Verificar si el tipo fuente puede convertirse al tipo destino
148
+ const compatibleTypes =
149
+ this.compatibilityMap[sourceType.toLowerCase()] || [];
150
+ return compatibleTypes.includes(targetType.toLowerCase());
151
+ }
152
+
153
+ castValue(value: any, sourceType: string, targetType: string): any {
154
+ if (value === null || value === undefined) {
155
+ return null;
156
+ }
157
+
158
+ try {
159
+ switch (targetType.toLowerCase()) {
160
+ case "integer":
161
+ case "bigint":
162
+ case "smallint":
163
+ return parseInt(value, 10);
164
+ case "numeric":
165
+ case "decimal":
166
+ case "real":
167
+ case "double precision":
168
+ return parseFloat(value);
169
+ case "boolean":
170
+ return Boolean(value);
171
+ case "json":
172
+ case "jsonb":
173
+ return typeof value === "string" ? JSON.parse(value) : value;
174
+ case "timestamp":
175
+ case "timestamptz":
176
+ return new Date(value).toISOString();
177
+ case "date":
178
+ return new Date(value).toISOString().split("T")[0];
179
+ case "text":
180
+ case "character varying":
181
+ case "varchar":
182
+ case "char":
183
+ return String(value);
184
+ default:
185
+ return value;
186
+ }
187
+ } catch (error) {
188
+ throw new Error(
189
+ `Error al convertir valor "${value}" de ${sourceType} a ${targetType}: ${error.message}`
190
+ );
191
+ }
192
+ }
193
+ }
@@ -1,113 +1,113 @@
1
- import { Pool } from "pg";
2
- import { PrismaClient } from "@prisma/client";
3
-
4
- export interface DatabaseConnection {
5
- pool?: Pool;
6
- prisma?: PrismaClient;
7
- query: (sql: string, params?: any[]) => Promise<any>;
8
- sourceId: string; // Identificador único para la fuente (ej: "colombia", "rd", "guatemala")
9
- }
10
-
11
- export interface DatabaseConnections {
12
- sourceConnections?: DatabaseConnection[];
13
- sourcePool?: Pool;
14
- targetPool: Pool;
15
- targetPrisma: PrismaClient;
16
- sourcePrisma?: PrismaClient;
17
- }
18
-
19
- export interface ColumnSchema {
20
- table_name: string;
21
- column_name: string;
22
- data_type: string;
23
- udt_name: string;
24
- character_maximum_length?: number;
25
- is_nullable: string;
26
- column_default?: string;
27
- constraint_type?: string;
28
- }
29
-
30
- // Updated EntityType to include filter configuration
31
- export interface EntityType {
32
- name: string; // Table name
33
- idField: string; // Primary key column name
34
- filterColumn?: string; // Column used for filtering (either directly or via join)
35
- filterVia?: string; // Intermediate table for filtering join (if needed)
36
- // Keep optional legacy fields for potential backward compatibility or specific use cases
37
- foreignKey?: string;
38
- joinTable?: string;
39
- joinField?: string;
40
- joinForeignKey?: string;
41
- directQuery?: string;
42
- }
43
-
44
- export interface EnumCastValue {
45
- needsEnumCast: boolean;
46
- value: any;
47
- enumType: string;
48
- }
49
-
50
- export interface MigrationOptions {
51
- publicOnly?: boolean;
52
- multiTenant?: boolean;
53
- sourceSchema?: string;
54
- targetSchema?: string;
55
- forceSingleTenant?: boolean;
56
- configPath?: string;
57
- batchSize?: number;
58
- retryAttempts?: number;
59
- validateData?: boolean;
60
- logLevel?: "debug" | "info" | "warn" | "error";
61
- }
62
-
63
- export interface ColumnConfig {
64
- sourceColumn?: string;
65
- targetColumn?: string;
66
- type?: string;
67
- nullable?: boolean;
68
- defaultValue?: any;
69
- transform?: (value: any) => any;
70
- }
71
-
72
- export interface TableConfig {
73
- type: "public" | "filteredPublic" | "tenantInfo" | "tenant";
74
- idField: string;
75
- filterColumn?: string;
76
- via?: string;
77
- providerLink?: string;
78
- tenantKey?: string;
79
- dependencies?: string[];
80
- sourceTable?: string;
81
- targetTable?: string;
82
- columns?: Record<string, ColumnConfig>;
83
- skipIfExists?: boolean;
84
- batchSize?: number;
85
- }
86
-
87
- export interface ExtendedTableConfig extends TableConfig {
88
- processed?: boolean;
89
- retryCount?: number;
90
- lastError?: Error;
91
- }
92
-
93
- export interface ExtendedMigrationConfig {
94
- tables: Record<string, ExtendedTableConfig>;
95
- version: string;
96
- sourceSchema?: string;
97
- targetSchema?: string;
98
- }
99
-
100
- export interface MigrationConfig {
101
- commonSchema: string;
102
- tables: Record<string, TableConfig>;
103
- tenantInfo: {
104
- sourceTable: string;
105
- tenantIdColumn: string;
106
- providerIdColumn: string;
107
- };
108
- migrationPriorities?: {
109
- high?: string[];
110
- medium?: string[];
111
- low?: string[];
112
- };
113
- }
1
+ import { Pool } from "pg";
2
+ import { PrismaClient } from "@prisma/client";
3
+
4
+ export interface DatabaseConnection {
5
+ pool?: Pool;
6
+ prisma?: PrismaClient;
7
+ query: (sql: string, params?: any[]) => Promise<any>;
8
+ sourceId: string; // Identificador único para la fuente (ej: "colombia", "rd", "guatemala")
9
+ }
10
+
11
+ export interface DatabaseConnections {
12
+ sourceConnections?: DatabaseConnection[];
13
+ sourcePool?: Pool;
14
+ targetPool: Pool;
15
+ targetPrisma: PrismaClient;
16
+ sourcePrisma?: PrismaClient;
17
+ }
18
+
19
+ export interface ColumnSchema {
20
+ table_name: string;
21
+ column_name: string;
22
+ data_type: string;
23
+ udt_name: string;
24
+ character_maximum_length?: number;
25
+ is_nullable: string;
26
+ column_default?: string;
27
+ constraint_type?: string;
28
+ }
29
+
30
+ // Updated EntityType to include filter configuration
31
+ export interface EntityType {
32
+ name: string; // Table name
33
+ idField: string; // Primary key column name
34
+ filterColumn?: string; // Column used for filtering (either directly or via join)
35
+ filterVia?: string; // Intermediate table for filtering join (if needed)
36
+ // Keep optional legacy fields for potential backward compatibility or specific use cases
37
+ foreignKey?: string;
38
+ joinTable?: string;
39
+ joinField?: string;
40
+ joinForeignKey?: string;
41
+ directQuery?: string;
42
+ }
43
+
44
+ export interface EnumCastValue {
45
+ needsEnumCast: boolean;
46
+ value: any;
47
+ enumType: string;
48
+ }
49
+
50
+ export interface MigrationOptions {
51
+ publicOnly?: boolean;
52
+ multiTenant?: boolean;
53
+ sourceSchema?: string;
54
+ targetSchema?: string;
55
+ forceSingleTenant?: boolean;
56
+ configPath?: string;
57
+ batchSize?: number;
58
+ retryAttempts?: number;
59
+ validateData?: boolean;
60
+ logLevel?: "debug" | "info" | "warn" | "error";
61
+ }
62
+
63
+ export interface ColumnConfig {
64
+ sourceColumn?: string;
65
+ targetColumn?: string;
66
+ type?: string;
67
+ nullable?: boolean;
68
+ defaultValue?: any;
69
+ transform?: (value: any) => any;
70
+ }
71
+
72
+ export interface TableConfig {
73
+ type: "public" | "filteredPublic" | "tenantInfo" | "tenant";
74
+ idField: string;
75
+ filterColumn?: string;
76
+ via?: string;
77
+ providerLink?: string;
78
+ tenantKey?: string;
79
+ dependencies?: string[];
80
+ sourceTable?: string;
81
+ targetTable?: string;
82
+ columns?: Record<string, ColumnConfig>;
83
+ skipIfExists?: boolean;
84
+ batchSize?: number;
85
+ }
86
+
87
+ export interface ExtendedTableConfig extends TableConfig {
88
+ processed?: boolean;
89
+ retryCount?: number;
90
+ lastError?: Error;
91
+ }
92
+
93
+ export interface ExtendedMigrationConfig {
94
+ tables: Record<string, ExtendedTableConfig>;
95
+ version: string;
96
+ sourceSchema?: string;
97
+ targetSchema?: string;
98
+ }
99
+
100
+ export interface MigrationConfig {
101
+ commonSchema: string;
102
+ tables: Record<string, TableConfig>;
103
+ tenantInfo: {
104
+ sourceTable: string;
105
+ tenantIdColumn: string;
106
+ providerIdColumn: string;
107
+ };
108
+ migrationPriorities?: {
109
+ high?: string[];
110
+ medium?: string[];
111
+ low?: string[];
112
+ };
113
+ }