@javalabs/prisma-client 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 +220 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/dist/prisma-factory.service.d.ts +9 -0
- package/dist/prisma-factory.service.js +47 -0
- package/dist/prisma-factory.service.js.map +1 -0
- package/dist/prisma.module.d.ts +2 -0
- package/dist/prisma.module.js +23 -0
- package/dist/prisma.module.js.map +1 -0
- package/dist/prisma.service.d.ts +6 -0
- package/dist/prisma.service.js +27 -0
- package/dist/prisma.service.js.map +1 -0
- package/dist/scripts/create-tenant-schemas.d.ts +1 -0
- package/dist/scripts/create-tenant-schemas.js +117 -0
- package/dist/scripts/create-tenant-schemas.js.map +1 -0
- package/dist/scripts/data-migration/batch-migrator.d.ts +25 -0
- package/dist/scripts/data-migration/batch-migrator.js +333 -0
- package/dist/scripts/data-migration/batch-migrator.js.map +1 -0
- package/dist/scripts/data-migration/data-transformer.d.ts +17 -0
- package/dist/scripts/data-migration/data-transformer.js +242 -0
- package/dist/scripts/data-migration/data-transformer.js.map +1 -0
- package/dist/scripts/data-migration/db-connector.d.ts +7 -0
- package/dist/scripts/data-migration/db-connector.js +58 -0
- package/dist/scripts/data-migration/db-connector.js.map +1 -0
- package/dist/scripts/data-migration/dependency-manager.d.ts +9 -0
- package/dist/scripts/data-migration/dependency-manager.js +86 -0
- package/dist/scripts/data-migration/dependency-manager.js.map +1 -0
- package/dist/scripts/data-migration/dependency-resolver.d.ts +18 -0
- package/dist/scripts/data-migration/dependency-resolver.js +251 -0
- package/dist/scripts/data-migration/dependency-resolver.js.map +1 -0
- package/dist/scripts/data-migration/entity-discovery.d.ts +11 -0
- package/dist/scripts/data-migration/entity-discovery.js +152 -0
- package/dist/scripts/data-migration/entity-discovery.js.map +1 -0
- package/dist/scripts/data-migration/foreign-key-manager.d.ts +17 -0
- package/dist/scripts/data-migration/foreign-key-manager.js +70 -0
- package/dist/scripts/data-migration/foreign-key-manager.js.map +1 -0
- package/dist/scripts/data-migration/migration-phases.d.ts +5 -0
- package/dist/scripts/data-migration/migration-phases.js +55 -0
- package/dist/scripts/data-migration/migration-phases.js.map +1 -0
- package/dist/scripts/data-migration/migration-tool.d.ts +29 -0
- package/dist/scripts/data-migration/migration-tool.js +250 -0
- package/dist/scripts/data-migration/migration-tool.js.map +1 -0
- package/dist/scripts/data-migration/phase-generator.d.ts +15 -0
- package/dist/scripts/data-migration/phase-generator.js +187 -0
- package/dist/scripts/data-migration/phase-generator.js.map +1 -0
- package/dist/scripts/data-migration/schema-utils.d.ts +18 -0
- package/dist/scripts/data-migration/schema-utils.js +164 -0
- package/dist/scripts/data-migration/schema-utils.js.map +1 -0
- package/dist/scripts/data-migration/tenant-migrator.d.ts +15 -0
- package/dist/scripts/data-migration/tenant-migrator.js +110 -0
- package/dist/scripts/data-migration/tenant-migrator.js.map +1 -0
- package/dist/scripts/data-migration/typecast-manager.d.ts +5 -0
- package/dist/scripts/data-migration/typecast-manager.js +35 -0
- package/dist/scripts/data-migration/typecast-manager.js.map +1 -0
- package/dist/scripts/data-migration/types.d.ts +34 -0
- package/dist/scripts/data-migration/types.js +3 -0
- package/dist/scripts/data-migration/types.js.map +1 -0
- package/dist/scripts/data-migration.d.ts +22 -0
- package/dist/scripts/data-migration.js +593 -0
- package/dist/scripts/data-migration.js.map +1 -0
- package/dist/scripts/drop-database.d.ts +10 -0
- package/dist/scripts/drop-database.js +81 -0
- package/dist/scripts/drop-database.js.map +1 -0
- package/dist/scripts/error-handler.d.ts +12 -0
- package/dist/scripts/error-handler.js +82 -0
- package/dist/scripts/error-handler.js.map +1 -0
- package/dist/scripts/fix-data-types.d.ts +10 -0
- package/dist/scripts/fix-data-types.js +185 -0
- package/dist/scripts/fix-data-types.js.map +1 -0
- package/dist/scripts/fix-enum-values.d.ts +17 -0
- package/dist/scripts/fix-enum-values.js +234 -0
- package/dist/scripts/fix-enum-values.js.map +1 -0
- package/dist/scripts/fix-schema-discrepancies.d.ts +21 -0
- package/dist/scripts/fix-schema-discrepancies.js +240 -0
- package/dist/scripts/fix-schema-discrepancies.js.map +1 -0
- package/dist/scripts/migrate-schema-structure.d.ts +1 -0
- package/dist/scripts/migrate-schema-structure.js +76 -0
- package/dist/scripts/migrate-schema-structure.js.map +1 -0
- package/dist/scripts/post-migration-validator.d.ts +21 -0
- package/dist/scripts/post-migration-validator.js +341 -0
- package/dist/scripts/post-migration-validator.js.map +1 -0
- package/dist/scripts/pre-migration-validator.d.ts +25 -0
- package/dist/scripts/pre-migration-validator.js +491 -0
- package/dist/scripts/pre-migration-validator.js.map +1 -0
- package/dist/scripts/reset-database.d.ts +17 -0
- package/dist/scripts/reset-database.js +202 -0
- package/dist/scripts/reset-database.js.map +1 -0
- package/dist/scripts/retry-failed-migrations.d.ts +14 -0
- package/dist/scripts/retry-failed-migrations.js +301 -0
- package/dist/scripts/retry-failed-migrations.js.map +1 -0
- package/dist/scripts/run-migration.d.ts +1 -0
- package/dist/scripts/run-migration.js +525 -0
- package/dist/scripts/run-migration.js.map +1 -0
- package/dist/scripts/schema-sync.d.ts +1 -0
- package/dist/scripts/schema-sync.js +85 -0
- package/dist/scripts/schema-sync.js.map +1 -0
- package/dist/scripts/sync-enum-types.d.ts +13 -0
- package/dist/scripts/sync-enum-types.js +139 -0
- package/dist/scripts/sync-enum-types.js.map +1 -0
- package/dist/scripts/sync-enum-values.d.ts +20 -0
- package/dist/scripts/sync-enum-values.js +336 -0
- package/dist/scripts/sync-enum-values.js.map +1 -0
- package/dist/scripts/truncate-database.d.ts +10 -0
- package/dist/scripts/truncate-database.js +100 -0
- package/dist/scripts/truncate-database.js.map +1 -0
- package/dist/scripts/verify-migration-setup.d.ts +11 -0
- package/dist/scripts/verify-migration-setup.js +120 -0
- package/dist/scripts/verify-migration-setup.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/migration-config-public.json +95 -0
- package/migration-config.json +95 -0
- package/package.json +33 -0
- package/prisma/migrations/migration_lock.toml +3 -0
- package/prisma/schema.prisma +360 -0
- package/src/index.ts +23 -0
- package/src/prisma-factory.service.ts +41 -0
- package/src/prisma.module.ts +10 -0
- package/src/prisma.service.ts +17 -0
- package/src/scripts/create-tenant-schemas.ts +146 -0
- package/src/scripts/data-migration/batch-migrator.ts +569 -0
- package/src/scripts/data-migration/data-transformer.ts +377 -0
- package/src/scripts/data-migration/db-connector.ts +67 -0
- package/src/scripts/data-migration/dependency-resolver.ts +319 -0
- package/src/scripts/data-migration/entity-discovery.ts +197 -0
- package/src/scripts/data-migration/foreign-key-manager.ts +95 -0
- package/src/scripts/data-migration/migration-tool.ts +357 -0
- package/src/scripts/data-migration/schema-utils.ts +186 -0
- package/src/scripts/data-migration/tenant-migrator.ts +194 -0
- package/src/scripts/data-migration/typecast-manager.ts +38 -0
- package/src/scripts/data-migration/types.ts +40 -0
- package/src/scripts/drop-database.ts +105 -0
- package/src/scripts/dump-source-db.sh +62 -0
- package/src/scripts/dumps/source_dump_20250413_112626.sql +1527 -0
- package/src/scripts/error-handler.ts +118 -0
- package/src/scripts/fix-data-types.ts +242 -0
- package/src/scripts/fix-enum-values.ts +357 -0
- package/src/scripts/fix-schema-discrepancies.ts +318 -0
- package/src/scripts/migrate-schema-structure.ts +90 -0
- package/src/scripts/post-migration-validator.ts +427 -0
- package/src/scripts/pre-migration-validator.ts +611 -0
- package/src/scripts/reset-database.ts +264 -0
- package/src/scripts/retry-failed-migrations.ts +416 -0
- package/src/scripts/run-migration.ts +691 -0
- package/src/scripts/schema-sync.ts +129 -0
- package/src/scripts/sync-enum-types.ts +171 -0
- package/src/scripts/sync-enum-values.ts +563 -0
- package/src/scripts/truncate-database.ts +124 -0
- package/src/scripts/verify-migration-setup.ts +136 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BatchMigrator = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
class BatchMigrator {
|
|
6
|
+
constructor(dataTransformer, schemaUtils, connections, dependencyResolver, schemaCache = {}, targetSchemaCache = {}) {
|
|
7
|
+
this.dataTransformer = dataTransformer;
|
|
8
|
+
this.schemaUtils = schemaUtils;
|
|
9
|
+
this.connections = connections;
|
|
10
|
+
this.dependencyResolver = dependencyResolver;
|
|
11
|
+
this.schemaCache = schemaCache;
|
|
12
|
+
this.targetSchemaCache = targetSchemaCache;
|
|
13
|
+
this.logger = new common_1.Logger("BatchMigrator");
|
|
14
|
+
this.BATCH_SIZE = 10;
|
|
15
|
+
}
|
|
16
|
+
async checkTableHasData(tenantId, tableName) {
|
|
17
|
+
var _a;
|
|
18
|
+
try {
|
|
19
|
+
const result = await this.connections.targetPool.query(`SELECT EXISTS (SELECT 1 FROM "${tenantId}"."${tableName}" LIMIT 1)`);
|
|
20
|
+
return ((_a = result.rows[0]) === null || _a === void 0 ? void 0 : _a.exists) || false;
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
this.logger.warn(`Error checking data existence for ${tableName}: ${error.message}`);
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async migrateEntityDataInBatches(prisma, entity, providerId, targetSchema) {
|
|
28
|
+
try {
|
|
29
|
+
if (targetSchema !== "public") {
|
|
30
|
+
await this.ensureSchemaExists(targetSchema);
|
|
31
|
+
}
|
|
32
|
+
await this.connections.targetPool.query(`SET session_replication_role = 'replica';`);
|
|
33
|
+
const { name: tableName, idField, filterColumn, filterVia } = entity;
|
|
34
|
+
this.logger.log(`Migrating ${tableName} -> target schema '${targetSchema}'. ${providerId
|
|
35
|
+
? `Filtering source by Provider ID: ${providerId}`
|
|
36
|
+
: "Migrating all source records."}`);
|
|
37
|
+
try {
|
|
38
|
+
const sourceTableSchema = await this.getSourceSchema(tableName);
|
|
39
|
+
const targetTableSchema = await this.getTargetSchema(targetSchema, tableName);
|
|
40
|
+
if (!sourceTableSchema.length) {
|
|
41
|
+
this.logger.warn(`Source table ${tableName} schema not found. Skipping.`);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (!targetTableSchema.length) {
|
|
45
|
+
this.logger.warn(`Target table ${tableName} schema '${targetSchema}' not found. Skipping.`);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
let selectQuery;
|
|
49
|
+
let queryParams = [];
|
|
50
|
+
const columnList = sourceTableSchema
|
|
51
|
+
.map((col) => `"${col.column_name}"`)
|
|
52
|
+
.join(", ");
|
|
53
|
+
let fromClause = `FROM "${tableName}" t`;
|
|
54
|
+
let whereClause = "";
|
|
55
|
+
if (providerId && filterColumn) {
|
|
56
|
+
if (filterVia) {
|
|
57
|
+
const joinTable = filterVia;
|
|
58
|
+
const joinCondition = `t."${filterColumn}" = j.id`;
|
|
59
|
+
const providerFilter = `j.provider_id = $1`;
|
|
60
|
+
fromClause = `FROM "${tableName}" t JOIN "${joinTable}" j ON ${joinCondition}`;
|
|
61
|
+
whereClause = `WHERE ${providerFilter}`;
|
|
62
|
+
queryParams = [providerId];
|
|
63
|
+
this.logger.log(`Using JOIN filter: ${fromClause} ${whereClause}`);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
whereClause = `WHERE t."${filterColumn}" = $1`;
|
|
67
|
+
queryParams = [providerId];
|
|
68
|
+
this.logger.log(`Using direct filter: ${whereClause}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
this.logger.log(`No providerId filter applied for ${tableName}. Selecting all records.`);
|
|
73
|
+
}
|
|
74
|
+
selectQuery = `SELECT t.* ${fromClause} ${whereClause}`;
|
|
75
|
+
const sourceData = await this.executeSourceQuery(selectQuery, queryParams);
|
|
76
|
+
const totalRecords = sourceData.rows.length;
|
|
77
|
+
this.logger.log(`Found ${totalRecords} ${tableName} records in source to migrate to '${targetSchema}'.`);
|
|
78
|
+
if (totalRecords === 0) {
|
|
79
|
+
this.logger.log(`No records to migrate for ${tableName} with current filter. Skipping processing.`);
|
|
80
|
+
await this.connections.targetPool.query(`SET session_replication_role = 'origin';`);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const primaryKeyField = idField;
|
|
84
|
+
await this.processRecords(prisma, targetSchema, tableName, primaryKeyField, sourceData.rows, sourceTableSchema, targetTableSchema);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
this.logger.error(`Error during migration step for ${tableName} to schema '${targetSchema}': ${error.message}`);
|
|
88
|
+
console.error(`Entity Config:`, JSON.stringify(entity));
|
|
89
|
+
console.error(`Provider ID used for filter:`, providerId);
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
this.logger.error(`Error preparing migration for ${entity.name} to schema '${targetSchema}': ${error.message}`);
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
try {
|
|
99
|
+
await this.connections.targetPool.query(`SET session_replication_role = 'origin';`);
|
|
100
|
+
}
|
|
101
|
+
catch (finallyError) {
|
|
102
|
+
this.logger.error(`Failed to reset session_replication_role: ${finallyError.message}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async ensureSchemaExists(schemaName) {
|
|
107
|
+
const schemaExistsResult = await this.connections.targetPool.query(`SELECT schema_name FROM information_schema.schemata WHERE schema_name = $1`, [schemaName]);
|
|
108
|
+
if (schemaExistsResult.rows.length === 0) {
|
|
109
|
+
this.logger.log(`Schema '${schemaName}' does not exist. Creating...`);
|
|
110
|
+
await this.schemaUtils.createSchema(schemaName);
|
|
111
|
+
this.logger.log(`Schema '${schemaName}' created.`);
|
|
112
|
+
try {
|
|
113
|
+
await this.connections.targetPool.query(`
|
|
114
|
+
INSERT INTO "${schemaName}"."_prisma_migrations" (id, checksum, finished_at, migration_name, logs, rolled_back_at, started_at, applied_steps_count)
|
|
115
|
+
SELECT id, checksum, finished_at, migration_name, logs, rolled_back_at, started_at, applied_steps_count
|
|
116
|
+
FROM "public"."_prisma_migrations" WHERE migration_name LIKE '%_init'
|
|
117
|
+
ON CONFLICT DO NOTHING;
|
|
118
|
+
`);
|
|
119
|
+
this.logger.log(`Attempted to copy initial migration record to schema '${schemaName}'.`);
|
|
120
|
+
}
|
|
121
|
+
catch (migrationError) {
|
|
122
|
+
this.logger.warn(`Could not copy baseline migration to schema '${schemaName}': ${migrationError.message}`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async getSourceSchema(tableName) {
|
|
127
|
+
const cacheKey = `source.${tableName}`;
|
|
128
|
+
if (!this.schemaCache[cacheKey]) {
|
|
129
|
+
this.logger.debug(`Cache miss for source schema: ${tableName}`);
|
|
130
|
+
this.schemaCache[cacheKey] = await this.schemaUtils.getTableSchema(tableName, "source", "public");
|
|
131
|
+
}
|
|
132
|
+
return this.schemaCache[cacheKey];
|
|
133
|
+
}
|
|
134
|
+
async getTargetSchema(schema, tableName) {
|
|
135
|
+
const cacheKey = `${schema}.${tableName}`;
|
|
136
|
+
if (!this.targetSchemaCache[cacheKey]) {
|
|
137
|
+
this.logger.debug(`Cache miss for target schema: ${cacheKey}`);
|
|
138
|
+
this.targetSchemaCache[cacheKey] = await this.schemaUtils.getTableSchema(tableName, "target", schema);
|
|
139
|
+
}
|
|
140
|
+
return this.targetSchemaCache[cacheKey];
|
|
141
|
+
}
|
|
142
|
+
async executeSourceQuery(query, params) {
|
|
143
|
+
var _a;
|
|
144
|
+
try {
|
|
145
|
+
this.logger.debug(`Executing source query: ${query.replace(/\s\s+/g, " ")} || PARAMS: ${JSON.stringify(params)}`);
|
|
146
|
+
const result = await this.connections.sourcePool.query(query, params);
|
|
147
|
+
this.logger.debug(`Query returned ${((_a = result.rows) === null || _a === void 0 ? void 0 : _a.length) || 0} rows`);
|
|
148
|
+
return result;
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
this.logger.error(`Error executing source query: ${error.message}`);
|
|
152
|
+
this.logger.error(`Query was: ${query}`);
|
|
153
|
+
this.logger.error(`Params were: ${JSON.stringify(params)}`);
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async getPrimaryKeyField(schemaName, tableName) {
|
|
158
|
+
var _a;
|
|
159
|
+
try {
|
|
160
|
+
const result = await this.connections.targetPool.query(`
|
|
161
|
+
SELECT kcu.column_name
|
|
162
|
+
FROM information_schema.table_constraints tc
|
|
163
|
+
JOIN information_schema.key_column_usage kcu
|
|
164
|
+
ON tc.constraint_name = kcu.constraint_name
|
|
165
|
+
AND tc.table_schema = kcu.table_schema
|
|
166
|
+
WHERE tc.constraint_type = 'PRIMARY KEY'
|
|
167
|
+
AND tc.table_schema = $1
|
|
168
|
+
AND tc.table_name = $2
|
|
169
|
+
`, [schemaName, tableName]);
|
|
170
|
+
return ((_a = result.rows[0]) === null || _a === void 0 ? void 0 : _a.column_name) || null;
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
this.logger.error(`Error getting primary key for ${schemaName}.${tableName}: ${error.message}`);
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async processRecords(prisma, targetSchema, tableName, idField, records, sourceSchema, targetSchemaInfo) {
|
|
178
|
+
if (!idField) {
|
|
179
|
+
this.logger.error(`Cannot process records for ${tableName}: idField is missing in configuration.`);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
for (const record of records) {
|
|
183
|
+
let recordId = record[idField];
|
|
184
|
+
try {
|
|
185
|
+
this.logger.debug(`Processing record ${recordId !== null && recordId !== void 0 ? recordId : "(no id found)"} for ${targetSchema}.${tableName}`);
|
|
186
|
+
if (!record || Object.keys(record).length === 0) {
|
|
187
|
+
this.logger.warn(`Empty record found for ${tableName}, skipping`);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (!recordId) {
|
|
191
|
+
this.logger.warn(`Record missing configured ID field '${idField}' in source data, skipping: ${JSON.stringify(record)}`);
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
const transformedData = await this.dataTransformer.transformRecord(record, sourceSchema, targetSchemaInfo, targetSchema);
|
|
195
|
+
const processedData = Object.entries(transformedData).reduce((acc, [key, value]) => {
|
|
196
|
+
const columnSchema = targetSchemaInfo.find((col) => col.column_name === key);
|
|
197
|
+
if (!columnSchema)
|
|
198
|
+
return acc;
|
|
199
|
+
const columnName = `"${columnSchema.column_name}"`;
|
|
200
|
+
const escapeValue = (val) => {
|
|
201
|
+
if (val === null || val === undefined)
|
|
202
|
+
return "NULL";
|
|
203
|
+
if (typeof val === "boolean")
|
|
204
|
+
return val ? "TRUE" : "FALSE";
|
|
205
|
+
if (typeof val === "number")
|
|
206
|
+
return String(val);
|
|
207
|
+
return String(val).replace(/'/g, "''").replace(/\\/g, "");
|
|
208
|
+
};
|
|
209
|
+
if (value === null || value === undefined) {
|
|
210
|
+
acc[columnName] = "NULL";
|
|
211
|
+
return acc;
|
|
212
|
+
}
|
|
213
|
+
if (typeof value === "object" &&
|
|
214
|
+
value !== null &&
|
|
215
|
+
value["needsEnumCast"]) {
|
|
216
|
+
const enumValue = value;
|
|
217
|
+
const schemaPrefix = targetSchema === "public" ? '"public".' : `"${targetSchema}".`;
|
|
218
|
+
const quotedEnumType = `"${enumValue.enumType}"`;
|
|
219
|
+
const escapedEnumValue = escapeValue(enumValue.value);
|
|
220
|
+
acc[columnName] = `CAST(E'${escapedEnumValue}' AS ${schemaPrefix}${quotedEnumType})`;
|
|
221
|
+
return acc;
|
|
222
|
+
}
|
|
223
|
+
let targetType = columnSchema.data_type.toLowerCase();
|
|
224
|
+
let udtName = columnSchema.udt_name;
|
|
225
|
+
let sqlValue;
|
|
226
|
+
let requiresQuotes = false;
|
|
227
|
+
if ([
|
|
228
|
+
"text",
|
|
229
|
+
"varchar",
|
|
230
|
+
"character varying",
|
|
231
|
+
"char",
|
|
232
|
+
"timestamp with time zone",
|
|
233
|
+
"timestamptz",
|
|
234
|
+
"timestamp without time zone",
|
|
235
|
+
"timestamp",
|
|
236
|
+
"date",
|
|
237
|
+
"uuid",
|
|
238
|
+
"json",
|
|
239
|
+
"jsonb",
|
|
240
|
+
].includes(targetType) ||
|
|
241
|
+
targetType.includes("enum") ||
|
|
242
|
+
(targetType === "user-defined" && (udtName === null || udtName === void 0 ? void 0 : udtName.startsWith("enum_")))) {
|
|
243
|
+
requiresQuotes = true;
|
|
244
|
+
}
|
|
245
|
+
const escaped = escapeValue(value);
|
|
246
|
+
if (escaped === "NULL") {
|
|
247
|
+
sqlValue = "NULL";
|
|
248
|
+
}
|
|
249
|
+
else if (requiresQuotes) {
|
|
250
|
+
sqlValue = `E'${escaped}'`;
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
sqlValue = escaped;
|
|
254
|
+
}
|
|
255
|
+
let castExpression = "";
|
|
256
|
+
if (targetType.includes("timestamp"))
|
|
257
|
+
castExpression = "::timestamp with time zone";
|
|
258
|
+
else if (targetType === "date")
|
|
259
|
+
castExpression = "::date";
|
|
260
|
+
else if (targetType === "integer" ||
|
|
261
|
+
targetType === "int" ||
|
|
262
|
+
targetType === "int4")
|
|
263
|
+
castExpression = "::integer";
|
|
264
|
+
else if (targetType === "bigint" || targetType === "int8")
|
|
265
|
+
castExpression = "::bigint";
|
|
266
|
+
else if (targetType === "smallint" || targetType === "int2")
|
|
267
|
+
castExpression = "::smallint";
|
|
268
|
+
else if (targetType === "numeric" || targetType === "decimal")
|
|
269
|
+
castExpression = "::numeric";
|
|
270
|
+
else if (targetType === "real" || targetType === "float4")
|
|
271
|
+
castExpression = "::real";
|
|
272
|
+
else if (targetType === "double precision" ||
|
|
273
|
+
targetType === "float8")
|
|
274
|
+
castExpression = "::double precision";
|
|
275
|
+
else if (targetType === "boolean" || targetType === "bool")
|
|
276
|
+
castExpression = "::boolean";
|
|
277
|
+
else if (targetType === "json" || targetType === "jsonb")
|
|
278
|
+
castExpression = `::${targetType}`;
|
|
279
|
+
else if (targetType === "uuid")
|
|
280
|
+
castExpression = "::uuid";
|
|
281
|
+
else if (targetType === "text" || targetType.includes("char"))
|
|
282
|
+
castExpression = "::text";
|
|
283
|
+
else if (targetType === "user-defined" &&
|
|
284
|
+
(udtName === null || udtName === void 0 ? void 0 : udtName.startsWith("enum_"))) {
|
|
285
|
+
const schemaPrefix = targetSchema === "public" ? '"public".' : `"${targetSchema}".`;
|
|
286
|
+
castExpression = `::${schemaPrefix}"${udtName}"`;
|
|
287
|
+
}
|
|
288
|
+
acc[columnName] = `${sqlValue}${castExpression}`;
|
|
289
|
+
return acc;
|
|
290
|
+
}, {});
|
|
291
|
+
const validProcessedData = Object.entries(processedData).reduce((acc, [key, val]) => {
|
|
292
|
+
if (val !== undefined &&
|
|
293
|
+
val !== "NULL" &&
|
|
294
|
+
val !== "E''" &&
|
|
295
|
+
val !== "E'undefined'" &&
|
|
296
|
+
val !== "E'null'") {
|
|
297
|
+
acc[key] = val;
|
|
298
|
+
}
|
|
299
|
+
return acc;
|
|
300
|
+
}, {});
|
|
301
|
+
const columns = Object.keys(validProcessedData);
|
|
302
|
+
const valuesString = Object.values(validProcessedData).join(", ");
|
|
303
|
+
if (columns.length === 0) {
|
|
304
|
+
this.logger.warn(`Record ${recordId} for ${tableName} resulted in no valid columns to insert/update after processing. Skipping.`);
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const updateSetClauses = columns
|
|
308
|
+
.filter((col) => col !== `"${idField}"`)
|
|
309
|
+
.map((col) => `${col} = EXCLUDED.${col}`)
|
|
310
|
+
.join(", ");
|
|
311
|
+
const quotedSchemaTable = `"${targetSchema}"."${tableName}"`;
|
|
312
|
+
const quotedIdField = `"${idField}"`;
|
|
313
|
+
const conflictClause = updateSetClauses
|
|
314
|
+
? `ON CONFLICT (${quotedIdField}) DO UPDATE SET ${updateSetClauses}`
|
|
315
|
+
: `ON CONFLICT (${quotedIdField}) DO NOTHING`;
|
|
316
|
+
const query = `
|
|
317
|
+
INSERT INTO ${quotedSchemaTable} (${columns.join(", ")})
|
|
318
|
+
VALUES (${valuesString})
|
|
319
|
+
${conflictClause}
|
|
320
|
+
`;
|
|
321
|
+
this.logger.debug(`Executing Upsert: ${query.replace(/\s\s+/g, " ")}`);
|
|
322
|
+
await this.connections.targetPool.query(query);
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
this.logger.error(`Error processing record ID '${recordId !== null && recordId !== void 0 ? recordId : "(unknown)"}' for ${targetSchema}.${tableName}: ${error.message}`);
|
|
326
|
+
this.logger.error(`Record data: ${JSON.stringify(record)}`);
|
|
327
|
+
throw error;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
exports.BatchMigrator = BatchMigrator;
|
|
333
|
+
//# sourceMappingURL=batch-migrator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"batch-migrator.js","sourceRoot":"","sources":["../../../src/scripts/data-migration/batch-migrator.ts"],"names":[],"mappings":";;;AAAA,2CAAwC;AASxC,MAAa,aAAa;IAKxB,YACmB,eAAgC,EAChC,WAAwB,EACxB,WAAgC,EAChC,kBAAsC,EACtC,cAA8C,EAAE,EAChD,oBAAoD,EAAE;QALtD,oBAAe,GAAf,eAAe,CAAiB;QAChC,gBAAW,GAAX,WAAW,CAAa;QACxB,gBAAW,GAAX,WAAW,CAAqB;QAChC,uBAAkB,GAAlB,kBAAkB,CAAoB;QACtC,gBAAW,GAAX,WAAW,CAAqC;QAChD,sBAAiB,GAAjB,iBAAiB,CAAqC;QAVxD,WAAM,GAAG,IAAI,eAAM,CAAC,eAAe,CAAC,CAAC;QACrC,eAAU,GAAG,EAAE,CAAC;IAU9B,CAAC;IAEI,KAAK,CAAC,iBAAiB,CAC7B,QAAgB,EAChB,SAAiB;;QAGjB,IAAI,CAAC;YACH,MAA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,MAAM,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACjD,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBAEf,IAAI,CAAC,MAAM,CAAC,KAAK,CACf,+BACE,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,WACd,SAAS,YAAY,IAAI,SAAS,KAAK,KAAK,CAAC,OAAO,EAAE,CACvD,CAAC;gBACF,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,gBAAgB,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;gBAK5D,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;IACH,CAAC;CACF;AA/iBD,sCA+iBC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ColumnSchema, EnumCastValue } from "./types";
|
|
2
|
+
import { SchemaUtils } from "./schema-utils";
|
|
3
|
+
export declare class DataTransformer {
|
|
4
|
+
readonly schemaUtils: SchemaUtils;
|
|
5
|
+
private readonly logger;
|
|
6
|
+
private enumValuesCache;
|
|
7
|
+
private columnValuesCache;
|
|
8
|
+
constructor(schemaUtils: SchemaUtils);
|
|
9
|
+
validateEnumValue(schemaName: string, enumType: string, value: string): Promise<string | null>;
|
|
10
|
+
transformToNumeric(value: any): number | null;
|
|
11
|
+
private getDefaultEnumValue;
|
|
12
|
+
prepareEnumValue(tenantId: string, enumType: string, value: string): Promise<EnumCastValue | null>;
|
|
13
|
+
transformColumnValue(value: any, columnName: string, targetColumn: ColumnSchema, tenantId: string): Promise<any>;
|
|
14
|
+
private extractEnumValueFromObject;
|
|
15
|
+
private getDefaultValueForType;
|
|
16
|
+
transformRecord(record: any, sourceSchema: ColumnSchema[], targetSchema: ColumnSchema[], tenantId: string): Promise<any>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DataTransformer = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
class DataTransformer {
|
|
6
|
+
constructor(schemaUtils) {
|
|
7
|
+
this.schemaUtils = schemaUtils;
|
|
8
|
+
this.logger = new common_1.Logger("DataTransformer");
|
|
9
|
+
this.enumValuesCache = new Map();
|
|
10
|
+
this.columnValuesCache = new Map();
|
|
11
|
+
}
|
|
12
|
+
async validateEnumValue(schemaName, enumType, value) {
|
|
13
|
+
try {
|
|
14
|
+
const cacheKey = `${schemaName}.${enumType}`;
|
|
15
|
+
if (!this.enumValuesCache.has(cacheKey)) {
|
|
16
|
+
const query = `
|
|
17
|
+
SELECT e.enumlabel
|
|
18
|
+
FROM pg_type t
|
|
19
|
+
JOIN pg_enum e ON t.oid = e.enumtypid
|
|
20
|
+
JOIN pg_namespace n ON n.oid = t.typnamespace
|
|
21
|
+
WHERE t.typname = $1 AND n.nspname = $2
|
|
22
|
+
`;
|
|
23
|
+
const result = await this.schemaUtils.queryTargetDb(query, [
|
|
24
|
+
enumType,
|
|
25
|
+
schemaName,
|
|
26
|
+
]);
|
|
27
|
+
const uniqueValues = new Set(result.rows.map((row) => String(row.enumlabel)));
|
|
28
|
+
const normalizedMap = new Map();
|
|
29
|
+
result.rows.forEach((row) => {
|
|
30
|
+
const original = String(row.enumlabel);
|
|
31
|
+
const normalized = original.toLowerCase().trim();
|
|
32
|
+
normalizedMap.set(normalized, original);
|
|
33
|
+
});
|
|
34
|
+
this.enumValuesCache.set(cacheKey, uniqueValues);
|
|
35
|
+
this.enumValuesCache.set(`${cacheKey}_normalized`, normalizedMap);
|
|
36
|
+
this.logger.log(`Valid values for ${enumType}: ${[...uniqueValues].join(", ")}`);
|
|
37
|
+
}
|
|
38
|
+
const validValues = this.enumValuesCache.get(cacheKey);
|
|
39
|
+
const normalizedMap = this.enumValuesCache.get(`${cacheKey}_normalized`);
|
|
40
|
+
if (!validValues || !normalizedMap) {
|
|
41
|
+
throw new Error(`No valid values found for enum ${enumType}`);
|
|
42
|
+
}
|
|
43
|
+
if (validValues.has(value)) {
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
const normalizedValue = value.toLowerCase().trim();
|
|
47
|
+
if (normalizedMap.has(normalizedValue)) {
|
|
48
|
+
return normalizedMap.get(normalizedValue);
|
|
49
|
+
}
|
|
50
|
+
this.logger.warn(`Invalid enum value "${value}" for type ${enumType}. Valid values are: ${[
|
|
51
|
+
...validValues,
|
|
52
|
+
].join(", ")}`);
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
this.logger.error(`Error validating enum value "${value}" for type ${enumType}: ${error.message}`);
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
transformToNumeric(value) {
|
|
61
|
+
if (value === null || value === "") {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
const numericValue = parseFloat(value);
|
|
65
|
+
if (isNaN(numericValue)) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
return numericValue;
|
|
69
|
+
}
|
|
70
|
+
async getDefaultEnumValue(enumType, tenantId = "public") {
|
|
71
|
+
try {
|
|
72
|
+
const query = `
|
|
73
|
+
SELECT e.enumlabel
|
|
74
|
+
FROM pg_type t
|
|
75
|
+
JOIN pg_enum e ON t.oid = e.enumtypid
|
|
76
|
+
JOIN pg_namespace n ON n.oid = t.typnamespace
|
|
77
|
+
WHERE t.typname = $1 AND n.nspname = $2
|
|
78
|
+
ORDER BY e.enumsortorder
|
|
79
|
+
LIMIT 1
|
|
80
|
+
`;
|
|
81
|
+
const result = await this.schemaUtils.queryTargetDb(query, [
|
|
82
|
+
enumType,
|
|
83
|
+
tenantId,
|
|
84
|
+
]);
|
|
85
|
+
if (result.rows.length > 0) {
|
|
86
|
+
return result.rows[0].enumlabel;
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
this.logger.error(`Error getting default enum value for ${enumType}: ${error.message}`);
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async prepareEnumValue(tenantId, enumType, value) {
|
|
96
|
+
if (value === null || value === undefined || value === "") {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
const validatedValue = await this.validateEnumValue(tenantId, enumType, value);
|
|
100
|
+
if (validatedValue === null) {
|
|
101
|
+
const defaultValue = await this.getDefaultEnumValue(enumType, tenantId);
|
|
102
|
+
if (defaultValue) {
|
|
103
|
+
this.logger.warn(`Using first enum value '${defaultValue}' as default for invalid value '${value}' of type ${enumType}`);
|
|
104
|
+
return {
|
|
105
|
+
needsEnumCast: true,
|
|
106
|
+
value: defaultValue,
|
|
107
|
+
enumType,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
needsEnumCast: true,
|
|
114
|
+
value: validatedValue,
|
|
115
|
+
enumType,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
async transformColumnValue(value, columnName, targetColumn, tenantId) {
|
|
119
|
+
this.logger.debug(`Transforming column ${columnName}, value: ${JSON.stringify(value)}, type: ${targetColumn.udt_name}`);
|
|
120
|
+
if (value === null && targetColumn.is_nullable === "YES") {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
if (targetColumn.udt_name.startsWith("enum_")) {
|
|
125
|
+
if (typeof value === "object" && value !== null) {
|
|
126
|
+
const stringValue = this.extractEnumValueFromObject(value);
|
|
127
|
+
if (stringValue) {
|
|
128
|
+
return await this.prepareEnumValue(tenantId, targetColumn.udt_name, stringValue);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (typeof value === "string") {
|
|
132
|
+
return await this.prepareEnumValue(tenantId, targetColumn.udt_name, value);
|
|
133
|
+
}
|
|
134
|
+
const defaultValue = await this.getDefaultEnumValue(targetColumn.udt_name, tenantId);
|
|
135
|
+
if (defaultValue) {
|
|
136
|
+
this.logger.warn(`Using default enum value '${defaultValue}' for column ${columnName}`);
|
|
137
|
+
return {
|
|
138
|
+
needsEnumCast: true,
|
|
139
|
+
value: defaultValue,
|
|
140
|
+
enumType: targetColumn.udt_name,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (targetColumn.udt_name === "numeric" ||
|
|
145
|
+
targetColumn.data_type === "decimal") {
|
|
146
|
+
const numericValue = this.transformToNumeric(value);
|
|
147
|
+
if (numericValue !== null || targetColumn.is_nullable === "YES") {
|
|
148
|
+
return numericValue;
|
|
149
|
+
}
|
|
150
|
+
return 0;
|
|
151
|
+
}
|
|
152
|
+
if (targetColumn.udt_name === "bool" ||
|
|
153
|
+
targetColumn.data_type === "boolean") {
|
|
154
|
+
if (typeof value === "string") {
|
|
155
|
+
return value.toLowerCase() === "true" || value === "1";
|
|
156
|
+
}
|
|
157
|
+
return Boolean(value);
|
|
158
|
+
}
|
|
159
|
+
if (targetColumn.data_type.includes("timestamp") ||
|
|
160
|
+
targetColumn.data_type.includes("date")) {
|
|
161
|
+
if (!value)
|
|
162
|
+
return null;
|
|
163
|
+
const date = new Date(value);
|
|
164
|
+
return isNaN(date.getTime()) ? null : date.toISOString();
|
|
165
|
+
}
|
|
166
|
+
if (targetColumn.data_type === "json" ||
|
|
167
|
+
targetColumn.data_type === "jsonb") {
|
|
168
|
+
if (typeof value === "string") {
|
|
169
|
+
try {
|
|
170
|
+
return JSON.parse(value);
|
|
171
|
+
}
|
|
172
|
+
catch (_a) {
|
|
173
|
+
return value;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return value;
|
|
177
|
+
}
|
|
178
|
+
return value;
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
this.logger.error(`Error transforming value for column ${columnName}: ${error.message}`);
|
|
182
|
+
if (targetColumn.is_nullable === "YES") {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
return this.getDefaultValueForType(targetColumn.udt_name);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
extractEnumValueFromObject(value) {
|
|
189
|
+
const possibleKeys = ["type", "value", "document_type", "status", "state"];
|
|
190
|
+
for (const key of possibleKeys) {
|
|
191
|
+
if (value[key] && typeof value[key] === "string") {
|
|
192
|
+
return value[key];
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
for (const val of Object.values(value)) {
|
|
196
|
+
if (typeof val === "string") {
|
|
197
|
+
return val;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
getDefaultValueForType(udtName) {
|
|
203
|
+
switch (udtName) {
|
|
204
|
+
case "int4":
|
|
205
|
+
case "int8":
|
|
206
|
+
case "numeric":
|
|
207
|
+
return 0;
|
|
208
|
+
case "bool":
|
|
209
|
+
return false;
|
|
210
|
+
case "text":
|
|
211
|
+
case "varchar":
|
|
212
|
+
return "";
|
|
213
|
+
case "jsonb":
|
|
214
|
+
case "json":
|
|
215
|
+
return {};
|
|
216
|
+
default:
|
|
217
|
+
if (udtName.startsWith("enum_")) {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
async transformRecord(record, sourceSchema, targetSchema, tenantId) {
|
|
224
|
+
const transformedData = {};
|
|
225
|
+
for (const [key, value] of Object.entries(record)) {
|
|
226
|
+
let targetColumn = targetSchema.find((col) => col.column_name === key);
|
|
227
|
+
if (!targetColumn) {
|
|
228
|
+
targetColumn = targetSchema.find((col) => col.column_name.toLowerCase() === key.toLowerCase());
|
|
229
|
+
}
|
|
230
|
+
if (!targetColumn) {
|
|
231
|
+
this.logger.warn(`Column ${key} not found in target schema, skipping`);
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
const columnName = targetColumn.column_name;
|
|
235
|
+
const transformedValue = await this.transformColumnValue(value, columnName, targetColumn, tenantId);
|
|
236
|
+
transformedData[columnName] = transformedValue;
|
|
237
|
+
}
|
|
238
|
+
return transformedData;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
exports.DataTransformer = DataTransformer;
|
|
242
|
+
//# sourceMappingURL=data-transformer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"data-transformer.js","sourceRoot":"","sources":["../../../src/scripts/data-migration/data-transformer.ts"],"names":[],"mappings":";;;AAAA,2CAAwC;AAIxC,MAAa,eAAe;IAK1B,YAA4B,WAAwB;QAAxB,gBAAW,GAAX,WAAW,CAAa;QAJnC,WAAM,GAAG,IAAI,eAAM,CAAC,iBAAiB,CAAC,CAAC;QAChD,oBAAe,GAAqB,IAAI,GAAG,EAAE,CAAC;QAC9C,sBAAiB,GAAkC,IAAI,GAAG,EAAE,CAAC;IAEd,CAAC;IAExD,KAAK,CAAC,iBAAiB,CACrB,UAAkB,EAClB,QAAgB,EAChB,KAAa;QAEb,IAAI,CAAC;YACH,MAAM,QAAQ,GAAG,GAAG,UAAU,IAAI,QAAQ,EAAE,CAAC;YAC7C,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;gBACxC,MAAM,KAAK,GAAG;;;;;;SAMb,CAAC;gBAEF,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,aAAa,CAAC,KAAK,EAAE;oBACzD,QAAQ;oBACR,UAAU;iBACX,CAAC,CAAC;gBACH,MAAM,YAAY,GAAgB,IAAI,GAAG,CACvC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAU,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CACxD,CAAC;gBAGF,MAAM,aAAa,GAAG,IAAI,GAAG,EAAkB,CAAC;gBAChD,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;oBAC1B,MAAM,QAAQ,G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G,CAAC,WAAW,KAAK,GAAG,CACjC,CAAC;YAGF,IAAI,CAAC,YAAY,EAAE,CAAC;gBAClB,YAAY,GAAG,YAAY,CAAC,IAAI,CAC9B,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,CAAC,WAAW,EAAE,KAAK,GAAG,CAAC,WAAW,EAAE,CAC7D,CAAC;YACJ,CAAC;YAED,IAAI,CAAC,YAAY,EAAE,CAAC;gBAClB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,GAAG,uCAAuC,CAAC,CAAC;gBACvE,SAAS;YACX,CAAC;YAGD,MAAM,UAAU,GAAG,YAAY,CAAC,WAAW,CAAC;YAE5C,MAAM,gBAAgB,GAAG,MAAM,IAAI,CAAC,oBAAoB,CACtD,KAAK,EACL,UAAU,EACV,YAAY,EACZ,QAAQ,CACT,CAAC;YAEF,eAAe,CAAC,UAAU,CAAC,GAAG,gBAAgB,CAAC;QACjD,CAAC;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;CACF;AApXD,0CAoXC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PrismaClient } from "@prisma/client";
|
|
2
|
+
import { DatabaseConnections } from "./types";
|
|
3
|
+
export declare class DatabaseConnector {
|
|
4
|
+
static createConnections(): DatabaseConnections;
|
|
5
|
+
static createTenantPrismaClient(tenantId: string): PrismaClient;
|
|
6
|
+
static cleanup(connections: DatabaseConnections): Promise<void>;
|
|
7
|
+
}
|