drizzle-seed 0.4.0-08bb2d5 → 0.4.0-08e4e66
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 +6 -2
- package/cockroach-core/index.d.mts +6 -2
- package/cockroach-core/index.d.ts +6 -2
- package/common.d.cts +23 -0
- package/common.d.mts +23 -0
- package/common.d.ts +23 -0
- package/generators/GeneratorFuncs.d.cts +6 -29
- package/generators/GeneratorFuncs.d.mts +6 -29
- package/generators/GeneratorFuncs.d.ts +6 -29
- package/generators/Generators.d.cts +140 -43
- package/generators/Generators.d.mts +140 -43
- package/generators/Generators.d.ts +140 -43
- package/generators/versioning/v2.d.cts +13 -6
- package/generators/versioning/v2.d.mts +13 -6
- package/generators/versioning/v2.d.ts +13 -6
- package/index.cjs +1071 -1183
- package/index.cjs.map +1 -1
- package/index.d.cts +10 -96
- package/index.d.mts +10 -96
- package/index.d.ts +10 -96
- package/index.mjs +1076 -1188
- 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 +6 -2
- package/mysql-core/index.d.mts +6 -2
- package/mysql-core/index.d.ts +6 -2
- package/package.json +3 -3
- package/pg-core/index.d.cts +6 -2
- package/pg-core/index.d.mts +6 -2
- package/pg-core/index.d.ts +6 -2
- package/singlestore-core/index.d.cts +6 -2
- package/singlestore-core/index.d.mts +6 -2
- package/singlestore-core/index.d.ts +6 -2
- package/sqlite-core/index.d.cts +6 -2
- package/sqlite-core/index.d.mts +6 -2
- package/sqlite-core/index.d.ts +6 -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 +18 -0
- package/types/tables.d.mts +18 -0
- package/types/tables.d.ts +18 -0
package/index.cjs
CHANGED
|
@@ -7,8 +7,201 @@ 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, mapColumns) => {
|
|
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
|
+
const stringsSet = [];
|
|
171
|
+
const uniqueConstraints = [];
|
|
172
|
+
for (const uniCon of tableConfig.uniqueConstraints) {
|
|
173
|
+
const uniConColumns = uniCon.columns.map((col) => dbToTsColumnNamesMap[col.name]);
|
|
174
|
+
const uniConColumnsStr = JSON.stringify(uniConColumns);
|
|
175
|
+
if (!stringsSet.includes(uniConColumnsStr)) {
|
|
176
|
+
stringsSet.push(uniConColumnsStr);
|
|
177
|
+
uniqueConstraints.push(uniConColumns);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
const mappedTable = {
|
|
181
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
182
|
+
uniqueConstraints,
|
|
183
|
+
primaryKeys: tableConfig.columns
|
|
184
|
+
.filter((column) => column.primary)
|
|
185
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
186
|
+
columns: mapColumns(tableConfig, dbToTsColumnNamesMap),
|
|
187
|
+
};
|
|
188
|
+
tables.push(mappedTable);
|
|
189
|
+
}
|
|
190
|
+
const transformedDrizzleRelations = transformFromDrizzleRelation(drizzleTablesAndRelations, getDbToTsColumnNamesMap, tableRelations);
|
|
191
|
+
relations.push(...transformedDrizzleRelations);
|
|
192
|
+
const isCyclicRelations = relations.map((relI) => {
|
|
193
|
+
// if (relations.some((relj) => relI.table === relj.refTable && relI.refTable === relj.table)) {
|
|
194
|
+
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
195
|
+
if (isRelationCyclic(relI)) {
|
|
196
|
+
tableRel['isCyclic'] = true;
|
|
197
|
+
return { ...relI, isCyclic: true };
|
|
198
|
+
}
|
|
199
|
+
tableRel['isCyclic'] = false;
|
|
200
|
+
return { ...relI, isCyclic: false };
|
|
201
|
+
});
|
|
202
|
+
return { tables, relations: isCyclicRelations, tableRelations };
|
|
203
|
+
};
|
|
204
|
+
|
|
12
205
|
/**
|
|
13
206
|
* The original source for the Adjectives data was taken from https://www.kaggle.com/datasets/jordansiem/adjectives-list
|
|
14
207
|
*/
|
|
@@ -131274,10 +131467,12 @@ const isObject = (value) => {
|
|
|
131274
131467
|
class AbstractGenerator {
|
|
131275
131468
|
static entityKind = 'AbstractGenerator';
|
|
131276
131469
|
static version = 1;
|
|
131470
|
+
isGeneratorUnique = false;
|
|
131277
131471
|
isUnique = false;
|
|
131278
131472
|
notNull = false;
|
|
131279
131473
|
// param for generators which have a unique version of themselves
|
|
131280
131474
|
uniqueVersionOfGen;
|
|
131475
|
+
maxUniqueCount = -1;
|
|
131281
131476
|
dataType;
|
|
131282
131477
|
timeSpent;
|
|
131283
131478
|
//
|
|
@@ -131289,6 +131484,7 @@ class AbstractGenerator {
|
|
|
131289
131484
|
weightedCountSeed;
|
|
131290
131485
|
maxRepeatedValuesCount;
|
|
131291
131486
|
typeParams = {};
|
|
131487
|
+
uniqueKey;
|
|
131292
131488
|
params;
|
|
131293
131489
|
constructor(params) {
|
|
131294
131490
|
this.params = params === undefined ? {} : params;
|
|
@@ -131311,9 +131507,13 @@ class AbstractGenerator {
|
|
|
131311
131507
|
const constructor = this.constructor;
|
|
131312
131508
|
return constructor.entityKind;
|
|
131313
131509
|
}
|
|
131510
|
+
getMaxUniqueCount() {
|
|
131511
|
+
// override if you need to initialize this.maxUniqueCount after constructor
|
|
131512
|
+
return this.maxUniqueCount;
|
|
131513
|
+
}
|
|
131314
131514
|
replaceIfUnique() {
|
|
131315
131515
|
this.updateParams();
|
|
131316
|
-
if (this.uniqueVersionOfGen !== undefined
|
|
131516
|
+
if ((this.uniqueVersionOfGen !== undefined)
|
|
131317
131517
|
&& this.isUnique === true) {
|
|
131318
131518
|
const uniqueGen = new this.uniqueVersionOfGen({
|
|
131319
131519
|
...this.params,
|
|
@@ -131403,6 +131603,23 @@ class GenerateValuesFromArray extends AbstractGenerator {
|
|
|
131403
131603
|
static entityKind = 'GenerateValuesFromArray';
|
|
131404
131604
|
state;
|
|
131405
131605
|
timeSpent = 0;
|
|
131606
|
+
maxUniqueCount;
|
|
131607
|
+
allValuesCount = 0; // TODO rewrite generator
|
|
131608
|
+
constructor(params) {
|
|
131609
|
+
super(params);
|
|
131610
|
+
this.allValuesCount = this.params.values.length;
|
|
131611
|
+
if (isObject(this.params.values[0])) {
|
|
131612
|
+
this.allValuesCount = this.params.values.reduce((acc, currVal) => acc + currVal.values.length, 0);
|
|
131613
|
+
}
|
|
131614
|
+
this.maxUniqueCount = this.allValuesCount;
|
|
131615
|
+
}
|
|
131616
|
+
getMaxUniqueCount() {
|
|
131617
|
+
this.allValuesCount = this.params.values.length;
|
|
131618
|
+
if (isObject(this.params.values[0])) {
|
|
131619
|
+
this.allValuesCount = this.params.values.reduce((acc, currVal) => acc + currVal.values.length, 0);
|
|
131620
|
+
}
|
|
131621
|
+
return this.allValuesCount;
|
|
131622
|
+
}
|
|
131406
131623
|
checks({ count }) {
|
|
131407
131624
|
const { values } = this.params;
|
|
131408
131625
|
const { maxRepeatedValuesCount, notNull, isUnique } = this;
|
|
@@ -131420,16 +131637,13 @@ class GenerateValuesFromArray extends AbstractGenerator {
|
|
|
131420
131637
|
: obj.count.every((count) => count > 0))))) {
|
|
131421
131638
|
throw new Error('maxRepeatedValuesCount should be greater than zero.');
|
|
131422
131639
|
}
|
|
131423
|
-
let allValuesCount = values.length;
|
|
131424
|
-
if (isObject(values[0])) {
|
|
131425
|
-
allValuesCount = values.reduce((acc, currVal) => acc + currVal.values.length, 0);
|
|
131426
|
-
}
|
|
131427
131640
|
if (notNull === true
|
|
131428
131641
|
&& maxRepeatedValuesCount !== undefined
|
|
131429
131642
|
&& ((!isObject(values[0]) && typeof maxRepeatedValuesCount === 'number'
|
|
131430
131643
|
&& maxRepeatedValuesCount * values.length < count)
|
|
131431
131644
|
|| (isObject(values[0]) && typeof maxRepeatedValuesCount === 'number'
|
|
131432
|
-
|
|
131645
|
+
// eslint-disable-next-line unicorn/consistent-destructuring
|
|
131646
|
+
&& maxRepeatedValuesCount * this.allValuesCount < count))) {
|
|
131433
131647
|
throw new Error("Can't fill notNull column with null values.");
|
|
131434
131648
|
}
|
|
131435
131649
|
if (isUnique === true && maxRepeatedValuesCount !== undefined && ((typeof maxRepeatedValuesCount === 'number' && maxRepeatedValuesCount > 1)
|
|
@@ -131440,7 +131654,8 @@ class GenerateValuesFromArray extends AbstractGenerator {
|
|
|
131440
131654
|
throw new Error("Can't be greater than 1 if column is unique.");
|
|
131441
131655
|
}
|
|
131442
131656
|
if (isUnique === true && notNull === true && ((!isObject(values[0]) && values.length < count)
|
|
131443
|
-
|
|
131657
|
+
// eslint-disable-next-line unicorn/consistent-destructuring
|
|
131658
|
+
|| (isObject(values[0]) && this.allValuesCount < count))) {
|
|
131444
131659
|
// console.log(maxRepeatedValuesCount, values.length, allValuesCount, count)
|
|
131445
131660
|
throw new Error('There are no enough values to fill unique column.');
|
|
131446
131661
|
}
|
|
@@ -131576,6 +131791,8 @@ class GenerateSelfRelationsValuesFromArray extends AbstractGenerator {
|
|
|
131576
131791
|
class GenerateIntPrimaryKey extends AbstractGenerator {
|
|
131577
131792
|
static entityKind = 'GenerateIntPrimaryKey';
|
|
131578
131793
|
maxValue;
|
|
131794
|
+
maxUniqueCount = Number.POSITIVE_INFINITY;
|
|
131795
|
+
isGeneratorUnique = true;
|
|
131579
131796
|
init({ count }) {
|
|
131580
131797
|
if (this.maxValue !== undefined && count > this.maxValue) {
|
|
131581
131798
|
throw new Error('count exceeds max number for this column type.');
|
|
@@ -131626,14 +131843,39 @@ class GenerateNumber extends AbstractGenerator {
|
|
|
131626
131843
|
class GenerateUniqueNumber extends AbstractGenerator {
|
|
131627
131844
|
static entityKind = 'GenerateUniqueNumber';
|
|
131628
131845
|
state;
|
|
131629
|
-
|
|
131630
|
-
|
|
131846
|
+
precision;
|
|
131847
|
+
isGeneratorUnique = true;
|
|
131848
|
+
maxUniqueCount;
|
|
131849
|
+
constructor(params) {
|
|
131850
|
+
super(params);
|
|
131851
|
+
let { minValue, maxValue } = this.params;
|
|
131852
|
+
const { precision } = this.params;
|
|
131853
|
+
this.precision = precision ?? 100;
|
|
131854
|
+
if (maxValue === undefined) {
|
|
131855
|
+
this.maxUniqueCount = Number.POSITIVE_INFINITY;
|
|
131856
|
+
return;
|
|
131857
|
+
}
|
|
131858
|
+
else {
|
|
131859
|
+
maxValue *= this.precision;
|
|
131860
|
+
}
|
|
131861
|
+
if (minValue === undefined) {
|
|
131862
|
+
minValue = -maxValue;
|
|
131863
|
+
}
|
|
131864
|
+
else {
|
|
131865
|
+
minValue *= this.precision;
|
|
131866
|
+
}
|
|
131867
|
+
this.maxUniqueCount = maxValue - minValue + 1;
|
|
131868
|
+
}
|
|
131869
|
+
getMaxUniqueCount() {
|
|
131870
|
+
if (this.maxUniqueCount !== undefined)
|
|
131871
|
+
return this.maxUniqueCount;
|
|
131631
131872
|
let { minValue, maxValue, precision } = this.params;
|
|
131632
131873
|
if (precision === undefined) {
|
|
131633
131874
|
precision = 100;
|
|
131634
131875
|
}
|
|
131635
131876
|
if (maxValue === undefined) {
|
|
131636
|
-
|
|
131877
|
+
this.maxUniqueCount = Number.POSITIVE_INFINITY;
|
|
131878
|
+
return this.maxUniqueCount;
|
|
131637
131879
|
}
|
|
131638
131880
|
else {
|
|
131639
131881
|
maxValue *= precision;
|
|
@@ -131644,9 +131886,26 @@ class GenerateUniqueNumber extends AbstractGenerator {
|
|
|
131644
131886
|
else {
|
|
131645
131887
|
minValue *= precision;
|
|
131646
131888
|
}
|
|
131889
|
+
this.maxUniqueCount = maxValue - minValue + 1;
|
|
131890
|
+
return this.maxUniqueCount;
|
|
131891
|
+
}
|
|
131892
|
+
init({ count, seed }) {
|
|
131893
|
+
let { minValue, maxValue } = this.params;
|
|
131894
|
+
if (maxValue === undefined) {
|
|
131895
|
+
maxValue = count * this.precision;
|
|
131896
|
+
}
|
|
131897
|
+
else {
|
|
131898
|
+
maxValue *= this.precision;
|
|
131899
|
+
}
|
|
131900
|
+
if (minValue === undefined) {
|
|
131901
|
+
minValue = -maxValue;
|
|
131902
|
+
}
|
|
131903
|
+
else {
|
|
131904
|
+
minValue *= this.precision;
|
|
131905
|
+
}
|
|
131647
131906
|
const genUniqueIntObj = new GenerateUniqueInt({ minValue, maxValue });
|
|
131648
131907
|
genUniqueIntObj.init({ count, seed });
|
|
131649
|
-
this.state = { genUniqueIntObj, minValue, maxValue, precision };
|
|
131908
|
+
this.state = { genUniqueIntObj, minValue, maxValue, precision: this.precision };
|
|
131650
131909
|
}
|
|
131651
131910
|
generate() {
|
|
131652
131911
|
if (this.state === undefined) {
|
|
@@ -131701,8 +131960,48 @@ class GenerateUniqueInt extends AbstractGenerator {
|
|
|
131701
131960
|
genMaxRepeatedValuesCount;
|
|
131702
131961
|
skipCheck = false;
|
|
131703
131962
|
state;
|
|
131704
|
-
|
|
131963
|
+
isGeneratorUnique = true;
|
|
131705
131964
|
timeSpent = 0;
|
|
131965
|
+
maxUniqueCount;
|
|
131966
|
+
constructor(params) {
|
|
131967
|
+
super(params);
|
|
131968
|
+
let minValue = this.params.minValue, maxValue = this.params.maxValue;
|
|
131969
|
+
if (maxValue === undefined) {
|
|
131970
|
+
this.maxUniqueCount = Number.POSITIVE_INFINITY;
|
|
131971
|
+
return;
|
|
131972
|
+
}
|
|
131973
|
+
if (minValue === undefined) {
|
|
131974
|
+
minValue = -maxValue;
|
|
131975
|
+
}
|
|
131976
|
+
if (typeof minValue === 'number' && typeof maxValue === 'number') {
|
|
131977
|
+
minValue = minValue >= 0 ? Math.ceil(minValue) : Math.floor(minValue);
|
|
131978
|
+
maxValue = maxValue >= 0 ? Math.floor(maxValue) : Math.ceil(maxValue);
|
|
131979
|
+
this.maxUniqueCount = Number(maxValue - minValue) + 1;
|
|
131980
|
+
}
|
|
131981
|
+
else if (typeof minValue === 'bigint' && typeof maxValue === 'bigint') {
|
|
131982
|
+
this.maxUniqueCount = Number(maxValue - minValue) + 1;
|
|
131983
|
+
}
|
|
131984
|
+
else
|
|
131985
|
+
this.maxUniqueCount = Number(Number(maxValue) - Number(minValue)) + 1; // error should be triggered in init method
|
|
131986
|
+
}
|
|
131987
|
+
getMaxUniqueCount() {
|
|
131988
|
+
if (this.maxUniqueCount !== undefined)
|
|
131989
|
+
return this.maxUniqueCount;
|
|
131990
|
+
let minValue = this.params.minValue, maxValue = this.params.maxValue;
|
|
131991
|
+
if (maxValue === undefined) {
|
|
131992
|
+
this.maxUniqueCount = Number.POSITIVE_INFINITY;
|
|
131993
|
+
return this.maxUniqueCount;
|
|
131994
|
+
}
|
|
131995
|
+
if (minValue === undefined) {
|
|
131996
|
+
minValue = -maxValue;
|
|
131997
|
+
}
|
|
131998
|
+
if (typeof minValue === 'number' && typeof maxValue === 'number') {
|
|
131999
|
+
minValue = minValue >= 0 ? Math.ceil(minValue) : Math.floor(minValue);
|
|
132000
|
+
maxValue = maxValue >= 0 ? Math.floor(maxValue) : Math.ceil(maxValue);
|
|
132001
|
+
}
|
|
132002
|
+
this.maxUniqueCount = Number(maxValue - minValue) + 1;
|
|
132003
|
+
return this.maxUniqueCount;
|
|
132004
|
+
}
|
|
131706
132005
|
init({ count, seed }) {
|
|
131707
132006
|
const rng = prand.xoroshiro128plus(seed);
|
|
131708
132007
|
let { minValue, maxValue } = this.params;
|
|
@@ -132132,11 +132431,10 @@ class GenerateInterval extends AbstractGenerator {
|
|
|
132132
132431
|
return interval;
|
|
132133
132432
|
}
|
|
132134
132433
|
}
|
|
132135
|
-
// has a newer version
|
|
132136
132434
|
class GenerateUniqueInterval extends AbstractGenerator {
|
|
132137
132435
|
static 'entityKind' = 'GenerateUniqueInterval';
|
|
132138
132436
|
state;
|
|
132139
|
-
|
|
132437
|
+
isGeneratorUnique = true;
|
|
132140
132438
|
config = {
|
|
132141
132439
|
year: {
|
|
132142
132440
|
from: 0,
|
|
@@ -132163,6 +132461,47 @@ class GenerateUniqueInterval extends AbstractGenerator {
|
|
|
132163
132461
|
to: 60,
|
|
132164
132462
|
},
|
|
132165
132463
|
};
|
|
132464
|
+
maxUniqueCount;
|
|
132465
|
+
constructor(params) {
|
|
132466
|
+
super(params);
|
|
132467
|
+
const allFields = ['year', 'month', 'day', 'hour', 'minute', 'second'];
|
|
132468
|
+
let fieldsToGenerate = allFields;
|
|
132469
|
+
if (this.params.fields !== undefined && this.params.fields?.includes(' to ')) {
|
|
132470
|
+
const tokens = this.params.fields.split(' to ');
|
|
132471
|
+
const endIdx = allFields.indexOf(tokens[1]);
|
|
132472
|
+
fieldsToGenerate = allFields.slice(0, endIdx + 1);
|
|
132473
|
+
}
|
|
132474
|
+
else if (this.params.fields !== undefined) {
|
|
132475
|
+
const endIdx = allFields.indexOf(this.params.fields);
|
|
132476
|
+
fieldsToGenerate = allFields.slice(0, endIdx + 1);
|
|
132477
|
+
}
|
|
132478
|
+
this.maxUniqueCount = 1;
|
|
132479
|
+
for (const field of fieldsToGenerate) {
|
|
132480
|
+
const from = this.config[field].from, to = this.config[field].to;
|
|
132481
|
+
this.maxUniqueCount *= from - to + 1;
|
|
132482
|
+
}
|
|
132483
|
+
}
|
|
132484
|
+
getMaxUniqueCount() {
|
|
132485
|
+
if (this.maxUniqueCount !== undefined)
|
|
132486
|
+
return this.maxUniqueCount;
|
|
132487
|
+
const allFields = ['year', 'month', 'day', 'hour', 'minute', 'second'];
|
|
132488
|
+
let fieldsToGenerate = allFields;
|
|
132489
|
+
if (this.params.fields !== undefined && this.params.fields?.includes(' to ')) {
|
|
132490
|
+
const tokens = this.params.fields.split(' to ');
|
|
132491
|
+
const endIdx = allFields.indexOf(tokens[1]);
|
|
132492
|
+
fieldsToGenerate = allFields.slice(0, endIdx + 1);
|
|
132493
|
+
}
|
|
132494
|
+
else if (this.params.fields !== undefined) {
|
|
132495
|
+
const endIdx = allFields.indexOf(this.params.fields);
|
|
132496
|
+
fieldsToGenerate = allFields.slice(0, endIdx + 1);
|
|
132497
|
+
}
|
|
132498
|
+
this.maxUniqueCount = 1;
|
|
132499
|
+
for (const field of fieldsToGenerate) {
|
|
132500
|
+
const from = this.config[field].from, to = this.config[field].to;
|
|
132501
|
+
this.maxUniqueCount *= from - to + 1;
|
|
132502
|
+
}
|
|
132503
|
+
return this.maxUniqueCount;
|
|
132504
|
+
}
|
|
132166
132505
|
init({ count, seed }) {
|
|
132167
132506
|
const allFields = ['year', 'month', 'day', 'hour', 'minute', 'second'];
|
|
132168
132507
|
let fieldsToGenerate = allFields;
|
|
@@ -132231,6 +132570,8 @@ class GenerateString extends AbstractGenerator {
|
|
|
132231
132570
|
[idx, this.state.rng] = prand.uniformIntDistribution(0, stringChars.length - 1, this.state.rng);
|
|
132232
132571
|
currStr += stringChars[idx];
|
|
132233
132572
|
}
|
|
132573
|
+
if (this.dataType === 'object')
|
|
132574
|
+
return Buffer.from(currStr);
|
|
132234
132575
|
return currStr;
|
|
132235
132576
|
}
|
|
132236
132577
|
}
|
|
@@ -132238,7 +132579,11 @@ class GenerateString extends AbstractGenerator {
|
|
|
132238
132579
|
class GenerateUniqueString extends AbstractGenerator {
|
|
132239
132580
|
static entityKind = 'GenerateUniqueString';
|
|
132240
132581
|
state;
|
|
132241
|
-
|
|
132582
|
+
isGeneratorUnique = true;
|
|
132583
|
+
maxUniqueCount = Number.POSITIVE_INFINITY;
|
|
132584
|
+
getMaxUniqueCount() {
|
|
132585
|
+
return Number.POSITIVE_INFINITY;
|
|
132586
|
+
}
|
|
132242
132587
|
init({ seed }) {
|
|
132243
132588
|
const rng = prand.xoroshiro128plus(seed);
|
|
132244
132589
|
this.state = { rng };
|
|
@@ -132259,13 +132604,20 @@ class GenerateUniqueString extends AbstractGenerator {
|
|
|
132259
132604
|
[idx, this.state.rng] = prand.uniformIntDistribution(0, stringChars.length - 1, this.state.rng);
|
|
132260
132605
|
currStr += stringChars[idx];
|
|
132261
132606
|
}
|
|
132262
|
-
|
|
132607
|
+
currStr = currStr.slice(0, 4) + uniqueStr + currStr.slice(4);
|
|
132608
|
+
if (this.dataType === 'object')
|
|
132609
|
+
return Buffer.from(currStr);
|
|
132610
|
+
return currStr;
|
|
132263
132611
|
}
|
|
132264
132612
|
}
|
|
132265
132613
|
class GenerateUUID extends AbstractGenerator {
|
|
132266
132614
|
static entityKind = 'GenerateUUID';
|
|
132267
|
-
|
|
132615
|
+
isGeneratorUnique = true;
|
|
132616
|
+
maxUniqueCount = Number.POSITIVE_INFINITY;
|
|
132268
132617
|
state;
|
|
132618
|
+
getMaxUniqueCount() {
|
|
132619
|
+
return Number.POSITIVE_INFINITY;
|
|
132620
|
+
}
|
|
132269
132621
|
init({ count, seed }) {
|
|
132270
132622
|
super.init({ count, seed });
|
|
132271
132623
|
const rng = prand.xoroshiro128plus(seed);
|
|
@@ -132320,9 +132672,16 @@ class GenerateFirstName extends AbstractGenerator {
|
|
|
132320
132672
|
class GenerateUniqueFirstName extends AbstractGenerator {
|
|
132321
132673
|
static entityKind = 'GenerateUniqueFirstName';
|
|
132322
132674
|
state;
|
|
132323
|
-
|
|
132675
|
+
isGeneratorUnique = true;
|
|
132676
|
+
maxUniqueCount = firstNames.length;
|
|
132677
|
+
getMaxUniqueCount() {
|
|
132678
|
+
if (this.maxUniqueCount !== undefined)
|
|
132679
|
+
return this.maxUniqueCount;
|
|
132680
|
+
this.maxUniqueCount = firstNames.length;
|
|
132681
|
+
return firstNames.length;
|
|
132682
|
+
}
|
|
132324
132683
|
init({ count, seed }) {
|
|
132325
|
-
if (count >
|
|
132684
|
+
if (count > this.getMaxUniqueCount()) {
|
|
132326
132685
|
throw new Error('count exceeds max number of unique first names.');
|
|
132327
132686
|
}
|
|
132328
132687
|
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$5) {
|
|
@@ -132366,9 +132725,16 @@ class GenerateLastName extends AbstractGenerator {
|
|
|
132366
132725
|
class GenerateUniqueLastName extends AbstractGenerator {
|
|
132367
132726
|
static entityKind = 'GenerateUniqueLastName';
|
|
132368
132727
|
state;
|
|
132369
|
-
|
|
132728
|
+
isGeneratorUnique = true;
|
|
132729
|
+
maxUniqueCount = lastNames.length;
|
|
132730
|
+
getMaxUniqueCount() {
|
|
132731
|
+
if (this.maxUniqueCount !== undefined)
|
|
132732
|
+
return this.maxUniqueCount;
|
|
132733
|
+
this.maxUniqueCount = lastNames.length;
|
|
132734
|
+
return lastNames.length;
|
|
132735
|
+
}
|
|
132370
132736
|
init({ count, seed }) {
|
|
132371
|
-
if (count >
|
|
132737
|
+
if (count > this.getMaxUniqueCount()) {
|
|
132372
132738
|
throw new Error('count exceeds max number of unique last names.');
|
|
132373
132739
|
}
|
|
132374
132740
|
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$3) {
|
|
@@ -132415,13 +132781,19 @@ class GenerateFullName extends AbstractGenerator {
|
|
|
132415
132781
|
class GenerateUniqueFullName extends AbstractGenerator {
|
|
132416
132782
|
static entityKind = 'GenerateUniqueFullName';
|
|
132417
132783
|
state;
|
|
132418
|
-
|
|
132784
|
+
isGeneratorUnique = true;
|
|
132419
132785
|
timeSpent = 0;
|
|
132786
|
+
maxUniqueCount = firstNames.length * lastNames.length;
|
|
132787
|
+
getMaxUniqueCount() {
|
|
132788
|
+
if (this.maxUniqueCount !== undefined)
|
|
132789
|
+
return this.maxUniqueCount;
|
|
132790
|
+
this.maxUniqueCount = firstNames.length * lastNames.length;
|
|
132791
|
+
return this.maxUniqueCount;
|
|
132792
|
+
}
|
|
132420
132793
|
init({ count, seed }) {
|
|
132421
132794
|
const t0 = new Date();
|
|
132422
|
-
|
|
132423
|
-
|
|
132424
|
-
throw new RangeError(`count exceeds max number of unique full names(${maxUniqueFullNamesNumber}).`);
|
|
132795
|
+
if (count > this.getMaxUniqueCount()) {
|
|
132796
|
+
throw new RangeError(`count exceeds max number of unique full names(${this.getMaxUniqueCount()}).`);
|
|
132425
132797
|
}
|
|
132426
132798
|
if (this.typeParams?.length !== undefined && this.typeParams?.length < (maxStringLength$5 + maxStringLength$3 + 1)) {
|
|
132427
132799
|
throw new Error(`You can't use full name generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxStringLength$5 + maxStringLength$3 + 1}.`);
|
|
@@ -132456,13 +132828,17 @@ class GenerateEmail extends AbstractGenerator {
|
|
|
132456
132828
|
static entityKind = 'GenerateEmail';
|
|
132457
132829
|
state;
|
|
132458
132830
|
timeSpent = 0;
|
|
132459
|
-
|
|
132831
|
+
isGeneratorUnique = true;
|
|
132832
|
+
maxUniqueCount = adjectives.length * firstNames.length * emailDomains.length;
|
|
132833
|
+
getMaxUniqueCount() {
|
|
132834
|
+
if (this.maxUniqueCount !== undefined)
|
|
132835
|
+
return this.maxUniqueCount;
|
|
132836
|
+
this.maxUniqueCount = adjectives.length * firstNames.length * emailDomains.length;
|
|
132837
|
+
return this.maxUniqueCount;
|
|
132838
|
+
}
|
|
132460
132839
|
init({ count, seed }) {
|
|
132461
132840
|
super.init({ count, seed });
|
|
132462
|
-
const
|
|
132463
|
-
const adjectivesArray = adjectives;
|
|
132464
|
-
const namesArray = firstNames;
|
|
132465
|
-
const maxUniqueEmailsNumber = adjectivesArray.length * namesArray.length * domainsArray.length;
|
|
132841
|
+
const maxUniqueEmailsNumber = adjectives.length * firstNames.length * emailDomains.length;
|
|
132466
132842
|
if (count > maxUniqueEmailsNumber) {
|
|
132467
132843
|
throw new RangeError(`count exceeds max number of unique emails(${maxUniqueEmailsNumber}).`);
|
|
132468
132844
|
}
|
|
@@ -132470,7 +132846,7 @@ class GenerateEmail extends AbstractGenerator {
|
|
|
132470
132846
|
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxEmailLength) {
|
|
132471
132847
|
throw new Error(`You can't use email generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxEmailLength}.`);
|
|
132472
132848
|
}
|
|
132473
|
-
const arraysToGenerateFrom = [
|
|
132849
|
+
const arraysToGenerateFrom = [adjectives, firstNames, emailDomains];
|
|
132474
132850
|
const genIndicesObj = new GenerateUniqueInt({
|
|
132475
132851
|
minValue: 0,
|
|
132476
132852
|
maxValue: maxUniqueEmailsNumber - 1,
|
|
@@ -132494,20 +132870,75 @@ class GenerateEmail extends AbstractGenerator {
|
|
|
132494
132870
|
class GeneratePhoneNumber extends AbstractGenerator {
|
|
132495
132871
|
static entityKind = 'GeneratePhoneNumber';
|
|
132496
132872
|
state;
|
|
132497
|
-
|
|
132873
|
+
isGeneratorUnique = true;
|
|
132874
|
+
maxUniqueCount;
|
|
132875
|
+
constructor(params) {
|
|
132876
|
+
super(params);
|
|
132877
|
+
const { template } = this.params;
|
|
132878
|
+
if (template === undefined) {
|
|
132879
|
+
const { generatedDigitsNumbers } = this.prepareWithoutTemplate();
|
|
132880
|
+
this.maxUniqueCount = generatedDigitsNumbers.reduce((a, b) => a + Math.pow(10, b), 0);
|
|
132881
|
+
}
|
|
132882
|
+
else {
|
|
132883
|
+
const { placeholdersCount } = this.prepareWithTemplate();
|
|
132884
|
+
this.maxUniqueCount = Math.pow(10, placeholdersCount);
|
|
132885
|
+
}
|
|
132886
|
+
}
|
|
132887
|
+
prepareWithTemplate() {
|
|
132888
|
+
const { template } = this.params;
|
|
132889
|
+
const iterArray = [...template.matchAll(/#/g)];
|
|
132890
|
+
const placeholdersCount = iterArray.length;
|
|
132891
|
+
return { placeholdersCount };
|
|
132892
|
+
}
|
|
132893
|
+
prepareWithoutTemplate() {
|
|
132894
|
+
let { generatedDigitsNumbers, prefixes } = this.params;
|
|
132895
|
+
if (prefixes === undefined || prefixes.length === 0) {
|
|
132896
|
+
prefixes = phonesInfo.map((phoneInfo) => phoneInfo.split(',').slice(0, -1).join(' '));
|
|
132897
|
+
generatedDigitsNumbers = phonesInfo.map((phoneInfo) => {
|
|
132898
|
+
// tokens = ["380","99","9"] =
|
|
132899
|
+
// = ["country prefix", "operator prefix", "number length including operator prefix and excluding country prefix"]
|
|
132900
|
+
const tokens = phoneInfo.split(',');
|
|
132901
|
+
const operatorPrefixLength = tokens[1].replaceAll(' ', '').length;
|
|
132902
|
+
return Number(tokens[2]) - operatorPrefixLength;
|
|
132903
|
+
});
|
|
132904
|
+
}
|
|
132905
|
+
else {
|
|
132906
|
+
if (typeof generatedDigitsNumbers === 'number') {
|
|
132907
|
+
generatedDigitsNumbers = Array.from({ length: prefixes.length }).fill(generatedDigitsNumbers);
|
|
132908
|
+
}
|
|
132909
|
+
else if (generatedDigitsNumbers === undefined
|
|
132910
|
+
|| generatedDigitsNumbers.length === 0) {
|
|
132911
|
+
generatedDigitsNumbers = Array.from({ length: prefixes.length }).fill(7);
|
|
132912
|
+
}
|
|
132913
|
+
}
|
|
132914
|
+
return { prefixes, generatedDigitsNumbers };
|
|
132915
|
+
}
|
|
132916
|
+
getMaxUniqueCount() {
|
|
132917
|
+
if (this.maxUniqueCount !== undefined)
|
|
132918
|
+
return this.maxUniqueCount;
|
|
132919
|
+
const { template } = this.params;
|
|
132920
|
+
if (template === undefined) {
|
|
132921
|
+
const { generatedDigitsNumbers } = this.prepareWithoutTemplate();
|
|
132922
|
+
this.maxUniqueCount = generatedDigitsNumbers.reduce((a, b) => a + Math.pow(10, b), 0);
|
|
132923
|
+
return this.maxUniqueCount;
|
|
132924
|
+
}
|
|
132925
|
+
else {
|
|
132926
|
+
const { placeholdersCount } = this.prepareWithTemplate();
|
|
132927
|
+
this.maxUniqueCount = Math.pow(10, placeholdersCount);
|
|
132928
|
+
return this.maxUniqueCount;
|
|
132929
|
+
}
|
|
132930
|
+
}
|
|
132498
132931
|
init({ count, seed }) {
|
|
132499
132932
|
super.init({ count, seed });
|
|
132500
|
-
|
|
132501
|
-
const { prefixes, template } = this.params;
|
|
132933
|
+
const { template } = this.params;
|
|
132502
132934
|
const rng = prand.xoroshiro128plus(seed);
|
|
132503
132935
|
if (template !== undefined) {
|
|
132504
132936
|
if (this.typeParams?.length !== undefined && this.typeParams?.length < template.length) {
|
|
132505
132937
|
throw new Error(`Length of phone number template is shorter than db column length restriction: ${this.typeParams?.length}.
|
|
132506
132938
|
Set the maximum string length to at least ${template.length}.`);
|
|
132507
132939
|
}
|
|
132508
|
-
const
|
|
132509
|
-
const
|
|
132510
|
-
const maxUniquePhoneNumbersCount = Math.pow(10, placeholdersCount);
|
|
132940
|
+
const { placeholdersCount } = this.prepareWithTemplate();
|
|
132941
|
+
const maxUniquePhoneNumbersCount = this.getMaxUniqueCount();
|
|
132511
132942
|
if (maxUniquePhoneNumbersCount < count) {
|
|
132512
132943
|
throw new RangeError(`count exceeds max number of unique phone numbers(${maxUniquePhoneNumbersCount}).`);
|
|
132513
132944
|
}
|
|
@@ -132524,27 +132955,8 @@ class GeneratePhoneNumber extends AbstractGenerator {
|
|
|
132524
132955
|
this.state = { rng, placeholdersCount, generatorsMap, prefixesArray, generatedDigitsNumbers, phoneNumbersSet };
|
|
132525
132956
|
return;
|
|
132526
132957
|
}
|
|
132527
|
-
|
|
132528
|
-
|
|
132529
|
-
prefixesArray = phonesInfo.map((phoneInfo) => phoneInfo.split(',').slice(0, -1).join(' '));
|
|
132530
|
-
generatedDigitsNumbers = phonesInfo.map((phoneInfo) => {
|
|
132531
|
-
// tokens = ["380","99","9"] =
|
|
132532
|
-
// = ["country prefix", "operator prefix", "number length including operator prefix and excluding country prefix"]
|
|
132533
|
-
const tokens = phoneInfo.split(',');
|
|
132534
|
-
const operatorPrefixLength = tokens[1].replaceAll(' ', '').length;
|
|
132535
|
-
return Number(tokens[2]) - operatorPrefixLength;
|
|
132536
|
-
});
|
|
132537
|
-
}
|
|
132538
|
-
else {
|
|
132539
|
-
prefixesArray = prefixes;
|
|
132540
|
-
if (typeof generatedDigitsNumbers === 'number') {
|
|
132541
|
-
generatedDigitsNumbers = Array.from({ length: prefixes.length }).fill(generatedDigitsNumbers);
|
|
132542
|
-
}
|
|
132543
|
-
else if (generatedDigitsNumbers === undefined
|
|
132544
|
-
|| generatedDigitsNumbers.length === 0) {
|
|
132545
|
-
generatedDigitsNumbers = Array.from({ length: prefixes.length }).fill(7);
|
|
132546
|
-
}
|
|
132547
|
-
}
|
|
132958
|
+
const { generatedDigitsNumbers, prefixes } = this.prepareWithoutTemplate();
|
|
132959
|
+
const prefixesArray = [...prefixes];
|
|
132548
132960
|
const maxPrefixLength = Math.max(...prefixesArray.map((prefix) => prefix.length));
|
|
132549
132961
|
const maxGeneratedDigits = Math.max(...generatedDigitsNumbers);
|
|
132550
132962
|
if (this.typeParams?.length !== undefined && this.typeParams?.length < (maxPrefixLength + maxGeneratedDigits)) {
|
|
@@ -132553,7 +132965,7 @@ class GeneratePhoneNumber extends AbstractGenerator {
|
|
|
132553
132965
|
if (new Set(prefixesArray).size !== prefixesArray.length) {
|
|
132554
132966
|
throw new Error('prefixes are not unique.');
|
|
132555
132967
|
}
|
|
132556
|
-
const maxUniquePhoneNumbersCount =
|
|
132968
|
+
const maxUniquePhoneNumbersCount = this.getMaxUniqueCount();
|
|
132557
132969
|
if (maxUniquePhoneNumbersCount < count) {
|
|
132558
132970
|
throw new RangeError(`count exceeds max number of unique phone numbers(${maxUniquePhoneNumbersCount}).`);
|
|
132559
132971
|
}
|
|
@@ -132643,9 +133055,16 @@ class GenerateCountry extends AbstractGenerator {
|
|
|
132643
133055
|
class GenerateUniqueCountry extends AbstractGenerator {
|
|
132644
133056
|
static entityKind = 'GenerateUniqueCountry';
|
|
132645
133057
|
state;
|
|
132646
|
-
|
|
133058
|
+
isGeneratorUnique = true;
|
|
133059
|
+
maxUniqueCount = countries.length;
|
|
133060
|
+
getMaxUniqueCount() {
|
|
133061
|
+
if (this.maxUniqueCount !== undefined)
|
|
133062
|
+
return this.maxUniqueCount;
|
|
133063
|
+
this.maxUniqueCount = countries.length;
|
|
133064
|
+
return this.maxUniqueCount;
|
|
133065
|
+
}
|
|
132647
133066
|
init({ count, seed }) {
|
|
132648
|
-
if (count >
|
|
133067
|
+
if (count > this.getMaxUniqueCount()) {
|
|
132649
133068
|
throw new Error('count exceeds max number of unique countries.');
|
|
132650
133069
|
}
|
|
132651
133070
|
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$7) {
|
|
@@ -132714,11 +133133,17 @@ class GenerateStreetAddress extends AbstractGenerator {
|
|
|
132714
133133
|
class GenerateUniqueStreetAddress extends AbstractGenerator {
|
|
132715
133134
|
static entityKind = 'GenerateUniqueStreetAddress';
|
|
132716
133135
|
state;
|
|
132717
|
-
|
|
133136
|
+
isGeneratorUnique = true;
|
|
133137
|
+
streetNumbersCount = 999;
|
|
133138
|
+
maxUniqueCount = this.streetNumbersCount * (firstNames.length + lastNames.length)
|
|
133139
|
+
* streetSuffix.length;
|
|
133140
|
+
getMaxUniqueCount() {
|
|
133141
|
+
return this.maxUniqueCount;
|
|
133142
|
+
}
|
|
132718
133143
|
init({ count, seed }) {
|
|
132719
|
-
const streetNumberStrs = Array.from({ length:
|
|
132720
|
-
const maxUniqueStreetnamesNumber = streetNumberStrs.length * firstNames.length
|
|
132721
|
-
|
|
133144
|
+
const streetNumberStrs = Array.from({ length: this.streetNumbersCount }, (_, i) => String(i + 1));
|
|
133145
|
+
const maxUniqueStreetnamesNumber = streetNumberStrs.length * (firstNames.length + lastNames.length)
|
|
133146
|
+
* streetSuffix.length;
|
|
132722
133147
|
if (count > maxUniqueStreetnamesNumber) {
|
|
132723
133148
|
throw new RangeError(`count exceeds max number of unique street names(${maxUniqueStreetnamesNumber}).`);
|
|
132724
133149
|
}
|
|
@@ -132743,7 +133168,7 @@ class GenerateUniqueStreetAddress extends AbstractGenerator {
|
|
|
132743
133168
|
minValue: 0,
|
|
132744
133169
|
maxValue: streetNumberStrs.length * lastNames.length * streetSuffix.length - 1,
|
|
132745
133170
|
}),
|
|
132746
|
-
maxUniqueStreetNamesNumber: streetNumberStrs.length *
|
|
133171
|
+
maxUniqueStreetNamesNumber: streetNumberStrs.length * lastNames.length * streetSuffix.length,
|
|
132747
133172
|
count: 0,
|
|
132748
133173
|
arraysToChooseFrom: [streetNumberStrs, lastNames, streetSuffix],
|
|
132749
133174
|
},
|
|
@@ -132797,9 +133222,10 @@ class GenerateCity extends AbstractGenerator {
|
|
|
132797
133222
|
class GenerateUniqueCity extends AbstractGenerator {
|
|
132798
133223
|
static entityKind = 'GenerateUniqueCity';
|
|
132799
133224
|
state;
|
|
132800
|
-
|
|
133225
|
+
isGeneratorUnique = true;
|
|
133226
|
+
maxUniqueCount = cityNames.length;
|
|
132801
133227
|
init({ count, seed }) {
|
|
132802
|
-
if (count >
|
|
133228
|
+
if (count > this.maxUniqueCount) {
|
|
132803
133229
|
throw new Error('count exceeds max number of unique cities.');
|
|
132804
133230
|
}
|
|
132805
133231
|
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$9) {
|
|
@@ -132854,11 +133280,11 @@ class GeneratePostcode extends AbstractGenerator {
|
|
|
132854
133280
|
class GenerateUniquePostcode extends AbstractGenerator {
|
|
132855
133281
|
static entityKind = 'GenerateUniquePostcode';
|
|
132856
133282
|
state;
|
|
132857
|
-
|
|
133283
|
+
isGeneratorUnique = true;
|
|
133284
|
+
maxUniqueCount = Math.pow(10, 5) + Math.pow(10, 9);
|
|
132858
133285
|
init({ count, seed }) {
|
|
132859
|
-
|
|
132860
|
-
|
|
132861
|
-
throw new RangeError(`count exceeds max number of unique postcodes(${maxUniquePostcodeNumber}).`);
|
|
133286
|
+
if (count > this.maxUniqueCount) {
|
|
133287
|
+
throw new RangeError(`count exceeds max number of unique postcodes(${this.maxUniqueCount}).`);
|
|
132862
133288
|
}
|
|
132863
133289
|
const rng = prand.xoroshiro128plus(seed);
|
|
132864
133290
|
const templates = [
|
|
@@ -132980,12 +133406,12 @@ class GenerateCompanyName extends AbstractGenerator {
|
|
|
132980
133406
|
class GenerateUniqueCompanyName extends AbstractGenerator {
|
|
132981
133407
|
static entityKind = 'GenerateUniqueCompanyName';
|
|
132982
133408
|
state;
|
|
132983
|
-
|
|
133409
|
+
isGeneratorUnique = true;
|
|
133410
|
+
maxUniqueCount = lastNames.length * companyNameSuffixes.length + Math.pow(lastNames.length, 2)
|
|
133411
|
+
+ Math.pow(lastNames.length, 2) + Math.pow(lastNames.length, 3);
|
|
132984
133412
|
init({ count, seed }) {
|
|
132985
|
-
|
|
132986
|
-
|
|
132987
|
-
if (count > maxUniqueCompanyNameNumber) {
|
|
132988
|
-
throw new RangeError(`count exceeds max number of unique company names(${maxUniqueCompanyNameNumber}).`);
|
|
133413
|
+
if (count > this.maxUniqueCount) {
|
|
133414
|
+
throw new RangeError(`count exceeds max number of unique company names(${this.maxUniqueCount}).`);
|
|
132989
133415
|
}
|
|
132990
133416
|
// max( { template: '#', placeholdersCount: 1 }, { template: '#, # and #', placeholdersCount: 3 } )
|
|
132991
133417
|
const maxCompanyNameLength = Math.max(maxStringLength$3 + maxStringLength$8 + 1, 3 * maxStringLength$3 + 7);
|
|
@@ -133156,7 +133582,7 @@ class GeneratePoint extends AbstractGenerator {
|
|
|
133156
133582
|
}
|
|
133157
133583
|
const x = this.state.xCoordinateGen.generate();
|
|
133158
133584
|
const y = this.state.yCoordinateGen.generate();
|
|
133159
|
-
if (this.dataType === '
|
|
133585
|
+
if (this.dataType === 'object') {
|
|
133160
133586
|
return { x, y };
|
|
133161
133587
|
}
|
|
133162
133588
|
else if (this.dataType === 'string') {
|
|
@@ -133171,22 +133597,29 @@ class GeneratePoint extends AbstractGenerator {
|
|
|
133171
133597
|
class GenerateUniquePoint extends AbstractGenerator {
|
|
133172
133598
|
static entityKind = 'GenerateUniquePoint';
|
|
133173
133599
|
state;
|
|
133174
|
-
|
|
133175
|
-
|
|
133176
|
-
|
|
133177
|
-
|
|
133600
|
+
isGeneratorUnique = true;
|
|
133601
|
+
xCoordinateGen;
|
|
133602
|
+
yCoordinateGen;
|
|
133603
|
+
maxUniqueCount;
|
|
133604
|
+
constructor(params) {
|
|
133605
|
+
super(params);
|
|
133606
|
+
this.xCoordinateGen = new GenerateUniqueNumber({
|
|
133178
133607
|
minValue: this.params.minXValue,
|
|
133179
133608
|
maxValue: this.params.maxXValue,
|
|
133180
133609
|
precision: 10,
|
|
133181
133610
|
});
|
|
133182
|
-
|
|
133183
|
-
const yCoordinateGen = new GenerateUniqueNumber({
|
|
133611
|
+
this.yCoordinateGen = new GenerateUniqueNumber({
|
|
133184
133612
|
minValue: this.params.minYValue,
|
|
133185
133613
|
maxValue: this.params.maxYValue,
|
|
133186
133614
|
precision: 10,
|
|
133187
133615
|
});
|
|
133188
|
-
|
|
133189
|
-
|
|
133616
|
+
this.maxUniqueCount = Math.min(this.xCoordinateGen.maxUniqueCount, this.yCoordinateGen.maxUniqueCount);
|
|
133617
|
+
}
|
|
133618
|
+
init({ count, seed }) {
|
|
133619
|
+
// TODO: rewrite the unique generator to use fastCartesianProduct for generating unique points.
|
|
133620
|
+
this.xCoordinateGen.init({ count, seed });
|
|
133621
|
+
this.yCoordinateGen.init({ count, seed });
|
|
133622
|
+
this.state = { xCoordinateGen: this.xCoordinateGen, yCoordinateGen: this.yCoordinateGen };
|
|
133190
133623
|
}
|
|
133191
133624
|
generate() {
|
|
133192
133625
|
if (this.state === undefined) {
|
|
@@ -133194,7 +133627,7 @@ class GenerateUniquePoint extends AbstractGenerator {
|
|
|
133194
133627
|
}
|
|
133195
133628
|
const x = this.state.xCoordinateGen.generate();
|
|
133196
133629
|
const y = this.state.yCoordinateGen.generate();
|
|
133197
|
-
if (this.dataType === '
|
|
133630
|
+
if (this.dataType === 'object') {
|
|
133198
133631
|
return { x, y };
|
|
133199
133632
|
}
|
|
133200
133633
|
else if (this.dataType === 'string') {
|
|
@@ -133243,7 +133676,7 @@ class GenerateLine extends AbstractGenerator {
|
|
|
133243
133676
|
b = this.state.bCoefficientGen.generate();
|
|
133244
133677
|
}
|
|
133245
133678
|
const c = this.state.cCoefficientGen.generate();
|
|
133246
|
-
if (this.dataType === '
|
|
133679
|
+
if (this.dataType === 'object') {
|
|
133247
133680
|
return { a, b, c };
|
|
133248
133681
|
}
|
|
133249
133682
|
else if (this.dataType === 'string') {
|
|
@@ -133258,28 +133691,40 @@ class GenerateLine extends AbstractGenerator {
|
|
|
133258
133691
|
class GenerateUniqueLine extends AbstractGenerator {
|
|
133259
133692
|
static entityKind = 'GenerateUniqueLine';
|
|
133260
133693
|
state;
|
|
133261
|
-
|
|
133262
|
-
|
|
133263
|
-
|
|
133264
|
-
|
|
133694
|
+
isGeneratorUnique = true;
|
|
133695
|
+
maxUniqueCount;
|
|
133696
|
+
aCoefficientGen;
|
|
133697
|
+
bCoefficientGen;
|
|
133698
|
+
cCoefficientGen;
|
|
133699
|
+
constructor(params) {
|
|
133700
|
+
super(params);
|
|
133701
|
+
this.aCoefficientGen = new GenerateUniqueNumber({
|
|
133265
133702
|
minValue: this.params.minAValue,
|
|
133266
133703
|
maxValue: this.params.maxAValue,
|
|
133267
133704
|
precision: 10,
|
|
133268
133705
|
});
|
|
133269
|
-
|
|
133270
|
-
const bCoefficientGen = new GenerateUniqueNumber({
|
|
133706
|
+
this.bCoefficientGen = new GenerateUniqueNumber({
|
|
133271
133707
|
minValue: this.params.minBValue,
|
|
133272
133708
|
maxValue: this.params.maxBValue,
|
|
133273
133709
|
precision: 10,
|
|
133274
133710
|
});
|
|
133275
|
-
|
|
133276
|
-
const cCoefficientGen = new GenerateUniqueNumber({
|
|
133711
|
+
this.cCoefficientGen = new GenerateUniqueNumber({
|
|
133277
133712
|
minValue: this.params.minCValue,
|
|
133278
133713
|
maxValue: this.params.maxCValue,
|
|
133279
133714
|
precision: 10,
|
|
133280
133715
|
});
|
|
133281
|
-
|
|
133282
|
-
|
|
133716
|
+
this.maxUniqueCount = Math.min(this.aCoefficientGen.maxUniqueCount, this.bCoefficientGen.maxUniqueCount, this.cCoefficientGen.maxUniqueCount);
|
|
133717
|
+
}
|
|
133718
|
+
init({ count, seed }) {
|
|
133719
|
+
// TODO: rewrite the unique generator to use fastCartesianProduct for generating unique triplets(liens).
|
|
133720
|
+
this.aCoefficientGen.init({ count, seed });
|
|
133721
|
+
this.bCoefficientGen.init({ count, seed });
|
|
133722
|
+
this.cCoefficientGen.init({ count, seed });
|
|
133723
|
+
this.state = {
|
|
133724
|
+
aCoefficientGen: this.aCoefficientGen,
|
|
133725
|
+
bCoefficientGen: this.bCoefficientGen,
|
|
133726
|
+
cCoefficientGen: this.cCoefficientGen,
|
|
133727
|
+
};
|
|
133283
133728
|
}
|
|
133284
133729
|
generate() {
|
|
133285
133730
|
if (this.state === undefined) {
|
|
@@ -133292,7 +133737,7 @@ class GenerateUniqueLine extends AbstractGenerator {
|
|
|
133292
133737
|
b = this.state.bCoefficientGen.generate();
|
|
133293
133738
|
}
|
|
133294
133739
|
const c = this.state.cCoefficientGen.generate();
|
|
133295
|
-
if (this.dataType === '
|
|
133740
|
+
if (this.dataType === 'object') {
|
|
133296
133741
|
return { a, b, c };
|
|
133297
133742
|
}
|
|
133298
133743
|
else if (this.dataType === 'string') {
|
|
@@ -133337,7 +133782,15 @@ class GenerateUniqueBitString extends AbstractGenerator {
|
|
|
133337
133782
|
static entityKind = 'GenerateUniqueBitString';
|
|
133338
133783
|
dimensions = 11;
|
|
133339
133784
|
state;
|
|
133340
|
-
|
|
133785
|
+
isGeneratorUnique = true;
|
|
133786
|
+
getMaxUniqueCount() {
|
|
133787
|
+
if (this.maxUniqueCount >= 0)
|
|
133788
|
+
return this.maxUniqueCount;
|
|
133789
|
+
this.dimensions = this.params.dimensions ?? this.typeParams?.length ?? this.dimensions;
|
|
133790
|
+
this.maxUniqueCount = Math.pow(2, this.dimensions);
|
|
133791
|
+
// TODO revise: will work incorrect with this.dimensions > 53, due to node js number limitations
|
|
133792
|
+
return this.maxUniqueCount;
|
|
133793
|
+
}
|
|
133341
133794
|
init({ count, seed }) {
|
|
133342
133795
|
this.dimensions = this.params.dimensions ?? this.typeParams?.length ?? this.dimensions;
|
|
133343
133796
|
let intGen;
|
|
@@ -133408,18 +133861,35 @@ class GenerateInet extends AbstractGenerator {
|
|
|
133408
133861
|
}
|
|
133409
133862
|
}
|
|
133410
133863
|
}
|
|
133411
|
-
// TODO: add defaults to js doc
|
|
133412
133864
|
class GenerateUniqueInet extends AbstractGenerator {
|
|
133413
133865
|
static entityKind = 'GenerateUniqueInet';
|
|
133414
133866
|
ipAddress = 'ipv4';
|
|
133415
133867
|
includeCidr = true;
|
|
133416
133868
|
delimiter = '.';
|
|
133417
133869
|
state;
|
|
133418
|
-
|
|
133419
|
-
|
|
133870
|
+
isGeneratorUnique = true;
|
|
133871
|
+
maxUniqueCount;
|
|
133872
|
+
constructor(params) {
|
|
133873
|
+
super(params);
|
|
133420
133874
|
this.ipAddress = this.params.ipAddress ?? this.ipAddress;
|
|
133421
|
-
this.delimiter = this.ipAddress === 'ipv4' ? '.' : ':';
|
|
133422
133875
|
this.includeCidr = this.params.includeCidr ?? this.includeCidr;
|
|
133876
|
+
if (this.ipAddress === 'ipv4') {
|
|
133877
|
+
this.maxUniqueCount = 256 ** 4;
|
|
133878
|
+
if (this.includeCidr) {
|
|
133879
|
+
this.maxUniqueCount *= 33;
|
|
133880
|
+
}
|
|
133881
|
+
}
|
|
133882
|
+
else {
|
|
133883
|
+
// this.ipAddress === 'ipv6'
|
|
133884
|
+
// TODO revise: this.maxUniqueCount can exceed Number.MAX_SAFE_INTEGER
|
|
133885
|
+
this.maxUniqueCount = 65535 ** 8;
|
|
133886
|
+
if (this.includeCidr) {
|
|
133887
|
+
this.maxUniqueCount *= 129;
|
|
133888
|
+
}
|
|
133889
|
+
}
|
|
133890
|
+
}
|
|
133891
|
+
init({ count, seed }) {
|
|
133892
|
+
this.delimiter = this.ipAddress === 'ipv4' ? '.' : ':';
|
|
133423
133893
|
// maxValue - number of combinations for cartesian product: {0…255} × {0…255} × {0…255} × {0…255} × {0…32}
|
|
133424
133894
|
// where pattern for ipv4 ip is {0–255}.{0–255}.{0–255}.{0–255}[/{0–32}?]
|
|
133425
133895
|
// or number of combinations for cartesian product: {0…65535} × {0…65535} × {0…65535} × {0…65535} × {0…65535} × {0…65535} × {0…65535} × {0…65535} × {0…128}
|
|
@@ -133535,11 +134005,42 @@ class GenerateUniqueGeometry extends AbstractGenerator {
|
|
|
133535
134005
|
srid = 4326;
|
|
133536
134006
|
decimalPlaces = 6;
|
|
133537
134007
|
state;
|
|
133538
|
-
|
|
133539
|
-
|
|
134008
|
+
isGeneratorUnique = true;
|
|
134009
|
+
maxUniqueCount;
|
|
134010
|
+
constructor(params) {
|
|
134011
|
+
super(params);
|
|
133540
134012
|
this.type = this.params.type ?? this.type;
|
|
133541
134013
|
this.srid = this.params.srid ?? this.srid;
|
|
133542
134014
|
this.decimalPlaces = this.params.decimalPlaces ?? this.decimalPlaces;
|
|
134015
|
+
let minXValue, maxXValue, minYValue, maxYValue, denominator;
|
|
134016
|
+
if (this.type === 'point') {
|
|
134017
|
+
if (this.srid === 4326) {
|
|
134018
|
+
// Degrees (latitude / longitude)
|
|
134019
|
+
denominator = 10 ** this.decimalPlaces;
|
|
134020
|
+
minXValue = -180 * denominator;
|
|
134021
|
+
maxXValue = 180 * denominator;
|
|
134022
|
+
minYValue = -90 * denominator;
|
|
134023
|
+
maxYValue = 90 * denominator;
|
|
134024
|
+
}
|
|
134025
|
+
else {
|
|
134026
|
+
// this.srid === 3857
|
|
134027
|
+
// Meters (projected X / Y)
|
|
134028
|
+
denominator = 1;
|
|
134029
|
+
minXValue = -20026376;
|
|
134030
|
+
maxXValue = 20026376;
|
|
134031
|
+
minYValue = -20048966;
|
|
134032
|
+
maxYValue = 20048966;
|
|
134033
|
+
}
|
|
134034
|
+
}
|
|
134035
|
+
else {
|
|
134036
|
+
// error should be triggered in init method
|
|
134037
|
+
this.maxUniqueCount = -1;
|
|
134038
|
+
return;
|
|
134039
|
+
}
|
|
134040
|
+
// TODO revise: can lose accuracy due to exceeding Number.MAX_SAFE_INTEGER
|
|
134041
|
+
this.maxUniqueCount = Number(BigInt(maxXValue - minXValue + 1) * BigInt(maxYValue - minYValue + 1));
|
|
134042
|
+
}
|
|
134043
|
+
init({ count, seed }) {
|
|
133543
134044
|
let minXValue, maxXValue, minYValue, maxYValue, denominator;
|
|
133544
134045
|
if (this.type === 'point') {
|
|
133545
134046
|
if (this.srid === 4326) {
|
|
@@ -133646,11 +134147,10 @@ class GenerateUniqueVector extends AbstractGenerator {
|
|
|
133646
134147
|
maxValue = 1000;
|
|
133647
134148
|
decimalPlaces = 2;
|
|
133648
134149
|
state;
|
|
133649
|
-
|
|
133650
|
-
|
|
133651
|
-
|
|
134150
|
+
isGeneratorUnique = true;
|
|
134151
|
+
constructor(params) {
|
|
134152
|
+
super(params);
|
|
133652
134153
|
this.decimalPlaces = this.params.decimalPlaces ?? this.decimalPlaces;
|
|
133653
|
-
const denominator = 10 ** this.decimalPlaces;
|
|
133654
134154
|
this.minValue = this.params.minValue ?? this.minValue;
|
|
133655
134155
|
this.maxValue = this.params.maxValue ?? this.maxValue;
|
|
133656
134156
|
if (this.minValue > this.maxValue) {
|
|
@@ -133660,6 +134160,18 @@ class GenerateUniqueVector extends AbstractGenerator {
|
|
|
133660
134160
|
if (this.decimalPlaces < 0) {
|
|
133661
134161
|
throw new Error(`decimalPlaces value must be greater than or equal to zero.`);
|
|
133662
134162
|
}
|
|
134163
|
+
}
|
|
134164
|
+
getMaxUniqueCount() {
|
|
134165
|
+
if (this.maxUniqueCount >= 0)
|
|
134166
|
+
return this.maxUniqueCount;
|
|
134167
|
+
this.dimensions = this.params.dimensions ?? this.typeParams.length ?? this.dimensions;
|
|
134168
|
+
const denominator = 10 ** this.decimalPlaces;
|
|
134169
|
+
this.maxUniqueCount = (this.maxValue * denominator - this.minValue * denominator + 1) ** this.dimensions;
|
|
134170
|
+
return this.maxUniqueCount;
|
|
134171
|
+
}
|
|
134172
|
+
init({ count, seed }) {
|
|
134173
|
+
this.dimensions = this.params.dimensions ?? this.typeParams.length ?? this.dimensions;
|
|
134174
|
+
const denominator = 10 ** this.decimalPlaces;
|
|
133663
134175
|
if (abs(BigInt(this.minValue) * BigInt(denominator)) > Number.MAX_SAFE_INTEGER
|
|
133664
134176
|
|| abs(BigInt(this.maxValue) * BigInt(denominator)) > Number.MAX_SAFE_INTEGER) {
|
|
133665
134177
|
console.warn(`vector generator: minValue or maxValue multiplied by 10^decimalPlaces exceeds Number.MAX_SAFE_INTEGER (2^53 -1).\n`
|
|
@@ -133695,13 +134207,80 @@ class GenerateUniqueVector extends AbstractGenerator {
|
|
|
133695
134207
|
return vector;
|
|
133696
134208
|
}
|
|
133697
134209
|
}
|
|
134210
|
+
class GenerateCompositeUniqueKey extends AbstractGenerator {
|
|
134211
|
+
static entityKind = 'GenerateCompositeUniqueKey';
|
|
134212
|
+
columnGenerators = [];
|
|
134213
|
+
isInitialized = false;
|
|
134214
|
+
state;
|
|
134215
|
+
addGenerator(columnName, generator) {
|
|
134216
|
+
this.columnGenerators.push({ columnName, generator });
|
|
134217
|
+
}
|
|
134218
|
+
init({ count, seed }) {
|
|
134219
|
+
if (this.isInitialized)
|
|
134220
|
+
return;
|
|
134221
|
+
if (this.columnGenerators.length === 0) {
|
|
134222
|
+
throw new Error(`composite unique key generator has no generators to work with.`);
|
|
134223
|
+
}
|
|
134224
|
+
let countPerGen = Math.ceil(count ** (1 / this.columnGenerators.length));
|
|
134225
|
+
// const gensMaxUniqueCount: { columnName: string; count: number; maxUniqueCount: number }[] = [];
|
|
134226
|
+
for (const colGen of this.columnGenerators) {
|
|
134227
|
+
colGen.maxUniqueCount = colGen.generator.getMaxUniqueCount();
|
|
134228
|
+
}
|
|
134229
|
+
this.columnGenerators.sort((a, b) => a.maxUniqueCount - b.maxUniqueCount);
|
|
134230
|
+
let currCount = count;
|
|
134231
|
+
let canGenerate = false;
|
|
134232
|
+
for (const [idx, colGen] of this.columnGenerators.entries()) {
|
|
134233
|
+
if (colGen.maxUniqueCount < countPerGen) {
|
|
134234
|
+
colGen.count = colGen.maxUniqueCount;
|
|
134235
|
+
currCount /= colGen.count;
|
|
134236
|
+
countPerGen = Math.ceil(currCount ** (1 / (this.columnGenerators.length - idx - 1)));
|
|
134237
|
+
canGenerate = false;
|
|
134238
|
+
}
|
|
134239
|
+
else {
|
|
134240
|
+
colGen.count = countPerGen;
|
|
134241
|
+
canGenerate = true;
|
|
134242
|
+
}
|
|
134243
|
+
}
|
|
134244
|
+
if (!canGenerate) {
|
|
134245
|
+
const colGensCountInfo = this.columnGenerators.map((colGen) => `generator:${colGen.generator.getEntityKind()};count:${colGen.count}`).join('\n');
|
|
134246
|
+
throw new Error(`There are no enough unique values in each generator to generate ${count} values; \n${colGensCountInfo}`);
|
|
134247
|
+
}
|
|
134248
|
+
const sets = [];
|
|
134249
|
+
for (const colGen of this.columnGenerators) {
|
|
134250
|
+
colGen.generator.init({ count: colGen.count, seed });
|
|
134251
|
+
const setI = [];
|
|
134252
|
+
for (let i = 0; i < countPerGen; i++) {
|
|
134253
|
+
setI.push(colGen.generator.generate({ i }));
|
|
134254
|
+
}
|
|
134255
|
+
sets.push(setI);
|
|
134256
|
+
}
|
|
134257
|
+
this.state = { sets, currI: -1, currValue: {} };
|
|
134258
|
+
this.isInitialized = true;
|
|
134259
|
+
}
|
|
134260
|
+
generate({ i, columnName }) {
|
|
134261
|
+
if (this.state === undefined) {
|
|
134262
|
+
throw new Error('state is not defined.');
|
|
134263
|
+
}
|
|
134264
|
+
if (i > this.state.currI) {
|
|
134265
|
+
const rowI = fastCartesianProduct(this.state.sets, i);
|
|
134266
|
+
const newCurrValue = {};
|
|
134267
|
+
for (const [idx, colGen] of this.columnGenerators.entries()) {
|
|
134268
|
+
newCurrValue[colGen.columnName] = rowI[idx];
|
|
134269
|
+
}
|
|
134270
|
+
this.state.currValue = newCurrValue;
|
|
134271
|
+
this.state.currI = i;
|
|
134272
|
+
}
|
|
134273
|
+
return this.state.currValue[columnName];
|
|
134274
|
+
}
|
|
134275
|
+
}
|
|
133698
134276
|
|
|
133699
134277
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
133700
134278
|
class GenerateUniqueIntervalV2 extends AbstractGenerator {
|
|
133701
134279
|
static 'entityKind' = 'GenerateUniqueInterval';
|
|
133702
134280
|
static version = 2;
|
|
133703
134281
|
state;
|
|
133704
|
-
|
|
134282
|
+
isGeneratorUnique = true;
|
|
134283
|
+
maxUniqueCount;
|
|
133705
134284
|
config = {
|
|
133706
134285
|
year: {
|
|
133707
134286
|
from: 0,
|
|
@@ -133728,29 +134307,33 @@ class GenerateUniqueIntervalV2 extends AbstractGenerator {
|
|
|
133728
134307
|
to: 59,
|
|
133729
134308
|
},
|
|
133730
134309
|
};
|
|
133731
|
-
|
|
134310
|
+
fieldsToGenerate;
|
|
134311
|
+
constructor(params) {
|
|
134312
|
+
super(params);
|
|
133732
134313
|
const allFields = ['year', 'month', 'day', 'hour', 'minute', 'second'];
|
|
133733
|
-
|
|
134314
|
+
this.fieldsToGenerate = allFields;
|
|
133734
134315
|
if (this.params.fields !== undefined && this.params.fields?.includes(' to ')) {
|
|
133735
134316
|
const tokens = this.params.fields.split(' to ');
|
|
133736
134317
|
const endIdx = allFields.indexOf(tokens[1]);
|
|
133737
|
-
fieldsToGenerate = allFields.slice(0, endIdx + 1);
|
|
134318
|
+
this.fieldsToGenerate = allFields.slice(0, endIdx + 1);
|
|
133738
134319
|
}
|
|
133739
134320
|
else if (this.params.fields !== undefined) {
|
|
133740
134321
|
const endIdx = allFields.indexOf(this.params.fields);
|
|
133741
|
-
fieldsToGenerate = allFields.slice(0, endIdx + 1);
|
|
134322
|
+
this.fieldsToGenerate = allFields.slice(0, endIdx + 1);
|
|
133742
134323
|
}
|
|
133743
|
-
|
|
133744
|
-
for (const field of fieldsToGenerate) {
|
|
134324
|
+
this.maxUniqueCount = 1;
|
|
134325
|
+
for (const field of this.fieldsToGenerate) {
|
|
133745
134326
|
const from = this.config[field].from, to = this.config[field].to;
|
|
133746
|
-
|
|
134327
|
+
this.maxUniqueCount *= from - to + 1;
|
|
133747
134328
|
}
|
|
133748
|
-
|
|
133749
|
-
|
|
134329
|
+
}
|
|
134330
|
+
init({ count, seed }) {
|
|
134331
|
+
if (count > this.maxUniqueCount) {
|
|
134332
|
+
throw new RangeError(`count exceeds max number of unique intervals(${this.maxUniqueCount})`);
|
|
133750
134333
|
}
|
|
133751
134334
|
const rng = prand.xoroshiro128plus(seed);
|
|
133752
134335
|
const intervalSet = new Set();
|
|
133753
|
-
this.state = { rng, fieldsToGenerate, intervalSet };
|
|
134336
|
+
this.state = { rng, fieldsToGenerate: this.fieldsToGenerate, intervalSet };
|
|
133754
134337
|
}
|
|
133755
134338
|
generate() {
|
|
133756
134339
|
if (this.state === undefined) {
|
|
@@ -133772,6 +134355,7 @@ class GenerateUniqueIntervalV2 extends AbstractGenerator {
|
|
|
133772
134355
|
return interval;
|
|
133773
134356
|
}
|
|
133774
134357
|
}
|
|
134358
|
+
// TODO need to rework this generator
|
|
133775
134359
|
class GenerateStringV2 extends AbstractGenerator {
|
|
133776
134360
|
static 'entityKind' = 'GenerateString';
|
|
133777
134361
|
static version = 2;
|
|
@@ -133804,7 +134388,7 @@ class GenerateStringV2 extends AbstractGenerator {
|
|
|
133804
134388
|
[idx, this.state.rng] = prand.uniformIntDistribution(0, stringChars.length - 1, this.state.rng);
|
|
133805
134389
|
currStr += stringChars[idx];
|
|
133806
134390
|
}
|
|
133807
|
-
if (this.dataType === '
|
|
134391
|
+
if (this.dataType === 'object')
|
|
133808
134392
|
return Buffer.from(currStr);
|
|
133809
134393
|
return currStr;
|
|
133810
134394
|
}
|
|
@@ -133813,21 +134397,27 @@ class GenerateUniqueStringV2 extends AbstractGenerator {
|
|
|
133813
134397
|
static 'entityKind' = 'GenerateUniqueString';
|
|
133814
134398
|
static version = 2;
|
|
133815
134399
|
state;
|
|
133816
|
-
|
|
134400
|
+
isGeneratorUnique = true;
|
|
134401
|
+
maxStringLength = 20;
|
|
134402
|
+
minStringLength = 7;
|
|
134403
|
+
getMaxUniqueCount() {
|
|
134404
|
+
if (this.maxUniqueCount >= 0)
|
|
134405
|
+
return this.maxUniqueCount;
|
|
134406
|
+
this.maxStringLength = this.typeParams?.length ?? this.maxStringLength;
|
|
134407
|
+
this.maxUniqueCount = Number.parseInt('f'.repeat(this.maxStringLength), 16);
|
|
134408
|
+
return this.maxUniqueCount;
|
|
134409
|
+
}
|
|
133817
134410
|
init({ seed, count }) {
|
|
133818
134411
|
const rng = prand.xoroshiro128plus(seed);
|
|
133819
|
-
let minStringLength = 7;
|
|
133820
|
-
let maxStringLength = 20;
|
|
133821
134412
|
// TODO: revise later
|
|
133822
|
-
|
|
133823
|
-
|
|
133824
|
-
|
|
133825
|
-
minStringLength = maxStringLength;
|
|
134413
|
+
this.maxStringLength = this.typeParams?.length ?? this.maxStringLength;
|
|
134414
|
+
if (this.maxStringLength === 1 || this.maxStringLength < this.minStringLength) {
|
|
134415
|
+
this.minStringLength = this.maxStringLength;
|
|
133826
134416
|
}
|
|
133827
|
-
if (
|
|
133828
|
-
throw new Error(`You can't generate ${count} unique strings, with a maximum string length of ${maxStringLength}.`);
|
|
134417
|
+
if (count > this.getMaxUniqueCount()) {
|
|
134418
|
+
throw new Error(`You can't generate ${count} unique strings, with a maximum string length of ${this.maxStringLength}.`);
|
|
133829
134419
|
}
|
|
133830
|
-
this.state = { rng, minStringLength, maxStringLength };
|
|
134420
|
+
this.state = { rng, minStringLength: this.minStringLength, maxStringLength: this.maxStringLength };
|
|
133831
134421
|
}
|
|
133832
134422
|
generate({ i }) {
|
|
133833
134423
|
if (this.state === undefined) {
|
|
@@ -133844,6 +134434,8 @@ class GenerateUniqueStringV2 extends AbstractGenerator {
|
|
|
133844
134434
|
[idx, this.state.rng] = prand.uniformIntDistribution(0, stringChars.length - 1, this.state.rng);
|
|
133845
134435
|
currStr += stringChars[idx];
|
|
133846
134436
|
}
|
|
134437
|
+
if (this.dataType === 'object')
|
|
134438
|
+
return Buffer.from(uniqueStr + currStr);
|
|
133847
134439
|
return uniqueStr + currStr;
|
|
133848
134440
|
}
|
|
133849
134441
|
}
|
|
@@ -134821,6 +135413,9 @@ const generatorsMap = {
|
|
|
134821
135413
|
GenerateUniqueVector: [
|
|
134822
135414
|
GenerateUniqueVector,
|
|
134823
135415
|
],
|
|
135416
|
+
GenerateCompositeUniqueKey: [
|
|
135417
|
+
GenerateCompositeUniqueKey,
|
|
135418
|
+
],
|
|
134824
135419
|
};
|
|
134825
135420
|
|
|
134826
135421
|
// TODO: revise serial part generators
|
|
@@ -134976,8 +135571,8 @@ const selectGeneratorForCockroachColumn = (table, col) => {
|
|
|
134976
135571
|
const generator = new generatorsMap.GenerateUUID[0]();
|
|
134977
135572
|
return generator;
|
|
134978
135573
|
}
|
|
134979
|
-
//
|
|
134980
|
-
if (col.columnType === '
|
|
135574
|
+
// BOOL
|
|
135575
|
+
if (col.columnType === 'bool') {
|
|
134981
135576
|
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
134982
135577
|
return generator;
|
|
134983
135578
|
}
|
|
@@ -135805,7 +136400,7 @@ const selectGeneratorForSqlite = (table, col) => {
|
|
|
135805
136400
|
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135806
136401
|
return generator;
|
|
135807
136402
|
}
|
|
135808
|
-
if ((col.columnType === 'integer' && col.dataType === '
|
|
136403
|
+
if ((col.columnType === 'integer' && col.dataType === 'object')) {
|
|
135809
136404
|
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135810
136405
|
return generator;
|
|
135811
136406
|
}
|
|
@@ -135876,49 +136471,6 @@ const selectGeneratorForSqlite = (table, col) => {
|
|
|
135876
136471
|
return generator;
|
|
135877
136472
|
};
|
|
135878
136473
|
|
|
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
136474
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
135923
136475
|
class SeedService {
|
|
135924
136476
|
static entityKind = 'SeedService';
|
|
@@ -135958,6 +136510,7 @@ class SeedService {
|
|
|
135958
136510
|
withFromTable: {},
|
|
135959
136511
|
}));
|
|
135960
136512
|
for (const [i, table] of tables.entries()) {
|
|
136513
|
+
const compositeUniqueKeyGenMap = {};
|
|
135961
136514
|
// get foreignKey columns relations
|
|
135962
136515
|
const foreignKeyColumns = {};
|
|
135963
136516
|
for (const rel of relations
|
|
@@ -136106,16 +136659,66 @@ class SeedService {
|
|
|
136106
136659
|
columnPossibleGenerator.generator = arrayGen;
|
|
136107
136660
|
}
|
|
136108
136661
|
columnPossibleGenerator.generator.isUnique = col.isUnique;
|
|
136662
|
+
// composite unique keys handling
|
|
136663
|
+
let compositeKeyColumnNames = table.uniqueConstraints.filter((colNames) => colNames.includes(col.name));
|
|
136664
|
+
if (compositeKeyColumnNames.some((colNames) => colNames.length === 1)) {
|
|
136665
|
+
// composite unique key contains only one column, therefore it equals to just unique column
|
|
136666
|
+
columnPossibleGenerator.generator.isUnique = true;
|
|
136667
|
+
}
|
|
136668
|
+
// removing column from composite unique keys if current column is unique
|
|
136669
|
+
if (columnPossibleGenerator.generator.isUnique && compositeKeyColumnNames.length > 0) {
|
|
136670
|
+
const newUniqueConstraints = [];
|
|
136671
|
+
for (const colNames of table.uniqueConstraints) {
|
|
136672
|
+
if (colNames.includes(col.name)) {
|
|
136673
|
+
const newColNames = colNames.filter((colName) => colName !== col.name);
|
|
136674
|
+
if (newColNames.length === 0)
|
|
136675
|
+
continue;
|
|
136676
|
+
newUniqueConstraints.push(newColNames);
|
|
136677
|
+
}
|
|
136678
|
+
else {
|
|
136679
|
+
newUniqueConstraints.push(colNames);
|
|
136680
|
+
}
|
|
136681
|
+
}
|
|
136682
|
+
table.uniqueConstraints = newUniqueConstraints;
|
|
136683
|
+
}
|
|
136684
|
+
compositeKeyColumnNames = table.uniqueConstraints.filter((colNames) => colNames.includes(col.name));
|
|
136685
|
+
if (compositeKeyColumnNames.length > 1) {
|
|
136686
|
+
throw new Error('Currently, multiple composite unique keys that share the same column are not supported.');
|
|
136687
|
+
}
|
|
136688
|
+
// to handle composite unique key generation, I will need a unique generator for each column in the composite key
|
|
136689
|
+
if (compositeKeyColumnNames.length === 1) {
|
|
136690
|
+
if (columnPossibleGenerator.generator.params.isUnique === false) {
|
|
136691
|
+
throw new Error(`To handle the composite unique key on columns: ${compositeKeyColumnNames[0]}, `
|
|
136692
|
+
+ `column: ${col.name} should either be assigned a generator with isUnique set to true, or have isUnique omitted.`);
|
|
136693
|
+
}
|
|
136694
|
+
columnPossibleGenerator.generator.params.isUnique = true;
|
|
136695
|
+
}
|
|
136109
136696
|
const uniqueGen = columnPossibleGenerator.generator.replaceIfUnique();
|
|
136110
136697
|
if (uniqueGen !== undefined) {
|
|
136111
136698
|
columnPossibleGenerator.generator = uniqueGen;
|
|
136112
136699
|
}
|
|
136700
|
+
if (compositeKeyColumnNames.length === 1 && !columnPossibleGenerator.generator.isGeneratorUnique
|
|
136701
|
+
&& !(columnPossibleGenerator.generator.getEntityKind() === 'GenerateValuesFromArray')) {
|
|
136702
|
+
throw new Error(`To handle the composite unique key on columns: ${compositeKeyColumnNames[0]}, `
|
|
136703
|
+
+ `column: ${col.name} should be assigned a generator with its own unique version.`);
|
|
136704
|
+
}
|
|
136113
136705
|
// selecting version of generator
|
|
136114
136706
|
columnPossibleGenerator.generator = this.selectVersionOfGenerator(columnPossibleGenerator.generator);
|
|
136115
136707
|
// TODO: for now only GenerateValuesFromArray support notNull property
|
|
136116
136708
|
columnPossibleGenerator.generator.notNull = col.notNull;
|
|
136117
136709
|
columnPossibleGenerator.generator.dataType = col.dataType;
|
|
136118
|
-
//
|
|
136710
|
+
// assigning composite key generator
|
|
136711
|
+
if (compositeKeyColumnNames.length === 1) {
|
|
136712
|
+
const key = compositeKeyColumnNames[0].join('_');
|
|
136713
|
+
if (compositeUniqueKeyGenMap[key] === undefined) {
|
|
136714
|
+
let compositeUniqueKeyGen = new generatorsMap.GenerateCompositeUniqueKey[0]();
|
|
136715
|
+
compositeUniqueKeyGen.uniqueKey = key;
|
|
136716
|
+
compositeUniqueKeyGen = this.selectVersionOfGenerator(compositeUniqueKeyGen);
|
|
136717
|
+
compositeUniqueKeyGenMap[key] = compositeUniqueKeyGen;
|
|
136718
|
+
}
|
|
136719
|
+
compositeUniqueKeyGenMap[key].addGenerator(col.name, columnPossibleGenerator.generator);
|
|
136720
|
+
columnPossibleGenerator.generator = compositeUniqueKeyGenMap[key];
|
|
136721
|
+
}
|
|
136119
136722
|
tablePossibleGenerators.columnsPossibleGenerators.push(columnPossibleGenerator);
|
|
136120
136723
|
}
|
|
136121
136724
|
}
|
|
@@ -136144,6 +136747,7 @@ class SeedService {
|
|
|
136144
136747
|
newGenerator.dataType = generator.dataType;
|
|
136145
136748
|
// newGenerator.stringLength = generator.stringLength;
|
|
136146
136749
|
newGenerator.typeParams = generator.typeParams ?? newGenerator.typeParams;
|
|
136750
|
+
newGenerator.uniqueKey = generator.uniqueKey;
|
|
136147
136751
|
return newGenerator;
|
|
136148
136752
|
};
|
|
136149
136753
|
cyclicTablesCompare = (table1, table2, relation, reverseRelation) => {
|
|
@@ -136304,8 +136908,11 @@ class SeedService {
|
|
|
136304
136908
|
const columnRelations = filteredRelations.filter((rel) => rel.columns.includes(col.columnName));
|
|
136305
136909
|
pRNGSeed = (columnRelations.length !== 0
|
|
136306
136910
|
&& columnRelations[0].columns.length >= 2)
|
|
136307
|
-
? (customSeed
|
|
136308
|
-
|
|
136911
|
+
? (customSeed
|
|
136912
|
+
+ generateHashFromString(`${columnRelations[0].table}.${columnRelations[0].columns.join('_')}`))
|
|
136913
|
+
: col.generator?.uniqueKey === undefined
|
|
136914
|
+
? (customSeed + generateHashFromString(`${table.tableName}.${col.columnName}`))
|
|
136915
|
+
: (customSeed + generateHashFromString(col.generator.uniqueKey));
|
|
136309
136916
|
tableGenerators[col.columnName] = {
|
|
136310
136917
|
pRNGSeed,
|
|
136311
136918
|
...col,
|
|
@@ -136476,11 +137083,7 @@ class SeedService {
|
|
|
136476
137083
|
row = {};
|
|
136477
137084
|
generatedValues.push(row);
|
|
136478
137085
|
for (const columnName of Object.keys(columnsGenerators)) {
|
|
136479
|
-
|
|
136480
|
-
// | string
|
|
136481
|
-
// | number
|
|
136482
|
-
// | boolean;
|
|
136483
|
-
generatedValue = columnsGenerators[columnName].generate({ i });
|
|
137086
|
+
generatedValue = columnsGenerators[columnName].generate({ i, columnName });
|
|
136484
137087
|
row[columnName] = generatedValue;
|
|
136485
137088
|
}
|
|
136486
137089
|
if ((insertDataInDb === true || updateDataInDb === true)
|
|
@@ -136620,14 +137223,14 @@ const resetCockroach = async (db, cockroachTables) => {
|
|
|
136620
137223
|
await db.execute(drizzleOrm.sql.raw(`truncate ${tablesToTruncate.join(',')} cascade;`));
|
|
136621
137224
|
};
|
|
136622
137225
|
const filterCockroachSchema = (schema) => {
|
|
136623
|
-
const cockroachSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], cockroachCore.CockroachTable) || drizzleOrm.is(keyValue[1],
|
|
137226
|
+
const cockroachSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], cockroachCore.CockroachTable) || drizzleOrm.is(keyValue[1], _relations.Relations)));
|
|
136624
137227
|
const cockroachTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], cockroachCore.CockroachTable)));
|
|
136625
137228
|
return { cockroachSchema, cockroachTables };
|
|
136626
137229
|
};
|
|
136627
137230
|
const seedCockroach = async (db, schema, options = {}, refinements) => {
|
|
136628
137231
|
const seedService = new SeedService();
|
|
136629
137232
|
const { cockroachSchema, cockroachTables } = filterCockroachSchema(schema);
|
|
136630
|
-
const { tables, relations } =
|
|
137233
|
+
const { tables, relations } = getSchemaInfo(cockroachSchema, cockroachTables, mapCockroachColumns);
|
|
136631
137234
|
const generatedTablesGenerators = seedService.generatePossibleGenerators('cockroach', tables, relations, refinements, options);
|
|
136632
137235
|
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
136633
137236
|
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, cockroachTables, { ...options, preserveCyclicTablesData });
|
|
@@ -136635,197 +137238,76 @@ const seedCockroach = async (db, schema, options = {}, refinements) => {
|
|
|
136635
137238
|
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
136636
137239
|
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, cockroachTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
136637
137240
|
};
|
|
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;
|
|
137241
|
+
const mapCockroachColumns = (tableConfig, dbToTsColumnNamesMap) => {
|
|
137242
|
+
const getAllBaseColumns = (baseColumn) => {
|
|
137243
|
+
const baseColumnResult = {
|
|
137244
|
+
name: baseColumn.name,
|
|
137245
|
+
columnType: baseColumn.getSQLType(),
|
|
137246
|
+
typeParams: getTypeParams(baseColumn.getSQLType()),
|
|
137247
|
+
dataType: baseColumn.dataType.split(' ')[0],
|
|
137248
|
+
size: baseColumn.length,
|
|
137249
|
+
hasDefault: baseColumn.hasDefault,
|
|
137250
|
+
enumValues: baseColumn.enumValues,
|
|
137251
|
+
default: baseColumn.default,
|
|
137252
|
+
isUnique: baseColumn.isUnique,
|
|
137253
|
+
notNull: baseColumn.notNull,
|
|
137254
|
+
primary: baseColumn.primary,
|
|
137255
|
+
baseColumn: baseColumn.baseColumn === undefined ? undefined : getAllBaseColumns(baseColumn.baseColumn),
|
|
137256
|
+
};
|
|
137257
|
+
return baseColumnResult;
|
|
136659
137258
|
};
|
|
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);
|
|
137259
|
+
const getTypeParams = (sqlType) => {
|
|
137260
|
+
// get type params
|
|
137261
|
+
const typeParams = {};
|
|
137262
|
+
// handle dimensions
|
|
137263
|
+
if (sqlType.includes('[')) {
|
|
137264
|
+
const match = sqlType.match(/\[\w*]/g);
|
|
137265
|
+
if (match) {
|
|
137266
|
+
typeParams['dimensions'] = match.length;
|
|
136705
137267
|
}
|
|
136706
137268
|
}
|
|
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] = [];
|
|
137269
|
+
if (sqlType.startsWith('numeric')
|
|
137270
|
+
|| sqlType.startsWith('decimal')
|
|
137271
|
+
|| sqlType.startsWith('double precision')
|
|
137272
|
+
|| sqlType.startsWith('real')) {
|
|
137273
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137274
|
+
if (match) {
|
|
137275
|
+
typeParams['precision'] = Number(match[1]);
|
|
137276
|
+
typeParams['scale'] = Number(match[2]);
|
|
136722
137277
|
}
|
|
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
137278
|
}
|
|
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
|
-
}
|
|
137279
|
+
else if (sqlType.startsWith('varchar')
|
|
137280
|
+
|| sqlType.startsWith('char')
|
|
137281
|
+
|| sqlType.startsWith('bit')
|
|
137282
|
+
|| sqlType.startsWith('vector')
|
|
137283
|
+
|| sqlType.startsWith('time')
|
|
137284
|
+
|| sqlType.startsWith('timestamp')
|
|
137285
|
+
|| sqlType.startsWith('interval')) {
|
|
137286
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137287
|
+
if (match) {
|
|
137288
|
+
typeParams['length'] = Number(match[1]);
|
|
136776
137289
|
}
|
|
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
137290
|
}
|
|
136825
|
-
|
|
136826
|
-
|
|
136827
|
-
|
|
136828
|
-
|
|
137291
|
+
return typeParams;
|
|
137292
|
+
};
|
|
137293
|
+
const mappedColumns = tableConfig.columns.map((column) => ({
|
|
137294
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137295
|
+
columnType: column.getSQLType(),
|
|
137296
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137297
|
+
dataType: column.dataType.split(' ')[0],
|
|
137298
|
+
size: column.length,
|
|
137299
|
+
hasDefault: column.hasDefault,
|
|
137300
|
+
default: column.default,
|
|
137301
|
+
enumValues: column.enumValues,
|
|
137302
|
+
isUnique: column.isUnique,
|
|
137303
|
+
notNull: column.notNull,
|
|
137304
|
+
primary: column.primary,
|
|
137305
|
+
generatedIdentityType: column.generatedIdentity?.type,
|
|
137306
|
+
baseColumn: (column.baseColumn === undefined)
|
|
137307
|
+
? undefined
|
|
137308
|
+
: getAllBaseColumns(column.baseColumn),
|
|
137309
|
+
}));
|
|
137310
|
+
return mappedColumns;
|
|
136829
137311
|
};
|
|
136830
137312
|
|
|
136831
137313
|
// MySql-----------------------------------------------------------------------------------------------------
|
|
@@ -136908,13 +137390,13 @@ const resetMsSql = async (db, schema) => {
|
|
|
136908
137390
|
}
|
|
136909
137391
|
};
|
|
136910
137392
|
const filterMsSqlTables = (schema) => {
|
|
136911
|
-
const mssqlSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mssqlCore.MsSqlTable) || drizzleOrm.is(keyValue[1],
|
|
137393
|
+
const mssqlSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mssqlCore.MsSqlTable) || drizzleOrm.is(keyValue[1], _relations.Relations)));
|
|
136912
137394
|
const mssqlTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mssqlCore.MsSqlTable)));
|
|
136913
137395
|
return { mssqlSchema, mssqlTables };
|
|
136914
137396
|
};
|
|
136915
137397
|
const seedMsSql = async (db, schema, options = {}, refinements) => {
|
|
136916
137398
|
const { mssqlSchema, mssqlTables } = filterMsSqlTables(schema);
|
|
136917
|
-
const { tables, relations } =
|
|
137399
|
+
const { tables, relations } = getSchemaInfo(mssqlSchema, mssqlTables, mapMsSqlColumns);
|
|
136918
137400
|
const seedService = new SeedService();
|
|
136919
137401
|
const generatedTablesGenerators = seedService.generatePossibleGenerators('mssql', tables, relations, refinements, options);
|
|
136920
137402
|
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
@@ -136923,163 +137405,45 @@ const seedMsSql = async (db, schema, options = {}, refinements) => {
|
|
|
136923
137405
|
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
136924
137406
|
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, mssqlTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
136925
137407
|
};
|
|
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);
|
|
137408
|
+
const mapMsSqlColumns = (tableConfig, dbToTsColumnNamesMap) => {
|
|
137409
|
+
// TODO: rewrite
|
|
137410
|
+
const getTypeParams = (sqlType) => {
|
|
137411
|
+
// get type params and set only type
|
|
137412
|
+
const typeParams = {};
|
|
137413
|
+
if (sqlType.startsWith('decimal')
|
|
137414
|
+
|| sqlType.startsWith('real')
|
|
137415
|
+
|| sqlType.startsWith('float')) {
|
|
137416
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137417
|
+
if (match) {
|
|
137418
|
+
typeParams['precision'] = Number(match[1]);
|
|
137419
|
+
typeParams['scale'] = Number(match[2]);
|
|
136993
137420
|
}
|
|
136994
137421
|
}
|
|
136995
|
-
|
|
136996
|
-
|
|
136997
|
-
|
|
136998
|
-
|
|
136999
|
-
|
|
137000
|
-
|
|
137001
|
-
|
|
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] = [];
|
|
137422
|
+
else if (sqlType.startsWith('char')
|
|
137423
|
+
|| sqlType.startsWith('varchar')
|
|
137424
|
+
|| sqlType.startsWith('binary')
|
|
137425
|
+
|| sqlType.startsWith('varbinary')) {
|
|
137426
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137427
|
+
if (match) {
|
|
137428
|
+
typeParams['length'] = Number(match[1]);
|
|
137009
137429
|
}
|
|
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
137430
|
}
|
|
137026
|
-
|
|
137027
|
-
|
|
137028
|
-
|
|
137029
|
-
|
|
137030
|
-
|
|
137031
|
-
|
|
137032
|
-
|
|
137033
|
-
|
|
137034
|
-
|
|
137035
|
-
|
|
137036
|
-
|
|
137037
|
-
|
|
137038
|
-
|
|
137039
|
-
|
|
137040
|
-
|
|
137041
|
-
|
|
137042
|
-
|| sqlType.startsWith('binary')
|
|
137043
|
-
|| sqlType.startsWith('varbinary')) {
|
|
137044
|
-
const match = sqlType.match(/\((\d+)\)/);
|
|
137045
|
-
if (match) {
|
|
137046
|
-
typeParams['length'] = Number(match[1]);
|
|
137047
|
-
}
|
|
137048
|
-
}
|
|
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
|
-
}
|
|
137079
|
-
tableRel['isCyclic'] = false;
|
|
137080
|
-
return { ...relI, isCyclic: false };
|
|
137081
|
-
});
|
|
137082
|
-
return { tables, relations: modifiedRelations, tableRelations };
|
|
137431
|
+
return typeParams;
|
|
137432
|
+
};
|
|
137433
|
+
const mappedColumns = tableConfig.columns.map((column) => ({
|
|
137434
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137435
|
+
columnType: column.getSQLType(),
|
|
137436
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137437
|
+
dataType: column.dataType.split(' ')[0],
|
|
137438
|
+
hasDefault: column.hasDefault,
|
|
137439
|
+
default: column.default,
|
|
137440
|
+
enumValues: column.enumValues,
|
|
137441
|
+
isUnique: column.isUnique,
|
|
137442
|
+
notNull: column.notNull,
|
|
137443
|
+
primary: column.primary,
|
|
137444
|
+
identity: column.identity ? true : false,
|
|
137445
|
+
}));
|
|
137446
|
+
return mappedColumns;
|
|
137083
137447
|
};
|
|
137084
137448
|
|
|
137085
137449
|
// MySql-----------------------------------------------------------------------------------------------------
|
|
@@ -137096,13 +137460,13 @@ const resetMySql = async (db, schema) => {
|
|
|
137096
137460
|
await db.execute(drizzleOrm.sql.raw('SET FOREIGN_KEY_CHECKS = 1;'));
|
|
137097
137461
|
};
|
|
137098
137462
|
const filterMysqlTables = (schema) => {
|
|
137099
|
-
const mysqlSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mysqlCore.MySqlTable) || drizzleOrm.is(keyValue[1],
|
|
137463
|
+
const mysqlSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mysqlCore.MySqlTable) || drizzleOrm.is(keyValue[1], _relations.Relations)));
|
|
137100
137464
|
const mysqlTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mysqlCore.MySqlTable)));
|
|
137101
137465
|
return { mysqlSchema, mysqlTables };
|
|
137102
137466
|
};
|
|
137103
137467
|
const seedMySql = async (db, schema, options = {}, refinements) => {
|
|
137104
137468
|
const { mysqlSchema, mysqlTables } = filterMysqlTables(schema);
|
|
137105
|
-
const { tables, relations } =
|
|
137469
|
+
const { tables, relations } = getSchemaInfo(mysqlSchema, mysqlTables, mapMySqlColumns);
|
|
137106
137470
|
const seedService = new SeedService();
|
|
137107
137471
|
const generatedTablesGenerators = seedService.generatePossibleGenerators('mysql', tables, relations, refinements, options);
|
|
137108
137472
|
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
@@ -137111,162 +137475,44 @@ const seedMySql = async (db, schema, options = {}, refinements) => {
|
|
|
137111
137475
|
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
137112
137476
|
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, mysqlTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
137113
137477
|
};
|
|
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] = [];
|
|
137478
|
+
const mapMySqlColumns = (tableConfig, dbToTsColumnNamesMap) => {
|
|
137479
|
+
const getTypeParams = (sqlType) => {
|
|
137480
|
+
// get type params and set only type
|
|
137481
|
+
const typeParams = {};
|
|
137482
|
+
if (sqlType.startsWith('decimal')
|
|
137483
|
+
|| sqlType.startsWith('real')
|
|
137484
|
+
|| sqlType.startsWith('double')
|
|
137485
|
+
|| sqlType.startsWith('float')) {
|
|
137486
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137487
|
+
if (match) {
|
|
137488
|
+
typeParams['precision'] = Number(match[1]);
|
|
137489
|
+
typeParams['scale'] = Number(match[2]);
|
|
137197
137490
|
}
|
|
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
137491
|
}
|
|
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
|
-
}
|
|
137227
|
-
}
|
|
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
|
-
}
|
|
137492
|
+
else if (sqlType.startsWith('char')
|
|
137493
|
+
|| sqlType.startsWith('varchar')
|
|
137494
|
+
|| sqlType.startsWith('binary')
|
|
137495
|
+
|| sqlType.startsWith('varbinary')) {
|
|
137496
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137497
|
+
if (match) {
|
|
137498
|
+
typeParams['length'] = Number(match[1]);
|
|
137236
137499
|
}
|
|
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
137500
|
}
|
|
137266
|
-
|
|
137267
|
-
|
|
137268
|
-
|
|
137269
|
-
|
|
137501
|
+
return typeParams;
|
|
137502
|
+
};
|
|
137503
|
+
const mappedColumns = tableConfig.columns.map((column) => ({
|
|
137504
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137505
|
+
columnType: column.getSQLType(),
|
|
137506
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137507
|
+
dataType: column.dataType.split(' ')[0],
|
|
137508
|
+
hasDefault: column.hasDefault,
|
|
137509
|
+
default: column.default,
|
|
137510
|
+
enumValues: column.enumValues,
|
|
137511
|
+
isUnique: column.isUnique,
|
|
137512
|
+
notNull: column.notNull,
|
|
137513
|
+
primary: column.primary,
|
|
137514
|
+
}));
|
|
137515
|
+
return mappedColumns;
|
|
137270
137516
|
};
|
|
137271
137517
|
|
|
137272
137518
|
// Postgres-----------------------------------------------------------------------------------------------------------
|
|
@@ -137279,14 +137525,15 @@ const resetPostgres = async (db, pgTables) => {
|
|
|
137279
137525
|
await db.execute(drizzleOrm.sql.raw(`truncate ${tablesToTruncate.join(',')} cascade;`));
|
|
137280
137526
|
};
|
|
137281
137527
|
const filterPgSchema = (schema) => {
|
|
137282
|
-
const pgSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], pgCore.PgTable) || drizzleOrm.is(keyValue[1],
|
|
137528
|
+
const pgSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], pgCore.PgTable) || drizzleOrm.is(keyValue[1], _relations.Relations)));
|
|
137283
137529
|
const pgTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], pgCore.PgTable)));
|
|
137284
137530
|
return { pgSchema, pgTables };
|
|
137285
137531
|
};
|
|
137286
137532
|
const seedPostgres = async (db, schema, options = {}, refinements) => {
|
|
137287
137533
|
const seedService = new SeedService();
|
|
137288
137534
|
const { pgSchema, pgTables } = filterPgSchema(schema);
|
|
137289
|
-
const { tables, relations } =
|
|
137535
|
+
const { tables, relations } = getSchemaInfo(pgSchema, pgTables, mapPgColumns);
|
|
137536
|
+
// const { tables, relations } = getPostgresInfo(pgSchema, pgTables);
|
|
137290
137537
|
const generatedTablesGenerators = seedService.generatePossibleGenerators('postgresql', tables, relations, refinements, options);
|
|
137291
137538
|
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
137292
137539
|
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, pgTables, { ...options, preserveCyclicTablesData });
|
|
@@ -137294,198 +137541,77 @@ const seedPostgres = async (db, schema, options = {}, refinements) => {
|
|
|
137294
137541
|
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
137295
137542
|
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, pgTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
137296
137543
|
};
|
|
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;
|
|
137544
|
+
const mapPgColumns = (tableConfig, dbToTsColumnNamesMap) => {
|
|
137545
|
+
const getAllBaseColumns = (baseColumn) => {
|
|
137546
|
+
const baseColumnResult = {
|
|
137547
|
+
name: baseColumn.name,
|
|
137548
|
+
columnType: baseColumn.getSQLType(),
|
|
137549
|
+
typeParams: getTypeParams(baseColumn.getSQLType()),
|
|
137550
|
+
dataType: baseColumn.dataType.split(' ')[0],
|
|
137551
|
+
size: baseColumn.length,
|
|
137552
|
+
hasDefault: baseColumn.hasDefault,
|
|
137553
|
+
enumValues: baseColumn.enumValues,
|
|
137554
|
+
default: baseColumn.default,
|
|
137555
|
+
isUnique: baseColumn.isUnique,
|
|
137556
|
+
notNull: baseColumn.notNull,
|
|
137557
|
+
primary: baseColumn.primary,
|
|
137558
|
+
baseColumn: baseColumn.baseColumn === undefined ? undefined : getAllBaseColumns(baseColumn.baseColumn),
|
|
137559
|
+
};
|
|
137560
|
+
return baseColumnResult;
|
|
137318
137561
|
};
|
|
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);
|
|
137562
|
+
const getTypeParams = (sqlType) => {
|
|
137563
|
+
// get type params
|
|
137564
|
+
const typeParams = {};
|
|
137565
|
+
// handle dimensions
|
|
137566
|
+
if (sqlType.includes('[')) {
|
|
137567
|
+
const match = sqlType.match(/\[\w*]/g);
|
|
137568
|
+
if (match) {
|
|
137569
|
+
typeParams['dimensions'] = match.length;
|
|
137364
137570
|
}
|
|
137365
137571
|
}
|
|
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] = [];
|
|
137572
|
+
if (sqlType.startsWith('numeric')
|
|
137573
|
+
|| sqlType.startsWith('decimal')
|
|
137574
|
+
|| sqlType.startsWith('double precision')
|
|
137575
|
+
|| sqlType.startsWith('real')) {
|
|
137576
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137577
|
+
if (match) {
|
|
137578
|
+
typeParams['precision'] = Number(match[1]);
|
|
137579
|
+
typeParams['scale'] = Number(match[2]);
|
|
137381
137580
|
}
|
|
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
137581
|
}
|
|
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
|
-
}
|
|
137582
|
+
else if (sqlType.startsWith('varchar')
|
|
137583
|
+
|| sqlType.startsWith('bpchar')
|
|
137584
|
+
|| sqlType.startsWith('char')
|
|
137585
|
+
|| sqlType.startsWith('bit')
|
|
137586
|
+
|| sqlType.startsWith('vector')
|
|
137587
|
+
|| sqlType.startsWith('time')
|
|
137588
|
+
|| sqlType.startsWith('timestamp')
|
|
137589
|
+
|| sqlType.startsWith('interval')) {
|
|
137590
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137591
|
+
if (match) {
|
|
137592
|
+
typeParams['length'] = Number(match[1]);
|
|
137425
137593
|
}
|
|
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
137594
|
}
|
|
137485
|
-
|
|
137486
|
-
|
|
137487
|
-
|
|
137488
|
-
|
|
137595
|
+
return typeParams;
|
|
137596
|
+
};
|
|
137597
|
+
const mappedColumns = tableConfig.columns.map((column) => ({
|
|
137598
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137599
|
+
columnType: column.getSQLType(),
|
|
137600
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137601
|
+
dataType: column.dataType.split(' ')[0],
|
|
137602
|
+
size: column.length,
|
|
137603
|
+
hasDefault: column.hasDefault,
|
|
137604
|
+
default: column.default,
|
|
137605
|
+
enumValues: column.enumValues,
|
|
137606
|
+
isUnique: column.isUnique,
|
|
137607
|
+
notNull: column.notNull,
|
|
137608
|
+
primary: column.primary,
|
|
137609
|
+
generatedIdentityType: column.generatedIdentity?.type,
|
|
137610
|
+
baseColumn: (column.baseColumn === undefined)
|
|
137611
|
+
? undefined
|
|
137612
|
+
: getAllBaseColumns(column.baseColumn),
|
|
137613
|
+
}));
|
|
137614
|
+
return mappedColumns;
|
|
137489
137615
|
};
|
|
137490
137616
|
|
|
137491
137617
|
// SingleStore-----------------------------------------------------------------------------------------------------
|
|
@@ -137502,13 +137628,13 @@ const resetSingleStore = async (db, schema) => {
|
|
|
137502
137628
|
await db.execute(drizzleOrm.sql.raw('SET FOREIGN_KEY_CHECKS = 1;'));
|
|
137503
137629
|
};
|
|
137504
137630
|
const filterSingleStoreTables = (schema) => {
|
|
137505
|
-
const singleStoreSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], singlestoreCore.SingleStoreTable) || drizzleOrm.is(keyValue[1],
|
|
137631
|
+
const singleStoreSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], singlestoreCore.SingleStoreTable) || drizzleOrm.is(keyValue[1], _relations.Relations)));
|
|
137506
137632
|
const singleStoreTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], singlestoreCore.SingleStoreTable)));
|
|
137507
137633
|
return { singleStoreSchema, singleStoreTables };
|
|
137508
137634
|
};
|
|
137509
137635
|
const seedSingleStore = async (db, schema, options = {}, refinements) => {
|
|
137510
137636
|
const { singleStoreSchema, singleStoreTables } = filterSingleStoreTables(schema);
|
|
137511
|
-
const { tables, relations } =
|
|
137637
|
+
const { tables, relations } = getSchemaInfo(singleStoreSchema, singleStoreTables, mapSingleStoreColumns);
|
|
137512
137638
|
const seedService = new SeedService();
|
|
137513
137639
|
const generatedTablesGenerators = seedService.generatePossibleGenerators('singlestore', tables, relations, refinements, options);
|
|
137514
137640
|
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
@@ -137517,175 +137643,52 @@ const seedSingleStore = async (db, schema, options = {}, refinements) => {
|
|
|
137517
137643
|
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
137518
137644
|
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, singleStoreTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
137519
137645
|
};
|
|
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);
|
|
137646
|
+
const mapSingleStoreColumns = (tableConfig, dbToTsColumnNamesMap) => {
|
|
137647
|
+
const getTypeParams = (sqlType) => {
|
|
137648
|
+
// get type params and set only type
|
|
137649
|
+
const typeParams = {};
|
|
137650
|
+
if (sqlType.startsWith('decimal')
|
|
137651
|
+
|| sqlType.startsWith('real')
|
|
137652
|
+
|| sqlType.startsWith('double')
|
|
137653
|
+
|| sqlType.startsWith('float')) {
|
|
137654
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137655
|
+
if (match) {
|
|
137656
|
+
typeParams['precision'] = Number(match[1]);
|
|
137657
|
+
typeParams['scale'] = Number(match[2]);
|
|
137587
137658
|
}
|
|
137588
137659
|
}
|
|
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
|
-
}
|
|
137660
|
+
else if (sqlType.startsWith('char')
|
|
137661
|
+
|| sqlType.startsWith('varchar')
|
|
137662
|
+
|| sqlType.startsWith('text')
|
|
137663
|
+
|| sqlType.startsWith('binary')
|
|
137664
|
+
|| sqlType.startsWith('varbinary')) {
|
|
137665
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137666
|
+
if (match) {
|
|
137667
|
+
typeParams['length'] = Number(match[1]);
|
|
137648
137668
|
}
|
|
137649
|
-
|
|
137650
|
-
|
|
137651
|
-
|
|
137652
|
-
|
|
137653
|
-
|
|
137654
|
-
|
|
137669
|
+
}
|
|
137670
|
+
else if (sqlType.startsWith('vector')) {
|
|
137671
|
+
const match = sqlType.match(/\((\d+),? ?((F|I)\d{1,2})?\)/);
|
|
137672
|
+
if (match) {
|
|
137673
|
+
typeParams['length'] = Number(match[1]);
|
|
137674
|
+
typeParams['vectorValueType'] = match[2];
|
|
137655
137675
|
}
|
|
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
137676
|
}
|
|
137685
|
-
|
|
137686
|
-
|
|
137687
|
-
|
|
137688
|
-
|
|
137677
|
+
return typeParams;
|
|
137678
|
+
};
|
|
137679
|
+
const mappedColumns = tableConfig.columns.map((column) => ({
|
|
137680
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137681
|
+
columnType: column.getSQLType(),
|
|
137682
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137683
|
+
dataType: column.dataType.split(' ')[0],
|
|
137684
|
+
hasDefault: column.hasDefault,
|
|
137685
|
+
default: column.default,
|
|
137686
|
+
enumValues: column.enumValues,
|
|
137687
|
+
isUnique: column.isUnique,
|
|
137688
|
+
notNull: column.notNull,
|
|
137689
|
+
primary: column.primary,
|
|
137690
|
+
}));
|
|
137691
|
+
return mappedColumns;
|
|
137689
137692
|
};
|
|
137690
137693
|
|
|
137691
137694
|
// Sqlite------------------------------------------------------------------------------------------------------------------------
|
|
@@ -137702,13 +137705,13 @@ const resetSqlite = async (db, schema) => {
|
|
|
137702
137705
|
await db.run(drizzleOrm.sql.raw('PRAGMA foreign_keys = ON'));
|
|
137703
137706
|
};
|
|
137704
137707
|
const filterSqliteTables = (schema) => {
|
|
137705
|
-
const sqliteSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], sqliteCore.SQLiteTable) || drizzleOrm.is(keyValue[1],
|
|
137708
|
+
const sqliteSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], sqliteCore.SQLiteTable) || drizzleOrm.is(keyValue[1], _relations.Relations)));
|
|
137706
137709
|
const sqliteTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], sqliteCore.SQLiteTable)));
|
|
137707
137710
|
return { sqliteSchema, sqliteTables };
|
|
137708
137711
|
};
|
|
137709
137712
|
const seedSqlite = async (db, schema, options = {}, refinements) => {
|
|
137710
137713
|
const { sqliteSchema, sqliteTables } = filterSqliteTables(schema);
|
|
137711
|
-
const { tables, relations } =
|
|
137714
|
+
const { tables, relations } = getSchemaInfo(sqliteSchema, sqliteTables, mapSqliteColumns);
|
|
137712
137715
|
const seedService = new SeedService();
|
|
137713
137716
|
const generatedTablesGenerators = seedService.generatePossibleGenerators('sqlite', tables, relations, refinements, options);
|
|
137714
137717
|
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
@@ -137717,159 +137720,43 @@ const seedSqlite = async (db, schema, options = {}, refinements) => {
|
|
|
137717
137720
|
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
137718
137721
|
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, sqliteTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
137719
137722
|
};
|
|
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);
|
|
137786
|
-
}
|
|
137787
|
-
}
|
|
137788
|
-
return relations;
|
|
137789
|
-
};
|
|
137790
|
-
for (const table of Object.values(sqliteTables)) {
|
|
137791
|
-
tableConfig = sqliteCore.getTableConfig(table);
|
|
137792
|
-
dbToTsColumnNamesMap = {};
|
|
137793
|
-
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
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] = [];
|
|
137723
|
+
const mapSqliteColumns = (tableConfig, dbToTsColumnNamesMap) => {
|
|
137724
|
+
const getTypeParams = (sqlType) => {
|
|
137725
|
+
// get type params and set only type
|
|
137726
|
+
const typeParams = {};
|
|
137727
|
+
if (sqlType.startsWith('decimal')) {
|
|
137728
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137729
|
+
if (match) {
|
|
137730
|
+
typeParams['precision'] = Number(match[1]);
|
|
137731
|
+
typeParams['scale'] = Number(match[2]);
|
|
137802
137732
|
}
|
|
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
137733
|
}
|
|
137819
|
-
|
|
137820
|
-
|
|
137821
|
-
|
|
137822
|
-
const
|
|
137823
|
-
if (
|
|
137824
|
-
|
|
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
|
-
}
|
|
137734
|
+
else if (sqlType.startsWith('char')
|
|
137735
|
+
|| sqlType.startsWith('varchar')
|
|
137736
|
+
|| sqlType.startsWith('text')) {
|
|
137737
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137738
|
+
if (match) {
|
|
137739
|
+
typeParams['length'] = Number(match[1]);
|
|
137837
137740
|
}
|
|
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
137741
|
}
|
|
137867
|
-
|
|
137868
|
-
|
|
137869
|
-
|
|
137870
|
-
|
|
137742
|
+
return typeParams;
|
|
137743
|
+
};
|
|
137744
|
+
const mappedColumns = tableConfig.columns.map((column) => ({
|
|
137745
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137746
|
+
columnType: column.getSQLType(),
|
|
137747
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137748
|
+
dataType: column.dataType.split(' ')[0],
|
|
137749
|
+
hasDefault: column.hasDefault,
|
|
137750
|
+
default: column.default,
|
|
137751
|
+
enumValues: column.enumValues,
|
|
137752
|
+
isUnique: column.isUnique,
|
|
137753
|
+
notNull: column.notNull,
|
|
137754
|
+
primary: column.primary,
|
|
137755
|
+
}));
|
|
137756
|
+
return mappedColumns;
|
|
137871
137757
|
};
|
|
137872
137758
|
|
|
137759
|
+
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
137873
137760
|
class SeedPromise {
|
|
137874
137761
|
db;
|
|
137875
137762
|
schema;
|
|
@@ -137930,6 +137817,7 @@ async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleSt
|
|
|
137930
137817
|
name: tableName,
|
|
137931
137818
|
columns,
|
|
137932
137819
|
primaryKeys: drizzleStudioColumns.filter((col) => col.primaryKey === true).map((col) => col.name),
|
|
137820
|
+
uniqueConstraints: [], // TODO change later
|
|
137933
137821
|
});
|
|
137934
137822
|
}
|
|
137935
137823
|
relations = drizzleStudioRelations.filter((rel) => rel.schema === schemaName && rel.refSchema === schemaName);
|