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