drizzle-seed 0.4.0-08bb2d5 → 0.4.0-4ec2def
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/cockroach-core/index.d.cts +8 -2
- package/cockroach-core/index.d.mts +8 -2
- package/cockroach-core/index.d.ts +8 -2
- package/common.d.cts +25 -0
- package/common.d.mts +25 -0
- package/common.d.ts +25 -0
- package/generators/Generators.d.cts +2 -2
- package/generators/Generators.d.mts +2 -2
- package/generators/Generators.d.ts +2 -2
- package/generators/versioning/v2.d.cts +2 -2
- package/generators/versioning/v2.d.mts +2 -2
- package/generators/versioning/v2.d.ts +2 -2
- package/index.cjs +503 -1049
- package/index.cjs.map +1 -1
- package/index.d.cts +8 -82
- package/index.d.mts +8 -82
- package/index.d.ts +8 -82
- package/index.mjs +508 -1054
- package/index.mjs.map +1 -1
- package/mssql-core/index.d.cts +2 -2
- package/mssql-core/index.d.mts +2 -2
- package/mssql-core/index.d.ts +2 -2
- package/mysql-core/index.d.cts +8 -2
- package/mysql-core/index.d.mts +8 -2
- package/mysql-core/index.d.ts +8 -2
- package/package.json +3 -3
- package/pg-core/index.d.cts +8 -2
- package/pg-core/index.d.mts +8 -2
- package/pg-core/index.d.ts +8 -2
- package/singlestore-core/index.d.cts +8 -2
- package/singlestore-core/index.d.mts +8 -2
- package/singlestore-core/index.d.ts +8 -2
- package/sqlite-core/index.d.cts +8 -2
- package/sqlite-core/index.d.mts +8 -2
- package/sqlite-core/index.d.ts +8 -2
- package/types/seedService.d.cts +1 -1
- package/types/seedService.d.mts +1 -1
- package/types/seedService.d.ts +1 -1
- package/types/tables.d.cts +15 -0
- package/types/tables.d.mts +15 -0
- package/types/tables.d.ts +15 -0
package/index.cjs
CHANGED
|
@@ -7,8 +7,184 @@ var sqliteCore = require('drizzle-orm/sqlite-core');
|
|
|
7
7
|
var mssqlCore = require('drizzle-orm/mssql-core');
|
|
8
8
|
var cockroachCore = require('drizzle-orm/cockroach-core');
|
|
9
9
|
var singlestoreCore = require('drizzle-orm/singlestore-core');
|
|
10
|
+
var _relations = require('drizzle-orm/_relations');
|
|
10
11
|
var prand = require('pure-rand');
|
|
11
12
|
|
|
13
|
+
const isRelationCyclic = (startRel) => {
|
|
14
|
+
// self relation
|
|
15
|
+
if (startRel.table === startRel.refTable)
|
|
16
|
+
return false;
|
|
17
|
+
// DFS
|
|
18
|
+
const targetTable = startRel.table;
|
|
19
|
+
const queue = [startRel];
|
|
20
|
+
let path = [];
|
|
21
|
+
while (queue.length !== 0) {
|
|
22
|
+
const currRel = queue.shift();
|
|
23
|
+
if (path.includes(currRel.table)) {
|
|
24
|
+
const idx = path.indexOf(currRel.table);
|
|
25
|
+
path = path.slice(0, idx);
|
|
26
|
+
}
|
|
27
|
+
path.push(currRel.table);
|
|
28
|
+
for (const rel of currRel.refTableRels) {
|
|
29
|
+
// self relation
|
|
30
|
+
if (rel.table === rel.refTable)
|
|
31
|
+
continue;
|
|
32
|
+
if (rel.refTable === targetTable)
|
|
33
|
+
return true;
|
|
34
|
+
// found cycle, but not the one we are looking for
|
|
35
|
+
if (path.includes(rel.refTable))
|
|
36
|
+
continue;
|
|
37
|
+
queue.unshift(rel);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
};
|
|
42
|
+
const generateHashFromString = (s) => {
|
|
43
|
+
let hash = 0;
|
|
44
|
+
// p and m are prime numbers
|
|
45
|
+
const p = 53;
|
|
46
|
+
const m = 28871271685163;
|
|
47
|
+
for (let i = 0; i < s.length; i++) {
|
|
48
|
+
hash += ((s.codePointAt(i) || 0) * Math.pow(p, i)) % m;
|
|
49
|
+
}
|
|
50
|
+
return hash;
|
|
51
|
+
};
|
|
52
|
+
const equalSets = (set1, set2) => {
|
|
53
|
+
return set1.size === set2.size && [...set1].every((si) => set2.has(si));
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const getTableConfig = (table) => {
|
|
57
|
+
if (drizzleOrm.is(table, pgCore.PgTable))
|
|
58
|
+
return pgCore.getTableConfig(table);
|
|
59
|
+
else if (drizzleOrm.is(table, mysqlCore.MySqlTable))
|
|
60
|
+
return mysqlCore.getTableConfig(table);
|
|
61
|
+
else if (drizzleOrm.is(table, sqliteCore.SQLiteTable))
|
|
62
|
+
return sqliteCore.getTableConfig(table);
|
|
63
|
+
else if (drizzleOrm.is(table, cockroachCore.CockroachTable))
|
|
64
|
+
return cockroachCore.getTableConfig(table);
|
|
65
|
+
else if (drizzleOrm.is(table, mssqlCore.MsSqlTable))
|
|
66
|
+
return mssqlCore.getTableConfig(table);
|
|
67
|
+
else
|
|
68
|
+
return singlestoreCore.getTableConfig(table); // if (is(table, SingleStoreTable))
|
|
69
|
+
};
|
|
70
|
+
const transformFromDrizzleRelation = (schema, getDbToTsColumnNamesMap, tableRelations) => {
|
|
71
|
+
const schemaConfig = _relations.extractTablesRelationalConfig(schema, _relations.createTableRelationsHelpers);
|
|
72
|
+
const relations = [];
|
|
73
|
+
for (const table of Object.values(schemaConfig.tables)) {
|
|
74
|
+
if (table.relations === undefined)
|
|
75
|
+
continue;
|
|
76
|
+
for (const drizzleRel of Object.values(table.relations)) {
|
|
77
|
+
if (!drizzleOrm.is(drizzleRel, _relations.One))
|
|
78
|
+
continue;
|
|
79
|
+
const tableConfig = getTableConfig(drizzleRel.sourceTable);
|
|
80
|
+
const tableDbSchema = tableConfig.schema ?? 'public';
|
|
81
|
+
const tableDbName = tableConfig.name;
|
|
82
|
+
const tableTsName = schemaConfig.tableNamesMap[`${tableDbSchema}.${tableDbName}`] ?? tableDbName;
|
|
83
|
+
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
84
|
+
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
85
|
+
?? [];
|
|
86
|
+
const refTableConfig = getTableConfig(drizzleRel.referencedTable);
|
|
87
|
+
const refTableDbSchema = refTableConfig.schema ?? 'public';
|
|
88
|
+
const refTableDbName = refTableConfig.name;
|
|
89
|
+
const refTableTsName = schemaConfig.tableNamesMap[`${refTableDbSchema}.${refTableDbName}`]
|
|
90
|
+
?? refTableDbName;
|
|
91
|
+
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
92
|
+
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
93
|
+
?? [];
|
|
94
|
+
if (tableRelations[refTableTsName] === undefined) {
|
|
95
|
+
tableRelations[refTableTsName] = [];
|
|
96
|
+
}
|
|
97
|
+
const relation = {
|
|
98
|
+
table: tableTsName,
|
|
99
|
+
columns,
|
|
100
|
+
refTable: refTableTsName,
|
|
101
|
+
refColumns,
|
|
102
|
+
refTableRels: tableRelations[refTableTsName],
|
|
103
|
+
type: 'one',
|
|
104
|
+
};
|
|
105
|
+
// do not add duplicate relation
|
|
106
|
+
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
107
|
+
&& rel.refTable === relation.refTable)) {
|
|
108
|
+
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
109
|
+
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
110
|
+
+ `In this case, the foreign key constraint will be used.\n`);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
relations.push(relation);
|
|
114
|
+
tableRelations[tableTsName].push(relation);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return relations;
|
|
118
|
+
};
|
|
119
|
+
const getSchemaInfo = (drizzleTablesAndRelations, drizzleTables, mapTable) => {
|
|
120
|
+
let tableConfig;
|
|
121
|
+
let dbToTsColumnNamesMap;
|
|
122
|
+
const dbToTsTableNamesMap = Object.fromEntries(Object.entries(drizzleTables).map(([key, value]) => [drizzleOrm.getTableName(value), key]));
|
|
123
|
+
const tables = [];
|
|
124
|
+
const relations = [];
|
|
125
|
+
const dbToTsColumnNamesMapGlobal = {};
|
|
126
|
+
const tableRelations = {};
|
|
127
|
+
const getDbToTsColumnNamesMap = (table) => {
|
|
128
|
+
let dbToTsColumnNamesMap = {};
|
|
129
|
+
const tableName = drizzleOrm.getTableName(table);
|
|
130
|
+
if (Object.hasOwn(dbToTsColumnNamesMapGlobal, tableName)) {
|
|
131
|
+
dbToTsColumnNamesMap = dbToTsColumnNamesMapGlobal[tableName];
|
|
132
|
+
return dbToTsColumnNamesMap;
|
|
133
|
+
}
|
|
134
|
+
const tableConfig = getTableConfig(table);
|
|
135
|
+
for (const [tsCol, col] of Object.entries(drizzleOrm.getColumnTable(tableConfig.columns[0]))) {
|
|
136
|
+
if (drizzleOrm.is(col, drizzleOrm.Column))
|
|
137
|
+
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
138
|
+
}
|
|
139
|
+
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
140
|
+
return dbToTsColumnNamesMap;
|
|
141
|
+
};
|
|
142
|
+
for (const table of Object.values(drizzleTables)) {
|
|
143
|
+
tableConfig = getTableConfig(table);
|
|
144
|
+
dbToTsColumnNamesMap = getDbToTsColumnNamesMap(table);
|
|
145
|
+
// might be empty list
|
|
146
|
+
const newRelations = tableConfig.foreignKeys === undefined ? [] : tableConfig.foreignKeys.map((fk) => {
|
|
147
|
+
const table = dbToTsTableNamesMap[tableConfig.name];
|
|
148
|
+
const refTable = dbToTsTableNamesMap[drizzleOrm.getTableName(fk.reference().foreignTable)];
|
|
149
|
+
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(fk.reference().foreignTable);
|
|
150
|
+
if (tableRelations[refTable] === undefined) {
|
|
151
|
+
tableRelations[refTable] = [];
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
table,
|
|
155
|
+
columns: fk
|
|
156
|
+
.reference()
|
|
157
|
+
.columns.map((col) => dbToTsColumnNamesMap[col.name]),
|
|
158
|
+
refTable,
|
|
159
|
+
refColumns: fk
|
|
160
|
+
.reference()
|
|
161
|
+
.foreignColumns.map((fCol) => dbToTsColumnNamesMapForRefTable[fCol.name]),
|
|
162
|
+
refTableRels: tableRelations[refTable],
|
|
163
|
+
};
|
|
164
|
+
});
|
|
165
|
+
relations.push(...newRelations);
|
|
166
|
+
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
167
|
+
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
168
|
+
}
|
|
169
|
+
tableRelations[dbToTsTableNamesMap[tableConfig.name]].push(...newRelations);
|
|
170
|
+
// console.log(tableConfig.columns);
|
|
171
|
+
tables.push(mapTable(tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap));
|
|
172
|
+
}
|
|
173
|
+
const transformedDrizzleRelations = transformFromDrizzleRelation(drizzleTablesAndRelations, getDbToTsColumnNamesMap, tableRelations);
|
|
174
|
+
relations.push(...transformedDrizzleRelations);
|
|
175
|
+
const isCyclicRelations = relations.map((relI) => {
|
|
176
|
+
// if (relations.some((relj) => relI.table === relj.refTable && relI.refTable === relj.table)) {
|
|
177
|
+
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
178
|
+
if (isRelationCyclic(relI)) {
|
|
179
|
+
tableRel['isCyclic'] = true;
|
|
180
|
+
return { ...relI, isCyclic: true };
|
|
181
|
+
}
|
|
182
|
+
tableRel['isCyclic'] = false;
|
|
183
|
+
return { ...relI, isCyclic: false };
|
|
184
|
+
});
|
|
185
|
+
return { tables, relations: isCyclicRelations, tableRelations };
|
|
186
|
+
};
|
|
187
|
+
|
|
12
188
|
/**
|
|
13
189
|
* The original source for the Adjectives data was taken from https://www.kaggle.com/datasets/jordansiem/adjectives-list
|
|
14
190
|
*/
|
|
@@ -132231,6 +132407,8 @@ class GenerateString extends AbstractGenerator {
|
|
|
132231
132407
|
[idx, this.state.rng] = prand.uniformIntDistribution(0, stringChars.length - 1, this.state.rng);
|
|
132232
132408
|
currStr += stringChars[idx];
|
|
132233
132409
|
}
|
|
132410
|
+
if (this.dataType === 'object')
|
|
132411
|
+
return Buffer.from(currStr);
|
|
132234
132412
|
return currStr;
|
|
132235
132413
|
}
|
|
132236
132414
|
}
|
|
@@ -132259,7 +132437,10 @@ class GenerateUniqueString extends AbstractGenerator {
|
|
|
132259
132437
|
[idx, this.state.rng] = prand.uniformIntDistribution(0, stringChars.length - 1, this.state.rng);
|
|
132260
132438
|
currStr += stringChars[idx];
|
|
132261
132439
|
}
|
|
132262
|
-
|
|
132440
|
+
currStr = currStr.slice(0, 4) + uniqueStr + currStr.slice(4);
|
|
132441
|
+
if (this.dataType === 'object')
|
|
132442
|
+
return Buffer.from(currStr);
|
|
132443
|
+
return currStr;
|
|
132263
132444
|
}
|
|
132264
132445
|
}
|
|
132265
132446
|
class GenerateUUID extends AbstractGenerator {
|
|
@@ -133156,7 +133337,7 @@ class GeneratePoint extends AbstractGenerator {
|
|
|
133156
133337
|
}
|
|
133157
133338
|
const x = this.state.xCoordinateGen.generate();
|
|
133158
133339
|
const y = this.state.yCoordinateGen.generate();
|
|
133159
|
-
if (this.dataType === '
|
|
133340
|
+
if (this.dataType === 'object') {
|
|
133160
133341
|
return { x, y };
|
|
133161
133342
|
}
|
|
133162
133343
|
else if (this.dataType === 'string') {
|
|
@@ -133194,7 +133375,7 @@ class GenerateUniquePoint extends AbstractGenerator {
|
|
|
133194
133375
|
}
|
|
133195
133376
|
const x = this.state.xCoordinateGen.generate();
|
|
133196
133377
|
const y = this.state.yCoordinateGen.generate();
|
|
133197
|
-
if (this.dataType === '
|
|
133378
|
+
if (this.dataType === 'object') {
|
|
133198
133379
|
return { x, y };
|
|
133199
133380
|
}
|
|
133200
133381
|
else if (this.dataType === 'string') {
|
|
@@ -133243,7 +133424,7 @@ class GenerateLine extends AbstractGenerator {
|
|
|
133243
133424
|
b = this.state.bCoefficientGen.generate();
|
|
133244
133425
|
}
|
|
133245
133426
|
const c = this.state.cCoefficientGen.generate();
|
|
133246
|
-
if (this.dataType === '
|
|
133427
|
+
if (this.dataType === 'object') {
|
|
133247
133428
|
return { a, b, c };
|
|
133248
133429
|
}
|
|
133249
133430
|
else if (this.dataType === 'string') {
|
|
@@ -133292,7 +133473,7 @@ class GenerateUniqueLine extends AbstractGenerator {
|
|
|
133292
133473
|
b = this.state.bCoefficientGen.generate();
|
|
133293
133474
|
}
|
|
133294
133475
|
const c = this.state.cCoefficientGen.generate();
|
|
133295
|
-
if (this.dataType === '
|
|
133476
|
+
if (this.dataType === 'object') {
|
|
133296
133477
|
return { a, b, c };
|
|
133297
133478
|
}
|
|
133298
133479
|
else if (this.dataType === 'string') {
|
|
@@ -133804,7 +133985,7 @@ class GenerateStringV2 extends AbstractGenerator {
|
|
|
133804
133985
|
[idx, this.state.rng] = prand.uniformIntDistribution(0, stringChars.length - 1, this.state.rng);
|
|
133805
133986
|
currStr += stringChars[idx];
|
|
133806
133987
|
}
|
|
133807
|
-
if (this.dataType === '
|
|
133988
|
+
if (this.dataType === 'object')
|
|
133808
133989
|
return Buffer.from(currStr);
|
|
133809
133990
|
return currStr;
|
|
133810
133991
|
}
|
|
@@ -133844,6 +134025,8 @@ class GenerateUniqueStringV2 extends AbstractGenerator {
|
|
|
133844
134025
|
[idx, this.state.rng] = prand.uniformIntDistribution(0, stringChars.length - 1, this.state.rng);
|
|
133845
134026
|
currStr += stringChars[idx];
|
|
133846
134027
|
}
|
|
134028
|
+
if (this.dataType === 'object')
|
|
134029
|
+
return Buffer.from(uniqueStr + currStr);
|
|
133847
134030
|
return uniqueStr + currStr;
|
|
133848
134031
|
}
|
|
133849
134032
|
}
|
|
@@ -134976,8 +135159,8 @@ const selectGeneratorForCockroachColumn = (table, col) => {
|
|
|
134976
135159
|
const generator = new generatorsMap.GenerateUUID[0]();
|
|
134977
135160
|
return generator;
|
|
134978
135161
|
}
|
|
134979
|
-
//
|
|
134980
|
-
if (col.columnType === '
|
|
135162
|
+
// BOOL
|
|
135163
|
+
if (col.columnType === 'bool') {
|
|
134981
135164
|
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
134982
135165
|
return generator;
|
|
134983
135166
|
}
|
|
@@ -135805,7 +135988,7 @@ const selectGeneratorForSqlite = (table, col) => {
|
|
|
135805
135988
|
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135806
135989
|
return generator;
|
|
135807
135990
|
}
|
|
135808
|
-
if ((col.columnType === 'integer' && col.dataType === '
|
|
135991
|
+
if ((col.columnType === 'integer' && col.dataType === 'object')) {
|
|
135809
135992
|
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135810
135993
|
return generator;
|
|
135811
135994
|
}
|
|
@@ -135876,49 +136059,6 @@ const selectGeneratorForSqlite = (table, col) => {
|
|
|
135876
136059
|
return generator;
|
|
135877
136060
|
};
|
|
135878
136061
|
|
|
135879
|
-
const isRelationCyclic = (startRel) => {
|
|
135880
|
-
// self relation
|
|
135881
|
-
if (startRel.table === startRel.refTable)
|
|
135882
|
-
return false;
|
|
135883
|
-
// DFS
|
|
135884
|
-
const targetTable = startRel.table;
|
|
135885
|
-
const queue = [startRel];
|
|
135886
|
-
let path = [];
|
|
135887
|
-
while (queue.length !== 0) {
|
|
135888
|
-
const currRel = queue.shift();
|
|
135889
|
-
if (path.includes(currRel.table)) {
|
|
135890
|
-
const idx = path.indexOf(currRel.table);
|
|
135891
|
-
path = path.slice(0, idx);
|
|
135892
|
-
}
|
|
135893
|
-
path.push(currRel.table);
|
|
135894
|
-
for (const rel of currRel.refTableRels) {
|
|
135895
|
-
// self relation
|
|
135896
|
-
if (rel.table === rel.refTable)
|
|
135897
|
-
continue;
|
|
135898
|
-
if (rel.refTable === targetTable)
|
|
135899
|
-
return true;
|
|
135900
|
-
// found cycle, but not the one we are looking for
|
|
135901
|
-
if (path.includes(rel.refTable))
|
|
135902
|
-
continue;
|
|
135903
|
-
queue.unshift(rel);
|
|
135904
|
-
}
|
|
135905
|
-
}
|
|
135906
|
-
return false;
|
|
135907
|
-
};
|
|
135908
|
-
const generateHashFromString = (s) => {
|
|
135909
|
-
let hash = 0;
|
|
135910
|
-
// p and m are prime numbers
|
|
135911
|
-
const p = 53;
|
|
135912
|
-
const m = 28871271685163;
|
|
135913
|
-
for (let i = 0; i < s.length; i++) {
|
|
135914
|
-
hash += ((s.codePointAt(i) || 0) * Math.pow(p, i)) % m;
|
|
135915
|
-
}
|
|
135916
|
-
return hash;
|
|
135917
|
-
};
|
|
135918
|
-
const equalSets = (set1, set2) => {
|
|
135919
|
-
return set1.size === set2.size && [...set1].every((si) => set2.has(si));
|
|
135920
|
-
};
|
|
135921
|
-
|
|
135922
136062
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
135923
136063
|
class SeedService {
|
|
135924
136064
|
static entityKind = 'SeedService';
|
|
@@ -136620,14 +136760,14 @@ const resetCockroach = async (db, cockroachTables) => {
|
|
|
136620
136760
|
await db.execute(drizzleOrm.sql.raw(`truncate ${tablesToTruncate.join(',')} cascade;`));
|
|
136621
136761
|
};
|
|
136622
136762
|
const filterCockroachSchema = (schema) => {
|
|
136623
|
-
const cockroachSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], cockroachCore.CockroachTable) || drizzleOrm.is(keyValue[1],
|
|
136763
|
+
const cockroachSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], cockroachCore.CockroachTable) || drizzleOrm.is(keyValue[1], _relations.Relations)));
|
|
136624
136764
|
const cockroachTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], cockroachCore.CockroachTable)));
|
|
136625
136765
|
return { cockroachSchema, cockroachTables };
|
|
136626
136766
|
};
|
|
136627
136767
|
const seedCockroach = async (db, schema, options = {}, refinements) => {
|
|
136628
136768
|
const seedService = new SeedService();
|
|
136629
136769
|
const { cockroachSchema, cockroachTables } = filterCockroachSchema(schema);
|
|
136630
|
-
const { tables, relations } =
|
|
136770
|
+
const { tables, relations } = getSchemaInfo(cockroachSchema, cockroachTables, mapCockroachTable);
|
|
136631
136771
|
const generatedTablesGenerators = seedService.generatePossibleGenerators('cockroach', tables, relations, refinements, options);
|
|
136632
136772
|
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
136633
136773
|
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, cockroachTables, { ...options, preserveCyclicTablesData });
|
|
@@ -136635,197 +136775,82 @@ const seedCockroach = async (db, schema, options = {}, refinements) => {
|
|
|
136635
136775
|
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
136636
136776
|
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, cockroachTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
136637
136777
|
};
|
|
136638
|
-
const
|
|
136639
|
-
|
|
136640
|
-
|
|
136641
|
-
|
|
136642
|
-
|
|
136643
|
-
|
|
136644
|
-
|
|
136645
|
-
|
|
136646
|
-
|
|
136647
|
-
|
|
136648
|
-
|
|
136649
|
-
|
|
136650
|
-
|
|
136651
|
-
|
|
136652
|
-
|
|
136653
|
-
|
|
136654
|
-
|
|
136655
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
136656
|
-
}
|
|
136657
|
-
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
136658
|
-
return dbToTsColumnNamesMap;
|
|
136778
|
+
const mapCockroachTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
|
|
136779
|
+
const getAllBaseColumns = (baseColumn) => {
|
|
136780
|
+
const baseColumnResult = {
|
|
136781
|
+
name: baseColumn.name,
|
|
136782
|
+
columnType: baseColumn.getSQLType(),
|
|
136783
|
+
typeParams: getTypeParams(baseColumn.getSQLType()),
|
|
136784
|
+
dataType: baseColumn.dataType.split(' ')[0],
|
|
136785
|
+
size: baseColumn.length,
|
|
136786
|
+
hasDefault: baseColumn.hasDefault,
|
|
136787
|
+
enumValues: baseColumn.enumValues,
|
|
136788
|
+
default: baseColumn.default,
|
|
136789
|
+
isUnique: baseColumn.isUnique,
|
|
136790
|
+
notNull: baseColumn.notNull,
|
|
136791
|
+
primary: baseColumn.primary,
|
|
136792
|
+
baseColumn: baseColumn.baseColumn === undefined ? undefined : getAllBaseColumns(baseColumn.baseColumn),
|
|
136793
|
+
};
|
|
136794
|
+
return baseColumnResult;
|
|
136659
136795
|
};
|
|
136660
|
-
const
|
|
136661
|
-
|
|
136662
|
-
const
|
|
136663
|
-
|
|
136664
|
-
|
|
136665
|
-
|
|
136666
|
-
|
|
136667
|
-
|
|
136668
|
-
continue;
|
|
136669
|
-
const tableConfig = cockroachCore.getTableConfig(drizzleRel.sourceTable);
|
|
136670
|
-
const tableDbSchema = tableConfig.schema ?? 'public';
|
|
136671
|
-
const tableDbName = tableConfig.name;
|
|
136672
|
-
const tableTsName = schemaConfig.tableNamesMap[`${tableDbSchema}.${tableDbName}`] ?? tableDbName;
|
|
136673
|
-
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
136674
|
-
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
136675
|
-
?? [];
|
|
136676
|
-
const refTableConfig = cockroachCore.getTableConfig(drizzleRel.referencedTable);
|
|
136677
|
-
const refTableDbSchema = refTableConfig.schema ?? 'public';
|
|
136678
|
-
const refTableDbName = refTableConfig.name;
|
|
136679
|
-
const refTableTsName = schemaConfig.tableNamesMap[`${refTableDbSchema}.${refTableDbName}`]
|
|
136680
|
-
?? refTableDbName;
|
|
136681
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
136682
|
-
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
136683
|
-
?? [];
|
|
136684
|
-
if (tableRelations[refTableTsName] === undefined) {
|
|
136685
|
-
tableRelations[refTableTsName] = [];
|
|
136686
|
-
}
|
|
136687
|
-
const relation = {
|
|
136688
|
-
table: tableTsName,
|
|
136689
|
-
columns,
|
|
136690
|
-
refTable: refTableTsName,
|
|
136691
|
-
refColumns,
|
|
136692
|
-
refTableRels: tableRelations[refTableTsName],
|
|
136693
|
-
type: 'one',
|
|
136694
|
-
};
|
|
136695
|
-
// do not add duplicate relation
|
|
136696
|
-
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
136697
|
-
&& rel.refTable === relation.refTable)) {
|
|
136698
|
-
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
136699
|
-
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
136700
|
-
+ `In this case, the foreign key constraint will be used.\n`);
|
|
136701
|
-
continue;
|
|
136702
|
-
}
|
|
136703
|
-
relations.push(relation);
|
|
136704
|
-
tableRelations[tableTsName].push(relation);
|
|
136796
|
+
const getTypeParams = (sqlType) => {
|
|
136797
|
+
// get type params
|
|
136798
|
+
const typeParams = {};
|
|
136799
|
+
// handle dimensions
|
|
136800
|
+
if (sqlType.includes('[')) {
|
|
136801
|
+
const match = sqlType.match(/\[\w*]/g);
|
|
136802
|
+
if (match) {
|
|
136803
|
+
typeParams['dimensions'] = match.length;
|
|
136705
136804
|
}
|
|
136706
136805
|
}
|
|
136707
|
-
|
|
136708
|
-
|
|
136709
|
-
|
|
136710
|
-
|
|
136711
|
-
|
|
136712
|
-
|
|
136713
|
-
|
|
136714
|
-
|
|
136715
|
-
// might be empty list
|
|
136716
|
-
const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
136717
|
-
const table = dbToTsTableNamesMap[tableConfig.name];
|
|
136718
|
-
const refTable = dbToTsTableNamesMap[drizzleOrm.getTableName(fk.reference().foreignTable)];
|
|
136719
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(fk.reference().foreignTable);
|
|
136720
|
-
if (tableRelations[refTable] === undefined) {
|
|
136721
|
-
tableRelations[refTable] = [];
|
|
136806
|
+
if (sqlType.startsWith('numeric')
|
|
136807
|
+
|| sqlType.startsWith('decimal')
|
|
136808
|
+
|| sqlType.startsWith('double precision')
|
|
136809
|
+
|| sqlType.startsWith('real')) {
|
|
136810
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
136811
|
+
if (match) {
|
|
136812
|
+
typeParams['precision'] = Number(match[1]);
|
|
136813
|
+
typeParams['scale'] = Number(match[2]);
|
|
136722
136814
|
}
|
|
136723
|
-
return {
|
|
136724
|
-
table,
|
|
136725
|
-
columns: fk
|
|
136726
|
-
.reference()
|
|
136727
|
-
.columns.map((col) => dbToTsColumnNamesMap[col.name]),
|
|
136728
|
-
refTable,
|
|
136729
|
-
refColumns: fk
|
|
136730
|
-
.reference()
|
|
136731
|
-
.foreignColumns.map((fCol) => dbToTsColumnNamesMapForRefTable[fCol.name]),
|
|
136732
|
-
refTableRels: tableRelations[refTable],
|
|
136733
|
-
};
|
|
136734
|
-
});
|
|
136735
|
-
relations.push(...newRelations);
|
|
136736
|
-
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
136737
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
136738
136815
|
}
|
|
136739
|
-
|
|
136740
|
-
|
|
136741
|
-
|
|
136742
|
-
|
|
136743
|
-
|
|
136744
|
-
|
|
136745
|
-
|
|
136746
|
-
|
|
136747
|
-
|
|
136748
|
-
|
|
136749
|
-
default: baseColumn.default,
|
|
136750
|
-
isUnique: baseColumn.isUnique,
|
|
136751
|
-
notNull: baseColumn.notNull,
|
|
136752
|
-
primary: baseColumn.primary,
|
|
136753
|
-
baseColumn: baseColumn.baseColumn === undefined ? undefined : getAllBaseColumns(baseColumn.baseColumn),
|
|
136754
|
-
};
|
|
136755
|
-
return baseColumnResult;
|
|
136756
|
-
};
|
|
136757
|
-
const getTypeParams = (sqlType) => {
|
|
136758
|
-
// get type params
|
|
136759
|
-
const typeParams = {};
|
|
136760
|
-
// handle dimensions
|
|
136761
|
-
if (sqlType.includes('[')) {
|
|
136762
|
-
const match = sqlType.match(/\[\w*]/g);
|
|
136763
|
-
if (match) {
|
|
136764
|
-
typeParams['dimensions'] = match.length;
|
|
136765
|
-
}
|
|
136766
|
-
}
|
|
136767
|
-
if (sqlType.startsWith('numeric')
|
|
136768
|
-
|| sqlType.startsWith('decimal')
|
|
136769
|
-
|| sqlType.startsWith('double precision')
|
|
136770
|
-
|| sqlType.startsWith('real')) {
|
|
136771
|
-
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
136772
|
-
if (match) {
|
|
136773
|
-
typeParams['precision'] = Number(match[1]);
|
|
136774
|
-
typeParams['scale'] = Number(match[2]);
|
|
136775
|
-
}
|
|
136816
|
+
else if (sqlType.startsWith('varchar')
|
|
136817
|
+
|| sqlType.startsWith('char')
|
|
136818
|
+
|| sqlType.startsWith('bit')
|
|
136819
|
+
|| sqlType.startsWith('vector')
|
|
136820
|
+
|| sqlType.startsWith('time')
|
|
136821
|
+
|| sqlType.startsWith('timestamp')
|
|
136822
|
+
|| sqlType.startsWith('interval')) {
|
|
136823
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
136824
|
+
if (match) {
|
|
136825
|
+
typeParams['length'] = Number(match[1]);
|
|
136776
136826
|
}
|
|
136777
|
-
else if (sqlType.startsWith('varchar')
|
|
136778
|
-
|| sqlType.startsWith('char')
|
|
136779
|
-
|| sqlType.startsWith('bit')
|
|
136780
|
-
|| sqlType.startsWith('vector')
|
|
136781
|
-
|| sqlType.startsWith('time')
|
|
136782
|
-
|| sqlType.startsWith('timestamp')
|
|
136783
|
-
|| sqlType.startsWith('interval')) {
|
|
136784
|
-
const match = sqlType.match(/\((\d+)\)/);
|
|
136785
|
-
if (match) {
|
|
136786
|
-
typeParams['length'] = Number(match[1]);
|
|
136787
|
-
}
|
|
136788
|
-
}
|
|
136789
|
-
return typeParams;
|
|
136790
|
-
};
|
|
136791
|
-
// console.log(tableConfig.columns);
|
|
136792
|
-
tables.push({
|
|
136793
|
-
name: dbToTsTableNamesMap[tableConfig.name],
|
|
136794
|
-
columns: tableConfig.columns.map((column) => ({
|
|
136795
|
-
name: dbToTsColumnNamesMap[column.name],
|
|
136796
|
-
columnType: column.getSQLType(),
|
|
136797
|
-
typeParams: getTypeParams(column.getSQLType()),
|
|
136798
|
-
dataType: column.dataType,
|
|
136799
|
-
size: column.size,
|
|
136800
|
-
hasDefault: column.hasDefault,
|
|
136801
|
-
default: column.default,
|
|
136802
|
-
enumValues: column.enumValues,
|
|
136803
|
-
isUnique: column.isUnique,
|
|
136804
|
-
notNull: column.notNull,
|
|
136805
|
-
primary: column.primary,
|
|
136806
|
-
generatedIdentityType: column.generatedIdentity?.type,
|
|
136807
|
-
baseColumn: (column.baseColumn === undefined)
|
|
136808
|
-
? undefined
|
|
136809
|
-
: getAllBaseColumns(column.baseColumn),
|
|
136810
|
-
})),
|
|
136811
|
-
primaryKeys: tableConfig.columns
|
|
136812
|
-
.filter((column) => column.primary)
|
|
136813
|
-
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
136814
|
-
});
|
|
136815
|
-
}
|
|
136816
|
-
const transformedDrizzleRelations = transformFromDrizzleRelation(cockroachSchema, getDbToTsColumnNamesMap, tableRelations);
|
|
136817
|
-
relations.push(...transformedDrizzleRelations);
|
|
136818
|
-
const isCyclicRelations = relations.map((relI) => {
|
|
136819
|
-
// if (relations.some((relj) => relI.table === relj.refTable && relI.refTable === relj.table)) {
|
|
136820
|
-
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
136821
|
-
if (isRelationCyclic(relI)) {
|
|
136822
|
-
tableRel['isCyclic'] = true;
|
|
136823
|
-
return { ...relI, isCyclic: true };
|
|
136824
136827
|
}
|
|
136825
|
-
|
|
136826
|
-
|
|
136827
|
-
|
|
136828
|
-
return {
|
|
136828
|
+
return typeParams;
|
|
136829
|
+
};
|
|
136830
|
+
// console.log(tableConfig.columns);
|
|
136831
|
+
return {
|
|
136832
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
136833
|
+
columns: tableConfig.columns.map((column) => ({
|
|
136834
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
136835
|
+
columnType: column.getSQLType(),
|
|
136836
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
136837
|
+
dataType: column.dataType.split(' ')[0],
|
|
136838
|
+
size: column.length,
|
|
136839
|
+
hasDefault: column.hasDefault,
|
|
136840
|
+
default: column.default,
|
|
136841
|
+
enumValues: column.enumValues,
|
|
136842
|
+
isUnique: column.isUnique,
|
|
136843
|
+
notNull: column.notNull,
|
|
136844
|
+
primary: column.primary,
|
|
136845
|
+
generatedIdentityType: column.generatedIdentity?.type,
|
|
136846
|
+
baseColumn: (column.baseColumn === undefined)
|
|
136847
|
+
? undefined
|
|
136848
|
+
: getAllBaseColumns(column.baseColumn),
|
|
136849
|
+
})),
|
|
136850
|
+
primaryKeys: tableConfig.columns
|
|
136851
|
+
.filter((column) => column.primary)
|
|
136852
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
136853
|
+
};
|
|
136829
136854
|
};
|
|
136830
136855
|
|
|
136831
136856
|
// MySql-----------------------------------------------------------------------------------------------------
|
|
@@ -136908,13 +136933,13 @@ const resetMsSql = async (db, schema) => {
|
|
|
136908
136933
|
}
|
|
136909
136934
|
};
|
|
136910
136935
|
const filterMsSqlTables = (schema) => {
|
|
136911
|
-
const mssqlSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mssqlCore.MsSqlTable) || drizzleOrm.is(keyValue[1],
|
|
136936
|
+
const mssqlSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mssqlCore.MsSqlTable) || drizzleOrm.is(keyValue[1], _relations.Relations)));
|
|
136912
136937
|
const mssqlTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mssqlCore.MsSqlTable)));
|
|
136913
136938
|
return { mssqlSchema, mssqlTables };
|
|
136914
136939
|
};
|
|
136915
136940
|
const seedMsSql = async (db, schema, options = {}, refinements) => {
|
|
136916
136941
|
const { mssqlSchema, mssqlTables } = filterMsSqlTables(schema);
|
|
136917
|
-
const { tables, relations } =
|
|
136942
|
+
const { tables, relations } = getSchemaInfo(mssqlSchema, mssqlTables, mapMsSqlTable);
|
|
136918
136943
|
const seedService = new SeedService();
|
|
136919
136944
|
const generatedTablesGenerators = seedService.generatePossibleGenerators('mssql', tables, relations, refinements, options);
|
|
136920
136945
|
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
@@ -136923,163 +136948,50 @@ const seedMsSql = async (db, schema, options = {}, refinements) => {
|
|
|
136923
136948
|
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
136924
136949
|
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, mssqlTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
136925
136950
|
};
|
|
136926
|
-
const
|
|
136927
|
-
|
|
136928
|
-
|
|
136929
|
-
|
|
136930
|
-
|
|
136931
|
-
|
|
136932
|
-
|
|
136933
|
-
|
|
136934
|
-
|
|
136935
|
-
|
|
136936
|
-
|
|
136937
|
-
|
|
136938
|
-
dbToTsColumnNamesMap = dbToTsColumnNamesMapGlobal[tableName];
|
|
136939
|
-
return dbToTsColumnNamesMap;
|
|
136940
|
-
}
|
|
136941
|
-
const tableConfig = mssqlCore.getTableConfig(table);
|
|
136942
|
-
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
136943
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
136944
|
-
}
|
|
136945
|
-
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
136946
|
-
return dbToTsColumnNamesMap;
|
|
136947
|
-
};
|
|
136948
|
-
const transformFromDrizzleRelation = (schema, getDbToTsColumnNamesMap, tableRelations) => {
|
|
136949
|
-
const schemaConfig = drizzleOrm.extractTablesRelationalConfig(schema, drizzleOrm.createTableRelationsHelpers);
|
|
136950
|
-
const relations = [];
|
|
136951
|
-
for (const table of Object.values(schemaConfig.tables)) {
|
|
136952
|
-
if (table.relations === undefined)
|
|
136953
|
-
continue;
|
|
136954
|
-
for (const drizzleRel of Object.values(table.relations)) {
|
|
136955
|
-
if (!drizzleOrm.is(drizzleRel, drizzleOrm.One))
|
|
136956
|
-
continue;
|
|
136957
|
-
const tableConfig = mssqlCore.getTableConfig(drizzleRel.sourceTable);
|
|
136958
|
-
const tableDbSchema = tableConfig.schema ?? 'public';
|
|
136959
|
-
const tableDbName = tableConfig.name;
|
|
136960
|
-
const tableTsName = schemaConfig.tableNamesMap[`${tableDbSchema}.${tableDbName}`] ?? tableDbName;
|
|
136961
|
-
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
136962
|
-
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
136963
|
-
?? [];
|
|
136964
|
-
const refTableConfig = mssqlCore.getTableConfig(drizzleRel.referencedTable);
|
|
136965
|
-
const refTableDbSchema = refTableConfig.schema ?? 'public';
|
|
136966
|
-
const refTableDbName = refTableConfig.name;
|
|
136967
|
-
const refTableTsName = schemaConfig.tableNamesMap[`${refTableDbSchema}.${refTableDbName}`]
|
|
136968
|
-
?? refTableDbName;
|
|
136969
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
136970
|
-
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
136971
|
-
?? [];
|
|
136972
|
-
if (tableRelations[refTableTsName] === undefined) {
|
|
136973
|
-
tableRelations[refTableTsName] = [];
|
|
136974
|
-
}
|
|
136975
|
-
const relation = {
|
|
136976
|
-
table: tableTsName,
|
|
136977
|
-
columns,
|
|
136978
|
-
refTable: refTableTsName,
|
|
136979
|
-
refColumns,
|
|
136980
|
-
refTableRels: tableRelations[refTableTsName],
|
|
136981
|
-
type: 'one',
|
|
136982
|
-
};
|
|
136983
|
-
// do not add duplicate relation
|
|
136984
|
-
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
136985
|
-
&& rel.refTable === relation.refTable)) {
|
|
136986
|
-
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
136987
|
-
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
136988
|
-
+ `In this case, the foreign key constraint will be used.\n`);
|
|
136989
|
-
continue;
|
|
136990
|
-
}
|
|
136991
|
-
relations.push(relation);
|
|
136992
|
-
tableRelations[tableTsName].push(relation);
|
|
136993
|
-
}
|
|
136994
|
-
}
|
|
136995
|
-
return relations;
|
|
136996
|
-
};
|
|
136997
|
-
for (const table of Object.values(mssqlTables)) {
|
|
136998
|
-
tableConfig = mssqlCore.getTableConfig(table);
|
|
136999
|
-
dbToTsColumnNamesMap = {};
|
|
137000
|
-
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
137001
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
137002
|
-
}
|
|
137003
|
-
const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
137004
|
-
const table = dbToTsTableNamesMap[tableConfig.name];
|
|
137005
|
-
const refTable = dbToTsTableNamesMap[drizzleOrm.getTableName(fk.reference().foreignTable)];
|
|
137006
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(fk.reference().foreignTable);
|
|
137007
|
-
if (tableRelations[refTable] === undefined) {
|
|
137008
|
-
tableRelations[refTable] = [];
|
|
136951
|
+
const mapMsSqlTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
|
|
136952
|
+
// TODO: rewrite
|
|
136953
|
+
const getTypeParams = (sqlType) => {
|
|
136954
|
+
// get type params and set only type
|
|
136955
|
+
const typeParams = {};
|
|
136956
|
+
if (sqlType.startsWith('decimal')
|
|
136957
|
+
|| sqlType.startsWith('real')
|
|
136958
|
+
|| sqlType.startsWith('float')) {
|
|
136959
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
136960
|
+
if (match) {
|
|
136961
|
+
typeParams['precision'] = Number(match[1]);
|
|
136962
|
+
typeParams['scale'] = Number(match[2]);
|
|
137009
136963
|
}
|
|
137010
|
-
return {
|
|
137011
|
-
table,
|
|
137012
|
-
columns: fk
|
|
137013
|
-
.reference()
|
|
137014
|
-
.columns.map((col) => dbToTsColumnNamesMap[col.name]),
|
|
137015
|
-
refTable,
|
|
137016
|
-
refColumns: fk
|
|
137017
|
-
.reference()
|
|
137018
|
-
.foreignColumns.map((fCol) => dbToTsColumnNamesMapForRefTable[fCol.name]),
|
|
137019
|
-
refTableRels: tableRelations[refTable],
|
|
137020
|
-
};
|
|
137021
|
-
});
|
|
137022
|
-
relations.push(...newRelations);
|
|
137023
|
-
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
137024
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
137025
136964
|
}
|
|
137026
|
-
|
|
137027
|
-
|
|
137028
|
-
|
|
137029
|
-
|
|
137030
|
-
const
|
|
137031
|
-
if (
|
|
137032
|
-
|
|
137033
|
-
|| sqlType.startsWith('float')) {
|
|
137034
|
-
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137035
|
-
if (match) {
|
|
137036
|
-
typeParams['precision'] = Number(match[1]);
|
|
137037
|
-
typeParams['scale'] = Number(match[2]);
|
|
137038
|
-
}
|
|
137039
|
-
}
|
|
137040
|
-
else if (sqlType.startsWith('char')
|
|
137041
|
-
|| sqlType.startsWith('varchar')
|
|
137042
|
-
|| sqlType.startsWith('binary')
|
|
137043
|
-
|| sqlType.startsWith('varbinary')) {
|
|
137044
|
-
const match = sqlType.match(/\((\d+)\)/);
|
|
137045
|
-
if (match) {
|
|
137046
|
-
typeParams['length'] = Number(match[1]);
|
|
137047
|
-
}
|
|
136965
|
+
else if (sqlType.startsWith('char')
|
|
136966
|
+
|| sqlType.startsWith('varchar')
|
|
136967
|
+
|| sqlType.startsWith('binary')
|
|
136968
|
+
|| sqlType.startsWith('varbinary')) {
|
|
136969
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
136970
|
+
if (match) {
|
|
136971
|
+
typeParams['length'] = Number(match[1]);
|
|
137048
136972
|
}
|
|
137049
|
-
return typeParams;
|
|
137050
|
-
};
|
|
137051
|
-
tables.push({
|
|
137052
|
-
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137053
|
-
columns: tableConfig.columns.map((column) => ({
|
|
137054
|
-
name: dbToTsColumnNamesMap[column.name],
|
|
137055
|
-
columnType: column.getSQLType(),
|
|
137056
|
-
typeParams: getTypeParams(column.getSQLType()),
|
|
137057
|
-
dataType: column.dataType,
|
|
137058
|
-
hasDefault: column.hasDefault,
|
|
137059
|
-
default: column.default,
|
|
137060
|
-
enumValues: column.enumValues,
|
|
137061
|
-
isUnique: column.isUnique,
|
|
137062
|
-
notNull: column.notNull,
|
|
137063
|
-
primary: column.primary,
|
|
137064
|
-
identity: column.identity ? true : false,
|
|
137065
|
-
})),
|
|
137066
|
-
primaryKeys: tableConfig.columns
|
|
137067
|
-
.filter((column) => column.primary)
|
|
137068
|
-
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137069
|
-
});
|
|
137070
|
-
}
|
|
137071
|
-
const transformedDrizzleRelations = transformFromDrizzleRelation(mssqlSchema, getDbToTsColumnNamesMap, tableRelations);
|
|
137072
|
-
relations.push(...transformedDrizzleRelations);
|
|
137073
|
-
const modifiedRelations = relations.map((relI) => {
|
|
137074
|
-
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
137075
|
-
if (isRelationCyclic(relI)) {
|
|
137076
|
-
tableRel['isCyclic'] = true;
|
|
137077
|
-
return { ...relI, isCyclic: true };
|
|
137078
136973
|
}
|
|
137079
|
-
|
|
137080
|
-
|
|
137081
|
-
|
|
137082
|
-
|
|
136974
|
+
return typeParams;
|
|
136975
|
+
};
|
|
136976
|
+
return {
|
|
136977
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
136978
|
+
columns: tableConfig.columns.map((column) => ({
|
|
136979
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
136980
|
+
columnType: column.getSQLType(),
|
|
136981
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
136982
|
+
dataType: column.dataType.split(' ')[0],
|
|
136983
|
+
hasDefault: column.hasDefault,
|
|
136984
|
+
default: column.default,
|
|
136985
|
+
enumValues: column.enumValues,
|
|
136986
|
+
isUnique: column.isUnique,
|
|
136987
|
+
notNull: column.notNull,
|
|
136988
|
+
primary: column.primary,
|
|
136989
|
+
identity: column.identity ? true : false,
|
|
136990
|
+
})),
|
|
136991
|
+
primaryKeys: tableConfig.columns
|
|
136992
|
+
.filter((column) => column.primary)
|
|
136993
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
136994
|
+
};
|
|
137083
136995
|
};
|
|
137084
136996
|
|
|
137085
136997
|
// MySql-----------------------------------------------------------------------------------------------------
|
|
@@ -137096,13 +137008,13 @@ const resetMySql = async (db, schema) => {
|
|
|
137096
137008
|
await db.execute(drizzleOrm.sql.raw('SET FOREIGN_KEY_CHECKS = 1;'));
|
|
137097
137009
|
};
|
|
137098
137010
|
const filterMysqlTables = (schema) => {
|
|
137099
|
-
const mysqlSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mysqlCore.MySqlTable) || drizzleOrm.is(keyValue[1],
|
|
137011
|
+
const mysqlSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mysqlCore.MySqlTable) || drizzleOrm.is(keyValue[1], _relations.Relations)));
|
|
137100
137012
|
const mysqlTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mysqlCore.MySqlTable)));
|
|
137101
137013
|
return { mysqlSchema, mysqlTables };
|
|
137102
137014
|
};
|
|
137103
137015
|
const seedMySql = async (db, schema, options = {}, refinements) => {
|
|
137104
137016
|
const { mysqlSchema, mysqlTables } = filterMysqlTables(schema);
|
|
137105
|
-
const { tables, relations } =
|
|
137017
|
+
const { tables, relations } = getSchemaInfo(mysqlSchema, mysqlTables, mapMySqlTable);
|
|
137106
137018
|
const seedService = new SeedService();
|
|
137107
137019
|
const generatedTablesGenerators = seedService.generatePossibleGenerators('mysql', tables, relations, refinements, options);
|
|
137108
137020
|
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
@@ -137111,162 +137023,49 @@ const seedMySql = async (db, schema, options = {}, refinements) => {
|
|
|
137111
137023
|
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
137112
137024
|
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, mysqlTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
137113
137025
|
};
|
|
137114
|
-
const
|
|
137115
|
-
|
|
137116
|
-
|
|
137117
|
-
|
|
137118
|
-
|
|
137119
|
-
|
|
137120
|
-
|
|
137121
|
-
|
|
137122
|
-
|
|
137123
|
-
|
|
137124
|
-
|
|
137125
|
-
|
|
137126
|
-
dbToTsColumnNamesMap = dbToTsColumnNamesMapGlobal[tableName];
|
|
137127
|
-
return dbToTsColumnNamesMap;
|
|
137128
|
-
}
|
|
137129
|
-
const tableConfig = mysqlCore.getTableConfig(table);
|
|
137130
|
-
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
137131
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
137132
|
-
}
|
|
137133
|
-
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
137134
|
-
return dbToTsColumnNamesMap;
|
|
137135
|
-
};
|
|
137136
|
-
const transformFromDrizzleRelation = (schema, getDbToTsColumnNamesMap, tableRelations) => {
|
|
137137
|
-
const schemaConfig = drizzleOrm.extractTablesRelationalConfig(schema, drizzleOrm.createTableRelationsHelpers);
|
|
137138
|
-
const relations = [];
|
|
137139
|
-
for (const table of Object.values(schemaConfig.tables)) {
|
|
137140
|
-
if (table.relations === undefined)
|
|
137141
|
-
continue;
|
|
137142
|
-
for (const drizzleRel of Object.values(table.relations)) {
|
|
137143
|
-
if (!drizzleOrm.is(drizzleRel, drizzleOrm.One))
|
|
137144
|
-
continue;
|
|
137145
|
-
const tableConfig = mysqlCore.getTableConfig(drizzleRel.sourceTable);
|
|
137146
|
-
const tableDbSchema = tableConfig.schema ?? 'public';
|
|
137147
|
-
const tableDbName = tableConfig.name;
|
|
137148
|
-
const tableTsName = schemaConfig.tableNamesMap[`${tableDbSchema}.${tableDbName}`] ?? tableDbName;
|
|
137149
|
-
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
137150
|
-
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
137151
|
-
?? [];
|
|
137152
|
-
const refTableConfig = mysqlCore.getTableConfig(drizzleRel.referencedTable);
|
|
137153
|
-
const refTableDbSchema = refTableConfig.schema ?? 'public';
|
|
137154
|
-
const refTableDbName = refTableConfig.name;
|
|
137155
|
-
const refTableTsName = schemaConfig.tableNamesMap[`${refTableDbSchema}.${refTableDbName}`]
|
|
137156
|
-
?? refTableDbName;
|
|
137157
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
137158
|
-
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
137159
|
-
?? [];
|
|
137160
|
-
if (tableRelations[refTableTsName] === undefined) {
|
|
137161
|
-
tableRelations[refTableTsName] = [];
|
|
137162
|
-
}
|
|
137163
|
-
const relation = {
|
|
137164
|
-
table: tableTsName,
|
|
137165
|
-
columns,
|
|
137166
|
-
refTable: refTableTsName,
|
|
137167
|
-
refColumns,
|
|
137168
|
-
refTableRels: tableRelations[refTableTsName],
|
|
137169
|
-
type: 'one',
|
|
137170
|
-
};
|
|
137171
|
-
// do not add duplicate relation
|
|
137172
|
-
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
137173
|
-
&& rel.refTable === relation.refTable)) {
|
|
137174
|
-
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
137175
|
-
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
137176
|
-
+ `In this case, the foreign key constraint will be used.\n`);
|
|
137177
|
-
continue;
|
|
137178
|
-
}
|
|
137179
|
-
relations.push(relation);
|
|
137180
|
-
tableRelations[tableTsName].push(relation);
|
|
137181
|
-
}
|
|
137182
|
-
}
|
|
137183
|
-
return relations;
|
|
137184
|
-
};
|
|
137185
|
-
for (const table of Object.values(mysqlTables)) {
|
|
137186
|
-
tableConfig = mysqlCore.getTableConfig(table);
|
|
137187
|
-
dbToTsColumnNamesMap = {};
|
|
137188
|
-
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
137189
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
137190
|
-
}
|
|
137191
|
-
const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
137192
|
-
const table = dbToTsTableNamesMap[tableConfig.name];
|
|
137193
|
-
const refTable = dbToTsTableNamesMap[drizzleOrm.getTableName(fk.reference().foreignTable)];
|
|
137194
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(fk.reference().foreignTable);
|
|
137195
|
-
if (tableRelations[refTable] === undefined) {
|
|
137196
|
-
tableRelations[refTable] = [];
|
|
137026
|
+
const mapMySqlTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
|
|
137027
|
+
const getTypeParams = (sqlType) => {
|
|
137028
|
+
// get type params and set only type
|
|
137029
|
+
const typeParams = {};
|
|
137030
|
+
if (sqlType.startsWith('decimal')
|
|
137031
|
+
|| sqlType.startsWith('real')
|
|
137032
|
+
|| sqlType.startsWith('double')
|
|
137033
|
+
|| sqlType.startsWith('float')) {
|
|
137034
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137035
|
+
if (match) {
|
|
137036
|
+
typeParams['precision'] = Number(match[1]);
|
|
137037
|
+
typeParams['scale'] = Number(match[2]);
|
|
137197
137038
|
}
|
|
137198
|
-
return {
|
|
137199
|
-
table,
|
|
137200
|
-
columns: fk
|
|
137201
|
-
.reference()
|
|
137202
|
-
.columns.map((col) => dbToTsColumnNamesMap[col.name]),
|
|
137203
|
-
refTable,
|
|
137204
|
-
refColumns: fk
|
|
137205
|
-
.reference()
|
|
137206
|
-
.foreignColumns.map((fCol) => dbToTsColumnNamesMapForRefTable[fCol.name]),
|
|
137207
|
-
refTableRels: tableRelations[refTable],
|
|
137208
|
-
};
|
|
137209
|
-
});
|
|
137210
|
-
relations.push(...newRelations);
|
|
137211
|
-
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
137212
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
137213
137039
|
}
|
|
137214
|
-
|
|
137215
|
-
|
|
137216
|
-
|
|
137217
|
-
|
|
137218
|
-
|
|
137219
|
-
|
|
137220
|
-
|
|
137221
|
-
|| sqlType.startsWith('float')) {
|
|
137222
|
-
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137223
|
-
if (match) {
|
|
137224
|
-
typeParams['precision'] = Number(match[1]);
|
|
137225
|
-
typeParams['scale'] = Number(match[2]);
|
|
137226
|
-
}
|
|
137040
|
+
else if (sqlType.startsWith('char')
|
|
137041
|
+
|| sqlType.startsWith('varchar')
|
|
137042
|
+
|| sqlType.startsWith('binary')
|
|
137043
|
+
|| sqlType.startsWith('varbinary')) {
|
|
137044
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137045
|
+
if (match) {
|
|
137046
|
+
typeParams['length'] = Number(match[1]);
|
|
137227
137047
|
}
|
|
137228
|
-
else if (sqlType.startsWith('char')
|
|
137229
|
-
|| sqlType.startsWith('varchar')
|
|
137230
|
-
|| sqlType.startsWith('binary')
|
|
137231
|
-
|| sqlType.startsWith('varbinary')) {
|
|
137232
|
-
const match = sqlType.match(/\((\d+)\)/);
|
|
137233
|
-
if (match) {
|
|
137234
|
-
typeParams['length'] = Number(match[1]);
|
|
137235
|
-
}
|
|
137236
|
-
}
|
|
137237
|
-
return typeParams;
|
|
137238
|
-
};
|
|
137239
|
-
tables.push({
|
|
137240
|
-
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137241
|
-
columns: tableConfig.columns.map((column) => ({
|
|
137242
|
-
name: dbToTsColumnNamesMap[column.name],
|
|
137243
|
-
columnType: column.getSQLType(),
|
|
137244
|
-
typeParams: getTypeParams(column.getSQLType()),
|
|
137245
|
-
dataType: column.dataType,
|
|
137246
|
-
hasDefault: column.hasDefault,
|
|
137247
|
-
default: column.default,
|
|
137248
|
-
enumValues: column.enumValues,
|
|
137249
|
-
isUnique: column.isUnique,
|
|
137250
|
-
notNull: column.notNull,
|
|
137251
|
-
primary: column.primary,
|
|
137252
|
-
})),
|
|
137253
|
-
primaryKeys: tableConfig.columns
|
|
137254
|
-
.filter((column) => column.primary)
|
|
137255
|
-
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137256
|
-
});
|
|
137257
|
-
}
|
|
137258
|
-
const transformedDrizzleRelations = transformFromDrizzleRelation(mysqlSchema, getDbToTsColumnNamesMap, tableRelations);
|
|
137259
|
-
relations.push(...transformedDrizzleRelations);
|
|
137260
|
-
const isCyclicRelations = relations.map((relI) => {
|
|
137261
|
-
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
137262
|
-
if (isRelationCyclic(relI)) {
|
|
137263
|
-
tableRel['isCyclic'] = true;
|
|
137264
|
-
return { ...relI, isCyclic: true };
|
|
137265
137048
|
}
|
|
137266
|
-
|
|
137267
|
-
|
|
137268
|
-
|
|
137269
|
-
|
|
137049
|
+
return typeParams;
|
|
137050
|
+
};
|
|
137051
|
+
return {
|
|
137052
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137053
|
+
columns: tableConfig.columns.map((column) => ({
|
|
137054
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137055
|
+
columnType: column.getSQLType(),
|
|
137056
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137057
|
+
dataType: column.dataType.split(' ')[0],
|
|
137058
|
+
hasDefault: column.hasDefault,
|
|
137059
|
+
default: column.default,
|
|
137060
|
+
enumValues: column.enumValues,
|
|
137061
|
+
isUnique: column.isUnique,
|
|
137062
|
+
notNull: column.notNull,
|
|
137063
|
+
primary: column.primary,
|
|
137064
|
+
})),
|
|
137065
|
+
primaryKeys: tableConfig.columns
|
|
137066
|
+
.filter((column) => column.primary)
|
|
137067
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137068
|
+
};
|
|
137270
137069
|
};
|
|
137271
137070
|
|
|
137272
137071
|
// Postgres-----------------------------------------------------------------------------------------------------------
|
|
@@ -137279,14 +137078,15 @@ const resetPostgres = async (db, pgTables) => {
|
|
|
137279
137078
|
await db.execute(drizzleOrm.sql.raw(`truncate ${tablesToTruncate.join(',')} cascade;`));
|
|
137280
137079
|
};
|
|
137281
137080
|
const filterPgSchema = (schema) => {
|
|
137282
|
-
const pgSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], pgCore.PgTable) || drizzleOrm.is(keyValue[1],
|
|
137081
|
+
const pgSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], pgCore.PgTable) || drizzleOrm.is(keyValue[1], _relations.Relations)));
|
|
137283
137082
|
const pgTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], pgCore.PgTable)));
|
|
137284
137083
|
return { pgSchema, pgTables };
|
|
137285
137084
|
};
|
|
137286
137085
|
const seedPostgres = async (db, schema, options = {}, refinements) => {
|
|
137287
137086
|
const seedService = new SeedService();
|
|
137288
137087
|
const { pgSchema, pgTables } = filterPgSchema(schema);
|
|
137289
|
-
const { tables, relations } =
|
|
137088
|
+
const { tables, relations } = getSchemaInfo(pgSchema, pgTables, mapPgTable);
|
|
137089
|
+
// const { tables, relations } = getPostgresInfo(pgSchema, pgTables);
|
|
137290
137090
|
const generatedTablesGenerators = seedService.generatePossibleGenerators('postgresql', tables, relations, refinements, options);
|
|
137291
137091
|
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
137292
137092
|
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, pgTables, { ...options, preserveCyclicTablesData });
|
|
@@ -137294,198 +137094,82 @@ const seedPostgres = async (db, schema, options = {}, refinements) => {
|
|
|
137294
137094
|
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
137295
137095
|
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, pgTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
137296
137096
|
};
|
|
137297
|
-
const
|
|
137298
|
-
|
|
137299
|
-
|
|
137300
|
-
|
|
137301
|
-
|
|
137302
|
-
|
|
137303
|
-
|
|
137304
|
-
|
|
137305
|
-
|
|
137306
|
-
|
|
137307
|
-
|
|
137308
|
-
|
|
137309
|
-
|
|
137310
|
-
|
|
137311
|
-
|
|
137312
|
-
|
|
137313
|
-
|
|
137314
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
137315
|
-
}
|
|
137316
|
-
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
137317
|
-
return dbToTsColumnNamesMap;
|
|
137097
|
+
const mapPgTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
|
|
137098
|
+
const getAllBaseColumns = (baseColumn) => {
|
|
137099
|
+
const baseColumnResult = {
|
|
137100
|
+
name: baseColumn.name,
|
|
137101
|
+
columnType: baseColumn.getSQLType(),
|
|
137102
|
+
typeParams: getTypeParams(baseColumn.getSQLType()),
|
|
137103
|
+
dataType: baseColumn.dataType.split(' ')[0],
|
|
137104
|
+
size: baseColumn.length,
|
|
137105
|
+
hasDefault: baseColumn.hasDefault,
|
|
137106
|
+
enumValues: baseColumn.enumValues,
|
|
137107
|
+
default: baseColumn.default,
|
|
137108
|
+
isUnique: baseColumn.isUnique,
|
|
137109
|
+
notNull: baseColumn.notNull,
|
|
137110
|
+
primary: baseColumn.primary,
|
|
137111
|
+
baseColumn: baseColumn.baseColumn === undefined ? undefined : getAllBaseColumns(baseColumn.baseColumn),
|
|
137112
|
+
};
|
|
137113
|
+
return baseColumnResult;
|
|
137318
137114
|
};
|
|
137319
|
-
const
|
|
137320
|
-
|
|
137321
|
-
const
|
|
137322
|
-
|
|
137323
|
-
|
|
137324
|
-
|
|
137325
|
-
|
|
137326
|
-
|
|
137327
|
-
continue;
|
|
137328
|
-
const tableConfig = pgCore.getTableConfig(drizzleRel.sourceTable);
|
|
137329
|
-
const tableDbSchema = tableConfig.schema ?? 'public';
|
|
137330
|
-
const tableDbName = tableConfig.name;
|
|
137331
|
-
const tableTsName = schemaConfig.tableNamesMap[`${tableDbSchema}.${tableDbName}`] ?? tableDbName;
|
|
137332
|
-
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
137333
|
-
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
137334
|
-
?? [];
|
|
137335
|
-
const refTableConfig = pgCore.getTableConfig(drizzleRel.referencedTable);
|
|
137336
|
-
const refTableDbSchema = refTableConfig.schema ?? 'public';
|
|
137337
|
-
const refTableDbName = refTableConfig.name;
|
|
137338
|
-
const refTableTsName = schemaConfig.tableNamesMap[`${refTableDbSchema}.${refTableDbName}`]
|
|
137339
|
-
?? refTableDbName;
|
|
137340
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
137341
|
-
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
137342
|
-
?? [];
|
|
137343
|
-
if (tableRelations[refTableTsName] === undefined) {
|
|
137344
|
-
tableRelations[refTableTsName] = [];
|
|
137345
|
-
}
|
|
137346
|
-
const relation = {
|
|
137347
|
-
table: tableTsName,
|
|
137348
|
-
columns,
|
|
137349
|
-
refTable: refTableTsName,
|
|
137350
|
-
refColumns,
|
|
137351
|
-
refTableRels: tableRelations[refTableTsName],
|
|
137352
|
-
type: 'one',
|
|
137353
|
-
};
|
|
137354
|
-
// do not add duplicate relation
|
|
137355
|
-
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
137356
|
-
&& rel.refTable === relation.refTable)) {
|
|
137357
|
-
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
137358
|
-
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
137359
|
-
+ `In this case, the foreign key constraint will be used.\n`);
|
|
137360
|
-
continue;
|
|
137361
|
-
}
|
|
137362
|
-
relations.push(relation);
|
|
137363
|
-
tableRelations[tableTsName].push(relation);
|
|
137115
|
+
const getTypeParams = (sqlType) => {
|
|
137116
|
+
// get type params
|
|
137117
|
+
const typeParams = {};
|
|
137118
|
+
// handle dimensions
|
|
137119
|
+
if (sqlType.includes('[')) {
|
|
137120
|
+
const match = sqlType.match(/\[\w*]/g);
|
|
137121
|
+
if (match) {
|
|
137122
|
+
typeParams['dimensions'] = match.length;
|
|
137364
137123
|
}
|
|
137365
137124
|
}
|
|
137366
|
-
|
|
137367
|
-
|
|
137368
|
-
|
|
137369
|
-
|
|
137370
|
-
|
|
137371
|
-
|
|
137372
|
-
|
|
137373
|
-
|
|
137374
|
-
// might be empty list
|
|
137375
|
-
const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
137376
|
-
const table = dbToTsTableNamesMap[tableConfig.name];
|
|
137377
|
-
const refTable = dbToTsTableNamesMap[drizzleOrm.getTableName(fk.reference().foreignTable)];
|
|
137378
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(fk.reference().foreignTable);
|
|
137379
|
-
if (tableRelations[refTable] === undefined) {
|
|
137380
|
-
tableRelations[refTable] = [];
|
|
137125
|
+
if (sqlType.startsWith('numeric')
|
|
137126
|
+
|| sqlType.startsWith('decimal')
|
|
137127
|
+
|| sqlType.startsWith('double precision')
|
|
137128
|
+
|| sqlType.startsWith('real')) {
|
|
137129
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137130
|
+
if (match) {
|
|
137131
|
+
typeParams['precision'] = Number(match[1]);
|
|
137132
|
+
typeParams['scale'] = Number(match[2]);
|
|
137381
137133
|
}
|
|
137382
|
-
return {
|
|
137383
|
-
table,
|
|
137384
|
-
columns: fk
|
|
137385
|
-
.reference()
|
|
137386
|
-
.columns.map((col) => dbToTsColumnNamesMap[col.name]),
|
|
137387
|
-
refTable,
|
|
137388
|
-
refColumns: fk
|
|
137389
|
-
.reference()
|
|
137390
|
-
.foreignColumns.map((fCol) => dbToTsColumnNamesMapForRefTable[fCol.name]),
|
|
137391
|
-
refTableRels: tableRelations[refTable],
|
|
137392
|
-
};
|
|
137393
|
-
});
|
|
137394
|
-
relations.push(...newRelations);
|
|
137395
|
-
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
137396
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
137397
137134
|
}
|
|
137398
|
-
|
|
137399
|
-
|
|
137400
|
-
|
|
137401
|
-
|
|
137402
|
-
|
|
137403
|
-
|
|
137404
|
-
|
|
137405
|
-
|
|
137406
|
-
|
|
137407
|
-
|
|
137408
|
-
|
|
137409
|
-
isUnique: baseColumn.isUnique,
|
|
137410
|
-
notNull: baseColumn.notNull,
|
|
137411
|
-
primary: baseColumn.primary,
|
|
137412
|
-
baseColumn: baseColumn.baseColumn === undefined ? undefined : getAllBaseColumns(baseColumn.baseColumn),
|
|
137413
|
-
};
|
|
137414
|
-
return baseColumnResult;
|
|
137415
|
-
};
|
|
137416
|
-
const getTypeParams = (sqlType) => {
|
|
137417
|
-
// get type params
|
|
137418
|
-
const typeParams = {};
|
|
137419
|
-
// handle dimensions
|
|
137420
|
-
if (sqlType.includes('[')) {
|
|
137421
|
-
const match = sqlType.match(/\[\w*]/g);
|
|
137422
|
-
if (match) {
|
|
137423
|
-
typeParams['dimensions'] = match.length;
|
|
137424
|
-
}
|
|
137135
|
+
else if (sqlType.startsWith('varchar')
|
|
137136
|
+
|| sqlType.startsWith('bpchar')
|
|
137137
|
+
|| sqlType.startsWith('char')
|
|
137138
|
+
|| sqlType.startsWith('bit')
|
|
137139
|
+
|| sqlType.startsWith('vector')
|
|
137140
|
+
|| sqlType.startsWith('time')
|
|
137141
|
+
|| sqlType.startsWith('timestamp')
|
|
137142
|
+
|| sqlType.startsWith('interval')) {
|
|
137143
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137144
|
+
if (match) {
|
|
137145
|
+
typeParams['length'] = Number(match[1]);
|
|
137425
137146
|
}
|
|
137426
|
-
if (sqlType.startsWith('numeric')
|
|
137427
|
-
|| sqlType.startsWith('decimal')
|
|
137428
|
-
|| sqlType.startsWith('double precision')
|
|
137429
|
-
|| sqlType.startsWith('real')) {
|
|
137430
|
-
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137431
|
-
if (match) {
|
|
137432
|
-
typeParams['precision'] = Number(match[1]);
|
|
137433
|
-
typeParams['scale'] = Number(match[2]);
|
|
137434
|
-
}
|
|
137435
|
-
}
|
|
137436
|
-
else if (sqlType.startsWith('varchar')
|
|
137437
|
-
|| sqlType.startsWith('bpchar')
|
|
137438
|
-
|| sqlType.startsWith('char')
|
|
137439
|
-
|| sqlType.startsWith('bit')
|
|
137440
|
-
|| sqlType.startsWith('vector')
|
|
137441
|
-
|| sqlType.startsWith('time')
|
|
137442
|
-
|| sqlType.startsWith('timestamp')
|
|
137443
|
-
|| sqlType.startsWith('interval')) {
|
|
137444
|
-
const match = sqlType.match(/\((\d+)\)/);
|
|
137445
|
-
if (match) {
|
|
137446
|
-
typeParams['length'] = Number(match[1]);
|
|
137447
|
-
}
|
|
137448
|
-
}
|
|
137449
|
-
return typeParams;
|
|
137450
|
-
};
|
|
137451
|
-
// console.log(tableConfig.columns);
|
|
137452
|
-
tables.push({
|
|
137453
|
-
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137454
|
-
columns: tableConfig.columns.map((column) => ({
|
|
137455
|
-
name: dbToTsColumnNamesMap[column.name],
|
|
137456
|
-
columnType: column.getSQLType(),
|
|
137457
|
-
typeParams: getTypeParams(column.getSQLType()),
|
|
137458
|
-
dataType: column.dataType,
|
|
137459
|
-
size: column.size,
|
|
137460
|
-
hasDefault: column.hasDefault,
|
|
137461
|
-
default: column.default,
|
|
137462
|
-
enumValues: column.enumValues,
|
|
137463
|
-
isUnique: column.isUnique,
|
|
137464
|
-
notNull: column.notNull,
|
|
137465
|
-
primary: column.primary,
|
|
137466
|
-
generatedIdentityType: column.generatedIdentity?.type,
|
|
137467
|
-
baseColumn: (column.baseColumn === undefined)
|
|
137468
|
-
? undefined
|
|
137469
|
-
: getAllBaseColumns(column.baseColumn),
|
|
137470
|
-
})),
|
|
137471
|
-
primaryKeys: tableConfig.columns
|
|
137472
|
-
.filter((column) => column.primary)
|
|
137473
|
-
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137474
|
-
});
|
|
137475
|
-
}
|
|
137476
|
-
const transformedDrizzleRelations = transformFromDrizzleRelation(pgSchema, getDbToTsColumnNamesMap, tableRelations);
|
|
137477
|
-
relations.push(...transformedDrizzleRelations);
|
|
137478
|
-
const isCyclicRelations = relations.map((relI) => {
|
|
137479
|
-
// if (relations.some((relj) => relI.table === relj.refTable && relI.refTable === relj.table)) {
|
|
137480
|
-
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
137481
|
-
if (isRelationCyclic(relI)) {
|
|
137482
|
-
tableRel['isCyclic'] = true;
|
|
137483
|
-
return { ...relI, isCyclic: true };
|
|
137484
137147
|
}
|
|
137485
|
-
|
|
137486
|
-
|
|
137487
|
-
|
|
137488
|
-
|
|
137148
|
+
return typeParams;
|
|
137149
|
+
};
|
|
137150
|
+
return {
|
|
137151
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137152
|
+
columns: tableConfig.columns.map((column) => ({
|
|
137153
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137154
|
+
columnType: column.getSQLType(),
|
|
137155
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137156
|
+
dataType: column.dataType.split(' ')[0],
|
|
137157
|
+
size: column.length,
|
|
137158
|
+
hasDefault: column.hasDefault,
|
|
137159
|
+
default: column.default,
|
|
137160
|
+
enumValues: column.enumValues,
|
|
137161
|
+
isUnique: column.isUnique,
|
|
137162
|
+
notNull: column.notNull,
|
|
137163
|
+
primary: column.primary,
|
|
137164
|
+
generatedIdentityType: column.generatedIdentity?.type,
|
|
137165
|
+
baseColumn: (column.baseColumn === undefined)
|
|
137166
|
+
? undefined
|
|
137167
|
+
: getAllBaseColumns(column.baseColumn),
|
|
137168
|
+
})),
|
|
137169
|
+
primaryKeys: tableConfig.columns
|
|
137170
|
+
.filter((column) => column.primary)
|
|
137171
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137172
|
+
};
|
|
137489
137173
|
};
|
|
137490
137174
|
|
|
137491
137175
|
// SingleStore-----------------------------------------------------------------------------------------------------
|
|
@@ -137502,13 +137186,13 @@ const resetSingleStore = async (db, schema) => {
|
|
|
137502
137186
|
await db.execute(drizzleOrm.sql.raw('SET FOREIGN_KEY_CHECKS = 1;'));
|
|
137503
137187
|
};
|
|
137504
137188
|
const filterSingleStoreTables = (schema) => {
|
|
137505
|
-
const singleStoreSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], singlestoreCore.SingleStoreTable) || drizzleOrm.is(keyValue[1],
|
|
137189
|
+
const singleStoreSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], singlestoreCore.SingleStoreTable) || drizzleOrm.is(keyValue[1], _relations.Relations)));
|
|
137506
137190
|
const singleStoreTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], singlestoreCore.SingleStoreTable)));
|
|
137507
137191
|
return { singleStoreSchema, singleStoreTables };
|
|
137508
137192
|
};
|
|
137509
137193
|
const seedSingleStore = async (db, schema, options = {}, refinements) => {
|
|
137510
137194
|
const { singleStoreSchema, singleStoreTables } = filterSingleStoreTables(schema);
|
|
137511
|
-
const { tables, relations } =
|
|
137195
|
+
const { tables, relations } = getSchemaInfo(singleStoreSchema, singleStoreTables, mapSingleStoreTable);
|
|
137512
137196
|
const seedService = new SeedService();
|
|
137513
137197
|
const generatedTablesGenerators = seedService.generatePossibleGenerators('singlestore', tables, relations, refinements, options);
|
|
137514
137198
|
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
@@ -137517,175 +137201,56 @@ const seedSingleStore = async (db, schema, options = {}, refinements) => {
|
|
|
137517
137201
|
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
137518
137202
|
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, singleStoreTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
137519
137203
|
};
|
|
137520
|
-
const
|
|
137521
|
-
|
|
137522
|
-
|
|
137523
|
-
|
|
137524
|
-
|
|
137525
|
-
|
|
137526
|
-
|
|
137527
|
-
|
|
137528
|
-
|
|
137529
|
-
|
|
137530
|
-
|
|
137531
|
-
|
|
137532
|
-
dbToTsColumnNamesMap = dbToTsColumnNamesMapGlobal[tableName];
|
|
137533
|
-
return dbToTsColumnNamesMap;
|
|
137534
|
-
}
|
|
137535
|
-
const tableConfig = singlestoreCore.getTableConfig(table);
|
|
137536
|
-
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
137537
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
137538
|
-
}
|
|
137539
|
-
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
137540
|
-
return dbToTsColumnNamesMap;
|
|
137541
|
-
};
|
|
137542
|
-
const transformFromDrizzleRelation = (schema, getDbToTsColumnNamesMap, tableRelations) => {
|
|
137543
|
-
const schemaConfig = drizzleOrm.extractTablesRelationalConfig(schema, drizzleOrm.createTableRelationsHelpers);
|
|
137544
|
-
const relations = [];
|
|
137545
|
-
for (const table of Object.values(schemaConfig.tables)) {
|
|
137546
|
-
if (table.relations === undefined)
|
|
137547
|
-
continue;
|
|
137548
|
-
for (const drizzleRel of Object.values(table.relations)) {
|
|
137549
|
-
if (!drizzleOrm.is(drizzleRel, drizzleOrm.One))
|
|
137550
|
-
continue;
|
|
137551
|
-
const tableConfig = singlestoreCore.getTableConfig(drizzleRel.sourceTable);
|
|
137552
|
-
const tableDbSchema = tableConfig.schema ?? 'public';
|
|
137553
|
-
const tableDbName = tableConfig.name;
|
|
137554
|
-
const tableTsName = schemaConfig.tableNamesMap[`${tableDbSchema}.${tableDbName}`] ?? tableDbName;
|
|
137555
|
-
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
137556
|
-
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
137557
|
-
?? [];
|
|
137558
|
-
const refTableConfig = singlestoreCore.getTableConfig(drizzleRel.referencedTable);
|
|
137559
|
-
const refTableDbSchema = refTableConfig.schema ?? 'public';
|
|
137560
|
-
const refTableDbName = refTableConfig.name;
|
|
137561
|
-
const refTableTsName = schemaConfig.tableNamesMap[`${refTableDbSchema}.${refTableDbName}`]
|
|
137562
|
-
?? refTableDbName;
|
|
137563
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
137564
|
-
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
137565
|
-
?? [];
|
|
137566
|
-
if (tableRelations[refTableTsName] === undefined) {
|
|
137567
|
-
tableRelations[refTableTsName] = [];
|
|
137568
|
-
}
|
|
137569
|
-
const relation = {
|
|
137570
|
-
table: tableTsName,
|
|
137571
|
-
columns,
|
|
137572
|
-
refTable: refTableTsName,
|
|
137573
|
-
refColumns,
|
|
137574
|
-
refTableRels: tableRelations[refTableTsName],
|
|
137575
|
-
type: 'one',
|
|
137576
|
-
};
|
|
137577
|
-
// do not add duplicate relation
|
|
137578
|
-
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
137579
|
-
&& rel.refTable === relation.refTable)) {
|
|
137580
|
-
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
137581
|
-
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
137582
|
-
+ `In this case, the foreign key constraint will be used.\n`);
|
|
137583
|
-
continue;
|
|
137584
|
-
}
|
|
137585
|
-
relations.push(relation);
|
|
137586
|
-
tableRelations[tableTsName].push(relation);
|
|
137204
|
+
const mapSingleStoreTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
|
|
137205
|
+
const getTypeParams = (sqlType) => {
|
|
137206
|
+
// get type params and set only type
|
|
137207
|
+
const typeParams = {};
|
|
137208
|
+
if (sqlType.startsWith('decimal')
|
|
137209
|
+
|| sqlType.startsWith('real')
|
|
137210
|
+
|| sqlType.startsWith('double')
|
|
137211
|
+
|| sqlType.startsWith('float')) {
|
|
137212
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137213
|
+
if (match) {
|
|
137214
|
+
typeParams['precision'] = Number(match[1]);
|
|
137215
|
+
typeParams['scale'] = Number(match[2]);
|
|
137587
137216
|
}
|
|
137588
137217
|
}
|
|
137589
|
-
|
|
137590
|
-
|
|
137591
|
-
|
|
137592
|
-
|
|
137593
|
-
|
|
137594
|
-
|
|
137595
|
-
|
|
137596
|
-
}
|
|
137597
|
-
// const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
137598
|
-
// const table = dbToTsTableNamesMap[tableConfig.name] as string;
|
|
137599
|
-
// const refTable = dbToTsTableNamesMap[getTableName(fk.reference().foreignTable)] as string;
|
|
137600
|
-
// const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(
|
|
137601
|
-
// fk.reference().foreignTable,
|
|
137602
|
-
// );
|
|
137603
|
-
// if (tableRelations[refTable] === undefined) {
|
|
137604
|
-
// tableRelations[refTable] = [];
|
|
137605
|
-
// }
|
|
137606
|
-
// return {
|
|
137607
|
-
// table,
|
|
137608
|
-
// columns: fk
|
|
137609
|
-
// .reference()
|
|
137610
|
-
// .columns.map((col) => dbToTsColumnNamesMap[col.name] as string),
|
|
137611
|
-
// refTable,
|
|
137612
|
-
// refColumns: fk
|
|
137613
|
-
// .reference()
|
|
137614
|
-
// .foreignColumns.map(
|
|
137615
|
-
// (fCol) => dbToTsColumnNamesMapForRefTable[fCol.name] as string,
|
|
137616
|
-
// ),
|
|
137617
|
-
// refTableRels: tableRelations[refTable],
|
|
137618
|
-
// };
|
|
137619
|
-
// });
|
|
137620
|
-
// relations.push(
|
|
137621
|
-
// ...newRelations,
|
|
137622
|
-
// );
|
|
137623
|
-
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
137624
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
137625
|
-
}
|
|
137626
|
-
// tableRelations[dbToTsTableNamesMap[tableConfig.name] as string]!.push(...newRelations);
|
|
137627
|
-
const getTypeParams = (sqlType) => {
|
|
137628
|
-
// get type params and set only type
|
|
137629
|
-
const typeParams = {};
|
|
137630
|
-
if (sqlType.startsWith('decimal')
|
|
137631
|
-
|| sqlType.startsWith('real')
|
|
137632
|
-
|| sqlType.startsWith('double')
|
|
137633
|
-
|| sqlType.startsWith('float')) {
|
|
137634
|
-
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137635
|
-
if (match) {
|
|
137636
|
-
typeParams['precision'] = Number(match[1]);
|
|
137637
|
-
typeParams['scale'] = Number(match[2]);
|
|
137638
|
-
}
|
|
137639
|
-
}
|
|
137640
|
-
else if (sqlType.startsWith('char')
|
|
137641
|
-
|| sqlType.startsWith('varchar')
|
|
137642
|
-
|| sqlType.startsWith('binary')
|
|
137643
|
-
|| sqlType.startsWith('varbinary')) {
|
|
137644
|
-
const match = sqlType.match(/\((\d+)\)/);
|
|
137645
|
-
if (match) {
|
|
137646
|
-
typeParams['length'] = Number(match[1]);
|
|
137647
|
-
}
|
|
137218
|
+
else if (sqlType.startsWith('char')
|
|
137219
|
+
|| sqlType.startsWith('varchar')
|
|
137220
|
+
|| sqlType.startsWith('binary')
|
|
137221
|
+
|| sqlType.startsWith('varbinary')) {
|
|
137222
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137223
|
+
if (match) {
|
|
137224
|
+
typeParams['length'] = Number(match[1]);
|
|
137648
137225
|
}
|
|
137649
|
-
|
|
137650
|
-
|
|
137651
|
-
|
|
137652
|
-
|
|
137653
|
-
|
|
137654
|
-
|
|
137226
|
+
}
|
|
137227
|
+
else if (sqlType.startsWith('vector')) {
|
|
137228
|
+
const match = sqlType.match(/\((\d+),? ?((F|I)\d{1,2})?\)/);
|
|
137229
|
+
if (match) {
|
|
137230
|
+
typeParams['length'] = Number(match[1]);
|
|
137231
|
+
typeParams['vectorValueType'] = match[2];
|
|
137655
137232
|
}
|
|
137656
|
-
return typeParams;
|
|
137657
|
-
};
|
|
137658
|
-
tables.push({
|
|
137659
|
-
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137660
|
-
columns: tableConfig.columns.map((column) => ({
|
|
137661
|
-
name: dbToTsColumnNamesMap[column.name],
|
|
137662
|
-
columnType: column.getSQLType(),
|
|
137663
|
-
typeParams: getTypeParams(column.getSQLType()),
|
|
137664
|
-
dataType: column.dataType,
|
|
137665
|
-
hasDefault: column.hasDefault,
|
|
137666
|
-
default: column.default,
|
|
137667
|
-
enumValues: column.enumValues,
|
|
137668
|
-
isUnique: column.isUnique,
|
|
137669
|
-
notNull: column.notNull,
|
|
137670
|
-
primary: column.primary,
|
|
137671
|
-
})),
|
|
137672
|
-
primaryKeys: tableConfig.columns
|
|
137673
|
-
.filter((column) => column.primary)
|
|
137674
|
-
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137675
|
-
});
|
|
137676
|
-
}
|
|
137677
|
-
const transformedDrizzleRelations = transformFromDrizzleRelation(singleStoreSchema, getDbToTsColumnNamesMap, tableRelations);
|
|
137678
|
-
relations.push(...transformedDrizzleRelations);
|
|
137679
|
-
const isCyclicRelations = relations.map((relI) => {
|
|
137680
|
-
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
137681
|
-
if (isRelationCyclic(relI)) {
|
|
137682
|
-
tableRel['isCyclic'] = true;
|
|
137683
|
-
return { ...relI, isCyclic: true };
|
|
137684
137233
|
}
|
|
137685
|
-
|
|
137686
|
-
|
|
137687
|
-
|
|
137688
|
-
|
|
137234
|
+
return typeParams;
|
|
137235
|
+
};
|
|
137236
|
+
return {
|
|
137237
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137238
|
+
columns: tableConfig.columns.map((column) => ({
|
|
137239
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137240
|
+
columnType: column.getSQLType(),
|
|
137241
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137242
|
+
dataType: column.dataType.split(' ')[0],
|
|
137243
|
+
hasDefault: column.hasDefault,
|
|
137244
|
+
default: column.default,
|
|
137245
|
+
enumValues: column.enumValues,
|
|
137246
|
+
isUnique: column.isUnique,
|
|
137247
|
+
notNull: column.notNull,
|
|
137248
|
+
primary: column.primary,
|
|
137249
|
+
})),
|
|
137250
|
+
primaryKeys: tableConfig.columns
|
|
137251
|
+
.filter((column) => column.primary)
|
|
137252
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137253
|
+
};
|
|
137689
137254
|
};
|
|
137690
137255
|
|
|
137691
137256
|
// Sqlite------------------------------------------------------------------------------------------------------------------------
|
|
@@ -137702,13 +137267,13 @@ const resetSqlite = async (db, schema) => {
|
|
|
137702
137267
|
await db.run(drizzleOrm.sql.raw('PRAGMA foreign_keys = ON'));
|
|
137703
137268
|
};
|
|
137704
137269
|
const filterSqliteTables = (schema) => {
|
|
137705
|
-
const sqliteSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], sqliteCore.SQLiteTable) || drizzleOrm.is(keyValue[1],
|
|
137270
|
+
const sqliteSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], sqliteCore.SQLiteTable) || drizzleOrm.is(keyValue[1], _relations.Relations)));
|
|
137706
137271
|
const sqliteTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], sqliteCore.SQLiteTable)));
|
|
137707
137272
|
return { sqliteSchema, sqliteTables };
|
|
137708
137273
|
};
|
|
137709
137274
|
const seedSqlite = async (db, schema, options = {}, refinements) => {
|
|
137710
137275
|
const { sqliteSchema, sqliteTables } = filterSqliteTables(schema);
|
|
137711
|
-
const { tables, relations } =
|
|
137276
|
+
const { tables, relations } = getSchemaInfo(sqliteSchema, sqliteTables, mapSqliteTable);
|
|
137712
137277
|
const seedService = new SeedService();
|
|
137713
137278
|
const generatedTablesGenerators = seedService.generatePossibleGenerators('sqlite', tables, relations, refinements, options);
|
|
137714
137279
|
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
@@ -137717,159 +137282,48 @@ const seedSqlite = async (db, schema, options = {}, refinements) => {
|
|
|
137717
137282
|
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
137718
137283
|
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, sqliteTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
137719
137284
|
};
|
|
137720
|
-
const
|
|
137721
|
-
|
|
137722
|
-
|
|
137723
|
-
|
|
137724
|
-
|
|
137725
|
-
|
|
137726
|
-
|
|
137727
|
-
|
|
137728
|
-
|
|
137729
|
-
let dbToTsColumnNamesMap = {};
|
|
137730
|
-
const tableName = drizzleOrm.getTableName(table);
|
|
137731
|
-
if (Object.hasOwn(dbToTsColumnNamesMapGlobal, tableName)) {
|
|
137732
|
-
dbToTsColumnNamesMap = dbToTsColumnNamesMapGlobal[tableName];
|
|
137733
|
-
return dbToTsColumnNamesMap;
|
|
137734
|
-
}
|
|
137735
|
-
const tableConfig = sqliteCore.getTableConfig(table);
|
|
137736
|
-
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
137737
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
137738
|
-
}
|
|
137739
|
-
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
137740
|
-
return dbToTsColumnNamesMap;
|
|
137741
|
-
};
|
|
137742
|
-
const transformFromDrizzleRelation = (schema, getDbToTsColumnNamesMap, tableRelations) => {
|
|
137743
|
-
const schemaConfig = drizzleOrm.extractTablesRelationalConfig(schema, drizzleOrm.createTableRelationsHelpers);
|
|
137744
|
-
const relations = [];
|
|
137745
|
-
for (const table of Object.values(schemaConfig.tables)) {
|
|
137746
|
-
if (table.relations === undefined)
|
|
137747
|
-
continue;
|
|
137748
|
-
for (const drizzleRel of Object.values(table.relations)) {
|
|
137749
|
-
if (!drizzleOrm.is(drizzleRel, drizzleOrm.One))
|
|
137750
|
-
continue;
|
|
137751
|
-
const tableConfig = sqliteCore.getTableConfig(drizzleRel.sourceTable);
|
|
137752
|
-
const tableDbName = tableConfig.name;
|
|
137753
|
-
// TODO: tableNamesMap: have {public.customer: 'customer'} structure in sqlite
|
|
137754
|
-
const tableTsName = schemaConfig.tableNamesMap[`public.${tableDbName}`] ?? tableDbName;
|
|
137755
|
-
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
137756
|
-
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
137757
|
-
?? [];
|
|
137758
|
-
const refTableConfig = sqliteCore.getTableConfig(drizzleRel.referencedTable);
|
|
137759
|
-
const refTableDbName = refTableConfig.name;
|
|
137760
|
-
const refTableTsName = schemaConfig.tableNamesMap[`public.${refTableDbName}`]
|
|
137761
|
-
?? refTableDbName;
|
|
137762
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
137763
|
-
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
137764
|
-
?? [];
|
|
137765
|
-
if (tableRelations[refTableTsName] === undefined) {
|
|
137766
|
-
tableRelations[refTableTsName] = [];
|
|
137767
|
-
}
|
|
137768
|
-
const relation = {
|
|
137769
|
-
table: tableTsName,
|
|
137770
|
-
columns,
|
|
137771
|
-
refTable: refTableTsName,
|
|
137772
|
-
refColumns,
|
|
137773
|
-
refTableRels: tableRelations[refTableTsName],
|
|
137774
|
-
type: 'one',
|
|
137775
|
-
};
|
|
137776
|
-
// do not add duplicate relation
|
|
137777
|
-
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
137778
|
-
&& rel.refTable === relation.refTable)) {
|
|
137779
|
-
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
137780
|
-
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
137781
|
-
+ `In this case, the foreign key constraint will be used.\n`);
|
|
137782
|
-
continue;
|
|
137783
|
-
}
|
|
137784
|
-
relations.push(relation);
|
|
137785
|
-
tableRelations[tableTsName].push(relation);
|
|
137285
|
+
const mapSqliteTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
|
|
137286
|
+
const getTypeParams = (sqlType) => {
|
|
137287
|
+
// get type params and set only type
|
|
137288
|
+
const typeParams = {};
|
|
137289
|
+
if (sqlType.startsWith('decimal')) {
|
|
137290
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137291
|
+
if (match) {
|
|
137292
|
+
typeParams['precision'] = Number(match[1]);
|
|
137293
|
+
typeParams['scale'] = Number(match[2]);
|
|
137786
137294
|
}
|
|
137787
137295
|
}
|
|
137788
|
-
|
|
137789
|
-
|
|
137790
|
-
|
|
137791
|
-
|
|
137792
|
-
|
|
137793
|
-
|
|
137794
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
137795
|
-
}
|
|
137796
|
-
const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
137797
|
-
const table = dbToTsTableNamesMap[tableConfig.name];
|
|
137798
|
-
const refTable = dbToTsTableNamesMap[drizzleOrm.getTableName(fk.reference().foreignTable)];
|
|
137799
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(fk.reference().foreignTable);
|
|
137800
|
-
if (tableRelations[refTable] === undefined) {
|
|
137801
|
-
tableRelations[refTable] = [];
|
|
137802
|
-
}
|
|
137803
|
-
return {
|
|
137804
|
-
table,
|
|
137805
|
-
columns: fk
|
|
137806
|
-
.reference()
|
|
137807
|
-
.columns.map((col) => dbToTsColumnNamesMap[col.name]),
|
|
137808
|
-
refTable,
|
|
137809
|
-
refColumns: fk
|
|
137810
|
-
.reference()
|
|
137811
|
-
.foreignColumns.map((fCol) => dbToTsColumnNamesMapForRefTable[fCol.name]),
|
|
137812
|
-
refTableRels: tableRelations[refTable],
|
|
137813
|
-
};
|
|
137814
|
-
});
|
|
137815
|
-
relations.push(...newRelations);
|
|
137816
|
-
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
137817
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
137818
|
-
}
|
|
137819
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]].push(...newRelations);
|
|
137820
|
-
const getTypeParams = (sqlType) => {
|
|
137821
|
-
// get type params and set only type
|
|
137822
|
-
const typeParams = {};
|
|
137823
|
-
if (sqlType.startsWith('decimal')) {
|
|
137824
|
-
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137825
|
-
if (match) {
|
|
137826
|
-
typeParams['precision'] = Number(match[1]);
|
|
137827
|
-
typeParams['scale'] = Number(match[2]);
|
|
137828
|
-
}
|
|
137829
|
-
}
|
|
137830
|
-
else if (sqlType.startsWith('char')
|
|
137831
|
-
|| sqlType.startsWith('varchar')
|
|
137832
|
-
|| sqlType.startsWith('text')) {
|
|
137833
|
-
const match = sqlType.match(/\((\d+)\)/);
|
|
137834
|
-
if (match) {
|
|
137835
|
-
typeParams['length'] = Number(match[1]);
|
|
137836
|
-
}
|
|
137296
|
+
else if (sqlType.startsWith('char')
|
|
137297
|
+
|| sqlType.startsWith('varchar')
|
|
137298
|
+
|| sqlType.startsWith('text')) {
|
|
137299
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137300
|
+
if (match) {
|
|
137301
|
+
typeParams['length'] = Number(match[1]);
|
|
137837
137302
|
}
|
|
137838
|
-
return typeParams;
|
|
137839
|
-
};
|
|
137840
|
-
tables.push({
|
|
137841
|
-
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137842
|
-
columns: tableConfig.columns.map((column) => ({
|
|
137843
|
-
name: dbToTsColumnNamesMap[column.name],
|
|
137844
|
-
columnType: column.getSQLType(),
|
|
137845
|
-
typeParams: getTypeParams(column.getSQLType()),
|
|
137846
|
-
dataType: column.dataType,
|
|
137847
|
-
hasDefault: column.hasDefault,
|
|
137848
|
-
default: column.default,
|
|
137849
|
-
enumValues: column.enumValues,
|
|
137850
|
-
isUnique: column.isUnique,
|
|
137851
|
-
notNull: column.notNull,
|
|
137852
|
-
primary: column.primary,
|
|
137853
|
-
})),
|
|
137854
|
-
primaryKeys: tableConfig.columns
|
|
137855
|
-
.filter((column) => column.primary)
|
|
137856
|
-
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137857
|
-
});
|
|
137858
|
-
}
|
|
137859
|
-
const transformedDrizzleRelations = transformFromDrizzleRelation(sqliteSchema, getDbToTsColumnNamesMap, tableRelations);
|
|
137860
|
-
relations.push(...transformedDrizzleRelations);
|
|
137861
|
-
const isCyclicRelations = relations.map((relI) => {
|
|
137862
|
-
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
137863
|
-
if (isRelationCyclic(relI)) {
|
|
137864
|
-
tableRel['isCyclic'] = true;
|
|
137865
|
-
return { ...relI, isCyclic: true };
|
|
137866
137303
|
}
|
|
137867
|
-
|
|
137868
|
-
|
|
137869
|
-
|
|
137870
|
-
|
|
137304
|
+
return typeParams;
|
|
137305
|
+
};
|
|
137306
|
+
return {
|
|
137307
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137308
|
+
columns: tableConfig.columns.map((column) => ({
|
|
137309
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137310
|
+
columnType: column.getSQLType(),
|
|
137311
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137312
|
+
dataType: column.dataType.split(' ')[0],
|
|
137313
|
+
hasDefault: column.hasDefault,
|
|
137314
|
+
default: column.default,
|
|
137315
|
+
enumValues: column.enumValues,
|
|
137316
|
+
isUnique: column.isUnique,
|
|
137317
|
+
notNull: column.notNull,
|
|
137318
|
+
primary: column.primary,
|
|
137319
|
+
})),
|
|
137320
|
+
primaryKeys: tableConfig.columns
|
|
137321
|
+
.filter((column) => column.primary)
|
|
137322
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137323
|
+
};
|
|
137871
137324
|
};
|
|
137872
137325
|
|
|
137326
|
+
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
137873
137327
|
class SeedPromise {
|
|
137874
137328
|
db;
|
|
137875
137329
|
schema;
|