drizzle-seed 0.3.2-f8a2f3c → 0.4.0-4ec2def
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{services/SeedService.d.cts → SeedService.d.cts} +18 -26
- package/{services/SeedService.d.mts → SeedService.d.mts} +18 -26
- package/{services/SeedService.d.ts → SeedService.d.ts} +18 -26
- package/cockroach-core/index.d.cts +30 -0
- package/cockroach-core/index.d.mts +30 -0
- package/cockroach-core/index.d.ts +30 -0
- package/cockroach-core/selectGensForColumn.d.cts +3 -0
- package/cockroach-core/selectGensForColumn.d.mts +3 -0
- package/cockroach-core/selectGensForColumn.d.ts +3 -0
- package/common.d.cts +25 -0
- package/common.d.mts +25 -0
- package/common.d.ts +25 -0
- package/{services → generators}/GeneratorFuncs.d.cts +311 -35
- package/{services → generators}/GeneratorFuncs.d.mts +311 -35
- package/{services → generators}/GeneratorFuncs.d.ts +311 -35
- package/{services → generators}/Generators.d.cts +151 -25
- package/{services → generators}/Generators.d.mts +151 -25
- package/{services → generators}/Generators.d.ts +151 -25
- package/generators/utils.d.cts +38 -0
- package/generators/utils.d.mts +38 -0
- package/generators/utils.d.ts +38 -0
- package/{services → generators}/versioning/v2.d.cts +2 -2
- package/{services → generators}/versioning/v2.d.mts +2 -2
- package/{services → generators}/versioning/v2.d.ts +2 -2
- package/index.cjs +2604 -1354
- package/index.cjs.map +1 -1
- package/index.d.cts +94 -113
- package/index.d.mts +94 -113
- package/index.d.ts +94 -113
- package/index.mjs +2609 -1358
- package/index.mjs.map +1 -1
- package/mssql-core/index.d.cts +24 -0
- package/mssql-core/index.d.mts +24 -0
- package/mssql-core/index.d.ts +24 -0
- package/mssql-core/selectGensForColumn.d.cts +2 -0
- package/mssql-core/selectGensForColumn.d.mts +2 -0
- package/mssql-core/selectGensForColumn.d.ts +2 -0
- package/mysql-core/index.d.cts +30 -0
- package/mysql-core/index.d.mts +30 -0
- package/mysql-core/index.d.ts +30 -0
- package/mysql-core/selectGensForColumn.d.cts +2 -0
- package/mysql-core/selectGensForColumn.d.mts +2 -0
- package/mysql-core/selectGensForColumn.d.ts +2 -0
- package/package.json +13 -9
- package/pg-core/index.d.cts +30 -0
- package/pg-core/index.d.mts +30 -0
- package/pg-core/index.d.ts +30 -0
- package/pg-core/selectGensForColumn.d.cts +3 -0
- package/pg-core/selectGensForColumn.d.mts +3 -0
- package/pg-core/selectGensForColumn.d.ts +3 -0
- package/singlestore-core/index.d.cts +30 -0
- package/singlestore-core/index.d.mts +30 -0
- package/singlestore-core/index.d.ts +30 -0
- package/singlestore-core/selectGensForColumn.d.cts +2 -0
- package/singlestore-core/selectGensForColumn.d.mts +2 -0
- package/singlestore-core/selectGensForColumn.d.ts +2 -0
- package/sqlite-core/index.d.cts +30 -0
- package/sqlite-core/index.d.mts +30 -0
- package/sqlite-core/index.d.ts +30 -0
- package/sqlite-core/selectGensForColumn.d.cts +2 -0
- package/sqlite-core/selectGensForColumn.d.mts +2 -0
- package/sqlite-core/selectGensForColumn.d.ts +2 -0
- package/types/seedService.d.cts +12 -2
- package/types/seedService.d.mts +12 -2
- package/types/seedService.d.ts +12 -2
- package/types/tables.d.cts +17 -0
- package/types/tables.d.mts +17 -0
- package/types/tables.d.ts +17 -0
- package/utils.d.cts +4 -0
- package/utils.d.mts +4 -0
- package/utils.d.ts +4 -0
- package/services/utils.d.cts +0 -23
- package/services/utils.d.mts +0 -23
- package/services/utils.d.ts +0 -23
- /package/{services → generators}/apiVersion.d.cts +0 -0
- /package/{services → generators}/apiVersion.d.mts +0 -0
- /package/{services → generators}/apiVersion.d.ts +0 -0
package/index.mjs
CHANGED
|
@@ -1,9 +1,188 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { getTableName, is, getColumnTable, Column, entityKind, sql, eq } from 'drizzle-orm';
|
|
2
|
+
import { MySqlTable, getTableConfig as getTableConfig$2, MySqlDatabase } from 'drizzle-orm/mysql-core';
|
|
3
|
+
import { PgTable, getTableConfig as getTableConfig$1, PgDatabase } from 'drizzle-orm/pg-core';
|
|
4
|
+
import { SQLiteTable, getTableConfig as getTableConfig$3, BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core';
|
|
5
|
+
import { MsSqlTable, getTableConfig as getTableConfig$5, MsSqlDatabase } from 'drizzle-orm/mssql-core';
|
|
6
|
+
import { CockroachTable, getTableConfig as getTableConfig$4, CockroachDatabase } from 'drizzle-orm/cockroach-core';
|
|
7
|
+
import { getTableConfig as getTableConfig$6, SingleStoreDatabase, SingleStoreTable } from 'drizzle-orm/singlestore-core';
|
|
8
|
+
import { extractTablesRelationalConfig, createTableRelationsHelpers, One, Relations } from 'drizzle-orm/_relations';
|
|
5
9
|
import prand from 'pure-rand';
|
|
6
10
|
|
|
11
|
+
const isRelationCyclic = (startRel) => {
|
|
12
|
+
// self relation
|
|
13
|
+
if (startRel.table === startRel.refTable)
|
|
14
|
+
return false;
|
|
15
|
+
// DFS
|
|
16
|
+
const targetTable = startRel.table;
|
|
17
|
+
const queue = [startRel];
|
|
18
|
+
let path = [];
|
|
19
|
+
while (queue.length !== 0) {
|
|
20
|
+
const currRel = queue.shift();
|
|
21
|
+
if (path.includes(currRel.table)) {
|
|
22
|
+
const idx = path.indexOf(currRel.table);
|
|
23
|
+
path = path.slice(0, idx);
|
|
24
|
+
}
|
|
25
|
+
path.push(currRel.table);
|
|
26
|
+
for (const rel of currRel.refTableRels) {
|
|
27
|
+
// self relation
|
|
28
|
+
if (rel.table === rel.refTable)
|
|
29
|
+
continue;
|
|
30
|
+
if (rel.refTable === targetTable)
|
|
31
|
+
return true;
|
|
32
|
+
// found cycle, but not the one we are looking for
|
|
33
|
+
if (path.includes(rel.refTable))
|
|
34
|
+
continue;
|
|
35
|
+
queue.unshift(rel);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
};
|
|
40
|
+
const generateHashFromString = (s) => {
|
|
41
|
+
let hash = 0;
|
|
42
|
+
// p and m are prime numbers
|
|
43
|
+
const p = 53;
|
|
44
|
+
const m = 28871271685163;
|
|
45
|
+
for (let i = 0; i < s.length; i++) {
|
|
46
|
+
hash += ((s.codePointAt(i) || 0) * Math.pow(p, i)) % m;
|
|
47
|
+
}
|
|
48
|
+
return hash;
|
|
49
|
+
};
|
|
50
|
+
const equalSets = (set1, set2) => {
|
|
51
|
+
return set1.size === set2.size && [...set1].every((si) => set2.has(si));
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const getTableConfig = (table) => {
|
|
55
|
+
if (is(table, PgTable))
|
|
56
|
+
return getTableConfig$1(table);
|
|
57
|
+
else if (is(table, MySqlTable))
|
|
58
|
+
return getTableConfig$2(table);
|
|
59
|
+
else if (is(table, SQLiteTable))
|
|
60
|
+
return getTableConfig$3(table);
|
|
61
|
+
else if (is(table, CockroachTable))
|
|
62
|
+
return getTableConfig$4(table);
|
|
63
|
+
else if (is(table, MsSqlTable))
|
|
64
|
+
return getTableConfig$5(table);
|
|
65
|
+
else
|
|
66
|
+
return getTableConfig$6(table); // if (is(table, SingleStoreTable))
|
|
67
|
+
};
|
|
68
|
+
const transformFromDrizzleRelation = (schema, getDbToTsColumnNamesMap, tableRelations) => {
|
|
69
|
+
const schemaConfig = extractTablesRelationalConfig(schema, createTableRelationsHelpers);
|
|
70
|
+
const relations = [];
|
|
71
|
+
for (const table of Object.values(schemaConfig.tables)) {
|
|
72
|
+
if (table.relations === undefined)
|
|
73
|
+
continue;
|
|
74
|
+
for (const drizzleRel of Object.values(table.relations)) {
|
|
75
|
+
if (!is(drizzleRel, One))
|
|
76
|
+
continue;
|
|
77
|
+
const tableConfig = getTableConfig(drizzleRel.sourceTable);
|
|
78
|
+
const tableDbSchema = tableConfig.schema ?? 'public';
|
|
79
|
+
const tableDbName = tableConfig.name;
|
|
80
|
+
const tableTsName = schemaConfig.tableNamesMap[`${tableDbSchema}.${tableDbName}`] ?? tableDbName;
|
|
81
|
+
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
82
|
+
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
83
|
+
?? [];
|
|
84
|
+
const refTableConfig = getTableConfig(drizzleRel.referencedTable);
|
|
85
|
+
const refTableDbSchema = refTableConfig.schema ?? 'public';
|
|
86
|
+
const refTableDbName = refTableConfig.name;
|
|
87
|
+
const refTableTsName = schemaConfig.tableNamesMap[`${refTableDbSchema}.${refTableDbName}`]
|
|
88
|
+
?? refTableDbName;
|
|
89
|
+
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
90
|
+
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
91
|
+
?? [];
|
|
92
|
+
if (tableRelations[refTableTsName] === undefined) {
|
|
93
|
+
tableRelations[refTableTsName] = [];
|
|
94
|
+
}
|
|
95
|
+
const relation = {
|
|
96
|
+
table: tableTsName,
|
|
97
|
+
columns,
|
|
98
|
+
refTable: refTableTsName,
|
|
99
|
+
refColumns,
|
|
100
|
+
refTableRels: tableRelations[refTableTsName],
|
|
101
|
+
type: 'one',
|
|
102
|
+
};
|
|
103
|
+
// do not add duplicate relation
|
|
104
|
+
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
105
|
+
&& rel.refTable === relation.refTable)) {
|
|
106
|
+
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
107
|
+
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
108
|
+
+ `In this case, the foreign key constraint will be used.\n`);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
relations.push(relation);
|
|
112
|
+
tableRelations[tableTsName].push(relation);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return relations;
|
|
116
|
+
};
|
|
117
|
+
const getSchemaInfo = (drizzleTablesAndRelations, drizzleTables, mapTable) => {
|
|
118
|
+
let tableConfig;
|
|
119
|
+
let dbToTsColumnNamesMap;
|
|
120
|
+
const dbToTsTableNamesMap = Object.fromEntries(Object.entries(drizzleTables).map(([key, value]) => [getTableName(value), key]));
|
|
121
|
+
const tables = [];
|
|
122
|
+
const relations = [];
|
|
123
|
+
const dbToTsColumnNamesMapGlobal = {};
|
|
124
|
+
const tableRelations = {};
|
|
125
|
+
const getDbToTsColumnNamesMap = (table) => {
|
|
126
|
+
let dbToTsColumnNamesMap = {};
|
|
127
|
+
const tableName = getTableName(table);
|
|
128
|
+
if (Object.hasOwn(dbToTsColumnNamesMapGlobal, tableName)) {
|
|
129
|
+
dbToTsColumnNamesMap = dbToTsColumnNamesMapGlobal[tableName];
|
|
130
|
+
return dbToTsColumnNamesMap;
|
|
131
|
+
}
|
|
132
|
+
const tableConfig = getTableConfig(table);
|
|
133
|
+
for (const [tsCol, col] of Object.entries(getColumnTable(tableConfig.columns[0]))) {
|
|
134
|
+
if (is(col, Column))
|
|
135
|
+
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
136
|
+
}
|
|
137
|
+
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
138
|
+
return dbToTsColumnNamesMap;
|
|
139
|
+
};
|
|
140
|
+
for (const table of Object.values(drizzleTables)) {
|
|
141
|
+
tableConfig = getTableConfig(table);
|
|
142
|
+
dbToTsColumnNamesMap = getDbToTsColumnNamesMap(table);
|
|
143
|
+
// might be empty list
|
|
144
|
+
const newRelations = tableConfig.foreignKeys === undefined ? [] : tableConfig.foreignKeys.map((fk) => {
|
|
145
|
+
const table = dbToTsTableNamesMap[tableConfig.name];
|
|
146
|
+
const refTable = dbToTsTableNamesMap[getTableName(fk.reference().foreignTable)];
|
|
147
|
+
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(fk.reference().foreignTable);
|
|
148
|
+
if (tableRelations[refTable] === undefined) {
|
|
149
|
+
tableRelations[refTable] = [];
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
table,
|
|
153
|
+
columns: fk
|
|
154
|
+
.reference()
|
|
155
|
+
.columns.map((col) => dbToTsColumnNamesMap[col.name]),
|
|
156
|
+
refTable,
|
|
157
|
+
refColumns: fk
|
|
158
|
+
.reference()
|
|
159
|
+
.foreignColumns.map((fCol) => dbToTsColumnNamesMapForRefTable[fCol.name]),
|
|
160
|
+
refTableRels: tableRelations[refTable],
|
|
161
|
+
};
|
|
162
|
+
});
|
|
163
|
+
relations.push(...newRelations);
|
|
164
|
+
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
165
|
+
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
166
|
+
}
|
|
167
|
+
tableRelations[dbToTsTableNamesMap[tableConfig.name]].push(...newRelations);
|
|
168
|
+
// console.log(tableConfig.columns);
|
|
169
|
+
tables.push(mapTable(tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap));
|
|
170
|
+
}
|
|
171
|
+
const transformedDrizzleRelations = transformFromDrizzleRelation(drizzleTablesAndRelations, getDbToTsColumnNamesMap, tableRelations);
|
|
172
|
+
relations.push(...transformedDrizzleRelations);
|
|
173
|
+
const isCyclicRelations = relations.map((relI) => {
|
|
174
|
+
// if (relations.some((relj) => relI.table === relj.refTable && relI.refTable === relj.table)) {
|
|
175
|
+
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
176
|
+
if (isRelationCyclic(relI)) {
|
|
177
|
+
tableRel['isCyclic'] = true;
|
|
178
|
+
return { ...relI, isCyclic: true };
|
|
179
|
+
}
|
|
180
|
+
tableRel['isCyclic'] = false;
|
|
181
|
+
return { ...relI, isCyclic: false };
|
|
182
|
+
});
|
|
183
|
+
return { tables, relations: isCyclicRelations, tableRelations };
|
|
184
|
+
};
|
|
185
|
+
|
|
7
186
|
/**
|
|
8
187
|
* The original source for the Adjectives data was taken from https://www.kaggle.com/datasets/jordansiem/adjectives-list
|
|
9
188
|
*/
|
|
@@ -131130,18 +131309,60 @@ var streetSuffix = [
|
|
|
131130
131309
|
];
|
|
131131
131310
|
const maxStringLength = 10;
|
|
131132
131311
|
|
|
131312
|
+
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
131133
131313
|
const fastCartesianProduct = (sets, index) => {
|
|
131134
131314
|
const resultList = [];
|
|
131135
131315
|
let currSet;
|
|
131136
131316
|
let element;
|
|
131137
131317
|
for (let i = sets.length - 1; i >= 0; i--) {
|
|
131138
131318
|
currSet = sets[i];
|
|
131139
|
-
element = currSet[index % currSet.length];
|
|
131319
|
+
element = currSet[index % Number(currSet.length)];
|
|
131140
131320
|
resultList.unshift(element);
|
|
131141
|
-
index = Math.floor(index / currSet.length);
|
|
131321
|
+
index = Math.floor(index / Number(currSet.length));
|
|
131142
131322
|
}
|
|
131143
131323
|
return resultList;
|
|
131144
131324
|
};
|
|
131325
|
+
const fastCartesianProductForBigint = (sets, index) => {
|
|
131326
|
+
const resultList = [];
|
|
131327
|
+
let currSet;
|
|
131328
|
+
let element;
|
|
131329
|
+
for (let i = sets.length - 1; i >= 0; i--) {
|
|
131330
|
+
currSet = sets[i];
|
|
131331
|
+
const remainder = index % BigInt(currSet.length);
|
|
131332
|
+
// TODO check how it works
|
|
131333
|
+
// remainder = remainder <= Number.MAX_SAFE_INTEGER ? Number(remainder) : remainder;
|
|
131334
|
+
element = currSet[remainder];
|
|
131335
|
+
resultList.unshift(element);
|
|
131336
|
+
index = index / BigInt(currSet.length);
|
|
131337
|
+
}
|
|
131338
|
+
return resultList;
|
|
131339
|
+
};
|
|
131340
|
+
class OrderedNumberRange {
|
|
131341
|
+
min;
|
|
131342
|
+
max;
|
|
131343
|
+
step;
|
|
131344
|
+
length;
|
|
131345
|
+
constructor(min, max, step) {
|
|
131346
|
+
this.min = min;
|
|
131347
|
+
this.max = max;
|
|
131348
|
+
this.step = step;
|
|
131349
|
+
this.length = Math.floor((this.max - this.min) / this.step) + 1;
|
|
131350
|
+
const handler = {
|
|
131351
|
+
get(target, prop, receiver) {
|
|
131352
|
+
if (typeof prop === 'string' && /^\d+$/.test(prop)) {
|
|
131353
|
+
const idx = Number(prop);
|
|
131354
|
+
if (idx >= target.length)
|
|
131355
|
+
return undefined;
|
|
131356
|
+
return (target.min + idx * target.step);
|
|
131357
|
+
}
|
|
131358
|
+
// fallback to normal lookup (and TS knows this has the right signature)
|
|
131359
|
+
return Reflect.get(target, prop, receiver);
|
|
131360
|
+
},
|
|
131361
|
+
};
|
|
131362
|
+
return new Proxy(this, handler);
|
|
131363
|
+
}
|
|
131364
|
+
}
|
|
131365
|
+
const abs = (n) => (n < 0n) ? -n : n;
|
|
131145
131366
|
const sumArray = (weights) => {
|
|
131146
131367
|
const scale = 1e10;
|
|
131147
131368
|
const scaledSum = weights.reduce((acc, currVal) => acc + Math.round(currVal * scale), 0);
|
|
@@ -131165,16 +131386,6 @@ const getWeightedIndices = (weights, accuracy = 100) => {
|
|
|
131165
131386
|
}
|
|
131166
131387
|
return weightedIndices;
|
|
131167
131388
|
};
|
|
131168
|
-
const generateHashFromString = (s) => {
|
|
131169
|
-
let hash = 0;
|
|
131170
|
-
// p and m are prime numbers
|
|
131171
|
-
const p = 53;
|
|
131172
|
-
const m = 28871271685163;
|
|
131173
|
-
for (let i = 0; i < s.length; i++) {
|
|
131174
|
-
hash += ((s.codePointAt(i) || 0) * Math.pow(p, i)) % m;
|
|
131175
|
-
}
|
|
131176
|
-
return hash;
|
|
131177
|
-
};
|
|
131178
131389
|
/**
|
|
131179
131390
|
* @param param0.template example: "#####" or "#####-####"
|
|
131180
131391
|
* @param param0.values example: ["3", "2", "h"]
|
|
@@ -131211,12 +131422,27 @@ const isObject = (value) => {
|
|
|
131211
131422
|
return true;
|
|
131212
131423
|
return false;
|
|
131213
131424
|
};
|
|
131214
|
-
const
|
|
131215
|
-
|
|
131216
|
-
|
|
131217
|
-
|
|
131218
|
-
|
|
131219
|
-
|
|
131425
|
+
// const main = () => {
|
|
131426
|
+
// console.time('range');
|
|
131427
|
+
// const range = new OrderedBigintRange(BigInt(-10), BigInt(10), BigInt(1));
|
|
131428
|
+
// console.log(range.length);
|
|
131429
|
+
// for (let i = 0; i < Number(range.length) + 1; i++) {
|
|
131430
|
+
// console.log(range[i]);
|
|
131431
|
+
// }
|
|
131432
|
+
// console.timeEnd('range');
|
|
131433
|
+
// const list = Array.from({ length: 2e6 + 1 }, (_, idx) => idx);
|
|
131434
|
+
// console.time('list');
|
|
131435
|
+
// console.log(list.length);
|
|
131436
|
+
// for (let i = 0; i < 2e6 + 1; i++) {
|
|
131437
|
+
// list[i];
|
|
131438
|
+
// }
|
|
131439
|
+
// console.timeEnd('list');
|
|
131440
|
+
// // const n = 5;
|
|
131441
|
+
// // for (let i = 0; i < n; i++) {
|
|
131442
|
+
// // console.log(fastCartesianProduct([[1, 2], [1, 2]], i));
|
|
131443
|
+
// // }
|
|
131444
|
+
// };
|
|
131445
|
+
// main();
|
|
131220
131446
|
|
|
131221
131447
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
131222
131448
|
class AbstractGenerator {
|
|
@@ -131232,10 +131458,11 @@ class AbstractGenerator {
|
|
|
131232
131458
|
arraySize;
|
|
131233
131459
|
baseColumnDataType;
|
|
131234
131460
|
// param for text-like generators
|
|
131235
|
-
stringLength;
|
|
131461
|
+
// public stringLength?: number;
|
|
131236
131462
|
// params for GenerateValuesFromArray
|
|
131237
131463
|
weightedCountSeed;
|
|
131238
131464
|
maxRepeatedValuesCount;
|
|
131465
|
+
typeParams = {};
|
|
131239
131466
|
params;
|
|
131240
131467
|
constructor(params) {
|
|
131241
131468
|
this.params = params === undefined ? {} : params;
|
|
@@ -131267,6 +131494,7 @@ class AbstractGenerator {
|
|
|
131267
131494
|
});
|
|
131268
131495
|
uniqueGen.isUnique = this.isUnique;
|
|
131269
131496
|
uniqueGen.dataType = this.dataType;
|
|
131497
|
+
uniqueGen.typeParams = this.typeParams;
|
|
131270
131498
|
return uniqueGen;
|
|
131271
131499
|
}
|
|
131272
131500
|
return;
|
|
@@ -131277,10 +131505,13 @@ class AbstractGenerator {
|
|
|
131277
131505
|
const uniqueGen = this.replaceIfUnique();
|
|
131278
131506
|
const baseColumnGen = uniqueGen === undefined ? this : uniqueGen;
|
|
131279
131507
|
baseColumnGen.dataType = this.baseColumnDataType;
|
|
131508
|
+
const { dimensions, ...rest } = baseColumnGen.typeParams;
|
|
131509
|
+
baseColumnGen.typeParams = rest;
|
|
131280
131510
|
const arrayGen = new GenerateArray({
|
|
131281
131511
|
baseColumnGen,
|
|
131282
131512
|
size: this.arraySize,
|
|
131283
131513
|
});
|
|
131514
|
+
arrayGen.typeParams = { dimensions };
|
|
131284
131515
|
return arrayGen;
|
|
131285
131516
|
}
|
|
131286
131517
|
return;
|
|
@@ -131535,6 +131766,7 @@ class GenerateNumber extends AbstractGenerator {
|
|
|
131535
131766
|
static entityKind = 'GenerateNumber';
|
|
131536
131767
|
state;
|
|
131537
131768
|
uniqueVersionOfGen = GenerateUniqueNumber;
|
|
131769
|
+
// TODO rewrite precision to decimalPlaces
|
|
131538
131770
|
init({ count, seed }) {
|
|
131539
131771
|
super.init({ count, seed });
|
|
131540
131772
|
let { minValue, maxValue, precision } = this.params;
|
|
@@ -131788,20 +132020,13 @@ class GenerateDate extends AbstractGenerator {
|
|
|
131788
132020
|
const rng = prand.xoroshiro128plus(seed);
|
|
131789
132021
|
let { minDate, maxDate } = this.params;
|
|
131790
132022
|
const anchorDate = new Date('2024-05-08');
|
|
131791
|
-
// 4 years in milliseconds
|
|
131792
132023
|
const deltaMilliseconds = 4 * 31536000000;
|
|
131793
132024
|
if (typeof minDate === 'string') {
|
|
131794
132025
|
minDate = new Date(minDate);
|
|
131795
132026
|
}
|
|
131796
|
-
if (typeof minDate === 'object' && !isValidDate(minDate)) {
|
|
131797
|
-
throw new Error('Invalid Date was provided for the minDate parameter.');
|
|
131798
|
-
}
|
|
131799
132027
|
if (typeof maxDate === 'string') {
|
|
131800
132028
|
maxDate = new Date(maxDate);
|
|
131801
132029
|
}
|
|
131802
|
-
if (typeof maxDate === 'object' && !isValidDate(maxDate)) {
|
|
131803
|
-
throw new Error('Invalid Date was provided for the maxDate parameter.');
|
|
131804
|
-
}
|
|
131805
132030
|
if (minDate === undefined) {
|
|
131806
132031
|
if (maxDate === undefined) {
|
|
131807
132032
|
minDate = new Date(anchorDate.getTime() - deltaMilliseconds);
|
|
@@ -131814,9 +132039,6 @@ class GenerateDate extends AbstractGenerator {
|
|
|
131814
132039
|
if (maxDate === undefined) {
|
|
131815
132040
|
maxDate = new Date(minDate.getTime() + (2 * deltaMilliseconds));
|
|
131816
132041
|
}
|
|
131817
|
-
if (minDate > maxDate) {
|
|
131818
|
-
throw new Error(`The minDate parameter must be less than or equal to the maxDate parameter.`);
|
|
131819
|
-
}
|
|
131820
132042
|
this.state = { rng, minDate, maxDate };
|
|
131821
132043
|
}
|
|
131822
132044
|
generate() {
|
|
@@ -131838,83 +132060,18 @@ class GenerateTime extends AbstractGenerator {
|
|
|
131838
132060
|
init({ count, seed }) {
|
|
131839
132061
|
super.init({ count, seed });
|
|
131840
132062
|
const rng = prand.xoroshiro128plus(seed);
|
|
131841
|
-
|
|
131842
|
-
if (minTime === undefined && maxTime === undefined) {
|
|
131843
|
-
// TODO: maybe need to change in major version release
|
|
131844
|
-
// This is required to ensure that this generator remains deterministic when used without minTime, maxTime parameters.
|
|
131845
|
-
const oneDayInMilliseconds = 86400000;
|
|
131846
|
-
minTime = new Date(new Date('2024-05-08T12:00:00.000Z').getTime() - oneDayInMilliseconds);
|
|
131847
|
-
maxTime = new Date(new Date('2024-05-08T12:00:00.000Z').getTime() + oneDayInMilliseconds);
|
|
131848
|
-
this.state = { rng, minTime, maxTime };
|
|
131849
|
-
return;
|
|
131850
|
-
}
|
|
131851
|
-
if (minTime === undefined) {
|
|
131852
|
-
if (maxTime === undefined) {
|
|
131853
|
-
minTime = '00:00:00.000Z';
|
|
131854
|
-
maxTime = '23:59:59.999Z';
|
|
131855
|
-
}
|
|
131856
|
-
else {
|
|
131857
|
-
minTime = '00:00:00.000Z';
|
|
131858
|
-
}
|
|
131859
|
-
}
|
|
131860
|
-
if (maxTime === undefined) {
|
|
131861
|
-
maxTime = '23:59:59.999Z';
|
|
131862
|
-
new Date().toISOString();
|
|
131863
|
-
}
|
|
131864
|
-
const anchorDate = new Date('2024-05-08');
|
|
131865
|
-
const anchorDateString0 = anchorDate.toISOString().replace(/T\d{2}:\d{2}:\d{2}.\d{3}Z/, '');
|
|
131866
|
-
if (typeof minTime === 'string') {
|
|
131867
|
-
// const timeMatch0 = minTime.match(/^\d{2}:\d{2}:\d{2}.\d{1,3}Z?$/);
|
|
131868
|
-
const timeMatch1 = minTime.match(/^\d{2}:\d{2}:\d{2}Z?$/);
|
|
131869
|
-
const timeMatch2 = minTime.match(/^\d{2}:\d{2}Z?$/);
|
|
131870
|
-
if (
|
|
131871
|
-
// timeMatch0 === null
|
|
131872
|
-
timeMatch1 === null
|
|
131873
|
-
&& timeMatch2 === null) {
|
|
131874
|
-
throw new Error(`You're using the wrong format for the minTime parameter.`
|
|
131875
|
-
+ `\nPlease use one of these formats: 'HH:mm:ss', 'HH:mm' (with or without a trailing 'Z')`);
|
|
131876
|
-
}
|
|
131877
|
-
minTime = minTime.at(-1) === 'Z' ? minTime : minTime + 'Z';
|
|
131878
|
-
minTime = new Date(anchorDate.toISOString().replace(/\d{2}:\d{2}:\d{2}.\d{3}Z/, minTime));
|
|
131879
|
-
}
|
|
131880
|
-
if (typeof minTime === 'object') {
|
|
131881
|
-
if (!isValidDate(minTime)) {
|
|
131882
|
-
throw new Error('Invalid Date was provided for the minTime parameter.');
|
|
131883
|
-
}
|
|
131884
|
-
minTime = new Date(minTime.toISOString().replace(/\d{4}-\d{2}-\d{2}/, anchorDateString0));
|
|
131885
|
-
}
|
|
131886
|
-
if (typeof maxTime === 'string') {
|
|
131887
|
-
// const timeMatch0 = maxTime.match(/^\d{2}:\d{2}:\d{2}.\d{1,3}Z?$/);
|
|
131888
|
-
const timeMatch1 = maxTime.match(/^\d{2}:\d{2}:\d{2}Z?$/);
|
|
131889
|
-
const timeMatch2 = maxTime.match(/^\d{2}:\d{2}Z?$/);
|
|
131890
|
-
if (
|
|
131891
|
-
// timeMatch0 === null
|
|
131892
|
-
timeMatch1 === null
|
|
131893
|
-
&& timeMatch2 === null) {
|
|
131894
|
-
throw new Error(`You're using the wrong format for the maxTime parameter.`
|
|
131895
|
-
+ `\nPlease use one of these formats: 'HH:mm:ss', 'HH:mm' (with or without a trailing 'Z').`);
|
|
131896
|
-
}
|
|
131897
|
-
maxTime = maxTime.at(-1) === 'Z' ? maxTime : maxTime + 'Z';
|
|
131898
|
-
maxTime = new Date(anchorDate.toISOString().replace(/\d{2}:\d{2}:\d{2}.\d{3}Z/, maxTime));
|
|
131899
|
-
}
|
|
131900
|
-
if (typeof maxTime === 'object') {
|
|
131901
|
-
if (!isValidDate(maxTime)) {
|
|
131902
|
-
throw new Error('Invalid Date was provided for the maxTime parameter.');
|
|
131903
|
-
}
|
|
131904
|
-
maxTime = new Date(maxTime.toISOString().replace(/\d{4}-\d{2}-\d{2}/, anchorDateString0));
|
|
131905
|
-
}
|
|
131906
|
-
if (minTime > maxTime) {
|
|
131907
|
-
throw new Error(`The minTime parameter must be less than or equal to the maxTime parameter.`);
|
|
131908
|
-
}
|
|
131909
|
-
this.state = { rng, minTime, maxTime };
|
|
132063
|
+
this.state = { rng };
|
|
131910
132064
|
}
|
|
131911
132065
|
generate() {
|
|
131912
132066
|
if (this.state === undefined) {
|
|
131913
132067
|
throw new Error('state is not defined.');
|
|
131914
132068
|
}
|
|
132069
|
+
const anchorDateTime = new Date('2024-05-08T12:00:00.000Z');
|
|
132070
|
+
const oneDayInMilliseconds = 86400000;
|
|
132071
|
+
let date = new Date();
|
|
131915
132072
|
let milliseconds;
|
|
131916
|
-
[milliseconds, this.state.rng] = prand.uniformIntDistribution(
|
|
131917
|
-
|
|
132073
|
+
[milliseconds, this.state.rng] = prand.uniformIntDistribution(-oneDayInMilliseconds, oneDayInMilliseconds, this.state.rng);
|
|
132074
|
+
date = new Date(date.setTime(anchorDateTime.getTime() + milliseconds));
|
|
131918
132075
|
return date.toISOString().replace(/(\d{4}-\d{2}-\d{2}T)|(\.\d{3}Z)/g, '');
|
|
131919
132076
|
}
|
|
131920
132077
|
}
|
|
@@ -131924,46 +132081,18 @@ class GenerateTimestamp extends AbstractGenerator {
|
|
|
131924
132081
|
init({ count, seed }) {
|
|
131925
132082
|
super.init({ count, seed });
|
|
131926
132083
|
const rng = prand.xoroshiro128plus(seed);
|
|
131927
|
-
|
|
131928
|
-
const anchorDate = new Date('2024-05-08');
|
|
131929
|
-
// 2 years in milliseconds
|
|
131930
|
-
const deltaMilliseconds = 2 * 31536000000;
|
|
131931
|
-
if (typeof minTimestamp === 'string') {
|
|
131932
|
-
minTimestamp = new Date(minTimestamp);
|
|
131933
|
-
}
|
|
131934
|
-
if (typeof minTimestamp === 'object' && !isValidDate(minTimestamp)) {
|
|
131935
|
-
throw new Error('Invalid Date was provided for the minTimestamp parameter.');
|
|
131936
|
-
}
|
|
131937
|
-
if (typeof maxTimestamp === 'string') {
|
|
131938
|
-
maxTimestamp = new Date(maxTimestamp);
|
|
131939
|
-
}
|
|
131940
|
-
if (typeof maxTimestamp === 'object' && !isValidDate(maxTimestamp)) {
|
|
131941
|
-
throw new Error('Invalid Date was provided for the maxTimestamp parameter.');
|
|
131942
|
-
}
|
|
131943
|
-
if (minTimestamp === undefined) {
|
|
131944
|
-
if (maxTimestamp === undefined) {
|
|
131945
|
-
minTimestamp = new Date(anchorDate.getTime() - deltaMilliseconds);
|
|
131946
|
-
maxTimestamp = new Date(anchorDate.getTime() + deltaMilliseconds);
|
|
131947
|
-
}
|
|
131948
|
-
else {
|
|
131949
|
-
minTimestamp = new Date(maxTimestamp.getTime() - (2 * deltaMilliseconds));
|
|
131950
|
-
}
|
|
131951
|
-
}
|
|
131952
|
-
if (maxTimestamp === undefined) {
|
|
131953
|
-
maxTimestamp = new Date(minTimestamp.getTime() + (2 * deltaMilliseconds));
|
|
131954
|
-
}
|
|
131955
|
-
if (minTimestamp > maxTimestamp) {
|
|
131956
|
-
throw new Error(`The minTimestamp parameter must be less than or equal to the maxTimestamp parameter.`);
|
|
131957
|
-
}
|
|
131958
|
-
this.state = { rng, minTimestamp, maxTimestamp };
|
|
132084
|
+
this.state = { rng };
|
|
131959
132085
|
}
|
|
131960
132086
|
generate() {
|
|
131961
132087
|
if (this.state === undefined) {
|
|
131962
132088
|
throw new Error('state is not defined.');
|
|
131963
132089
|
}
|
|
132090
|
+
const anchorTimestamp = new Date('2024-05-08');
|
|
132091
|
+
const twoYearsInMilliseconds = 2 * 31536000000;
|
|
132092
|
+
let date = new Date();
|
|
131964
132093
|
let milliseconds;
|
|
131965
|
-
[milliseconds, this.state.rng] = prand.uniformIntDistribution(
|
|
131966
|
-
|
|
132094
|
+
[milliseconds, this.state.rng] = prand.uniformIntDistribution(-twoYearsInMilliseconds, twoYearsInMilliseconds, this.state.rng);
|
|
132095
|
+
date = new Date(date.setTime(anchorTimestamp.getTime() + milliseconds));
|
|
131967
132096
|
if (this.dataType === 'string') {
|
|
131968
132097
|
return date
|
|
131969
132098
|
.toISOString()
|
|
@@ -131979,46 +132108,18 @@ class GenerateDatetime extends AbstractGenerator {
|
|
|
131979
132108
|
init({ count, seed }) {
|
|
131980
132109
|
super.init({ count, seed });
|
|
131981
132110
|
const rng = prand.xoroshiro128plus(seed);
|
|
131982
|
-
|
|
131983
|
-
const anchorDate = new Date('2024-05-08');
|
|
131984
|
-
// 2 years in milliseconds
|
|
131985
|
-
const deltaMilliseconds = 2 * 31536000000;
|
|
131986
|
-
if (typeof minDatetime === 'string') {
|
|
131987
|
-
minDatetime = new Date(minDatetime);
|
|
131988
|
-
}
|
|
131989
|
-
if (typeof minDatetime === 'object' && !isValidDate(minDatetime)) {
|
|
131990
|
-
throw new Error('Invalid Date was provided for the minDatetime parameter.');
|
|
131991
|
-
}
|
|
131992
|
-
if (typeof maxDatetime === 'string') {
|
|
131993
|
-
maxDatetime = new Date(maxDatetime);
|
|
131994
|
-
}
|
|
131995
|
-
if (typeof maxDatetime === 'object' && !isValidDate(maxDatetime)) {
|
|
131996
|
-
throw new Error('Invalid Date was provided for the maxDatetime parameter.');
|
|
131997
|
-
}
|
|
131998
|
-
if (minDatetime === undefined) {
|
|
131999
|
-
if (maxDatetime === undefined) {
|
|
132000
|
-
minDatetime = new Date(anchorDate.getTime() - deltaMilliseconds);
|
|
132001
|
-
maxDatetime = new Date(anchorDate.getTime() + deltaMilliseconds);
|
|
132002
|
-
}
|
|
132003
|
-
else {
|
|
132004
|
-
minDatetime = new Date(maxDatetime.getTime() - (2 * deltaMilliseconds));
|
|
132005
|
-
}
|
|
132006
|
-
}
|
|
132007
|
-
if (maxDatetime === undefined) {
|
|
132008
|
-
maxDatetime = new Date(minDatetime.getTime() + (2 * deltaMilliseconds));
|
|
132009
|
-
}
|
|
132010
|
-
if (minDatetime > maxDatetime) {
|
|
132011
|
-
throw new Error(`The minDatetime parameter must be less than or equal to the maxDatetime parameter.`);
|
|
132012
|
-
}
|
|
132013
|
-
this.state = { rng, minDatetime, maxDatetime };
|
|
132111
|
+
this.state = { rng };
|
|
132014
132112
|
}
|
|
132015
132113
|
generate() {
|
|
132016
132114
|
if (this.state === undefined) {
|
|
132017
132115
|
throw new Error('state is not defined.');
|
|
132018
132116
|
}
|
|
132117
|
+
const anchorDate = new Date('2024-05-08');
|
|
132118
|
+
const twoYearsInMilliseconds = 2 * 31536000000;
|
|
132119
|
+
let date = new Date();
|
|
132019
132120
|
let milliseconds;
|
|
132020
|
-
[milliseconds, this.state.rng] = prand.uniformIntDistribution(
|
|
132021
|
-
|
|
132121
|
+
[milliseconds, this.state.rng] = prand.uniformIntDistribution(-twoYearsInMilliseconds, twoYearsInMilliseconds, this.state.rng);
|
|
132122
|
+
date = new Date(date.setTime(anchorDate.getTime() + milliseconds));
|
|
132022
132123
|
if (this.dataType === 'string') {
|
|
132023
132124
|
return date
|
|
132024
132125
|
.toISOString()
|
|
@@ -132304,6 +132405,8 @@ class GenerateString extends AbstractGenerator {
|
|
|
132304
132405
|
[idx, this.state.rng] = prand.uniformIntDistribution(0, stringChars.length - 1, this.state.rng);
|
|
132305
132406
|
currStr += stringChars[idx];
|
|
132306
132407
|
}
|
|
132408
|
+
if (this.dataType === 'object')
|
|
132409
|
+
return Buffer.from(currStr);
|
|
132307
132410
|
return currStr;
|
|
132308
132411
|
}
|
|
132309
132412
|
}
|
|
@@ -132332,7 +132435,10 @@ class GenerateUniqueString extends AbstractGenerator {
|
|
|
132332
132435
|
[idx, this.state.rng] = prand.uniformIntDistribution(0, stringChars.length - 1, this.state.rng);
|
|
132333
132436
|
currStr += stringChars[idx];
|
|
132334
132437
|
}
|
|
132335
|
-
|
|
132438
|
+
currStr = currStr.slice(0, 4) + uniqueStr + currStr.slice(4);
|
|
132439
|
+
if (this.dataType === 'object')
|
|
132440
|
+
return Buffer.from(currStr);
|
|
132441
|
+
return currStr;
|
|
132336
132442
|
}
|
|
132337
132443
|
}
|
|
132338
132444
|
class GenerateUUID extends AbstractGenerator {
|
|
@@ -132374,8 +132480,8 @@ class GenerateFirstName extends AbstractGenerator {
|
|
|
132374
132480
|
init({ count, seed }) {
|
|
132375
132481
|
super.init({ count, seed });
|
|
132376
132482
|
const rng = prand.xoroshiro128plus(seed);
|
|
132377
|
-
if (this.
|
|
132378
|
-
throw new Error(`You can't use first name generator with a db column length restriction of ${this.
|
|
132483
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$5) {
|
|
132484
|
+
throw new Error(`You can't use first name generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxStringLength$5}.`);
|
|
132379
132485
|
}
|
|
132380
132486
|
this.state = { rng };
|
|
132381
132487
|
}
|
|
@@ -132398,8 +132504,8 @@ class GenerateUniqueFirstName extends AbstractGenerator {
|
|
|
132398
132504
|
if (count > firstNames.length) {
|
|
132399
132505
|
throw new Error('count exceeds max number of unique first names.');
|
|
132400
132506
|
}
|
|
132401
|
-
if (this.
|
|
132402
|
-
throw new Error(`You can't use first name generator with a db column length restriction of ${this.
|
|
132507
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$5) {
|
|
132508
|
+
throw new Error(`You can't use first name generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxStringLength$5}.`);
|
|
132403
132509
|
}
|
|
132404
132510
|
const genIndicesObj = new GenerateUniqueInt({ minValue: 0, maxValue: firstNames.length - 1 });
|
|
132405
132511
|
genIndicesObj.init({ count, seed });
|
|
@@ -132422,8 +132528,8 @@ class GenerateLastName extends AbstractGenerator {
|
|
|
132422
132528
|
init({ count, seed }) {
|
|
132423
132529
|
super.init({ count, seed });
|
|
132424
132530
|
const rng = prand.xoroshiro128plus(seed);
|
|
132425
|
-
if (this.
|
|
132426
|
-
throw new Error(`You can't use last name generator with a db column length restriction of ${this.
|
|
132531
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$3) {
|
|
132532
|
+
throw new Error(`You can't use last name generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxStringLength$3}.`);
|
|
132427
132533
|
}
|
|
132428
132534
|
this.state = { rng };
|
|
132429
132535
|
}
|
|
@@ -132444,8 +132550,8 @@ class GenerateUniqueLastName extends AbstractGenerator {
|
|
|
132444
132550
|
if (count > lastNames.length) {
|
|
132445
132551
|
throw new Error('count exceeds max number of unique last names.');
|
|
132446
132552
|
}
|
|
132447
|
-
if (this.
|
|
132448
|
-
throw new Error(`You can't use last name generator with a db column length restriction of ${this.
|
|
132553
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$3) {
|
|
132554
|
+
throw new Error(`You can't use last name generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxStringLength$3}.`);
|
|
132449
132555
|
}
|
|
132450
132556
|
const genIndicesObj = new GenerateUniqueInt({ minValue: 0, maxValue: lastNames.length - 1 });
|
|
132451
132557
|
genIndicesObj.init({ count, seed });
|
|
@@ -132467,8 +132573,8 @@ class GenerateFullName extends AbstractGenerator {
|
|
|
132467
132573
|
init({ count, seed }) {
|
|
132468
132574
|
super.init({ count, seed });
|
|
132469
132575
|
const rng = prand.xoroshiro128plus(seed);
|
|
132470
|
-
if (this.
|
|
132471
|
-
throw new Error(`You can't use full name generator with a db column length restriction of ${this.
|
|
132576
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < (maxStringLength$5 + maxStringLength$3 + 1)) {
|
|
132577
|
+
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}.`);
|
|
132472
132578
|
}
|
|
132473
132579
|
this.state = { rng };
|
|
132474
132580
|
}
|
|
@@ -132496,8 +132602,8 @@ class GenerateUniqueFullName extends AbstractGenerator {
|
|
|
132496
132602
|
if (count > maxUniqueFullNamesNumber) {
|
|
132497
132603
|
throw new RangeError(`count exceeds max number of unique full names(${maxUniqueFullNamesNumber}).`);
|
|
132498
132604
|
}
|
|
132499
|
-
if (this.
|
|
132500
|
-
throw new Error(`You can't use full name generator with a db column length restriction of ${this.
|
|
132605
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < (maxStringLength$5 + maxStringLength$3 + 1)) {
|
|
132606
|
+
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}.`);
|
|
132501
132607
|
}
|
|
132502
132608
|
const rng = prand.xoroshiro128plus(seed);
|
|
132503
132609
|
const fullnameSet = new Set();
|
|
@@ -132540,8 +132646,8 @@ class GenerateEmail extends AbstractGenerator {
|
|
|
132540
132646
|
throw new RangeError(`count exceeds max number of unique emails(${maxUniqueEmailsNumber}).`);
|
|
132541
132647
|
}
|
|
132542
132648
|
const maxEmailLength = maxStringLength$a + maxStringLength$5 + maxStringLength$6 + 2;
|
|
132543
|
-
if (this.
|
|
132544
|
-
throw new Error(`You can't use email generator with a db column length restriction of ${this.
|
|
132649
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxEmailLength) {
|
|
132650
|
+
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}.`);
|
|
132545
132651
|
}
|
|
132546
132652
|
const arraysToGenerateFrom = [adjectivesArray, namesArray, domainsArray];
|
|
132547
132653
|
const genIndicesObj = new GenerateUniqueInt({
|
|
@@ -132574,8 +132680,8 @@ class GeneratePhoneNumber extends AbstractGenerator {
|
|
|
132574
132680
|
const { prefixes, template } = this.params;
|
|
132575
132681
|
const rng = prand.xoroshiro128plus(seed);
|
|
132576
132682
|
if (template !== undefined) {
|
|
132577
|
-
if (this.
|
|
132578
|
-
throw new Error(`Length of phone number template is shorter than db column length restriction: ${this.
|
|
132683
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < template.length) {
|
|
132684
|
+
throw new Error(`Length of phone number template is shorter than db column length restriction: ${this.typeParams?.length}.
|
|
132579
132685
|
Set the maximum string length to at least ${template.length}.`);
|
|
132580
132686
|
}
|
|
132581
132687
|
const iterArray = [...template.matchAll(/#/g)];
|
|
@@ -132620,8 +132726,8 @@ class GeneratePhoneNumber extends AbstractGenerator {
|
|
|
132620
132726
|
}
|
|
132621
132727
|
const maxPrefixLength = Math.max(...prefixesArray.map((prefix) => prefix.length));
|
|
132622
132728
|
const maxGeneratedDigits = Math.max(...generatedDigitsNumbers);
|
|
132623
|
-
if (this.
|
|
132624
|
-
throw new Error(`You can't use phone number generator with a db column length restriction of ${this.
|
|
132729
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < (maxPrefixLength + maxGeneratedDigits)) {
|
|
132730
|
+
throw new Error(`You can't use phone number generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxPrefixLength + maxGeneratedDigits}.`);
|
|
132625
132731
|
}
|
|
132626
132732
|
if (new Set(prefixesArray).size !== prefixesArray.length) {
|
|
132627
132733
|
throw new Error('prefixes are not unique.');
|
|
@@ -132698,8 +132804,8 @@ class GenerateCountry extends AbstractGenerator {
|
|
|
132698
132804
|
init({ count, seed }) {
|
|
132699
132805
|
super.init({ count, seed });
|
|
132700
132806
|
const rng = prand.xoroshiro128plus(seed);
|
|
132701
|
-
if (this.
|
|
132702
|
-
throw new Error(`You can't use country generator with a db column length restriction of ${this.
|
|
132807
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$7) {
|
|
132808
|
+
throw new Error(`You can't use country generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxStringLength$7}.`);
|
|
132703
132809
|
}
|
|
132704
132810
|
this.state = { rng };
|
|
132705
132811
|
}
|
|
@@ -132721,8 +132827,8 @@ class GenerateUniqueCountry extends AbstractGenerator {
|
|
|
132721
132827
|
if (count > countries.length) {
|
|
132722
132828
|
throw new Error('count exceeds max number of unique countries.');
|
|
132723
132829
|
}
|
|
132724
|
-
if (this.
|
|
132725
|
-
throw new Error(`You can't use country generator with a db column length restriction of ${this.
|
|
132830
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$7) {
|
|
132831
|
+
throw new Error(`You can't use country generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxStringLength$7}.`);
|
|
132726
132832
|
}
|
|
132727
132833
|
const genIndicesObj = new GenerateUniqueInt({ minValue: 0, maxValue: countries.length - 1 });
|
|
132728
132834
|
genIndicesObj.init({ count, seed });
|
|
@@ -132743,8 +132849,8 @@ class GenerateJobTitle extends AbstractGenerator {
|
|
|
132743
132849
|
init({ count, seed }) {
|
|
132744
132850
|
super.init({ count, seed });
|
|
132745
132851
|
const rng = prand.xoroshiro128plus(seed);
|
|
132746
|
-
if (this.
|
|
132747
|
-
throw new Error(`You can't use job title generator with a db column length restriction of ${this.
|
|
132852
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$4) {
|
|
132853
|
+
throw new Error(`You can't use job title generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxStringLength$4}.`);
|
|
132748
132854
|
}
|
|
132749
132855
|
this.state = { rng };
|
|
132750
132856
|
}
|
|
@@ -132766,8 +132872,8 @@ class GenerateStreetAddress extends AbstractGenerator {
|
|
|
132766
132872
|
const rng = prand.xoroshiro128plus(seed);
|
|
132767
132873
|
const possStreetNames = [firstNames, lastNames];
|
|
132768
132874
|
const maxStreetAddressLength = 4 + Math.max(maxStringLength$5, maxStringLength$3) + 1 + maxStringLength;
|
|
132769
|
-
if (this.
|
|
132770
|
-
throw new Error(`You can't use street address generator with a db column length restriction of ${this.
|
|
132875
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStreetAddressLength) {
|
|
132876
|
+
throw new Error(`You can't use street address generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxStreetAddressLength}.`);
|
|
132771
132877
|
}
|
|
132772
132878
|
this.state = { rng, possStreetNames };
|
|
132773
132879
|
}
|
|
@@ -132796,8 +132902,8 @@ class GenerateUniqueStreetAddress extends AbstractGenerator {
|
|
|
132796
132902
|
throw new RangeError(`count exceeds max number of unique street names(${maxUniqueStreetnamesNumber}).`);
|
|
132797
132903
|
}
|
|
132798
132904
|
const maxStreetAddressLength = 4 + Math.max(maxStringLength$5, maxStringLength$3) + 1 + maxStringLength;
|
|
132799
|
-
if (this.
|
|
132800
|
-
throw new Error(`You can't use street address generator with a db column length restriction of ${this.
|
|
132905
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStreetAddressLength) {
|
|
132906
|
+
throw new Error(`You can't use street address generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxStreetAddressLength}.`);
|
|
132801
132907
|
}
|
|
132802
132908
|
const rng = prand.xoroshiro128plus(seed);
|
|
132803
132909
|
// ["1", "2", ..., "999"]
|
|
@@ -132853,8 +132959,8 @@ class GenerateCity extends AbstractGenerator {
|
|
|
132853
132959
|
init({ count, seed }) {
|
|
132854
132960
|
super.init({ count, seed });
|
|
132855
132961
|
const rng = prand.xoroshiro128plus(seed);
|
|
132856
|
-
if (this.
|
|
132857
|
-
throw new Error(`You can't use city generator with a db column length restriction of ${this.
|
|
132962
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$9) {
|
|
132963
|
+
throw new Error(`You can't use city generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxStringLength$9}.`);
|
|
132858
132964
|
}
|
|
132859
132965
|
this.state = { rng };
|
|
132860
132966
|
}
|
|
@@ -132875,8 +132981,8 @@ class GenerateUniqueCity extends AbstractGenerator {
|
|
|
132875
132981
|
if (count > cityNames.length) {
|
|
132876
132982
|
throw new Error('count exceeds max number of unique cities.');
|
|
132877
132983
|
}
|
|
132878
|
-
if (this.
|
|
132879
|
-
throw new Error(`You can't use city generator with a db column length restriction of ${this.
|
|
132984
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$9) {
|
|
132985
|
+
throw new Error(`You can't use city generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxStringLength$9}.`);
|
|
132880
132986
|
}
|
|
132881
132987
|
const genIndicesObj = new GenerateUniqueInt({ minValue: 0, maxValue: cityNames.length - 1 });
|
|
132882
132988
|
genIndicesObj.init({ count, seed });
|
|
@@ -132900,8 +133006,8 @@ class GeneratePostcode extends AbstractGenerator {
|
|
|
132900
133006
|
const rng = prand.xoroshiro128plus(seed);
|
|
132901
133007
|
const templates = ['#####', '#####-####'];
|
|
132902
133008
|
const maxPostcodeLength = Math.max(...templates.map((template) => template.length));
|
|
132903
|
-
if (this.
|
|
132904
|
-
throw new Error(`You can't use postcode generator with a db column length restriction of ${this.
|
|
133009
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxPostcodeLength) {
|
|
133010
|
+
throw new Error(`You can't use postcode generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxPostcodeLength}.`);
|
|
132905
133011
|
}
|
|
132906
133012
|
this.state = { rng, templates };
|
|
132907
133013
|
}
|
|
@@ -132951,8 +133057,8 @@ class GenerateUniquePostcode extends AbstractGenerator {
|
|
|
132951
133057
|
},
|
|
132952
133058
|
];
|
|
132953
133059
|
const maxPostcodeLength = Math.max(...templates.map((template) => template.template.length));
|
|
132954
|
-
if (this.
|
|
132955
|
-
throw new Error(`You can't use postcode generator with a db column length restriction of ${this.
|
|
133060
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxPostcodeLength) {
|
|
133061
|
+
throw new Error(`You can't use postcode generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxPostcodeLength}.`);
|
|
132956
133062
|
}
|
|
132957
133063
|
for (const templateObj of templates) {
|
|
132958
133064
|
templateObj.indicesGen.skipCheck = true;
|
|
@@ -132988,8 +133094,8 @@ class GenerateState extends AbstractGenerator {
|
|
|
132988
133094
|
init({ count, seed }) {
|
|
132989
133095
|
super.init({ count, seed });
|
|
132990
133096
|
const rng = prand.xoroshiro128plus(seed);
|
|
132991
|
-
if (this.
|
|
132992
|
-
throw new Error(`You can't use state generator with a db column length restriction of ${this.
|
|
133097
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$1) {
|
|
133098
|
+
throw new Error(`You can't use state generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxStringLength$1}.`);
|
|
132993
133099
|
}
|
|
132994
133100
|
this.state = { rng };
|
|
132995
133101
|
}
|
|
@@ -133017,8 +133123,8 @@ class GenerateCompanyName extends AbstractGenerator {
|
|
|
133017
133123
|
];
|
|
133018
133124
|
// max( { template: '#', placeholdersCount: 1 }, { template: '#, # and #', placeholdersCount: 3 } )
|
|
133019
133125
|
const maxCompanyNameLength = Math.max(maxStringLength$3 + maxStringLength$8 + 1, 3 * maxStringLength$3 + 7);
|
|
133020
|
-
if (this.
|
|
133021
|
-
throw new Error(`You can't use company name generator with a db column length restriction of ${this.
|
|
133126
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxCompanyNameLength) {
|
|
133127
|
+
throw new Error(`You can't use company name generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxCompanyNameLength}.`);
|
|
133022
133128
|
}
|
|
133023
133129
|
this.state = { rng, templates };
|
|
133024
133130
|
}
|
|
@@ -133062,8 +133168,8 @@ class GenerateUniqueCompanyName extends AbstractGenerator {
|
|
|
133062
133168
|
}
|
|
133063
133169
|
// max( { template: '#', placeholdersCount: 1 }, { template: '#, # and #', placeholdersCount: 3 } )
|
|
133064
133170
|
const maxCompanyNameLength = Math.max(maxStringLength$3 + maxStringLength$8 + 1, 3 * maxStringLength$3 + 7);
|
|
133065
|
-
if (this.
|
|
133066
|
-
throw new Error(`You can't use company name generator with a db column length restriction of ${this.
|
|
133171
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxCompanyNameLength) {
|
|
133172
|
+
throw new Error(`You can't use company name generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxCompanyNameLength}.`);
|
|
133067
133173
|
}
|
|
133068
133174
|
const rng = prand.xoroshiro128plus(seed);
|
|
133069
133175
|
// when count reach maxUniqueCompanyNameNumber template will be deleted from array
|
|
@@ -133139,8 +133245,8 @@ class GenerateLoremIpsum extends AbstractGenerator {
|
|
|
133139
133245
|
this.params.sentencesCount = 1;
|
|
133140
133246
|
const maxLoremIpsumSentencesLength = maxStringLength$2 * this.params.sentencesCount + this.params.sentencesCount
|
|
133141
133247
|
- 1;
|
|
133142
|
-
if (this.
|
|
133143
|
-
throw new Error(`You can't use lorem ipsum generator with a db column length restriction of ${this.
|
|
133248
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxLoremIpsumSentencesLength) {
|
|
133249
|
+
throw new Error(`You can't use lorem ipsum generator with a db column length restriction of ${this.typeParams?.length}. Set the maximum string length to at least ${maxLoremIpsumSentencesLength}.`);
|
|
133144
133250
|
}
|
|
133145
133251
|
this.state = { rng };
|
|
133146
133252
|
}
|
|
@@ -133229,7 +133335,7 @@ class GeneratePoint extends AbstractGenerator {
|
|
|
133229
133335
|
}
|
|
133230
133336
|
const x = this.state.xCoordinateGen.generate();
|
|
133231
133337
|
const y = this.state.yCoordinateGen.generate();
|
|
133232
|
-
if (this.dataType === '
|
|
133338
|
+
if (this.dataType === 'object') {
|
|
133233
133339
|
return { x, y };
|
|
133234
133340
|
}
|
|
133235
133341
|
else if (this.dataType === 'string') {
|
|
@@ -133246,6 +133352,7 @@ class GenerateUniquePoint extends AbstractGenerator {
|
|
|
133246
133352
|
state;
|
|
133247
133353
|
isUnique = true;
|
|
133248
133354
|
init({ count, seed }) {
|
|
133355
|
+
// TODO: rewrite the unique generator to use fastCartesianProduct for generating unique points.
|
|
133249
133356
|
const xCoordinateGen = new GenerateUniqueNumber({
|
|
133250
133357
|
minValue: this.params.minXValue,
|
|
133251
133358
|
maxValue: this.params.maxXValue,
|
|
@@ -133266,7 +133373,7 @@ class GenerateUniquePoint extends AbstractGenerator {
|
|
|
133266
133373
|
}
|
|
133267
133374
|
const x = this.state.xCoordinateGen.generate();
|
|
133268
133375
|
const y = this.state.yCoordinateGen.generate();
|
|
133269
|
-
if (this.dataType === '
|
|
133376
|
+
if (this.dataType === 'object') {
|
|
133270
133377
|
return { x, y };
|
|
133271
133378
|
}
|
|
133272
133379
|
else if (this.dataType === 'string') {
|
|
@@ -133315,7 +133422,7 @@ class GenerateLine extends AbstractGenerator {
|
|
|
133315
133422
|
b = this.state.bCoefficientGen.generate();
|
|
133316
133423
|
}
|
|
133317
133424
|
const c = this.state.cCoefficientGen.generate();
|
|
133318
|
-
if (this.dataType === '
|
|
133425
|
+
if (this.dataType === 'object') {
|
|
133319
133426
|
return { a, b, c };
|
|
133320
133427
|
}
|
|
133321
133428
|
else if (this.dataType === 'string') {
|
|
@@ -133332,6 +133439,7 @@ class GenerateUniqueLine extends AbstractGenerator {
|
|
|
133332
133439
|
state;
|
|
133333
133440
|
isUnique = true;
|
|
133334
133441
|
init({ count, seed }) {
|
|
133442
|
+
// TODO: rewrite the unique generator to use fastCartesianProduct for generating unique triplets(liens).
|
|
133335
133443
|
const aCoefficientGen = new GenerateUniqueNumber({
|
|
133336
133444
|
minValue: this.params.minAValue,
|
|
133337
133445
|
maxValue: this.params.maxAValue,
|
|
@@ -133363,7 +133471,7 @@ class GenerateUniqueLine extends AbstractGenerator {
|
|
|
133363
133471
|
b = this.state.bCoefficientGen.generate();
|
|
133364
133472
|
}
|
|
133365
133473
|
const c = this.state.cCoefficientGen.generate();
|
|
133366
|
-
if (this.dataType === '
|
|
133474
|
+
if (this.dataType === 'object') {
|
|
133367
133475
|
return { a, b, c };
|
|
133368
133476
|
}
|
|
133369
133477
|
else if (this.dataType === 'string') {
|
|
@@ -133375,6 +133483,397 @@ class GenerateUniqueLine extends AbstractGenerator {
|
|
|
133375
133483
|
}
|
|
133376
133484
|
}
|
|
133377
133485
|
}
|
|
133486
|
+
class GenerateBitString extends AbstractGenerator {
|
|
133487
|
+
static entityKind = 'GenerateBitString';
|
|
133488
|
+
dimensions = 11;
|
|
133489
|
+
state;
|
|
133490
|
+
uniqueVersionOfGen = GenerateUniqueBitString;
|
|
133491
|
+
init({ count, seed }) {
|
|
133492
|
+
super.init({ count, seed });
|
|
133493
|
+
this.dimensions = this.params.dimensions ?? this.typeParams?.length ?? this.dimensions;
|
|
133494
|
+
let intGen;
|
|
133495
|
+
if (this.dimensions > 53) {
|
|
133496
|
+
const maxValue = (BigInt(2) ** BigInt(this.dimensions)) - BigInt(1);
|
|
133497
|
+
intGen = new GenerateInt({ minValue: BigInt(0), maxValue });
|
|
133498
|
+
}
|
|
133499
|
+
else {
|
|
133500
|
+
// dimensions <= 53
|
|
133501
|
+
const maxValue = Math.pow(2, this.dimensions) - 1;
|
|
133502
|
+
intGen = new GenerateInt({ minValue: 0, maxValue });
|
|
133503
|
+
}
|
|
133504
|
+
intGen.init({ count, seed });
|
|
133505
|
+
this.state = { intGen };
|
|
133506
|
+
}
|
|
133507
|
+
generate() {
|
|
133508
|
+
if (this.state === undefined) {
|
|
133509
|
+
throw new Error('state is not defined.');
|
|
133510
|
+
}
|
|
133511
|
+
const bitString = this.state.intGen.generate().toString(2);
|
|
133512
|
+
return bitString.padStart(this.dimensions, '0');
|
|
133513
|
+
}
|
|
133514
|
+
}
|
|
133515
|
+
class GenerateUniqueBitString extends AbstractGenerator {
|
|
133516
|
+
static entityKind = 'GenerateUniqueBitString';
|
|
133517
|
+
dimensions = 11;
|
|
133518
|
+
state;
|
|
133519
|
+
isUnique = true;
|
|
133520
|
+
init({ count, seed }) {
|
|
133521
|
+
this.dimensions = this.params.dimensions ?? this.typeParams?.length ?? this.dimensions;
|
|
133522
|
+
let intGen;
|
|
133523
|
+
if (this.dimensions > 53) {
|
|
133524
|
+
const maxValue = (BigInt(2) ** BigInt(this.dimensions)) - BigInt(1);
|
|
133525
|
+
intGen = new GenerateUniqueInt({ minValue: BigInt(0), maxValue });
|
|
133526
|
+
}
|
|
133527
|
+
else {
|
|
133528
|
+
// dimensions <= 53
|
|
133529
|
+
const maxValue = Math.pow(2, this.dimensions) - 1;
|
|
133530
|
+
intGen = new GenerateUniqueInt({ minValue: 0, maxValue });
|
|
133531
|
+
}
|
|
133532
|
+
intGen.init({ count, seed });
|
|
133533
|
+
this.state = { intGen };
|
|
133534
|
+
}
|
|
133535
|
+
generate() {
|
|
133536
|
+
if (this.state === undefined) {
|
|
133537
|
+
throw new Error('state is not defined.');
|
|
133538
|
+
}
|
|
133539
|
+
const bitString = this.state.intGen.generate().toString(2);
|
|
133540
|
+
return bitString.padStart(this.dimensions, '0');
|
|
133541
|
+
}
|
|
133542
|
+
}
|
|
133543
|
+
class GenerateInet extends AbstractGenerator {
|
|
133544
|
+
static entityKind = 'GenerateInet';
|
|
133545
|
+
ipAddress = 'ipv4';
|
|
133546
|
+
includeCidr = true;
|
|
133547
|
+
state;
|
|
133548
|
+
uniqueVersionOfGen = GenerateUniqueInet;
|
|
133549
|
+
init({ count, seed }) {
|
|
133550
|
+
super.init({ count, seed });
|
|
133551
|
+
this.ipAddress = this.params.ipAddress ?? this.ipAddress;
|
|
133552
|
+
this.includeCidr = this.params.includeCidr ?? this.includeCidr;
|
|
133553
|
+
const rng = prand.xoroshiro128plus(seed);
|
|
133554
|
+
this.state = { rng };
|
|
133555
|
+
}
|
|
133556
|
+
generate() {
|
|
133557
|
+
if (this.state === undefined) {
|
|
133558
|
+
throw new Error('state is not defined.');
|
|
133559
|
+
}
|
|
133560
|
+
let value;
|
|
133561
|
+
const values = [];
|
|
133562
|
+
let inetVal = '';
|
|
133563
|
+
if (this.ipAddress === 'ipv4') {
|
|
133564
|
+
for (let octet = 0; octet < 4; octet++) {
|
|
133565
|
+
[value, this.state.rng] = prand.uniformIntDistribution(0, 255, this.state.rng);
|
|
133566
|
+
values.push(value.toString());
|
|
133567
|
+
}
|
|
133568
|
+
inetVal += values.join('.');
|
|
133569
|
+
if (this.includeCidr) {
|
|
133570
|
+
[value, this.state.rng] = prand.uniformIntDistribution(0, 32, this.state.rng);
|
|
133571
|
+
inetVal += `/${value}`;
|
|
133572
|
+
}
|
|
133573
|
+
return inetVal;
|
|
133574
|
+
}
|
|
133575
|
+
else {
|
|
133576
|
+
// this.ipAddress === 'ipv6'
|
|
133577
|
+
for (let hextet = 0; hextet < 8; hextet++) {
|
|
133578
|
+
[value, this.state.rng] = prand.uniformIntDistribution(0, 65535, this.state.rng);
|
|
133579
|
+
values.push(value.toString(16));
|
|
133580
|
+
}
|
|
133581
|
+
inetVal += values.join(':');
|
|
133582
|
+
if (this.includeCidr) {
|
|
133583
|
+
[value, this.state.rng] = prand.uniformIntDistribution(0, 128, this.state.rng);
|
|
133584
|
+
inetVal += `/${value}`;
|
|
133585
|
+
}
|
|
133586
|
+
return inetVal;
|
|
133587
|
+
}
|
|
133588
|
+
}
|
|
133589
|
+
}
|
|
133590
|
+
// TODO: add defaults to js doc
|
|
133591
|
+
class GenerateUniqueInet extends AbstractGenerator {
|
|
133592
|
+
static entityKind = 'GenerateUniqueInet';
|
|
133593
|
+
ipAddress = 'ipv4';
|
|
133594
|
+
includeCidr = true;
|
|
133595
|
+
delimiter = '.';
|
|
133596
|
+
state;
|
|
133597
|
+
isUnique = true;
|
|
133598
|
+
init({ count, seed }) {
|
|
133599
|
+
this.ipAddress = this.params.ipAddress ?? this.ipAddress;
|
|
133600
|
+
this.delimiter = this.ipAddress === 'ipv4' ? '.' : ':';
|
|
133601
|
+
this.includeCidr = this.params.includeCidr ?? this.includeCidr;
|
|
133602
|
+
// maxValue - number of combinations for cartesian product: {0…255} × {0…255} × {0…255} × {0…255} × {0…32}
|
|
133603
|
+
// where pattern for ipv4 ip is {0–255}.{0–255}.{0–255}.{0–255}[/{0–32}?]
|
|
133604
|
+
// 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}
|
|
133605
|
+
// where pattern for ipv6 ip is {0-65535}:{0-65535}:{0-65535}:{0-65535}:{0-65535}:{0-65535}:{0-65535}:{0-65535}[/0-128?]
|
|
133606
|
+
let minValue, maxValue;
|
|
133607
|
+
if (this.ipAddress === 'ipv4') {
|
|
133608
|
+
minValue = 0;
|
|
133609
|
+
maxValue = 256 ** 4;
|
|
133610
|
+
if (this.includeCidr) {
|
|
133611
|
+
maxValue = maxValue * 33;
|
|
133612
|
+
}
|
|
133613
|
+
}
|
|
133614
|
+
else {
|
|
133615
|
+
// this.ipAddress === 'ipv6'
|
|
133616
|
+
minValue = BigInt(0);
|
|
133617
|
+
maxValue = BigInt(65535) ** BigInt(8);
|
|
133618
|
+
if (this.includeCidr) {
|
|
133619
|
+
maxValue = maxValue * BigInt(129);
|
|
133620
|
+
}
|
|
133621
|
+
}
|
|
133622
|
+
const indexGen = new GenerateUniqueInt({ minValue, maxValue });
|
|
133623
|
+
indexGen.init({ count, seed });
|
|
133624
|
+
const octetSet = Array.from({ length: 256 }, (_, i) => i.toString());
|
|
133625
|
+
const ipv4PrefixSet = Array.from({ length: 33 }, (_, i) => i.toString());
|
|
133626
|
+
const hextetSet = Array.from({ length: 65536 }, (_, i) => i.toString(16));
|
|
133627
|
+
const ipv6PrefixSet = Array.from({ length: 129 }, (_, i) => i.toString());
|
|
133628
|
+
this.state = { indexGen, octetSet, ipv4PrefixSet, hextetSet, ipv6PrefixSet };
|
|
133629
|
+
}
|
|
133630
|
+
generate() {
|
|
133631
|
+
if (this.state === undefined) {
|
|
133632
|
+
throw new Error('state is not defined.');
|
|
133633
|
+
}
|
|
133634
|
+
let inetVal = '';
|
|
133635
|
+
let tokens = [];
|
|
133636
|
+
if (this.ipAddress === 'ipv4') {
|
|
133637
|
+
const sets = Array.from({ length: 4 }).fill(this.state.octetSet);
|
|
133638
|
+
if (this.includeCidr)
|
|
133639
|
+
sets.push(this.state.ipv4PrefixSet);
|
|
133640
|
+
const index = this.state.indexGen.generate();
|
|
133641
|
+
tokens = fastCartesianProduct(sets, index);
|
|
133642
|
+
}
|
|
133643
|
+
else {
|
|
133644
|
+
// this.ipAddress === 'ipv6'
|
|
133645
|
+
const sets = Array.from({ length: 8 }).fill(this.state.hextetSet);
|
|
133646
|
+
if (this.includeCidr)
|
|
133647
|
+
sets.push(this.state.ipv6PrefixSet);
|
|
133648
|
+
const idx = this.state.indexGen.generate();
|
|
133649
|
+
tokens = fastCartesianProductForBigint(sets, idx);
|
|
133650
|
+
}
|
|
133651
|
+
inetVal = this.includeCidr
|
|
133652
|
+
? tokens.slice(0, -1).join(this.delimiter) + `/${tokens.at(-1)}`
|
|
133653
|
+
: tokens.join(this.delimiter);
|
|
133654
|
+
return inetVal;
|
|
133655
|
+
}
|
|
133656
|
+
}
|
|
133657
|
+
class GenerateGeometry extends AbstractGenerator {
|
|
133658
|
+
static entityKind = 'GenerateGeometry';
|
|
133659
|
+
type = 'point';
|
|
133660
|
+
srid = 4326;
|
|
133661
|
+
decimalPlaces = 6;
|
|
133662
|
+
state;
|
|
133663
|
+
uniqueVersionOfGen = GenerateUniqueGeometry;
|
|
133664
|
+
init({ count, seed }) {
|
|
133665
|
+
super.init({ count, seed });
|
|
133666
|
+
this.type = this.params.type ?? this.type;
|
|
133667
|
+
this.srid = this.params.srid ?? this.srid;
|
|
133668
|
+
this.decimalPlaces = this.params.decimalPlaces ?? this.decimalPlaces;
|
|
133669
|
+
let minXValue, maxXValue, minYValue, maxYValue, denominator;
|
|
133670
|
+
if (this.type === 'point') {
|
|
133671
|
+
if (this.srid === 4326) {
|
|
133672
|
+
// Degrees (latitude / longitude)
|
|
133673
|
+
denominator = 10 ** this.decimalPlaces;
|
|
133674
|
+
minXValue = -180 * denominator;
|
|
133675
|
+
maxXValue = 180 * denominator;
|
|
133676
|
+
minYValue = -90 * denominator;
|
|
133677
|
+
maxYValue = 90 * denominator;
|
|
133678
|
+
}
|
|
133679
|
+
else {
|
|
133680
|
+
// this.srid === 3857
|
|
133681
|
+
// Meters (projected X / Y)
|
|
133682
|
+
denominator = 1;
|
|
133683
|
+
minXValue = -20026376;
|
|
133684
|
+
maxXValue = 20026376;
|
|
133685
|
+
minYValue = -20048966;
|
|
133686
|
+
maxYValue = 20048966;
|
|
133687
|
+
}
|
|
133688
|
+
}
|
|
133689
|
+
else {
|
|
133690
|
+
throw new Error('geometry generator currently supports only the point type.');
|
|
133691
|
+
}
|
|
133692
|
+
const rng = prand.xoroshiro128plus(seed);
|
|
133693
|
+
this.state = { rng, minXValue, maxXValue, minYValue, maxYValue, denominator };
|
|
133694
|
+
}
|
|
133695
|
+
generate() {
|
|
133696
|
+
if (this.state === undefined) {
|
|
133697
|
+
throw new Error('state is not defined.');
|
|
133698
|
+
}
|
|
133699
|
+
let x, y;
|
|
133700
|
+
[x, this.state.rng] = prand.uniformIntDistribution(this.state.minXValue, this.state.maxXValue, this.state.rng);
|
|
133701
|
+
x = x / this.state.denominator;
|
|
133702
|
+
[y, this.state.rng] = prand.uniformIntDistribution(this.state.minYValue, this.state.maxYValue, this.state.rng);
|
|
133703
|
+
y = y / this.state.denominator;
|
|
133704
|
+
if (this.dataType === 'array') {
|
|
133705
|
+
return [x, y];
|
|
133706
|
+
}
|
|
133707
|
+
// this.dataType === 'object'
|
|
133708
|
+
return { x, y };
|
|
133709
|
+
}
|
|
133710
|
+
}
|
|
133711
|
+
class GenerateUniqueGeometry extends AbstractGenerator {
|
|
133712
|
+
static entityKind = 'GenerateUniqueGeometry';
|
|
133713
|
+
type = 'point';
|
|
133714
|
+
srid = 4326;
|
|
133715
|
+
decimalPlaces = 6;
|
|
133716
|
+
state;
|
|
133717
|
+
isUnique = true;
|
|
133718
|
+
init({ count, seed }) {
|
|
133719
|
+
this.type = this.params.type ?? this.type;
|
|
133720
|
+
this.srid = this.params.srid ?? this.srid;
|
|
133721
|
+
this.decimalPlaces = this.params.decimalPlaces ?? this.decimalPlaces;
|
|
133722
|
+
let minXValue, maxXValue, minYValue, maxYValue, denominator;
|
|
133723
|
+
if (this.type === 'point') {
|
|
133724
|
+
if (this.srid === 4326) {
|
|
133725
|
+
// Degrees (latitude / longitude)
|
|
133726
|
+
denominator = 10 ** this.decimalPlaces;
|
|
133727
|
+
minXValue = -180 * denominator;
|
|
133728
|
+
maxXValue = 180 * denominator;
|
|
133729
|
+
minYValue = -90 * denominator;
|
|
133730
|
+
maxYValue = 90 * denominator;
|
|
133731
|
+
}
|
|
133732
|
+
else {
|
|
133733
|
+
// this.srid === 3857
|
|
133734
|
+
// Meters (projected X / Y)
|
|
133735
|
+
denominator = 1;
|
|
133736
|
+
minXValue = -20026376;
|
|
133737
|
+
maxXValue = 20026376;
|
|
133738
|
+
minYValue = -20048966;
|
|
133739
|
+
maxYValue = 20048966;
|
|
133740
|
+
}
|
|
133741
|
+
}
|
|
133742
|
+
else {
|
|
133743
|
+
throw new Error('geometry generator currently supports only the point type.');
|
|
133744
|
+
}
|
|
133745
|
+
const xRange = new OrderedNumberRange(minXValue, maxXValue, 1);
|
|
133746
|
+
const yRange = new OrderedNumberRange(minYValue, maxYValue, 1);
|
|
133747
|
+
const xySets = [xRange, yRange];
|
|
133748
|
+
const maxCombIdx = BigInt(maxXValue - minXValue + 1) * BigInt(maxYValue - minYValue + 1) - BigInt(1);
|
|
133749
|
+
const indexGen = maxCombIdx <= 2 ** 53
|
|
133750
|
+
? new GenerateUniqueInt({ minValue: 0, maxValue: Number(maxCombIdx) })
|
|
133751
|
+
: new GenerateUniqueInt({ minValue: BigInt(0), maxValue: maxCombIdx });
|
|
133752
|
+
indexGen.init({ count, seed });
|
|
133753
|
+
this.state = { denominator, indexGen, xySets };
|
|
133754
|
+
}
|
|
133755
|
+
generate() {
|
|
133756
|
+
if (this.state === undefined) {
|
|
133757
|
+
throw new Error('state is not defined.');
|
|
133758
|
+
}
|
|
133759
|
+
const idx = this.state.indexGen.generate();
|
|
133760
|
+
let x, y;
|
|
133761
|
+
if (typeof idx === 'number') {
|
|
133762
|
+
[x, y] = fastCartesianProduct(this.state.xySets, idx);
|
|
133763
|
+
}
|
|
133764
|
+
else {
|
|
133765
|
+
// typeof idx === 'bigint'
|
|
133766
|
+
[x, y] = fastCartesianProductForBigint(this.state.xySets, idx);
|
|
133767
|
+
}
|
|
133768
|
+
if (this.dataType === 'array') {
|
|
133769
|
+
return [x, y];
|
|
133770
|
+
}
|
|
133771
|
+
// this.dataType === 'object'
|
|
133772
|
+
return { x, y };
|
|
133773
|
+
}
|
|
133774
|
+
}
|
|
133775
|
+
class GenerateVector extends AbstractGenerator {
|
|
133776
|
+
static entityKind = 'GenerateVector';
|
|
133777
|
+
// property below should be overridden in init
|
|
133778
|
+
dimensions = 3;
|
|
133779
|
+
minValue = -1000;
|
|
133780
|
+
maxValue = 1000;
|
|
133781
|
+
decimalPlaces = 2;
|
|
133782
|
+
state;
|
|
133783
|
+
uniqueVersionOfGen = GenerateUniqueVector;
|
|
133784
|
+
init({ count, seed }) {
|
|
133785
|
+
super.init({ count, seed });
|
|
133786
|
+
this.dimensions = this.params.dimensions ?? this.typeParams.length ?? this.dimensions;
|
|
133787
|
+
this.decimalPlaces = this.params.decimalPlaces ?? this.decimalPlaces;
|
|
133788
|
+
this.minValue = this.params.minValue ?? this.minValue;
|
|
133789
|
+
this.maxValue = this.params.maxValue ?? this.maxValue;
|
|
133790
|
+
if (this.minValue > this.maxValue) {
|
|
133791
|
+
throw new Error(`minValue ( ${this.minValue} ) cannot be greater than maxValue ( ${this.maxValue} ).\n`
|
|
133792
|
+
+ `Did you forget to pass both minValue and maxValue to the generator's properties?`);
|
|
133793
|
+
}
|
|
133794
|
+
if (this.decimalPlaces < 0) {
|
|
133795
|
+
throw new Error(`decimalPlaces value must be greater than or equal to zero.`);
|
|
133796
|
+
}
|
|
133797
|
+
if (abs(BigInt(this.minValue) * BigInt(10 ** this.decimalPlaces)) > Number.MAX_SAFE_INTEGER
|
|
133798
|
+
|| abs(BigInt(this.maxValue) * BigInt(10 ** this.decimalPlaces)) > Number.MAX_SAFE_INTEGER) {
|
|
133799
|
+
console.warn(`vector generator: minValue or maxValue multiplied by 10^decimalPlaces exceeds Number.MAX_SAFE_INTEGER (2^53 -1).\n`
|
|
133800
|
+
+ `This overflow may result in less accurate values being generated.`);
|
|
133801
|
+
}
|
|
133802
|
+
// `numberGen` is initialized in the `init` method of `GenerateArray`
|
|
133803
|
+
const numberGen = new GenerateNumber({
|
|
133804
|
+
minValue: this.minValue,
|
|
133805
|
+
maxValue: this.maxValue,
|
|
133806
|
+
precision: 10 ** this.decimalPlaces,
|
|
133807
|
+
});
|
|
133808
|
+
const vectorGen = new GenerateArray({ baseColumnGen: numberGen, size: this.dimensions });
|
|
133809
|
+
vectorGen.init({ count, seed });
|
|
133810
|
+
this.state = { vectorGen };
|
|
133811
|
+
}
|
|
133812
|
+
generate() {
|
|
133813
|
+
if (this.state === undefined) {
|
|
133814
|
+
throw new Error('state is not defined.');
|
|
133815
|
+
}
|
|
133816
|
+
const vectorVal = this.state.vectorGen.generate();
|
|
133817
|
+
return vectorVal;
|
|
133818
|
+
}
|
|
133819
|
+
}
|
|
133820
|
+
class GenerateUniqueVector extends AbstractGenerator {
|
|
133821
|
+
static entityKind = 'GenerateUniqueVector';
|
|
133822
|
+
// property below should be overridden in init
|
|
133823
|
+
dimensions = 3;
|
|
133824
|
+
minValue = -1000;
|
|
133825
|
+
maxValue = 1000;
|
|
133826
|
+
decimalPlaces = 2;
|
|
133827
|
+
state;
|
|
133828
|
+
isUnique = true;
|
|
133829
|
+
init({ count, seed }) {
|
|
133830
|
+
this.dimensions = this.params.dimensions ?? this.typeParams.length ?? this.dimensions;
|
|
133831
|
+
this.decimalPlaces = this.params.decimalPlaces ?? this.decimalPlaces;
|
|
133832
|
+
const denominator = 10 ** this.decimalPlaces;
|
|
133833
|
+
this.minValue = this.params.minValue ?? this.minValue;
|
|
133834
|
+
this.maxValue = this.params.maxValue ?? this.maxValue;
|
|
133835
|
+
if (this.minValue > this.maxValue) {
|
|
133836
|
+
throw new Error(`minValue ( ${this.minValue} ) cannot be greater than maxValue ( ${this.maxValue} ).\n`
|
|
133837
|
+
+ `Did you forget to pass both minValue and maxValue to the generator's properties?`);
|
|
133838
|
+
}
|
|
133839
|
+
if (this.decimalPlaces < 0) {
|
|
133840
|
+
throw new Error(`decimalPlaces value must be greater than or equal to zero.`);
|
|
133841
|
+
}
|
|
133842
|
+
if (abs(BigInt(this.minValue) * BigInt(denominator)) > Number.MAX_SAFE_INTEGER
|
|
133843
|
+
|| abs(BigInt(this.maxValue) * BigInt(denominator)) > Number.MAX_SAFE_INTEGER) {
|
|
133844
|
+
console.warn(`vector generator: minValue or maxValue multiplied by 10^decimalPlaces exceeds Number.MAX_SAFE_INTEGER (2^53 -1).\n`
|
|
133845
|
+
+ `This overflow may result in less accurate values being generated.`);
|
|
133846
|
+
}
|
|
133847
|
+
const dimensionRange = new OrderedNumberRange(this.minValue * denominator, this.maxValue * denominator, 1);
|
|
133848
|
+
const vectorSets = Array.from({ length: this.dimensions }).fill(dimensionRange);
|
|
133849
|
+
const maxCombIdx = vectorSets.reduce((acc, curr) => acc * BigInt(curr.length), BigInt(1)) - BigInt(1);
|
|
133850
|
+
const indexGen = maxCombIdx <= Number.MAX_SAFE_INTEGER
|
|
133851
|
+
? new GenerateUniqueInt({ minValue: 0, maxValue: Number(maxCombIdx) })
|
|
133852
|
+
: new GenerateUniqueInt({ minValue: BigInt(0), maxValue: maxCombIdx });
|
|
133853
|
+
indexGen.init({ count, seed });
|
|
133854
|
+
const transformVector = denominator === 1
|
|
133855
|
+
? (_vector, _denominator) => { }
|
|
133856
|
+
: (vector, denominator) => {
|
|
133857
|
+
for (let i = 0; i < vector.length; i++) {
|
|
133858
|
+
vector[i] = vector[i] / denominator;
|
|
133859
|
+
}
|
|
133860
|
+
return;
|
|
133861
|
+
};
|
|
133862
|
+
this.state = { indexGen, vectorSets, denominator, transformVector };
|
|
133863
|
+
}
|
|
133864
|
+
generate() {
|
|
133865
|
+
if (this.state === undefined) {
|
|
133866
|
+
throw new Error('state is not defined.');
|
|
133867
|
+
}
|
|
133868
|
+
const idx = this.state.indexGen.generate();
|
|
133869
|
+
const vector = typeof idx === 'number'
|
|
133870
|
+
? fastCartesianProduct(this.state.vectorSets, idx)
|
|
133871
|
+
// typeof idx === 'bigint'
|
|
133872
|
+
: fastCartesianProductForBigint(this.state.vectorSets, idx);
|
|
133873
|
+
this.state.transformVector(vector, this.state.denominator);
|
|
133874
|
+
return vector;
|
|
133875
|
+
}
|
|
133876
|
+
}
|
|
133378
133877
|
|
|
133379
133878
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
133380
133879
|
class GenerateUniqueIntervalV2 extends AbstractGenerator {
|
|
@@ -133461,8 +133960,8 @@ class GenerateStringV2 extends AbstractGenerator {
|
|
|
133461
133960
|
super.init({ count, seed });
|
|
133462
133961
|
let minStringLength = 7;
|
|
133463
133962
|
let maxStringLength = 20;
|
|
133464
|
-
if (this.
|
|
133465
|
-
maxStringLength = this.
|
|
133963
|
+
if (this.typeParams?.length !== undefined) {
|
|
133964
|
+
maxStringLength = this.typeParams?.length;
|
|
133466
133965
|
if (maxStringLength === 1)
|
|
133467
133966
|
minStringLength = maxStringLength;
|
|
133468
133967
|
if (maxStringLength < minStringLength)
|
|
@@ -133484,6 +133983,8 @@ class GenerateStringV2 extends AbstractGenerator {
|
|
|
133484
133983
|
[idx, this.state.rng] = prand.uniformIntDistribution(0, stringChars.length - 1, this.state.rng);
|
|
133485
133984
|
currStr += stringChars[idx];
|
|
133486
133985
|
}
|
|
133986
|
+
if (this.dataType === 'object')
|
|
133987
|
+
return Buffer.from(currStr);
|
|
133487
133988
|
return currStr;
|
|
133488
133989
|
}
|
|
133489
133990
|
}
|
|
@@ -133497,8 +133998,8 @@ class GenerateUniqueStringV2 extends AbstractGenerator {
|
|
|
133497
133998
|
let minStringLength = 7;
|
|
133498
133999
|
let maxStringLength = 20;
|
|
133499
134000
|
// TODO: revise later
|
|
133500
|
-
if (this.
|
|
133501
|
-
maxStringLength = this.
|
|
134001
|
+
if (this.typeParams?.length !== undefined) {
|
|
134002
|
+
maxStringLength = this.typeParams?.length;
|
|
133502
134003
|
if (maxStringLength === 1 || maxStringLength < minStringLength)
|
|
133503
134004
|
minStringLength = maxStringLength;
|
|
133504
134005
|
}
|
|
@@ -133522,6 +134023,8 @@ class GenerateUniqueStringV2 extends AbstractGenerator {
|
|
|
133522
134023
|
[idx, this.state.rng] = prand.uniformIntDistribution(0, stringChars.length - 1, this.state.rng);
|
|
133523
134024
|
currStr += stringChars[idx];
|
|
133524
134025
|
}
|
|
134026
|
+
if (this.dataType === 'object')
|
|
134027
|
+
return Buffer.from(uniqueStr + currStr);
|
|
133525
134028
|
return uniqueStr + currStr;
|
|
133526
134029
|
}
|
|
133527
134030
|
}
|
|
@@ -133691,8 +134194,6 @@ const generatorsFuncs = {
|
|
|
133691
134194
|
date: createGenerator(GenerateDate),
|
|
133692
134195
|
/**
|
|
133693
134196
|
* generates time in 24 hours style.
|
|
133694
|
-
* @param minTime - lower border of range.
|
|
133695
|
-
* @param maxTime - upper border of range.
|
|
133696
134197
|
* @param arraySize - number of elements in each one-dimensional array. (If specified, arrays will be generated.)
|
|
133697
134198
|
*
|
|
133698
134199
|
* @example
|
|
@@ -133700,7 +134201,7 @@ const generatorsFuncs = {
|
|
|
133700
134201
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
133701
134202
|
* users: {
|
|
133702
134203
|
* columns: {
|
|
133703
|
-
* birthTime: funcs.time(
|
|
134204
|
+
* birthTime: funcs.time()
|
|
133704
134205
|
* },
|
|
133705
134206
|
* },
|
|
133706
134207
|
* }));
|
|
@@ -133710,8 +134211,6 @@ const generatorsFuncs = {
|
|
|
133710
134211
|
time: createGenerator(GenerateTime),
|
|
133711
134212
|
/**
|
|
133712
134213
|
* generates timestamps.
|
|
133713
|
-
* @param minTimestamp - lower border of range.
|
|
133714
|
-
* @param maxTimestamp - upper border of range.
|
|
133715
134214
|
* @param arraySize - number of elements in each one-dimensional array. (If specified, arrays will be generated.)
|
|
133716
134215
|
*
|
|
133717
134216
|
* @example
|
|
@@ -133719,7 +134218,7 @@ const generatorsFuncs = {
|
|
|
133719
134218
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
133720
134219
|
* orders: {
|
|
133721
134220
|
* columns: {
|
|
133722
|
-
* shippedDate: funcs.timestamp(
|
|
134221
|
+
* shippedDate: funcs.timestamp()
|
|
133723
134222
|
* },
|
|
133724
134223
|
* },
|
|
133725
134224
|
* }));
|
|
@@ -133729,8 +134228,6 @@ const generatorsFuncs = {
|
|
|
133729
134228
|
timestamp: createGenerator(GenerateTimestamp),
|
|
133730
134229
|
/**
|
|
133731
134230
|
* generates datetime objects.
|
|
133732
|
-
* @param minDatetime - lower border of range.
|
|
133733
|
-
* @param maxDatetime - upper border of range.
|
|
133734
134231
|
* @param arraySize - number of elements in each one-dimensional array. (If specified, arrays will be generated.)
|
|
133735
134232
|
*
|
|
133736
134233
|
* @example
|
|
@@ -133738,7 +134235,7 @@ const generatorsFuncs = {
|
|
|
133738
134235
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
133739
134236
|
* orders: {
|
|
133740
134237
|
* columns: {
|
|
133741
|
-
* shippedDate: funcs.datetime(
|
|
134238
|
+
* shippedDate: funcs.datetime()
|
|
133742
134239
|
* },
|
|
133743
134240
|
* },
|
|
133744
134241
|
* }));
|
|
@@ -134198,8 +134695,132 @@ const generatorsFuncs = {
|
|
|
134198
134695
|
* ```
|
|
134199
134696
|
*/
|
|
134200
134697
|
weightedRandom: createGenerator(WeightedRandomGenerator),
|
|
134698
|
+
/**
|
|
134699
|
+
* generates bit strings based on specified parameters
|
|
134700
|
+
*
|
|
134701
|
+
* @param isUnique - property that controls if generated values gonna be unique or not.
|
|
134702
|
+
* @param arraySize - number of elements in each one-dimensional array (If specified, arrays will be generated).
|
|
134703
|
+
* @param dimensions - desired length of each bit string (e.g., `dimensions = 3` produces values like `'010'`).
|
|
134704
|
+
*
|
|
134705
|
+
* Defaults to the value of the database column bit-length.
|
|
134706
|
+
*
|
|
134707
|
+
* @example
|
|
134708
|
+
* ```ts
|
|
134709
|
+
* await seed(db, { bitStringTable: schema.bitStringTable }).refine((funcs) => ({
|
|
134710
|
+
* bitStringTable: {
|
|
134711
|
+
* count,
|
|
134712
|
+
* columns: {
|
|
134713
|
+
* bit: funcs.bitString({
|
|
134714
|
+
* dimensions: 12,
|
|
134715
|
+
* isUnique: true
|
|
134716
|
+
* }),
|
|
134717
|
+
* },
|
|
134718
|
+
* },
|
|
134719
|
+
* }));
|
|
134720
|
+
* ```
|
|
134721
|
+
*/
|
|
134722
|
+
bitString: createGenerator(GenerateBitString),
|
|
134723
|
+
/**
|
|
134724
|
+
* generates ip addresses based on specified parameters
|
|
134725
|
+
*
|
|
134726
|
+
* @param isUnique - property that controls if generated values gonna be unique or not.
|
|
134727
|
+
* @param arraySize - number of elements in each one-dimensional array (If specified, arrays will be generated).
|
|
134728
|
+
* @param ipAddress - type of IP address to generate — either "ipv4" or "ipv6".
|
|
134729
|
+
*
|
|
134730
|
+
* Defaults to `'ipv4'`.
|
|
134731
|
+
* @param includeCidr - determines whether generated IPs include a CIDR suffix.
|
|
134732
|
+
*
|
|
134733
|
+
* Defaults to `true`.
|
|
134734
|
+
*
|
|
134735
|
+
* @example
|
|
134736
|
+
* ```ts
|
|
134737
|
+
* await seed(db, { inetTable: schema.inetTable }).refine((funcs) => ({
|
|
134738
|
+
* inetTable: {
|
|
134739
|
+
* count,
|
|
134740
|
+
* columns: {
|
|
134741
|
+
* inet: funcs.inet({
|
|
134742
|
+
* ipAddress: 'ipv4',
|
|
134743
|
+
* includeCidr: true,
|
|
134744
|
+
* isUnique: true
|
|
134745
|
+
* }),
|
|
134746
|
+
* },
|
|
134747
|
+
* },
|
|
134748
|
+
* }));
|
|
134749
|
+
* ```
|
|
134750
|
+
*/
|
|
134751
|
+
inet: createGenerator(GenerateInet),
|
|
134752
|
+
/**
|
|
134753
|
+
* generates PostGIS geometry objects based on the given parameters.
|
|
134754
|
+
*
|
|
134755
|
+
* @param isUnique - property that controls if generated values gonna be unique or not.
|
|
134756
|
+
* @param arraySize - number of elements in each one-dimensional array (If specified, arrays will be generated).
|
|
134757
|
+
* @param type - geometry type to generate; currently only `'point'` is supported.
|
|
134758
|
+
*
|
|
134759
|
+
* Defaults to `'point'`.
|
|
134760
|
+
* @param srid - Spatial Reference System Identifier: determines what type of point will be generated - either `4326` or `3857`.
|
|
134761
|
+
*
|
|
134762
|
+
* Defaults to `4326`.
|
|
134763
|
+
* @param decimalPlaces - number of decimal places for points when `srid` is `4326` (e.g., `decimalPlaces = 3` produces values like `'point(30.723 46.482)'`).
|
|
134764
|
+
*
|
|
134765
|
+
* Defaults to `6`.
|
|
134766
|
+
*
|
|
134767
|
+
* @example
|
|
134768
|
+
* ```ts
|
|
134769
|
+
* await seed(db, { geometryTable: schema.geometryTable }).refine((funcs) => ({
|
|
134770
|
+
* geometryTable: {
|
|
134771
|
+
* count,
|
|
134772
|
+
* columns: {
|
|
134773
|
+
* geometryPointTuple: funcs.geometry({
|
|
134774
|
+
* type: 'point',
|
|
134775
|
+
* srid: 4326,
|
|
134776
|
+
* decimalPlaces: 5,
|
|
134777
|
+
* isUnique: true
|
|
134778
|
+
* })
|
|
134779
|
+
* },
|
|
134780
|
+
* },
|
|
134781
|
+
* }));
|
|
134782
|
+
* ```
|
|
134783
|
+
*/
|
|
134784
|
+
geometry: createGenerator(GenerateGeometry),
|
|
134785
|
+
/**
|
|
134786
|
+
* generates vectors based on the provided parameters.
|
|
134787
|
+
*
|
|
134788
|
+
* @param isUnique - property that controls if generated values gonna be unique or not.
|
|
134789
|
+
* @param arraySize - number of elements in each one-dimensional array (If specified, arrays will be generated).
|
|
134790
|
+
* @param decimalPlaces - number of decimal places for each vector element (e.g., `decimalPlaces = 3` produces values like `1.123`).
|
|
134791
|
+
*
|
|
134792
|
+
* Defaults to `2`.
|
|
134793
|
+
* @param dimensions - number of elements in each generated vector (e.g., `dimensions = 3` produces values like `[1,2,3]`).
|
|
134794
|
+
*
|
|
134795
|
+
* Defaults to the value of the database column’s dimensions.
|
|
134796
|
+
* @param minValue - minimum allowed value for each vector element.
|
|
134797
|
+
*
|
|
134798
|
+
* Defaults to `-1000`.
|
|
134799
|
+
* @param maxValue - maximum allowed value for each vector element.
|
|
134800
|
+
*
|
|
134801
|
+
* Defaults to `1000`.
|
|
134802
|
+
*
|
|
134803
|
+
* @example
|
|
134804
|
+
* ```ts
|
|
134805
|
+
* await seed(db, { vectorTable: schema.vectorTable }).refine((funcs) => ({
|
|
134806
|
+
* vectorTable: {
|
|
134807
|
+
* count,
|
|
134808
|
+
* columns: {
|
|
134809
|
+
* vector: funcs.vector({
|
|
134810
|
+
* decimalPlaces: 5,
|
|
134811
|
+
* dimensions: 12,
|
|
134812
|
+
* minValue: -100,
|
|
134813
|
+
* maxValue: 100,
|
|
134814
|
+
* isUnique: true
|
|
134815
|
+
* }),
|
|
134816
|
+
* },
|
|
134817
|
+
* },
|
|
134818
|
+
* }));
|
|
134819
|
+
* ```
|
|
134820
|
+
*/
|
|
134821
|
+
vector: createGenerator(GenerateVector),
|
|
134201
134822
|
};
|
|
134202
|
-
// so far, version changes don’t
|
|
134823
|
+
// so far, version changes don’t affect generator parameters.
|
|
134203
134824
|
const generatorsFuncsV2 = {
|
|
134204
134825
|
...generatorsFuncs,
|
|
134205
134826
|
};
|
|
@@ -134357,10 +134978,1085 @@ const generatorsMap = {
|
|
|
134357
134978
|
GenerateWeightedCount: [
|
|
134358
134979
|
GenerateWeightedCount,
|
|
134359
134980
|
],
|
|
134981
|
+
GenerateBitString: [
|
|
134982
|
+
GenerateBitString,
|
|
134983
|
+
],
|
|
134984
|
+
GenerateUniqueBitString: [
|
|
134985
|
+
GenerateUniqueBitString,
|
|
134986
|
+
],
|
|
134987
|
+
GenerateInet: [
|
|
134988
|
+
GenerateInet,
|
|
134989
|
+
],
|
|
134990
|
+
GenerateUniqueInet: [
|
|
134991
|
+
GenerateUniqueInet,
|
|
134992
|
+
],
|
|
134993
|
+
GenerateGeometry: [
|
|
134994
|
+
GenerateGeometry,
|
|
134995
|
+
],
|
|
134996
|
+
GenerateUniqueGeometry: [
|
|
134997
|
+
GenerateUniqueGeometry,
|
|
134998
|
+
],
|
|
134999
|
+
GenerateVector: [
|
|
135000
|
+
GenerateVector,
|
|
135001
|
+
],
|
|
135002
|
+
GenerateUniqueVector: [
|
|
135003
|
+
GenerateUniqueVector,
|
|
135004
|
+
],
|
|
135005
|
+
};
|
|
135006
|
+
|
|
135007
|
+
// TODO: revise serial part generators
|
|
135008
|
+
const selectGeneratorForCockroachColumn = (table, col) => {
|
|
135009
|
+
const pickGenerator = (table, col) => {
|
|
135010
|
+
// ARRAY
|
|
135011
|
+
if (col.columnType.match(/\[\w*]/g) !== null && col.baseColumn !== undefined) {
|
|
135012
|
+
const baseColumnGen = selectGeneratorForCockroachColumn(table, col.baseColumn);
|
|
135013
|
+
if (baseColumnGen === undefined) {
|
|
135014
|
+
throw new Error(`column with type ${col.baseColumn.columnType} is not supported for now.`);
|
|
135015
|
+
}
|
|
135016
|
+
// const getBaseColumnDataType = (baseColumn: Column) => {
|
|
135017
|
+
// if (baseColumn.baseColumn !== undefined) {
|
|
135018
|
+
// return getBaseColumnDataType(baseColumn.baseColumn);
|
|
135019
|
+
// }
|
|
135020
|
+
// return baseColumn.dataType;
|
|
135021
|
+
// };
|
|
135022
|
+
// const baseColumnDataType = getBaseColumnDataType(col.baseColumn);
|
|
135023
|
+
const generator = new generatorsMap.GenerateArray[0]({ baseColumnGen, size: col.size });
|
|
135024
|
+
// generator.baseColumnDataType = baseColumnDataType;
|
|
135025
|
+
return generator;
|
|
135026
|
+
}
|
|
135027
|
+
// ARRAY for studio
|
|
135028
|
+
if (col.columnType.match(/\[\w*]/g) !== null) {
|
|
135029
|
+
// remove dimensions from type
|
|
135030
|
+
const baseColumnType = col.columnType.replace(/\[\w*]/g, '');
|
|
135031
|
+
const baseColumn = {
|
|
135032
|
+
...col,
|
|
135033
|
+
};
|
|
135034
|
+
baseColumn.columnType = baseColumnType;
|
|
135035
|
+
const baseColumnGen = selectGeneratorForCockroachColumn(table, baseColumn);
|
|
135036
|
+
if (baseColumnGen === undefined) {
|
|
135037
|
+
throw new Error(`column with type ${col.baseColumn.columnType} is not supported for now.`);
|
|
135038
|
+
}
|
|
135039
|
+
let generator = new generatorsMap.GenerateArray[0]({ baseColumnGen });
|
|
135040
|
+
for (let i = 0; i < col.typeParams.dimensions - 1; i++) {
|
|
135041
|
+
generator = new generatorsMap.GenerateArray[0]({ baseColumnGen: generator });
|
|
135042
|
+
}
|
|
135043
|
+
return generator;
|
|
135044
|
+
}
|
|
135045
|
+
// INT ------------------------------------------------------------------------------------------------------------
|
|
135046
|
+
if ((col.columnType === 'int2'
|
|
135047
|
+
|| col.columnType === 'int4'
|
|
135048
|
+
|| col.columnType.includes('int8'))
|
|
135049
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135050
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135051
|
+
return generator;
|
|
135052
|
+
}
|
|
135053
|
+
let minValue;
|
|
135054
|
+
let maxValue;
|
|
135055
|
+
if (col.columnType.startsWith('int')) {
|
|
135056
|
+
if (col.columnType === 'int2') {
|
|
135057
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135058
|
+
minValue = -32768;
|
|
135059
|
+
maxValue = 32767;
|
|
135060
|
+
}
|
|
135061
|
+
else if (col.columnType === 'int4') {
|
|
135062
|
+
// 2^32 / 2 - 1, 4 bytes
|
|
135063
|
+
minValue = -2147483648;
|
|
135064
|
+
maxValue = 2147483647;
|
|
135065
|
+
}
|
|
135066
|
+
else if (col.columnType.includes('int8')) {
|
|
135067
|
+
if (col.dataType === 'bigint') {
|
|
135068
|
+
// 2^64 / 2 - 1, 8 bytes
|
|
135069
|
+
minValue = BigInt('-9223372036854775808');
|
|
135070
|
+
maxValue = BigInt('9223372036854775807');
|
|
135071
|
+
}
|
|
135072
|
+
else {
|
|
135073
|
+
// if (col.dataType === 'number')
|
|
135074
|
+
// if you’re expecting values above 2^31 but below 2^53
|
|
135075
|
+
minValue = -9007199254740991;
|
|
135076
|
+
maxValue = 9007199254740991;
|
|
135077
|
+
}
|
|
135078
|
+
}
|
|
135079
|
+
}
|
|
135080
|
+
if (col.columnType.startsWith('int')
|
|
135081
|
+
&& !col.columnType.includes('interval')) {
|
|
135082
|
+
const generator = new generatorsMap.GenerateInt[0]({
|
|
135083
|
+
minValue,
|
|
135084
|
+
maxValue,
|
|
135085
|
+
});
|
|
135086
|
+
return generator;
|
|
135087
|
+
}
|
|
135088
|
+
// NUMBER(real, double, decimal, numeric)
|
|
135089
|
+
if (col.columnType.startsWith('real')
|
|
135090
|
+
|| col.columnType.startsWith('float')
|
|
135091
|
+
|| col.columnType.startsWith('decimal')
|
|
135092
|
+
|| col.columnType.startsWith('numeric')) {
|
|
135093
|
+
if (col.typeParams.precision !== undefined) {
|
|
135094
|
+
const precision = col.typeParams.precision;
|
|
135095
|
+
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
135096
|
+
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
135097
|
+
const generator = new generatorsMap.GenerateNumber[0]({
|
|
135098
|
+
minValue: -maxAbsoluteValue,
|
|
135099
|
+
maxValue: maxAbsoluteValue,
|
|
135100
|
+
precision: Math.pow(10, scale),
|
|
135101
|
+
});
|
|
135102
|
+
return generator;
|
|
135103
|
+
}
|
|
135104
|
+
const generator = new generatorsMap.GenerateNumber[0]();
|
|
135105
|
+
return generator;
|
|
135106
|
+
}
|
|
135107
|
+
// STRING
|
|
135108
|
+
if ((col.columnType === 'string'
|
|
135109
|
+
|| col.columnType.startsWith('varchar')
|
|
135110
|
+
|| col.columnType.startsWith('char'))
|
|
135111
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135112
|
+
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135113
|
+
return generator;
|
|
135114
|
+
}
|
|
135115
|
+
if ((col.columnType === 'string'
|
|
135116
|
+
|| col.columnType.startsWith('varchar')
|
|
135117
|
+
|| col.columnType.startsWith('char'))
|
|
135118
|
+
&& col.name.toLowerCase().includes('name')) {
|
|
135119
|
+
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135120
|
+
return generator;
|
|
135121
|
+
}
|
|
135122
|
+
if ((col.columnType === 'string'
|
|
135123
|
+
|| col.columnType.startsWith('varchar')
|
|
135124
|
+
|| col.columnType.startsWith('char'))
|
|
135125
|
+
&& col.name.toLowerCase().includes('email')) {
|
|
135126
|
+
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135127
|
+
return generator;
|
|
135128
|
+
}
|
|
135129
|
+
if (col.columnType === 'string'
|
|
135130
|
+
|| col.columnType.startsWith('varchar')
|
|
135131
|
+
|| col.columnType.startsWith('char')) {
|
|
135132
|
+
const generator = new generatorsMap.GenerateString[0]();
|
|
135133
|
+
return generator;
|
|
135134
|
+
}
|
|
135135
|
+
// BIT
|
|
135136
|
+
if (col.columnType.startsWith('bit')) {
|
|
135137
|
+
const generator = new generatorsMap.GenerateBitString[0]();
|
|
135138
|
+
return generator;
|
|
135139
|
+
}
|
|
135140
|
+
// INET
|
|
135141
|
+
if (col.columnType === 'inet') {
|
|
135142
|
+
const generator = new generatorsMap.GenerateInet[0]();
|
|
135143
|
+
return generator;
|
|
135144
|
+
}
|
|
135145
|
+
// geometry(point)
|
|
135146
|
+
if (col.columnType.startsWith('geometry')) {
|
|
135147
|
+
const generator = new generatorsMap.GenerateGeometry[0]();
|
|
135148
|
+
return generator;
|
|
135149
|
+
}
|
|
135150
|
+
// vector
|
|
135151
|
+
if (col.columnType.startsWith('vector')) {
|
|
135152
|
+
const generator = new generatorsMap.GenerateVector[0]();
|
|
135153
|
+
return generator;
|
|
135154
|
+
}
|
|
135155
|
+
// UUID
|
|
135156
|
+
if (col.columnType === 'uuid') {
|
|
135157
|
+
const generator = new generatorsMap.GenerateUUID[0]();
|
|
135158
|
+
return generator;
|
|
135159
|
+
}
|
|
135160
|
+
// BOOL
|
|
135161
|
+
if (col.columnType === 'bool') {
|
|
135162
|
+
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135163
|
+
return generator;
|
|
135164
|
+
}
|
|
135165
|
+
// DATE, TIME, TIMESTAMP
|
|
135166
|
+
if (col.columnType.includes('date')) {
|
|
135167
|
+
const generator = new generatorsMap.GenerateDate[0]();
|
|
135168
|
+
return generator;
|
|
135169
|
+
}
|
|
135170
|
+
if (col.columnType === 'time') {
|
|
135171
|
+
const generator = new generatorsMap.GenerateTime[0]();
|
|
135172
|
+
return generator;
|
|
135173
|
+
}
|
|
135174
|
+
if (col.columnType.includes('timestamp')) {
|
|
135175
|
+
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135176
|
+
return generator;
|
|
135177
|
+
}
|
|
135178
|
+
// JSON, JSONB
|
|
135179
|
+
if (col.columnType === 'json' || col.columnType === 'jsonb') {
|
|
135180
|
+
const generator = new generatorsMap.GenerateJson[0]();
|
|
135181
|
+
return generator;
|
|
135182
|
+
}
|
|
135183
|
+
// if (col.columnType === "jsonb") {
|
|
135184
|
+
// const generator = new GenerateJsonb({});
|
|
135185
|
+
// return generator;
|
|
135186
|
+
// }
|
|
135187
|
+
// ENUM
|
|
135188
|
+
if (col.enumValues !== undefined) {
|
|
135189
|
+
const generator = new generatorsMap.GenerateEnum[0]({
|
|
135190
|
+
enumValues: col.enumValues,
|
|
135191
|
+
});
|
|
135192
|
+
return generator;
|
|
135193
|
+
}
|
|
135194
|
+
// INTERVAL
|
|
135195
|
+
if (col.columnType.startsWith('interval')) {
|
|
135196
|
+
if (col.columnType === 'interval') {
|
|
135197
|
+
const generator = new generatorsMap.GenerateInterval[0]();
|
|
135198
|
+
return generator;
|
|
135199
|
+
}
|
|
135200
|
+
const fields = col.columnType.replace('interval ', '');
|
|
135201
|
+
const generator = new generatorsMap.GenerateInterval[0]({ fields });
|
|
135202
|
+
return generator;
|
|
135203
|
+
}
|
|
135204
|
+
if (col.hasDefault && col.default !== undefined) {
|
|
135205
|
+
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135206
|
+
defaultValue: col.default,
|
|
135207
|
+
});
|
|
135208
|
+
return generator;
|
|
135209
|
+
}
|
|
135210
|
+
return;
|
|
135211
|
+
};
|
|
135212
|
+
const generator = pickGenerator(table, col);
|
|
135213
|
+
// set params for base column
|
|
135214
|
+
if (generator !== undefined) {
|
|
135215
|
+
generator.isUnique = col.isUnique;
|
|
135216
|
+
generator.dataType = col.dataType;
|
|
135217
|
+
// generator.stringLength = col.typeParams.length;
|
|
135218
|
+
generator.typeParams = col.typeParams;
|
|
135219
|
+
}
|
|
135220
|
+
return generator;
|
|
134360
135221
|
};
|
|
134361
135222
|
|
|
134362
135223
|
const latestVersion = 2;
|
|
134363
135224
|
|
|
135225
|
+
const selectGeneratorForMssqlColumn = (table, col) => {
|
|
135226
|
+
const pickGenerator = (table, col) => {
|
|
135227
|
+
// INT ------------------------------------------------------------------------------------------------------------
|
|
135228
|
+
if (col.columnType.includes('int') && table.primaryKeys.includes(col.name)) {
|
|
135229
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135230
|
+
return generator;
|
|
135231
|
+
}
|
|
135232
|
+
let minValue;
|
|
135233
|
+
let maxValue;
|
|
135234
|
+
if (col.columnType.includes('int')) {
|
|
135235
|
+
if (col.columnType === 'tinyint') {
|
|
135236
|
+
// 2^8 / 2 - 1, 1 bytes
|
|
135237
|
+
// more like unsigned tinyint
|
|
135238
|
+
minValue = 0;
|
|
135239
|
+
maxValue = 255;
|
|
135240
|
+
}
|
|
135241
|
+
else if (col.columnType === 'smallint') {
|
|
135242
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135243
|
+
minValue = -32768;
|
|
135244
|
+
maxValue = 32767;
|
|
135245
|
+
}
|
|
135246
|
+
else if (col.columnType === 'int') {
|
|
135247
|
+
// 2^32 / 2 - 1, 4 bytes
|
|
135248
|
+
minValue = -2147483648;
|
|
135249
|
+
maxValue = 2147483647;
|
|
135250
|
+
}
|
|
135251
|
+
else if (col.columnType === 'bigint') {
|
|
135252
|
+
// 2^64 / 2 - 1, 8 bytes
|
|
135253
|
+
minValue = BigInt('-9223372036854775808');
|
|
135254
|
+
maxValue = BigInt('9223372036854775807');
|
|
135255
|
+
}
|
|
135256
|
+
const generator = new generatorsMap.GenerateInt[0]({
|
|
135257
|
+
minValue,
|
|
135258
|
+
maxValue,
|
|
135259
|
+
});
|
|
135260
|
+
return generator;
|
|
135261
|
+
}
|
|
135262
|
+
// NUMBER(real, decimal, numeric, float)
|
|
135263
|
+
if (col.columnType.startsWith('real')
|
|
135264
|
+
|| col.columnType.startsWith('decimal')
|
|
135265
|
+
|| col.columnType.startsWith('float')
|
|
135266
|
+
|| col.columnType.startsWith('numeric')) {
|
|
135267
|
+
if (col.typeParams.precision !== undefined) {
|
|
135268
|
+
const precision = col.typeParams.precision;
|
|
135269
|
+
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
135270
|
+
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
135271
|
+
const generator = new generatorsMap.GenerateNumber[0]({
|
|
135272
|
+
minValue: -maxAbsoluteValue,
|
|
135273
|
+
maxValue: maxAbsoluteValue,
|
|
135274
|
+
precision: Math.pow(10, scale),
|
|
135275
|
+
});
|
|
135276
|
+
return generator;
|
|
135277
|
+
}
|
|
135278
|
+
const generator = new generatorsMap.GenerateNumber[0]();
|
|
135279
|
+
return generator;
|
|
135280
|
+
}
|
|
135281
|
+
// STRING
|
|
135282
|
+
if ((col.columnType === 'text'
|
|
135283
|
+
|| col.columnType.startsWith('char')
|
|
135284
|
+
|| col.columnType.startsWith('varchar')
|
|
135285
|
+
|| col.columnType.startsWith('binary')
|
|
135286
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135287
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135288
|
+
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135289
|
+
return generator;
|
|
135290
|
+
}
|
|
135291
|
+
if ((col.columnType === 'text'
|
|
135292
|
+
|| col.columnType.startsWith('char')
|
|
135293
|
+
|| col.columnType.startsWith('varchar')
|
|
135294
|
+
|| col.columnType.startsWith('binary')
|
|
135295
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135296
|
+
&& col.name.toLowerCase().includes('name')) {
|
|
135297
|
+
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135298
|
+
return generator;
|
|
135299
|
+
}
|
|
135300
|
+
if ((col.columnType === 'text'
|
|
135301
|
+
|| col.columnType.startsWith('char')
|
|
135302
|
+
|| col.columnType.startsWith('varchar')
|
|
135303
|
+
|| col.columnType.startsWith('binary')
|
|
135304
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135305
|
+
&& col.name.toLowerCase().includes('email')) {
|
|
135306
|
+
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135307
|
+
return generator;
|
|
135308
|
+
}
|
|
135309
|
+
if (col.columnType === 'text'
|
|
135310
|
+
|| col.columnType.startsWith('char')
|
|
135311
|
+
|| col.columnType.startsWith('varchar')
|
|
135312
|
+
|| col.columnType.startsWith('binary')
|
|
135313
|
+
|| col.columnType.startsWith('varbinary')) {
|
|
135314
|
+
const generator = new generatorsMap.GenerateString[0]();
|
|
135315
|
+
return generator;
|
|
135316
|
+
}
|
|
135317
|
+
// bit
|
|
135318
|
+
if (col.columnType === 'bit') {
|
|
135319
|
+
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135320
|
+
return generator;
|
|
135321
|
+
}
|
|
135322
|
+
// DATE, TIME, TIMESTAMP, DATETIME, YEAR
|
|
135323
|
+
if (col.columnType.includes('datetime')) {
|
|
135324
|
+
const generator = new generatorsMap.GenerateDatetime[0]();
|
|
135325
|
+
return generator;
|
|
135326
|
+
}
|
|
135327
|
+
if (col.columnType.includes('date')) {
|
|
135328
|
+
const generator = new generatorsMap.GenerateDate[0]();
|
|
135329
|
+
return generator;
|
|
135330
|
+
}
|
|
135331
|
+
if (col.columnType === 'time') {
|
|
135332
|
+
const generator = new generatorsMap.GenerateTime[0]();
|
|
135333
|
+
return generator;
|
|
135334
|
+
}
|
|
135335
|
+
// JSON
|
|
135336
|
+
if (col.columnType === 'json') {
|
|
135337
|
+
const generator = new generatorsMap.GenerateJson[0]();
|
|
135338
|
+
return generator;
|
|
135339
|
+
}
|
|
135340
|
+
if (col.hasDefault && col.default !== undefined) {
|
|
135341
|
+
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135342
|
+
defaultValue: col.default,
|
|
135343
|
+
});
|
|
135344
|
+
return generator;
|
|
135345
|
+
}
|
|
135346
|
+
return;
|
|
135347
|
+
};
|
|
135348
|
+
const generator = pickGenerator(table, col);
|
|
135349
|
+
return generator;
|
|
135350
|
+
};
|
|
135351
|
+
|
|
135352
|
+
const selectGeneratorForMysqlColumn = (table, col) => {
|
|
135353
|
+
const pickGenerator = (table, col) => {
|
|
135354
|
+
// INT ------------------------------------------------------------------------------------------------------------
|
|
135355
|
+
if ((col.columnType.includes('serial') || col.columnType.includes('int'))
|
|
135356
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135357
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135358
|
+
return generator;
|
|
135359
|
+
}
|
|
135360
|
+
let minValue;
|
|
135361
|
+
let maxValue;
|
|
135362
|
+
if (col.columnType === 'serial') {
|
|
135363
|
+
// 2^64 % 2 - 1, 8 bytes
|
|
135364
|
+
minValue = BigInt(0);
|
|
135365
|
+
maxValue = BigInt('9223372036854775807');
|
|
135366
|
+
}
|
|
135367
|
+
else if (col.columnType.includes('int')) {
|
|
135368
|
+
if (col.columnType === 'tinyint') {
|
|
135369
|
+
// 2^8 / 2 - 1, 1 bytes
|
|
135370
|
+
minValue = -128;
|
|
135371
|
+
maxValue = 127;
|
|
135372
|
+
}
|
|
135373
|
+
else if (col.columnType === 'smallint') {
|
|
135374
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135375
|
+
minValue = -32768;
|
|
135376
|
+
maxValue = 32767;
|
|
135377
|
+
}
|
|
135378
|
+
else if (col.columnType === 'mediumint') {
|
|
135379
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135380
|
+
minValue = -8388608;
|
|
135381
|
+
maxValue = 8388607;
|
|
135382
|
+
}
|
|
135383
|
+
else if (col.columnType === 'int') {
|
|
135384
|
+
// 2^32 / 2 - 1, 4 bytes
|
|
135385
|
+
minValue = -2147483648;
|
|
135386
|
+
maxValue = 2147483647;
|
|
135387
|
+
}
|
|
135388
|
+
else if (col.columnType === 'bigint') {
|
|
135389
|
+
// 2^64 / 2 - 1, 8 bytes
|
|
135390
|
+
minValue = BigInt('-9223372036854775808');
|
|
135391
|
+
maxValue = BigInt('9223372036854775807');
|
|
135392
|
+
}
|
|
135393
|
+
}
|
|
135394
|
+
if (col.columnType.includes('int')) {
|
|
135395
|
+
const generator = new generatorsMap.GenerateInt[0]({
|
|
135396
|
+
minValue,
|
|
135397
|
+
maxValue,
|
|
135398
|
+
});
|
|
135399
|
+
return generator;
|
|
135400
|
+
}
|
|
135401
|
+
if (col.columnType.includes('serial')) {
|
|
135402
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135403
|
+
generator.maxValue = maxValue;
|
|
135404
|
+
return generator;
|
|
135405
|
+
}
|
|
135406
|
+
// NUMBER(real, double, decimal, float)
|
|
135407
|
+
if (col.columnType.startsWith('real')
|
|
135408
|
+
|| col.columnType.startsWith('double')
|
|
135409
|
+
|| col.columnType.startsWith('decimal')
|
|
135410
|
+
|| col.columnType.startsWith('float')
|
|
135411
|
+
|| col.columnType.startsWith('numeric')) {
|
|
135412
|
+
if (col.typeParams.precision !== undefined) {
|
|
135413
|
+
const precision = col.typeParams.precision;
|
|
135414
|
+
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
135415
|
+
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
135416
|
+
const generator = new generatorsMap.GenerateNumber[0]({
|
|
135417
|
+
minValue: -maxAbsoluteValue,
|
|
135418
|
+
maxValue: maxAbsoluteValue,
|
|
135419
|
+
precision: Math.pow(10, scale),
|
|
135420
|
+
});
|
|
135421
|
+
return generator;
|
|
135422
|
+
}
|
|
135423
|
+
const generator = new generatorsMap.GenerateNumber[0]();
|
|
135424
|
+
return generator;
|
|
135425
|
+
}
|
|
135426
|
+
// STRING
|
|
135427
|
+
if ((col.columnType === 'text'
|
|
135428
|
+
|| col.columnType === 'blob'
|
|
135429
|
+
|| col.columnType.startsWith('char')
|
|
135430
|
+
|| col.columnType.startsWith('varchar')
|
|
135431
|
+
|| col.columnType.startsWith('binary')
|
|
135432
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135433
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135434
|
+
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135435
|
+
return generator;
|
|
135436
|
+
}
|
|
135437
|
+
if ((col.columnType === 'text'
|
|
135438
|
+
|| col.columnType === 'blob'
|
|
135439
|
+
|| col.columnType.startsWith('char')
|
|
135440
|
+
|| col.columnType.startsWith('varchar')
|
|
135441
|
+
|| col.columnType.startsWith('binary')
|
|
135442
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135443
|
+
&& col.name.toLowerCase().includes('name')) {
|
|
135444
|
+
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135445
|
+
return generator;
|
|
135446
|
+
}
|
|
135447
|
+
if ((col.columnType === 'text'
|
|
135448
|
+
|| col.columnType === 'blob'
|
|
135449
|
+
|| col.columnType.startsWith('char')
|
|
135450
|
+
|| col.columnType.startsWith('varchar')
|
|
135451
|
+
|| col.columnType.startsWith('binary')
|
|
135452
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135453
|
+
&& col.name.toLowerCase().includes('email')) {
|
|
135454
|
+
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135455
|
+
return generator;
|
|
135456
|
+
}
|
|
135457
|
+
if (col.columnType === 'text'
|
|
135458
|
+
|| col.columnType === 'blob'
|
|
135459
|
+
|| col.columnType.startsWith('char')
|
|
135460
|
+
|| col.columnType.startsWith('varchar')
|
|
135461
|
+
|| col.columnType.startsWith('binary')
|
|
135462
|
+
|| col.columnType.startsWith('varbinary')) {
|
|
135463
|
+
const generator = new generatorsMap.GenerateString[0]();
|
|
135464
|
+
return generator;
|
|
135465
|
+
}
|
|
135466
|
+
// BOOLEAN
|
|
135467
|
+
if (col.columnType === 'boolean') {
|
|
135468
|
+
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135469
|
+
return generator;
|
|
135470
|
+
}
|
|
135471
|
+
// DATE, TIME, TIMESTAMP, DATETIME, YEAR
|
|
135472
|
+
if (col.columnType.includes('datetime')) {
|
|
135473
|
+
const generator = new generatorsMap.GenerateDatetime[0]();
|
|
135474
|
+
return generator;
|
|
135475
|
+
}
|
|
135476
|
+
if (col.columnType.includes('date')) {
|
|
135477
|
+
const generator = new generatorsMap.GenerateDate[0]();
|
|
135478
|
+
return generator;
|
|
135479
|
+
}
|
|
135480
|
+
if (col.columnType === 'time') {
|
|
135481
|
+
const generator = new generatorsMap.GenerateTime[0]();
|
|
135482
|
+
return generator;
|
|
135483
|
+
}
|
|
135484
|
+
if (col.columnType.includes('timestamp')) {
|
|
135485
|
+
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135486
|
+
return generator;
|
|
135487
|
+
}
|
|
135488
|
+
if (col.columnType === 'year') {
|
|
135489
|
+
const generator = new generatorsMap.GenerateYear[0]();
|
|
135490
|
+
return generator;
|
|
135491
|
+
}
|
|
135492
|
+
// JSON
|
|
135493
|
+
if (col.columnType === 'json') {
|
|
135494
|
+
const generator = new generatorsMap.GenerateJson[0]();
|
|
135495
|
+
return generator;
|
|
135496
|
+
}
|
|
135497
|
+
// ENUM
|
|
135498
|
+
if (col.enumValues !== undefined) {
|
|
135499
|
+
const generator = new generatorsMap.GenerateEnum[0]({
|
|
135500
|
+
enumValues: col.enumValues,
|
|
135501
|
+
});
|
|
135502
|
+
return generator;
|
|
135503
|
+
}
|
|
135504
|
+
if (col.hasDefault && col.default !== undefined) {
|
|
135505
|
+
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135506
|
+
defaultValue: col.default,
|
|
135507
|
+
});
|
|
135508
|
+
return generator;
|
|
135509
|
+
}
|
|
135510
|
+
return;
|
|
135511
|
+
};
|
|
135512
|
+
const generator = pickGenerator(table, col);
|
|
135513
|
+
return generator;
|
|
135514
|
+
};
|
|
135515
|
+
|
|
135516
|
+
// TODO: revise serial part generators
|
|
135517
|
+
const selectGeneratorForPostgresColumn = (table, col) => {
|
|
135518
|
+
const pickGenerator = (table, col) => {
|
|
135519
|
+
// ARRAY
|
|
135520
|
+
if (col.columnType.match(/\[\w*]/g) !== null && col.baseColumn !== undefined) {
|
|
135521
|
+
const baseColumnGen = selectGeneratorForPostgresColumn(table, col.baseColumn);
|
|
135522
|
+
if (baseColumnGen === undefined) {
|
|
135523
|
+
throw new Error(`column with type ${col.baseColumn.columnType} is not supported for now.`);
|
|
135524
|
+
}
|
|
135525
|
+
// const getBaseColumnDataType = (baseColumn: Column) => {
|
|
135526
|
+
// if (baseColumn.baseColumn !== undefined) {
|
|
135527
|
+
// return getBaseColumnDataType(baseColumn.baseColumn);
|
|
135528
|
+
// }
|
|
135529
|
+
// return baseColumn.dataType;
|
|
135530
|
+
// };
|
|
135531
|
+
// const baseColumnDataType = getBaseColumnDataType(col.baseColumn);
|
|
135532
|
+
const generator = new generatorsMap.GenerateArray[0]({ baseColumnGen, size: col.size });
|
|
135533
|
+
// generator.baseColumnDataType = baseColumnDataType;
|
|
135534
|
+
return generator;
|
|
135535
|
+
}
|
|
135536
|
+
// ARRAY for studio
|
|
135537
|
+
if (col.columnType.match(/\[\w*]/g) !== null) {
|
|
135538
|
+
// remove dimensions from type
|
|
135539
|
+
const baseColumnType = col.columnType.replace(/\[\w*]/g, '');
|
|
135540
|
+
const baseColumn = {
|
|
135541
|
+
...col,
|
|
135542
|
+
};
|
|
135543
|
+
baseColumn.columnType = baseColumnType;
|
|
135544
|
+
const baseColumnGen = selectGeneratorForPostgresColumn(table, baseColumn);
|
|
135545
|
+
if (baseColumnGen === undefined) {
|
|
135546
|
+
throw new Error(`column with type ${col.baseColumn.columnType} is not supported for now.`);
|
|
135547
|
+
}
|
|
135548
|
+
let generator = new generatorsMap.GenerateArray[0]({ baseColumnGen });
|
|
135549
|
+
for (let i = 0; i < col.typeParams.dimensions - 1; i++) {
|
|
135550
|
+
generator = new generatorsMap.GenerateArray[0]({ baseColumnGen: generator });
|
|
135551
|
+
}
|
|
135552
|
+
return generator;
|
|
135553
|
+
}
|
|
135554
|
+
// INT ------------------------------------------------------------------------------------------------------------
|
|
135555
|
+
if ((col.columnType.includes('serial')
|
|
135556
|
+
|| col.columnType === 'integer'
|
|
135557
|
+
|| col.columnType === 'smallint'
|
|
135558
|
+
|| col.columnType.includes('bigint'))
|
|
135559
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135560
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135561
|
+
return generator;
|
|
135562
|
+
}
|
|
135563
|
+
let minValue;
|
|
135564
|
+
let maxValue;
|
|
135565
|
+
if (col.columnType.includes('serial')) {
|
|
135566
|
+
minValue = 1;
|
|
135567
|
+
if (col.columnType === 'smallserial') {
|
|
135568
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135569
|
+
maxValue = 32767;
|
|
135570
|
+
}
|
|
135571
|
+
else if (col.columnType === 'serial') {
|
|
135572
|
+
// 2^32 / 2 - 1, 4 bytes
|
|
135573
|
+
maxValue = 2147483647;
|
|
135574
|
+
}
|
|
135575
|
+
else if (col.columnType === 'bigserial') {
|
|
135576
|
+
// 2^64 / 2 - 1, 8 bytes
|
|
135577
|
+
minValue = BigInt(1);
|
|
135578
|
+
maxValue = BigInt('9223372036854775807');
|
|
135579
|
+
}
|
|
135580
|
+
}
|
|
135581
|
+
else if (col.columnType.includes('int')) {
|
|
135582
|
+
if (col.columnType === 'smallint') {
|
|
135583
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135584
|
+
minValue = -32768;
|
|
135585
|
+
maxValue = 32767;
|
|
135586
|
+
}
|
|
135587
|
+
else if (col.columnType === 'integer') {
|
|
135588
|
+
// 2^32 / 2 - 1, 4 bytes
|
|
135589
|
+
minValue = -2147483648;
|
|
135590
|
+
maxValue = 2147483647;
|
|
135591
|
+
}
|
|
135592
|
+
else if (col.columnType.includes('bigint')) {
|
|
135593
|
+
if (col.dataType === 'bigint') {
|
|
135594
|
+
// 2^64 / 2 - 1, 8 bytes
|
|
135595
|
+
minValue = BigInt('-9223372036854775808');
|
|
135596
|
+
maxValue = BigInt('9223372036854775807');
|
|
135597
|
+
}
|
|
135598
|
+
else {
|
|
135599
|
+
// if (col.dataType === 'number')
|
|
135600
|
+
// if you’re expecting values above 2^31 but below 2^53
|
|
135601
|
+
minValue = -9007199254740991;
|
|
135602
|
+
maxValue = 9007199254740991;
|
|
135603
|
+
}
|
|
135604
|
+
}
|
|
135605
|
+
}
|
|
135606
|
+
if (col.columnType.includes('int')
|
|
135607
|
+
&& !col.columnType.includes('interval')
|
|
135608
|
+
&& !col.columnType.includes('point')) {
|
|
135609
|
+
const generator = new generatorsMap.GenerateInt[0]({
|
|
135610
|
+
minValue,
|
|
135611
|
+
maxValue,
|
|
135612
|
+
});
|
|
135613
|
+
return generator;
|
|
135614
|
+
}
|
|
135615
|
+
if (col.columnType.includes('serial')) {
|
|
135616
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135617
|
+
generator.maxValue = maxValue;
|
|
135618
|
+
return generator;
|
|
135619
|
+
}
|
|
135620
|
+
// NUMBER(real, double, decimal, numeric)
|
|
135621
|
+
if (col.columnType.startsWith('real')
|
|
135622
|
+
|| col.columnType.startsWith('double precision')
|
|
135623
|
+
|| col.columnType.startsWith('decimal')
|
|
135624
|
+
|| col.columnType.startsWith('numeric')) {
|
|
135625
|
+
if (col.typeParams.precision !== undefined) {
|
|
135626
|
+
const precision = col.typeParams.precision;
|
|
135627
|
+
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
135628
|
+
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
135629
|
+
const generator = new generatorsMap.GenerateNumber[0]({
|
|
135630
|
+
minValue: -maxAbsoluteValue,
|
|
135631
|
+
maxValue: maxAbsoluteValue,
|
|
135632
|
+
precision: Math.pow(10, scale),
|
|
135633
|
+
});
|
|
135634
|
+
return generator;
|
|
135635
|
+
}
|
|
135636
|
+
const generator = new generatorsMap.GenerateNumber[0]();
|
|
135637
|
+
return generator;
|
|
135638
|
+
}
|
|
135639
|
+
// STRING
|
|
135640
|
+
if ((col.columnType === 'text'
|
|
135641
|
+
|| col.columnType.startsWith('varchar')
|
|
135642
|
+
|| col.columnType.startsWith('char'))
|
|
135643
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135644
|
+
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135645
|
+
return generator;
|
|
135646
|
+
}
|
|
135647
|
+
if ((col.columnType === 'text'
|
|
135648
|
+
|| col.columnType.startsWith('varchar')
|
|
135649
|
+
|| col.columnType.startsWith('char'))
|
|
135650
|
+
&& col.name.toLowerCase().includes('name')) {
|
|
135651
|
+
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135652
|
+
return generator;
|
|
135653
|
+
}
|
|
135654
|
+
if ((col.columnType === 'text'
|
|
135655
|
+
|| col.columnType.startsWith('varchar')
|
|
135656
|
+
|| col.columnType.startsWith('char'))
|
|
135657
|
+
&& col.name.toLowerCase().includes('email')) {
|
|
135658
|
+
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135659
|
+
return generator;
|
|
135660
|
+
}
|
|
135661
|
+
if (col.columnType === 'text'
|
|
135662
|
+
|| col.columnType.startsWith('varchar')
|
|
135663
|
+
|| col.columnType.startsWith('char')) {
|
|
135664
|
+
const generator = new generatorsMap.GenerateString[0]();
|
|
135665
|
+
return generator;
|
|
135666
|
+
}
|
|
135667
|
+
// BIT
|
|
135668
|
+
if (col.columnType.startsWith('bit')) {
|
|
135669
|
+
const generator = new generatorsMap.GenerateBitString[0]();
|
|
135670
|
+
return generator;
|
|
135671
|
+
}
|
|
135672
|
+
// INET
|
|
135673
|
+
if (col.columnType === 'inet') {
|
|
135674
|
+
const generator = new generatorsMap.GenerateInet[0]();
|
|
135675
|
+
return generator;
|
|
135676
|
+
}
|
|
135677
|
+
// geometry(point)
|
|
135678
|
+
if (col.columnType.startsWith('geometry')) {
|
|
135679
|
+
const generator = new generatorsMap.GenerateGeometry[0]();
|
|
135680
|
+
return generator;
|
|
135681
|
+
}
|
|
135682
|
+
// vector
|
|
135683
|
+
if (col.columnType.startsWith('vector')) {
|
|
135684
|
+
const generator = new generatorsMap.GenerateVector[0]();
|
|
135685
|
+
return generator;
|
|
135686
|
+
}
|
|
135687
|
+
// UUID
|
|
135688
|
+
if (col.columnType === 'uuid') {
|
|
135689
|
+
const generator = new generatorsMap.GenerateUUID[0]();
|
|
135690
|
+
return generator;
|
|
135691
|
+
}
|
|
135692
|
+
// BOOLEAN
|
|
135693
|
+
if (col.columnType === 'boolean') {
|
|
135694
|
+
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135695
|
+
return generator;
|
|
135696
|
+
}
|
|
135697
|
+
// DATE, TIME, TIMESTAMP
|
|
135698
|
+
if (col.columnType.includes('date')) {
|
|
135699
|
+
const generator = new generatorsMap.GenerateDate[0]();
|
|
135700
|
+
return generator;
|
|
135701
|
+
}
|
|
135702
|
+
if (col.columnType === 'time') {
|
|
135703
|
+
const generator = new generatorsMap.GenerateTime[0]();
|
|
135704
|
+
return generator;
|
|
135705
|
+
}
|
|
135706
|
+
if (col.columnType.includes('timestamp')) {
|
|
135707
|
+
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135708
|
+
return generator;
|
|
135709
|
+
}
|
|
135710
|
+
// JSON, JSONB
|
|
135711
|
+
if (col.columnType === 'json' || col.columnType === 'jsonb') {
|
|
135712
|
+
const generator = new generatorsMap.GenerateJson[0]();
|
|
135713
|
+
return generator;
|
|
135714
|
+
}
|
|
135715
|
+
// if (col.columnType === "jsonb") {
|
|
135716
|
+
// const generator = new GenerateJsonb({});
|
|
135717
|
+
// return generator;
|
|
135718
|
+
// }
|
|
135719
|
+
// ENUM
|
|
135720
|
+
if (col.enumValues !== undefined) {
|
|
135721
|
+
const generator = new generatorsMap.GenerateEnum[0]({
|
|
135722
|
+
enumValues: col.enumValues,
|
|
135723
|
+
});
|
|
135724
|
+
return generator;
|
|
135725
|
+
}
|
|
135726
|
+
// INTERVAL
|
|
135727
|
+
if (col.columnType.startsWith('interval')) {
|
|
135728
|
+
if (col.columnType === 'interval') {
|
|
135729
|
+
const generator = new generatorsMap.GenerateInterval[0]();
|
|
135730
|
+
return generator;
|
|
135731
|
+
}
|
|
135732
|
+
const fields = col.columnType.replace('interval ', '');
|
|
135733
|
+
const generator = new generatorsMap.GenerateInterval[0]({ fields });
|
|
135734
|
+
return generator;
|
|
135735
|
+
}
|
|
135736
|
+
// POINT, LINE
|
|
135737
|
+
if (col.columnType.includes('point')) {
|
|
135738
|
+
const generator = new generatorsMap.GeneratePoint[0]();
|
|
135739
|
+
return generator;
|
|
135740
|
+
}
|
|
135741
|
+
if (col.columnType.includes('line')) {
|
|
135742
|
+
const generator = new generatorsMap.GenerateLine[0]();
|
|
135743
|
+
return generator;
|
|
135744
|
+
}
|
|
135745
|
+
if (col.hasDefault && col.default !== undefined) {
|
|
135746
|
+
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135747
|
+
defaultValue: col.default,
|
|
135748
|
+
});
|
|
135749
|
+
return generator;
|
|
135750
|
+
}
|
|
135751
|
+
return;
|
|
135752
|
+
};
|
|
135753
|
+
const generator = pickGenerator(table, col);
|
|
135754
|
+
if (generator !== undefined) {
|
|
135755
|
+
generator.isUnique = col.isUnique;
|
|
135756
|
+
generator.dataType = col.dataType;
|
|
135757
|
+
generator.typeParams = col.typeParams;
|
|
135758
|
+
// generator.stringLength = col.typeParams.length;
|
|
135759
|
+
}
|
|
135760
|
+
return generator;
|
|
135761
|
+
};
|
|
135762
|
+
|
|
135763
|
+
const selectGeneratorForSingleStoreColumn = (table, col) => {
|
|
135764
|
+
const pickGenerator = (table, col) => {
|
|
135765
|
+
// INT ------------------------------------------------------------------------------------------------------------
|
|
135766
|
+
if ((col.columnType.includes('serial') || col.columnType.includes('int'))
|
|
135767
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135768
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135769
|
+
return generator;
|
|
135770
|
+
}
|
|
135771
|
+
let minValue;
|
|
135772
|
+
let maxValue;
|
|
135773
|
+
if (col.columnType === 'serial') {
|
|
135774
|
+
// 2^64 % 2 - 1, 8 bytes
|
|
135775
|
+
minValue = BigInt(0);
|
|
135776
|
+
maxValue = BigInt('9223372036854775807');
|
|
135777
|
+
}
|
|
135778
|
+
else if (col.columnType.includes('int')) {
|
|
135779
|
+
if (col.columnType === 'tinyint') {
|
|
135780
|
+
// 2^8 / 2 - 1, 1 bytes
|
|
135781
|
+
minValue = -128;
|
|
135782
|
+
maxValue = 127;
|
|
135783
|
+
}
|
|
135784
|
+
else if (col.columnType === 'smallint') {
|
|
135785
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135786
|
+
minValue = -32768;
|
|
135787
|
+
maxValue = 32767;
|
|
135788
|
+
}
|
|
135789
|
+
else if (col.columnType === 'mediumint') {
|
|
135790
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135791
|
+
minValue = -8388608;
|
|
135792
|
+
maxValue = 8388607;
|
|
135793
|
+
}
|
|
135794
|
+
else if (col.columnType === 'int') {
|
|
135795
|
+
// 2^32 / 2 - 1, 4 bytes
|
|
135796
|
+
minValue = -2147483648;
|
|
135797
|
+
maxValue = 2147483647;
|
|
135798
|
+
}
|
|
135799
|
+
else if (col.columnType === 'bigint') {
|
|
135800
|
+
// 2^64 / 2 - 1, 8 bytes
|
|
135801
|
+
minValue = BigInt('-9223372036854775808');
|
|
135802
|
+
maxValue = BigInt('9223372036854775807');
|
|
135803
|
+
}
|
|
135804
|
+
}
|
|
135805
|
+
if (col.columnType.includes('int')) {
|
|
135806
|
+
const generator = new generatorsMap.GenerateInt[0]({
|
|
135807
|
+
minValue,
|
|
135808
|
+
maxValue,
|
|
135809
|
+
});
|
|
135810
|
+
return generator;
|
|
135811
|
+
}
|
|
135812
|
+
if (col.columnType.includes('serial')) {
|
|
135813
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135814
|
+
generator.maxValue = maxValue;
|
|
135815
|
+
return generator;
|
|
135816
|
+
}
|
|
135817
|
+
// NUMBER(real, double, decimal, float)
|
|
135818
|
+
if (col.columnType.startsWith('real')
|
|
135819
|
+
|| col.columnType.startsWith('double')
|
|
135820
|
+
|| col.columnType.startsWith('decimal')
|
|
135821
|
+
|| col.columnType.startsWith('float')
|
|
135822
|
+
|| col.columnType.startsWith('numeric')) {
|
|
135823
|
+
if (col.typeParams.precision !== undefined) {
|
|
135824
|
+
const precision = col.typeParams.precision;
|
|
135825
|
+
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
135826
|
+
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
135827
|
+
const generator = new generatorsMap.GenerateNumber[0]({
|
|
135828
|
+
minValue: -maxAbsoluteValue,
|
|
135829
|
+
maxValue: maxAbsoluteValue,
|
|
135830
|
+
precision: Math.pow(10, scale),
|
|
135831
|
+
});
|
|
135832
|
+
return generator;
|
|
135833
|
+
}
|
|
135834
|
+
const generator = new generatorsMap.GenerateNumber[0]();
|
|
135835
|
+
return generator;
|
|
135836
|
+
}
|
|
135837
|
+
// STRING
|
|
135838
|
+
if ((col.columnType === 'tinytext'
|
|
135839
|
+
|| col.columnType === 'mediumtext'
|
|
135840
|
+
|| col.columnType === 'text'
|
|
135841
|
+
|| col.columnType === 'longtext'
|
|
135842
|
+
|| col.columnType === 'blob'
|
|
135843
|
+
|| col.columnType.startsWith('char')
|
|
135844
|
+
|| col.columnType.startsWith('varchar')
|
|
135845
|
+
|| col.columnType.startsWith('binary')
|
|
135846
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135847
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135848
|
+
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135849
|
+
return generator;
|
|
135850
|
+
}
|
|
135851
|
+
if ((col.columnType === 'tinytext'
|
|
135852
|
+
|| col.columnType === 'mediumtext'
|
|
135853
|
+
|| col.columnType === 'text'
|
|
135854
|
+
|| col.columnType === 'longtext'
|
|
135855
|
+
|| col.columnType === 'blob'
|
|
135856
|
+
|| col.columnType.startsWith('char')
|
|
135857
|
+
|| col.columnType.startsWith('varchar')
|
|
135858
|
+
|| col.columnType.startsWith('binary')
|
|
135859
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135860
|
+
&& col.name.toLowerCase().includes('name')) {
|
|
135861
|
+
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135862
|
+
return generator;
|
|
135863
|
+
}
|
|
135864
|
+
if ((col.columnType === 'tinytext'
|
|
135865
|
+
|| col.columnType === 'mediumtext'
|
|
135866
|
+
|| col.columnType === 'text'
|
|
135867
|
+
|| col.columnType === 'longtext'
|
|
135868
|
+
|| col.columnType === 'blob'
|
|
135869
|
+
|| col.columnType.startsWith('char')
|
|
135870
|
+
|| col.columnType.startsWith('varchar')
|
|
135871
|
+
|| col.columnType.startsWith('binary')
|
|
135872
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135873
|
+
&& col.name.toLowerCase().includes('email')) {
|
|
135874
|
+
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135875
|
+
return generator;
|
|
135876
|
+
}
|
|
135877
|
+
if (col.columnType === 'tinytext'
|
|
135878
|
+
|| col.columnType === 'mediumtext'
|
|
135879
|
+
|| col.columnType === 'text'
|
|
135880
|
+
|| col.columnType === 'longtext'
|
|
135881
|
+
|| col.columnType === 'blob'
|
|
135882
|
+
|| col.columnType.startsWith('char')
|
|
135883
|
+
|| col.columnType.startsWith('varchar')
|
|
135884
|
+
|| col.columnType.startsWith('binary')
|
|
135885
|
+
|| col.columnType.startsWith('varbinary')) {
|
|
135886
|
+
const generator = new generatorsMap.GenerateString[0]();
|
|
135887
|
+
return generator;
|
|
135888
|
+
}
|
|
135889
|
+
// BOOLEAN
|
|
135890
|
+
if (col.columnType === 'boolean') {
|
|
135891
|
+
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135892
|
+
return generator;
|
|
135893
|
+
}
|
|
135894
|
+
// DATE, TIME, TIMESTAMP, DATETIME, YEAR
|
|
135895
|
+
if (col.columnType.includes('datetime')) {
|
|
135896
|
+
const generator = new generatorsMap.GenerateDatetime[0]();
|
|
135897
|
+
return generator;
|
|
135898
|
+
}
|
|
135899
|
+
if (col.columnType.includes('date')) {
|
|
135900
|
+
const generator = new generatorsMap.GenerateDate[0]();
|
|
135901
|
+
return generator;
|
|
135902
|
+
}
|
|
135903
|
+
if (col.columnType === 'time') {
|
|
135904
|
+
const generator = new generatorsMap.GenerateTime[0]();
|
|
135905
|
+
return generator;
|
|
135906
|
+
}
|
|
135907
|
+
if (col.columnType.includes('timestamp')) {
|
|
135908
|
+
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135909
|
+
return generator;
|
|
135910
|
+
}
|
|
135911
|
+
if (col.columnType === 'year') {
|
|
135912
|
+
const generator = new generatorsMap.GenerateYear[0]();
|
|
135913
|
+
return generator;
|
|
135914
|
+
}
|
|
135915
|
+
// JSON
|
|
135916
|
+
if (col.columnType === 'json') {
|
|
135917
|
+
const generator = new generatorsMap.GenerateJson[0]();
|
|
135918
|
+
return generator;
|
|
135919
|
+
}
|
|
135920
|
+
// ENUM
|
|
135921
|
+
if (col.enumValues !== undefined) {
|
|
135922
|
+
const generator = new generatorsMap.GenerateEnum[0]({
|
|
135923
|
+
enumValues: col.enumValues,
|
|
135924
|
+
});
|
|
135925
|
+
return generator;
|
|
135926
|
+
}
|
|
135927
|
+
// vector
|
|
135928
|
+
if (col.columnType.startsWith('vector')) {
|
|
135929
|
+
let minValue, maxValue, decimalPlaces;
|
|
135930
|
+
if (col.typeParams.vectorValueType === 'I8') {
|
|
135931
|
+
minValue = -128;
|
|
135932
|
+
maxValue = 127;
|
|
135933
|
+
decimalPlaces = 0;
|
|
135934
|
+
}
|
|
135935
|
+
else if (col.typeParams.vectorValueType === 'I16') {
|
|
135936
|
+
minValue = -32768;
|
|
135937
|
+
maxValue = 32767;
|
|
135938
|
+
decimalPlaces = 0;
|
|
135939
|
+
}
|
|
135940
|
+
else if (col.typeParams.vectorValueType === 'I32') {
|
|
135941
|
+
minValue = -2147483648;
|
|
135942
|
+
maxValue = 2147483647;
|
|
135943
|
+
decimalPlaces = 0;
|
|
135944
|
+
}
|
|
135945
|
+
else if (col.typeParams.vectorValueType === 'I64') {
|
|
135946
|
+
minValue = Number.MIN_SAFE_INTEGER;
|
|
135947
|
+
maxValue = Number.MAX_SAFE_INTEGER;
|
|
135948
|
+
// minValue = -BigInt('9223372036854775808');
|
|
135949
|
+
// maxValue = BigInt('9223372036854775807');
|
|
135950
|
+
decimalPlaces = 0;
|
|
135951
|
+
}
|
|
135952
|
+
else if (col.typeParams.vectorValueType === 'F32') {
|
|
135953
|
+
minValue = -2147483648;
|
|
135954
|
+
maxValue = 2147483647;
|
|
135955
|
+
decimalPlaces = 6;
|
|
135956
|
+
}
|
|
135957
|
+
else if (col.typeParams.vectorValueType === 'F64') {
|
|
135958
|
+
minValue = -524288;
|
|
135959
|
+
maxValue = 524287;
|
|
135960
|
+
decimalPlaces = 10;
|
|
135961
|
+
}
|
|
135962
|
+
const generator = new generatorsMap.GenerateVector[0]({ minValue, maxValue, decimalPlaces });
|
|
135963
|
+
return generator;
|
|
135964
|
+
}
|
|
135965
|
+
if (col.hasDefault && col.default !== undefined) {
|
|
135966
|
+
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135967
|
+
defaultValue: col.default,
|
|
135968
|
+
});
|
|
135969
|
+
return generator;
|
|
135970
|
+
}
|
|
135971
|
+
return;
|
|
135972
|
+
};
|
|
135973
|
+
const generator = pickGenerator(table, col);
|
|
135974
|
+
return generator;
|
|
135975
|
+
};
|
|
135976
|
+
|
|
135977
|
+
const selectGeneratorForSqlite = (table, col) => {
|
|
135978
|
+
const pickGenerator = (table, col) => {
|
|
135979
|
+
// int section ---------------------------------------------------------------------------------------
|
|
135980
|
+
if ((col.columnType === 'integer' || col.columnType === 'numeric')
|
|
135981
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135982
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135983
|
+
return generator;
|
|
135984
|
+
}
|
|
135985
|
+
if (col.columnType === 'integer' && col.dataType === 'boolean') {
|
|
135986
|
+
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135987
|
+
return generator;
|
|
135988
|
+
}
|
|
135989
|
+
if ((col.columnType === 'integer' && col.dataType === 'object')) {
|
|
135990
|
+
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135991
|
+
return generator;
|
|
135992
|
+
}
|
|
135993
|
+
if (col.columnType === 'integer'
|
|
135994
|
+
|| (col.dataType === 'bigint' && col.columnType === 'blob')) {
|
|
135995
|
+
const generator = new generatorsMap.GenerateInt[0]();
|
|
135996
|
+
return generator;
|
|
135997
|
+
}
|
|
135998
|
+
// number section ------------------------------------------------------------------------------------
|
|
135999
|
+
if (col.columnType.startsWith('real') || col.columnType.startsWith('numeric')) {
|
|
136000
|
+
if (col.typeParams.precision !== undefined) {
|
|
136001
|
+
const precision = col.typeParams.precision;
|
|
136002
|
+
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
136003
|
+
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
136004
|
+
const generator = new generatorsMap.GenerateNumber[0]({
|
|
136005
|
+
minValue: -maxAbsoluteValue,
|
|
136006
|
+
maxValue: maxAbsoluteValue,
|
|
136007
|
+
precision: Math.pow(10, scale),
|
|
136008
|
+
});
|
|
136009
|
+
return generator;
|
|
136010
|
+
}
|
|
136011
|
+
const generator = new generatorsMap.GenerateNumber[0]();
|
|
136012
|
+
return generator;
|
|
136013
|
+
}
|
|
136014
|
+
// string section ------------------------------------------------------------------------------------
|
|
136015
|
+
if ((col.columnType.startsWith('text')
|
|
136016
|
+
|| col.columnType.startsWith('numeric')
|
|
136017
|
+
|| col.columnType.startsWith('blob'))
|
|
136018
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
136019
|
+
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
136020
|
+
return generator;
|
|
136021
|
+
}
|
|
136022
|
+
if ((col.columnType.startsWith('text')
|
|
136023
|
+
|| col.columnType.startsWith('numeric')
|
|
136024
|
+
|| col.columnType.startsWith('blob'))
|
|
136025
|
+
&& col.name.toLowerCase().includes('name')) {
|
|
136026
|
+
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
136027
|
+
return generator;
|
|
136028
|
+
}
|
|
136029
|
+
if ((col.columnType.startsWith('text')
|
|
136030
|
+
|| col.columnType.startsWith('numeric')
|
|
136031
|
+
|| col.columnType.startsWith('blob'))
|
|
136032
|
+
&& col.name.toLowerCase().includes('email')) {
|
|
136033
|
+
const generator = new generatorsMap.GenerateEmail[0]();
|
|
136034
|
+
return generator;
|
|
136035
|
+
}
|
|
136036
|
+
if (col.columnType.startsWith('text')
|
|
136037
|
+
|| col.columnType.startsWith('numeric')
|
|
136038
|
+
|| col.columnType.startsWith('blob')
|
|
136039
|
+
|| col.columnType.startsWith('blobbuffer')) {
|
|
136040
|
+
const generator = new generatorsMap.GenerateString[0]();
|
|
136041
|
+
return generator;
|
|
136042
|
+
}
|
|
136043
|
+
if ((col.columnType.startsWith('text') && col.dataType === 'json')
|
|
136044
|
+
|| (col.columnType.startsWith('blob') && col.dataType === 'json')) {
|
|
136045
|
+
const generator = new generatorsMap.GenerateJson[0]();
|
|
136046
|
+
return generator;
|
|
136047
|
+
}
|
|
136048
|
+
if (col.hasDefault && col.default !== undefined) {
|
|
136049
|
+
const generator = new generatorsMap.GenerateDefault[0]({
|
|
136050
|
+
defaultValue: col.default,
|
|
136051
|
+
});
|
|
136052
|
+
return generator;
|
|
136053
|
+
}
|
|
136054
|
+
return;
|
|
136055
|
+
};
|
|
136056
|
+
const generator = pickGenerator(table, col);
|
|
136057
|
+
return generator;
|
|
136058
|
+
};
|
|
136059
|
+
|
|
134364
136060
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
134365
136061
|
class SeedService {
|
|
134366
136062
|
static entityKind = 'SeedService';
|
|
@@ -134371,6 +136067,7 @@ class SeedService {
|
|
|
134371
136067
|
mysqlMaxParametersNumber = 100000;
|
|
134372
136068
|
// SQLITE_MAX_VARIABLE_NUMBER, which by default equals to 999 for SQLite versions prior to 3.32.0 (2020-05-22) or 32766 for SQLite versions after 3.32.0.
|
|
134373
136069
|
sqliteMaxParametersNumber = 32766;
|
|
136070
|
+
mssqlMaxParametersNumber = 2100;
|
|
134374
136071
|
version;
|
|
134375
136072
|
generatePossibleGenerators = (connectionType, tables, relations, refinements, options) => {
|
|
134376
136073
|
let columnPossibleGenerator;
|
|
@@ -134410,7 +136107,6 @@ class SeedService {
|
|
|
134410
136107
|
};
|
|
134411
136108
|
}
|
|
134412
136109
|
}
|
|
134413
|
-
// handling refinements (count, with)
|
|
134414
136110
|
if (refinements !== undefined && refinements[table.name] !== undefined) {
|
|
134415
136111
|
if (refinements[table.name].count !== undefined) {
|
|
134416
136112
|
tablesPossibleGenerators[i].count = refinements[table.name].count;
|
|
@@ -134466,29 +136162,17 @@ class SeedService {
|
|
|
134466
136162
|
notNull: col.notNull,
|
|
134467
136163
|
primary: col.primary,
|
|
134468
136164
|
generatedIdentityType: col.generatedIdentityType,
|
|
136165
|
+
identity: col.identity,
|
|
134469
136166
|
generator: undefined,
|
|
134470
136167
|
isCyclic: false,
|
|
134471
136168
|
wasDefinedBefore: false,
|
|
134472
136169
|
wasRefined: false,
|
|
134473
136170
|
};
|
|
134474
|
-
// handling refinements (columnGenerator)
|
|
134475
136171
|
if (refinements !== undefined
|
|
134476
136172
|
&& refinements[table.name] !== undefined
|
|
134477
136173
|
&& refinements[table.name].columns !== undefined
|
|
134478
136174
|
&& refinements[table.name].columns[col.name] !== undefined) {
|
|
134479
136175
|
const genObj = refinements[table.name].columns[col.name];
|
|
134480
|
-
if (genObj === false) {
|
|
134481
|
-
if (col.notNull === true && col.hasDefault === false) {
|
|
134482
|
-
throw new Error(`You cannot set the '${col.name}' column in the '${table.name}' table to false in your refinements.`
|
|
134483
|
-
+ `\nDoing so will result in a null value being inserted into the '${col.name}' column,`
|
|
134484
|
-
+ `\nwhich will cause an error because the column has a not null constraint and no default value.`);
|
|
134485
|
-
}
|
|
134486
|
-
// Generating undefined as a value for a column and then inserting it via drizzle-orm
|
|
134487
|
-
// will result in the value not being inserted into that column.
|
|
134488
|
-
columnPossibleGenerator.generator = new generatorsMap.GenerateDefault[0]({ defaultValue: undefined });
|
|
134489
|
-
columnPossibleGenerator.wasRefined = true;
|
|
134490
|
-
continue;
|
|
134491
|
-
}
|
|
134492
136176
|
if (col.columnType.match(/\[\w*]/g) !== null) {
|
|
134493
136177
|
if ((col.baseColumn?.dataType === 'array' && col.baseColumn.columnType.match(/\[\w*]/g) !== null)
|
|
134494
136178
|
// studio case
|
|
@@ -134534,17 +136218,27 @@ class SeedService {
|
|
|
134534
136218
|
}
|
|
134535
136219
|
} // TODO: rewrite pickGeneratorFor... using new col properties: isUnique and notNull
|
|
134536
136220
|
else if (connectionType === 'postgresql') {
|
|
134537
|
-
columnPossibleGenerator.generator =
|
|
136221
|
+
columnPossibleGenerator.generator = selectGeneratorForPostgresColumn(table, col);
|
|
134538
136222
|
}
|
|
134539
136223
|
else if (connectionType === 'mysql') {
|
|
134540
|
-
columnPossibleGenerator.generator =
|
|
136224
|
+
columnPossibleGenerator.generator = selectGeneratorForMysqlColumn(table, col);
|
|
134541
136225
|
}
|
|
134542
136226
|
else if (connectionType === 'sqlite') {
|
|
134543
|
-
columnPossibleGenerator.generator =
|
|
136227
|
+
columnPossibleGenerator.generator = selectGeneratorForSqlite(table, col);
|
|
136228
|
+
}
|
|
136229
|
+
else if (connectionType === 'mssql') {
|
|
136230
|
+
columnPossibleGenerator.generator = selectGeneratorForMssqlColumn(table, col);
|
|
136231
|
+
}
|
|
136232
|
+
else if (connectionType === 'cockroach') {
|
|
136233
|
+
columnPossibleGenerator.generator = selectGeneratorForCockroachColumn(table, col);
|
|
136234
|
+
}
|
|
136235
|
+
else if (connectionType === 'singlestore') {
|
|
136236
|
+
columnPossibleGenerator.generator = selectGeneratorForSingleStoreColumn(table, col);
|
|
134544
136237
|
}
|
|
134545
136238
|
if (columnPossibleGenerator.generator === undefined) {
|
|
134546
136239
|
throw new Error(`column with type ${col.columnType} is not supported for now.`);
|
|
134547
136240
|
}
|
|
136241
|
+
columnPossibleGenerator.generator.typeParams = col.typeParams ?? columnPossibleGenerator.generator.typeParams;
|
|
134548
136242
|
const arrayGen = columnPossibleGenerator.generator.replaceIfArray();
|
|
134549
136243
|
if (arrayGen !== undefined) {
|
|
134550
136244
|
columnPossibleGenerator.generator = arrayGen;
|
|
@@ -134559,7 +136253,7 @@ class SeedService {
|
|
|
134559
136253
|
// TODO: for now only GenerateValuesFromArray support notNull property
|
|
134560
136254
|
columnPossibleGenerator.generator.notNull = col.notNull;
|
|
134561
136255
|
columnPossibleGenerator.generator.dataType = col.dataType;
|
|
134562
|
-
columnPossibleGenerator.generator.stringLength = col.typeParams.length;
|
|
136256
|
+
// columnPossibleGenerator.generator.stringLength = col.typeParams.length;
|
|
134563
136257
|
tablePossibleGenerators.columnsPossibleGenerators.push(columnPossibleGenerator);
|
|
134564
136258
|
}
|
|
134565
136259
|
}
|
|
@@ -134570,7 +136264,7 @@ class SeedService {
|
|
|
134570
136264
|
if (entityKind === 'GenerateArray') {
|
|
134571
136265
|
const oldBaseColumnGen = generator.params.baseColumnGen;
|
|
134572
136266
|
const newBaseColumnGen = this.selectVersionOfGenerator(oldBaseColumnGen);
|
|
134573
|
-
|
|
136267
|
+
newBaseColumnGen.typeParams = oldBaseColumnGen.typeParams;
|
|
134574
136268
|
generator.params.baseColumnGen = newBaseColumnGen;
|
|
134575
136269
|
}
|
|
134576
136270
|
const possibleGeneratorConstructors = generatorsMap[entityKind];
|
|
@@ -134586,7 +136280,8 @@ class SeedService {
|
|
|
134586
136280
|
// TODO: for now only GenerateValuesFromArray support notNull property
|
|
134587
136281
|
newGenerator.notNull = generator.notNull;
|
|
134588
136282
|
newGenerator.dataType = generator.dataType;
|
|
134589
|
-
newGenerator.stringLength = generator.stringLength;
|
|
136283
|
+
// newGenerator.stringLength = generator.stringLength;
|
|
136284
|
+
newGenerator.typeParams = generator.typeParams ?? newGenerator.typeParams;
|
|
134590
136285
|
return newGenerator;
|
|
134591
136286
|
};
|
|
134592
136287
|
cyclicTablesCompare = (table1, table2, relation, reverseRelation) => {
|
|
@@ -134697,474 +136392,6 @@ class SeedService {
|
|
|
134697
136392
|
}
|
|
134698
136393
|
return weightedWithCount;
|
|
134699
136394
|
};
|
|
134700
|
-
// TODO: revise serial part generators
|
|
134701
|
-
selectGeneratorForPostgresColumn = (table, col) => {
|
|
134702
|
-
const pickGenerator = (table, col) => {
|
|
134703
|
-
// ARRAY
|
|
134704
|
-
if (col.columnType.match(/\[\w*]/g) !== null && col.baseColumn !== undefined) {
|
|
134705
|
-
const baseColumnGen = this.selectGeneratorForPostgresColumn(table, col.baseColumn);
|
|
134706
|
-
if (baseColumnGen === undefined) {
|
|
134707
|
-
throw new Error(`column with type ${col.baseColumn.columnType} is not supported for now.`);
|
|
134708
|
-
}
|
|
134709
|
-
// const getBaseColumnDataType = (baseColumn: Column) => {
|
|
134710
|
-
// if (baseColumn.baseColumn !== undefined) {
|
|
134711
|
-
// return getBaseColumnDataType(baseColumn.baseColumn);
|
|
134712
|
-
// }
|
|
134713
|
-
// return baseColumn.dataType;
|
|
134714
|
-
// };
|
|
134715
|
-
// const baseColumnDataType = getBaseColumnDataType(col.baseColumn);
|
|
134716
|
-
const generator = new generatorsMap.GenerateArray[0]({ baseColumnGen, size: col.size });
|
|
134717
|
-
// generator.baseColumnDataType = baseColumnDataType;
|
|
134718
|
-
return generator;
|
|
134719
|
-
}
|
|
134720
|
-
// ARRAY for studio
|
|
134721
|
-
if (col.columnType.match(/\[\w*]/g) !== null) {
|
|
134722
|
-
// remove dimensions from type
|
|
134723
|
-
const baseColumnType = col.columnType.replace(/\[\w*]/g, '');
|
|
134724
|
-
const baseColumn = {
|
|
134725
|
-
...col,
|
|
134726
|
-
};
|
|
134727
|
-
baseColumn.columnType = baseColumnType;
|
|
134728
|
-
const baseColumnGen = this.selectGeneratorForPostgresColumn(table, baseColumn);
|
|
134729
|
-
if (baseColumnGen === undefined) {
|
|
134730
|
-
throw new Error(`column with type ${col.baseColumn.columnType} is not supported for now.`);
|
|
134731
|
-
}
|
|
134732
|
-
let generator = new generatorsMap.GenerateArray[0]({ baseColumnGen });
|
|
134733
|
-
for (let i = 0; i < col.typeParams.dimensions - 1; i++) {
|
|
134734
|
-
generator = new generatorsMap.GenerateArray[0]({ baseColumnGen: generator });
|
|
134735
|
-
}
|
|
134736
|
-
return generator;
|
|
134737
|
-
}
|
|
134738
|
-
// INT ------------------------------------------------------------------------------------------------------------
|
|
134739
|
-
if ((['smallserial', 'serial', 'bigserial'].includes(col.columnType)
|
|
134740
|
-
|| ['smallint', 'integer', 'bigint'].includes(col.columnType))
|
|
134741
|
-
&& table.primaryKeys.includes(col.name)) {
|
|
134742
|
-
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
134743
|
-
return generator;
|
|
134744
|
-
}
|
|
134745
|
-
let minValue;
|
|
134746
|
-
let maxValue;
|
|
134747
|
-
if (['smallserial', 'serial', 'bigserial'].includes(col.columnType)) {
|
|
134748
|
-
minValue = 1;
|
|
134749
|
-
if (col.columnType === 'smallserial') {
|
|
134750
|
-
// 2^16 / 2 - 1, 2 bytes
|
|
134751
|
-
maxValue = 32767;
|
|
134752
|
-
}
|
|
134753
|
-
else if (col.columnType === 'serial') {
|
|
134754
|
-
// 2^32 / 2 - 1, 4 bytes
|
|
134755
|
-
maxValue = 2147483647;
|
|
134756
|
-
}
|
|
134757
|
-
else if (col.columnType === 'bigserial') {
|
|
134758
|
-
// 2^64 / 2 - 1, 8 bytes
|
|
134759
|
-
minValue = BigInt(1);
|
|
134760
|
-
maxValue = BigInt('9223372036854775807');
|
|
134761
|
-
}
|
|
134762
|
-
}
|
|
134763
|
-
else if (['smallint', 'integer', 'bigint'].includes(col.columnType)) {
|
|
134764
|
-
if (col.columnType === 'smallint') {
|
|
134765
|
-
// 2^16 / 2 - 1, 2 bytes
|
|
134766
|
-
minValue = -32768;
|
|
134767
|
-
maxValue = 32767;
|
|
134768
|
-
}
|
|
134769
|
-
else if (col.columnType === 'integer') {
|
|
134770
|
-
// 2^32 / 2 - 1, 4 bytes
|
|
134771
|
-
minValue = -2147483648;
|
|
134772
|
-
maxValue = 2147483647;
|
|
134773
|
-
}
|
|
134774
|
-
else if (col.columnType.includes('bigint')) {
|
|
134775
|
-
if (col.dataType === 'bigint') {
|
|
134776
|
-
// 2^64 / 2 - 1, 8 bytes
|
|
134777
|
-
minValue = BigInt('-9223372036854775808');
|
|
134778
|
-
maxValue = BigInt('9223372036854775807');
|
|
134779
|
-
}
|
|
134780
|
-
else {
|
|
134781
|
-
// if (col.dataType === 'number')
|
|
134782
|
-
// if you’re expecting values above 2^31 but below 2^53
|
|
134783
|
-
minValue = -9007199254740991;
|
|
134784
|
-
maxValue = 9007199254740991;
|
|
134785
|
-
}
|
|
134786
|
-
}
|
|
134787
|
-
}
|
|
134788
|
-
if (['smallint', 'integer', 'bigint'].includes(col.columnType)) {
|
|
134789
|
-
const generator = new generatorsMap.GenerateInt[0]({
|
|
134790
|
-
minValue,
|
|
134791
|
-
maxValue,
|
|
134792
|
-
});
|
|
134793
|
-
return generator;
|
|
134794
|
-
}
|
|
134795
|
-
if (['smallserial', 'serial', 'bigserial'].includes(col.columnType)) {
|
|
134796
|
-
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
134797
|
-
generator.maxValue = maxValue;
|
|
134798
|
-
return generator;
|
|
134799
|
-
}
|
|
134800
|
-
// NUMBER(real, double, decimal, numeric)
|
|
134801
|
-
if (col.columnType === 'real'
|
|
134802
|
-
|| col.columnType === 'double precision'
|
|
134803
|
-
|| col.columnType.match(/^decimal(\(\d{1,6}(, ?-?\d{0,5})?\))?$/) !== null
|
|
134804
|
-
|| col.columnType.match(/^numeric(\(\d{1,6}(, ?-?\d{0,5})?\))?$/) !== null) {
|
|
134805
|
-
if (col.typeParams.precision !== undefined) {
|
|
134806
|
-
const precision = col.typeParams.precision;
|
|
134807
|
-
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
134808
|
-
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
134809
|
-
const generator = new generatorsMap.GenerateNumber[0]({
|
|
134810
|
-
minValue: -maxAbsoluteValue,
|
|
134811
|
-
maxValue: maxAbsoluteValue,
|
|
134812
|
-
precision: Math.pow(10, scale),
|
|
134813
|
-
});
|
|
134814
|
-
return generator;
|
|
134815
|
-
}
|
|
134816
|
-
const generator = new generatorsMap.GenerateNumber[0]();
|
|
134817
|
-
return generator;
|
|
134818
|
-
}
|
|
134819
|
-
// STRING
|
|
134820
|
-
if ((col.columnType === 'text'
|
|
134821
|
-
|| col.columnType.match(/^varchar(\(\d+\))?$/) !== null
|
|
134822
|
-
|| col.columnType.match(/^char(\(\d+\))?$/) !== null)
|
|
134823
|
-
&& table.primaryKeys.includes(col.name)) {
|
|
134824
|
-
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
134825
|
-
return generator;
|
|
134826
|
-
}
|
|
134827
|
-
if ((col.columnType === 'text'
|
|
134828
|
-
|| col.columnType.match(/^varchar(\(\d+\))?$/) !== null
|
|
134829
|
-
|| col.columnType.match(/^char(\(\d+\))?$/) !== null)
|
|
134830
|
-
&& col.name.toLowerCase().includes('name')) {
|
|
134831
|
-
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
134832
|
-
return generator;
|
|
134833
|
-
}
|
|
134834
|
-
if ((col.columnType === 'text'
|
|
134835
|
-
|| col.columnType.match(/^varchar(\(\d+\))?$/) !== null
|
|
134836
|
-
|| col.columnType.match(/^char(\(\d+\))?$/) !== null)
|
|
134837
|
-
&& col.name.toLowerCase().includes('email')) {
|
|
134838
|
-
const generator = new generatorsMap.GenerateEmail[0]();
|
|
134839
|
-
return generator;
|
|
134840
|
-
}
|
|
134841
|
-
if (col.columnType === 'text'
|
|
134842
|
-
|| col.columnType.match(/^varchar(\(\d+\))?$/) !== null
|
|
134843
|
-
|| col.columnType.match(/^char(\(\d+\))?$/) !== null) {
|
|
134844
|
-
const generator = new generatorsMap.GenerateString[0]();
|
|
134845
|
-
return generator;
|
|
134846
|
-
}
|
|
134847
|
-
// UUID
|
|
134848
|
-
if (col.columnType === 'uuid') {
|
|
134849
|
-
const generator = new generatorsMap.GenerateUUID[0]();
|
|
134850
|
-
return generator;
|
|
134851
|
-
}
|
|
134852
|
-
// BOOLEAN
|
|
134853
|
-
if (col.columnType === 'boolean') {
|
|
134854
|
-
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
134855
|
-
return generator;
|
|
134856
|
-
}
|
|
134857
|
-
// DATE, TIME, TIMESTAMP
|
|
134858
|
-
if (col.columnType === 'date') {
|
|
134859
|
-
const generator = new generatorsMap.GenerateDate[0]();
|
|
134860
|
-
return generator;
|
|
134861
|
-
}
|
|
134862
|
-
if (col.columnType.match(/^time((\(\d+\))|( with time zone))?$/) !== null) {
|
|
134863
|
-
const generator = new generatorsMap.GenerateTime[0]();
|
|
134864
|
-
return generator;
|
|
134865
|
-
}
|
|
134866
|
-
if (col.columnType.match(/^timestamp((\(\d+\))|( with time zone))?$/) !== null) {
|
|
134867
|
-
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
134868
|
-
return generator;
|
|
134869
|
-
}
|
|
134870
|
-
// JSON, JSONB
|
|
134871
|
-
if (col.columnType === 'json' || col.columnType === 'jsonb') {
|
|
134872
|
-
const generator = new generatorsMap.GenerateJson[0]();
|
|
134873
|
-
return generator;
|
|
134874
|
-
}
|
|
134875
|
-
// if (col.columnType === "jsonb") {
|
|
134876
|
-
// const generator = new GenerateJsonb({});
|
|
134877
|
-
// return generator;
|
|
134878
|
-
// }
|
|
134879
|
-
// ENUM
|
|
134880
|
-
if (col.enumValues !== undefined) {
|
|
134881
|
-
const generator = new generatorsMap.GenerateEnum[0]({
|
|
134882
|
-
enumValues: col.enumValues,
|
|
134883
|
-
});
|
|
134884
|
-
return generator;
|
|
134885
|
-
}
|
|
134886
|
-
// INTERVAL
|
|
134887
|
-
if (col.columnType.match(/^interval( .+)?$/) !== null) {
|
|
134888
|
-
if (col.columnType === 'interval') {
|
|
134889
|
-
const generator = new generatorsMap.GenerateInterval[0]();
|
|
134890
|
-
return generator;
|
|
134891
|
-
}
|
|
134892
|
-
const fields = col.columnType.replace('interval ', '');
|
|
134893
|
-
const generator = new generatorsMap.GenerateInterval[0]({ fields });
|
|
134894
|
-
return generator;
|
|
134895
|
-
}
|
|
134896
|
-
// POINT, LINE
|
|
134897
|
-
if (col.columnType === 'point') {
|
|
134898
|
-
const generator = new generatorsMap.GeneratePoint[0]();
|
|
134899
|
-
return generator;
|
|
134900
|
-
}
|
|
134901
|
-
if (col.columnType === 'line') {
|
|
134902
|
-
const generator = new generatorsMap.GenerateLine[0]();
|
|
134903
|
-
return generator;
|
|
134904
|
-
}
|
|
134905
|
-
if (col.hasDefault && col.default !== undefined) {
|
|
134906
|
-
const generator = new generatorsMap.GenerateDefault[0]({
|
|
134907
|
-
defaultValue: col.default,
|
|
134908
|
-
});
|
|
134909
|
-
return generator;
|
|
134910
|
-
}
|
|
134911
|
-
return;
|
|
134912
|
-
};
|
|
134913
|
-
const generator = pickGenerator(table, col);
|
|
134914
|
-
if (generator !== undefined) {
|
|
134915
|
-
generator.isUnique = col.isUnique;
|
|
134916
|
-
generator.dataType = col.dataType;
|
|
134917
|
-
generator.stringLength = col.typeParams.length;
|
|
134918
|
-
}
|
|
134919
|
-
return generator;
|
|
134920
|
-
};
|
|
134921
|
-
selectGeneratorForMysqlColumn = (table, col) => {
|
|
134922
|
-
const pickGenerator = (table, col) => {
|
|
134923
|
-
// INT ------------------------------------------------------------------------------------------------------------
|
|
134924
|
-
if ((col.columnType === 'serial'
|
|
134925
|
-
|| ['tinyint', 'smallint', 'mediumint', 'int', 'bigint'].includes(col.columnType))
|
|
134926
|
-
&& table.primaryKeys.includes(col.name)) {
|
|
134927
|
-
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
134928
|
-
return generator;
|
|
134929
|
-
}
|
|
134930
|
-
let minValue;
|
|
134931
|
-
let maxValue;
|
|
134932
|
-
if (col.columnType === 'serial') {
|
|
134933
|
-
// 2^64 % 2 - 1, 8 bytes
|
|
134934
|
-
minValue = BigInt(0);
|
|
134935
|
-
maxValue = BigInt('9223372036854775807');
|
|
134936
|
-
}
|
|
134937
|
-
else if (['tinyint', 'smallint', 'mediumint', 'int', 'bigint'].includes(col.columnType)) {
|
|
134938
|
-
if (col.columnType === 'tinyint') {
|
|
134939
|
-
// 2^8 / 2 - 1, 1 bytes
|
|
134940
|
-
minValue = -128;
|
|
134941
|
-
maxValue = 127;
|
|
134942
|
-
}
|
|
134943
|
-
else if (col.columnType === 'smallint') {
|
|
134944
|
-
// 2^16 / 2 - 1, 2 bytes
|
|
134945
|
-
minValue = -32768;
|
|
134946
|
-
maxValue = 32767;
|
|
134947
|
-
}
|
|
134948
|
-
else if (col.columnType === 'mediumint') {
|
|
134949
|
-
// 2^16 / 2 - 1, 2 bytes
|
|
134950
|
-
minValue = -8388608;
|
|
134951
|
-
maxValue = 8388607;
|
|
134952
|
-
}
|
|
134953
|
-
else if (col.columnType === 'int') {
|
|
134954
|
-
// 2^32 / 2 - 1, 4 bytes
|
|
134955
|
-
minValue = -2147483648;
|
|
134956
|
-
maxValue = 2147483647;
|
|
134957
|
-
}
|
|
134958
|
-
else if (col.columnType === 'bigint') {
|
|
134959
|
-
// 2^64 / 2 - 1, 8 bytes
|
|
134960
|
-
minValue = BigInt('-9223372036854775808');
|
|
134961
|
-
maxValue = BigInt('9223372036854775807');
|
|
134962
|
-
}
|
|
134963
|
-
}
|
|
134964
|
-
if (['tinyint', 'smallint', 'mediumint', 'int', 'bigint'].includes(col.columnType)) {
|
|
134965
|
-
const generator = new generatorsMap.GenerateInt[0]({
|
|
134966
|
-
minValue,
|
|
134967
|
-
maxValue,
|
|
134968
|
-
});
|
|
134969
|
-
return generator;
|
|
134970
|
-
}
|
|
134971
|
-
if (col.columnType === 'serial') {
|
|
134972
|
-
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
134973
|
-
generator.maxValue = maxValue;
|
|
134974
|
-
return generator;
|
|
134975
|
-
}
|
|
134976
|
-
// NUMBER(real, double, decimal, float)
|
|
134977
|
-
if (col.columnType === 'real'
|
|
134978
|
-
|| col.columnType === 'double'
|
|
134979
|
-
|| col.columnType === 'float'
|
|
134980
|
-
|| col.columnType.startsWith('decimal')
|
|
134981
|
-
|| col.columnType.startsWith('numeric')) {
|
|
134982
|
-
if (col.typeParams.precision !== undefined) {
|
|
134983
|
-
const precision = col.typeParams.precision;
|
|
134984
|
-
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
134985
|
-
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
134986
|
-
const generator = new generatorsMap.GenerateNumber[0]({
|
|
134987
|
-
minValue: -maxAbsoluteValue,
|
|
134988
|
-
maxValue: maxAbsoluteValue,
|
|
134989
|
-
precision: Math.pow(10, scale),
|
|
134990
|
-
});
|
|
134991
|
-
return generator;
|
|
134992
|
-
}
|
|
134993
|
-
const generator = new generatorsMap.GenerateNumber[0]();
|
|
134994
|
-
return generator;
|
|
134995
|
-
}
|
|
134996
|
-
// STRING
|
|
134997
|
-
if ((col.columnType === 'text'
|
|
134998
|
-
|| col.columnType === 'blob'
|
|
134999
|
-
|| col.columnType.startsWith('char')
|
|
135000
|
-
|| col.columnType.startsWith('varchar')
|
|
135001
|
-
|| col.columnType.startsWith('binary')
|
|
135002
|
-
|| col.columnType.startsWith('varbinary'))
|
|
135003
|
-
&& table.primaryKeys.includes(col.name)) {
|
|
135004
|
-
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135005
|
-
return generator;
|
|
135006
|
-
}
|
|
135007
|
-
if ((col.columnType === 'text'
|
|
135008
|
-
|| col.columnType === 'blob'
|
|
135009
|
-
|| col.columnType.startsWith('char')
|
|
135010
|
-
|| col.columnType.startsWith('varchar')
|
|
135011
|
-
|| col.columnType.startsWith('binary')
|
|
135012
|
-
|| col.columnType.startsWith('varbinary'))
|
|
135013
|
-
&& col.name.toLowerCase().includes('name')) {
|
|
135014
|
-
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135015
|
-
return generator;
|
|
135016
|
-
}
|
|
135017
|
-
if ((col.columnType === 'text'
|
|
135018
|
-
|| col.columnType === 'blob'
|
|
135019
|
-
|| col.columnType.startsWith('char')
|
|
135020
|
-
|| col.columnType.startsWith('varchar')
|
|
135021
|
-
|| col.columnType.startsWith('binary')
|
|
135022
|
-
|| col.columnType.startsWith('varbinary'))
|
|
135023
|
-
&& col.name.toLowerCase().includes('email')) {
|
|
135024
|
-
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135025
|
-
return generator;
|
|
135026
|
-
}
|
|
135027
|
-
if (col.columnType === 'text'
|
|
135028
|
-
|| col.columnType === 'blob'
|
|
135029
|
-
|| col.columnType.startsWith('char')
|
|
135030
|
-
|| col.columnType.startsWith('varchar')
|
|
135031
|
-
|| col.columnType.startsWith('binary')
|
|
135032
|
-
|| col.columnType.startsWith('varbinary')) {
|
|
135033
|
-
const generator = new generatorsMap.GenerateString[0]();
|
|
135034
|
-
return generator;
|
|
135035
|
-
}
|
|
135036
|
-
// BOOLEAN
|
|
135037
|
-
if (col.columnType === 'boolean') {
|
|
135038
|
-
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135039
|
-
return generator;
|
|
135040
|
-
}
|
|
135041
|
-
// DATE, TIME, TIMESTAMP, DATETIME, YEAR
|
|
135042
|
-
if (col.columnType.startsWith('datetime')) {
|
|
135043
|
-
const generator = new generatorsMap.GenerateDatetime[0]();
|
|
135044
|
-
return generator;
|
|
135045
|
-
}
|
|
135046
|
-
if (col.columnType === 'date') {
|
|
135047
|
-
const generator = new generatorsMap.GenerateDate[0]();
|
|
135048
|
-
return generator;
|
|
135049
|
-
}
|
|
135050
|
-
if (col.columnType === 'time') {
|
|
135051
|
-
const generator = new generatorsMap.GenerateTime[0]();
|
|
135052
|
-
return generator;
|
|
135053
|
-
}
|
|
135054
|
-
if (col.columnType.startsWith('timestamp')) {
|
|
135055
|
-
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135056
|
-
return generator;
|
|
135057
|
-
}
|
|
135058
|
-
if (col.columnType === 'year') {
|
|
135059
|
-
const generator = new generatorsMap.GenerateYear[0]();
|
|
135060
|
-
return generator;
|
|
135061
|
-
}
|
|
135062
|
-
// JSON
|
|
135063
|
-
if (col.columnType === 'json') {
|
|
135064
|
-
const generator = new generatorsMap.GenerateJson[0]();
|
|
135065
|
-
return generator;
|
|
135066
|
-
}
|
|
135067
|
-
// ENUM
|
|
135068
|
-
if (col.enumValues !== undefined) {
|
|
135069
|
-
const generator = new generatorsMap.GenerateEnum[0]({
|
|
135070
|
-
enumValues: col.enumValues,
|
|
135071
|
-
});
|
|
135072
|
-
return generator;
|
|
135073
|
-
}
|
|
135074
|
-
if (col.hasDefault && col.default !== undefined) {
|
|
135075
|
-
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135076
|
-
defaultValue: col.default,
|
|
135077
|
-
});
|
|
135078
|
-
return generator;
|
|
135079
|
-
}
|
|
135080
|
-
return;
|
|
135081
|
-
};
|
|
135082
|
-
const generator = pickGenerator(table, col);
|
|
135083
|
-
return generator;
|
|
135084
|
-
};
|
|
135085
|
-
selectGeneratorForSqlite = (table, col) => {
|
|
135086
|
-
const pickGenerator = (table, col) => {
|
|
135087
|
-
// int section ---------------------------------------------------------------------------------------
|
|
135088
|
-
if ((col.columnType === 'integer' || col.columnType === 'numeric')
|
|
135089
|
-
&& table.primaryKeys.includes(col.name)) {
|
|
135090
|
-
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135091
|
-
return generator;
|
|
135092
|
-
}
|
|
135093
|
-
if (col.columnType === 'integer' && col.dataType === 'boolean') {
|
|
135094
|
-
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135095
|
-
return generator;
|
|
135096
|
-
}
|
|
135097
|
-
if ((col.columnType === 'integer' && col.dataType === 'date')) {
|
|
135098
|
-
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135099
|
-
return generator;
|
|
135100
|
-
}
|
|
135101
|
-
if (col.columnType === 'integer'
|
|
135102
|
-
|| (col.dataType === 'bigint' && col.columnType === 'blob')) {
|
|
135103
|
-
const generator = new generatorsMap.GenerateInt[0]();
|
|
135104
|
-
return generator;
|
|
135105
|
-
}
|
|
135106
|
-
// number section ------------------------------------------------------------------------------------
|
|
135107
|
-
if (col.columnType.startsWith('real')
|
|
135108
|
-
|| col.columnType.startsWith('numeric')) {
|
|
135109
|
-
if (col.typeParams.precision !== undefined) {
|
|
135110
|
-
const precision = col.typeParams.precision;
|
|
135111
|
-
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
135112
|
-
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
135113
|
-
const generator = new generatorsMap.GenerateNumber[0]({
|
|
135114
|
-
minValue: -maxAbsoluteValue,
|
|
135115
|
-
maxValue: maxAbsoluteValue,
|
|
135116
|
-
precision: Math.pow(10, scale),
|
|
135117
|
-
});
|
|
135118
|
-
return generator;
|
|
135119
|
-
}
|
|
135120
|
-
const generator = new generatorsMap.GenerateNumber[0]();
|
|
135121
|
-
return generator;
|
|
135122
|
-
}
|
|
135123
|
-
// string section ------------------------------------------------------------------------------------
|
|
135124
|
-
if ((col.columnType.startsWith('text')
|
|
135125
|
-
|| col.columnType.startsWith('numeric')
|
|
135126
|
-
|| col.columnType.startsWith('blob'))
|
|
135127
|
-
&& table.primaryKeys.includes(col.name)) {
|
|
135128
|
-
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135129
|
-
return generator;
|
|
135130
|
-
}
|
|
135131
|
-
if ((col.columnType.startsWith('text')
|
|
135132
|
-
|| col.columnType.startsWith('numeric')
|
|
135133
|
-
|| col.columnType.startsWith('blob'))
|
|
135134
|
-
&& col.name.toLowerCase().includes('name')) {
|
|
135135
|
-
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135136
|
-
return generator;
|
|
135137
|
-
}
|
|
135138
|
-
if ((col.columnType.startsWith('text')
|
|
135139
|
-
|| col.columnType.startsWith('numeric')
|
|
135140
|
-
|| col.columnType.startsWith('blob'))
|
|
135141
|
-
&& col.name.toLowerCase().includes('email')) {
|
|
135142
|
-
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135143
|
-
return generator;
|
|
135144
|
-
}
|
|
135145
|
-
if (col.columnType.startsWith('text')
|
|
135146
|
-
|| col.columnType.startsWith('numeric')
|
|
135147
|
-
|| col.columnType.startsWith('blob')
|
|
135148
|
-
|| col.columnType.startsWith('blobbuffer')) {
|
|
135149
|
-
const generator = new generatorsMap.GenerateString[0]();
|
|
135150
|
-
return generator;
|
|
135151
|
-
}
|
|
135152
|
-
if ((col.columnType.startsWith('text') && col.dataType === 'json')
|
|
135153
|
-
|| (col.columnType.startsWith('blob') && col.dataType === 'json')) {
|
|
135154
|
-
const generator = new generatorsMap.GenerateJson[0]();
|
|
135155
|
-
return generator;
|
|
135156
|
-
}
|
|
135157
|
-
if (col.hasDefault && col.default !== undefined) {
|
|
135158
|
-
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135159
|
-
defaultValue: col.default,
|
|
135160
|
-
});
|
|
135161
|
-
return generator;
|
|
135162
|
-
}
|
|
135163
|
-
return;
|
|
135164
|
-
};
|
|
135165
|
-
const generator = pickGenerator(table, col);
|
|
135166
|
-
return generator;
|
|
135167
|
-
};
|
|
135168
136395
|
filterCyclicTables = (tablesGenerators) => {
|
|
135169
136396
|
const filteredTablesGenerators = tablesGenerators.filter((tableGen) => tableGen.columnsPossibleGenerators.some((columnGen) => columnGen.isCyclic === true && columnGen.wasDefinedBefore === true));
|
|
135170
136397
|
const tablesUniqueNotNullColumn = {};
|
|
@@ -135176,7 +136403,8 @@ class SeedService {
|
|
|
135176
136403
|
throw new Error(`Table '${tableGen.tableName}' does not have primary or (unique and notNull) column. Can't seed table with cyclic relation.`);
|
|
135177
136404
|
}
|
|
135178
136405
|
tablesUniqueNotNullColumn[tableGen.tableName] = { uniqueNotNullColName };
|
|
135179
|
-
filteredTablesGenerators[idx].columnsPossibleGenerators = tableGen.columnsPossibleGenerators.filter((colGen) => (colGen.isCyclic === true && colGen.wasDefinedBefore === true) || colGen.columnName === uniqueNotNullColName)
|
|
136406
|
+
filteredTablesGenerators[idx].columnsPossibleGenerators = tableGen.columnsPossibleGenerators.filter((colGen) => (colGen.isCyclic === true && colGen.wasDefinedBefore === true) || colGen.columnName === uniqueNotNullColName)
|
|
136407
|
+
.map((colGen) => {
|
|
135180
136408
|
const newColGen = { ...colGen };
|
|
135181
136409
|
newColGen.wasDefinedBefore = false;
|
|
135182
136410
|
return newColGen;
|
|
@@ -135270,7 +136498,9 @@ class SeedService {
|
|
|
135270
136498
|
weightedCountSeed = table.withFromTable[rel.refTable].weightedCountSeed;
|
|
135271
136499
|
}
|
|
135272
136500
|
// TODO: revise maybe need to select version of generator here too
|
|
135273
|
-
genObj = new generatorsMap.GenerateValuesFromArray[0]({
|
|
136501
|
+
genObj = new generatorsMap.GenerateValuesFromArray[0]({
|
|
136502
|
+
values: refColumnValues,
|
|
136503
|
+
});
|
|
135274
136504
|
genObj.notNull = tableGenerators[rel.columns[colIdx]].notNull;
|
|
135275
136505
|
genObj.weightedCountSeed = weightedCountSeed;
|
|
135276
136506
|
genObj.maxRepeatedValuesCount = repeatedValuesCount;
|
|
@@ -135338,7 +136568,10 @@ class SeedService {
|
|
|
135338
136568
|
for (const columnName of Object.keys(tableGenerators)) {
|
|
135339
136569
|
columnsNumber += 1;
|
|
135340
136570
|
columnGenerator = tableGenerators[columnName];
|
|
136571
|
+
// postgres identity columns
|
|
135341
136572
|
override = tableGenerators[columnName]?.generatedIdentityType === 'always' ? true : override;
|
|
136573
|
+
// mssql identity columns
|
|
136574
|
+
override = tableGenerators[columnName]?.identity === true ? true : override;
|
|
135342
136575
|
columnsGenerators[columnName] = columnGenerator.generator;
|
|
135343
136576
|
columnsGenerators[columnName].init({
|
|
135344
136577
|
count,
|
|
@@ -135363,10 +136596,13 @@ class SeedService {
|
|
|
135363
136596
|
else if (is(db, (MySqlDatabase))) {
|
|
135364
136597
|
maxParametersNumber = this.mysqlMaxParametersNumber;
|
|
135365
136598
|
}
|
|
135366
|
-
else {
|
|
135367
|
-
// is(db, BaseSQLiteDatabase<any, any>)
|
|
136599
|
+
else if (is(db, (BaseSQLiteDatabase))) {
|
|
135368
136600
|
maxParametersNumber = this.sqliteMaxParametersNumber;
|
|
135369
136601
|
}
|
|
136602
|
+
else {
|
|
136603
|
+
// is(db, MsSqlDatabase<any, any>)
|
|
136604
|
+
maxParametersNumber = this.mssqlMaxParametersNumber;
|
|
136605
|
+
}
|
|
135370
136606
|
const maxBatchSize = Math.floor(maxParametersNumber / columnsNumber);
|
|
135371
136607
|
batchSize = batchSize > maxBatchSize ? maxBatchSize : batchSize;
|
|
135372
136608
|
if ((insertDataInDb === true || updateDataInDb === true)
|
|
@@ -135451,24 +136687,640 @@ class SeedService {
|
|
|
135451
136687
|
.insert(schema[tableName])
|
|
135452
136688
|
.values(generatedValues);
|
|
135453
136689
|
}
|
|
136690
|
+
else if (is(db, (MsSqlDatabase))) {
|
|
136691
|
+
let schemaDbName;
|
|
136692
|
+
let tableDbName;
|
|
136693
|
+
if (override === true) {
|
|
136694
|
+
const tableConfig = getTableConfig$5(schema[tableName]);
|
|
136695
|
+
schemaDbName = tableConfig.schema ?? 'dbo';
|
|
136696
|
+
tableDbName = tableConfig.name;
|
|
136697
|
+
await db.execute(sql.raw(`SET IDENTITY_INSERT [${schemaDbName}].[${tableDbName}] ON;`));
|
|
136698
|
+
}
|
|
136699
|
+
await db
|
|
136700
|
+
.insert(schema[tableName])
|
|
136701
|
+
.values(generatedValues);
|
|
136702
|
+
if (override === true) {
|
|
136703
|
+
await db.execute(sql.raw(`SET IDENTITY_INSERT [${schemaDbName}].[${tableDbName}] OFF;`));
|
|
136704
|
+
}
|
|
136705
|
+
}
|
|
136706
|
+
else if (is(db, (CockroachDatabase))) {
|
|
136707
|
+
const query = db
|
|
136708
|
+
.insert(schema[tableName])
|
|
136709
|
+
.values(generatedValues);
|
|
136710
|
+
await query;
|
|
136711
|
+
}
|
|
136712
|
+
else if (is(db, (SingleStoreDatabase))) {
|
|
136713
|
+
const query = db
|
|
136714
|
+
.insert(schema[tableName])
|
|
136715
|
+
.values(generatedValues);
|
|
136716
|
+
await query;
|
|
136717
|
+
}
|
|
135454
136718
|
};
|
|
135455
136719
|
updateDb = async ({ generatedValues, db, schema, tableName, uniqueNotNullColName, }) => {
|
|
136720
|
+
let values = generatedValues[0];
|
|
136721
|
+
const uniqueNotNullColValue = values[uniqueNotNullColName];
|
|
136722
|
+
values = Object.fromEntries(Object.entries(values).filter(([colName]) => colName !== uniqueNotNullColName));
|
|
135456
136723
|
if (is(db, (PgDatabase))) {
|
|
135457
136724
|
const table = schema[tableName];
|
|
135458
136725
|
const uniqueNotNullCol = table[uniqueNotNullColName];
|
|
135459
|
-
await db.update(table).set(
|
|
136726
|
+
await db.update(table).set(values).where(eq(uniqueNotNullCol, uniqueNotNullColValue));
|
|
135460
136727
|
}
|
|
135461
136728
|
else if (is(db, (MySqlDatabase))) {
|
|
135462
136729
|
const table = schema[tableName];
|
|
135463
|
-
await db.update(table).set(
|
|
136730
|
+
await db.update(table).set(values).where(eq(table[uniqueNotNullColName], uniqueNotNullColValue));
|
|
135464
136731
|
}
|
|
135465
136732
|
else if (is(db, (BaseSQLiteDatabase))) {
|
|
135466
136733
|
const table = schema[tableName];
|
|
135467
|
-
await db.update(table).set(
|
|
136734
|
+
await db.update(table).set(values).where(eq(table[uniqueNotNullColName], uniqueNotNullColValue));
|
|
136735
|
+
}
|
|
136736
|
+
else if (is(db, (MsSqlDatabase))) {
|
|
136737
|
+
const table = schema[tableName];
|
|
136738
|
+
await db.update(table).set(values).where(eq(table[uniqueNotNullColName], uniqueNotNullColValue));
|
|
136739
|
+
}
|
|
136740
|
+
else if (is(db, (CockroachDatabase))) {
|
|
136741
|
+
const table = schema[tableName];
|
|
136742
|
+
await db.update(table).set(values).where(eq(table[uniqueNotNullColName], uniqueNotNullColValue));
|
|
136743
|
+
}
|
|
136744
|
+
else if (is(db, (SingleStoreDatabase))) {
|
|
136745
|
+
const table = schema[tableName];
|
|
136746
|
+
await db.update(table).set(values).where(eq(table[uniqueNotNullColName], uniqueNotNullColValue));
|
|
135468
136747
|
}
|
|
135469
136748
|
};
|
|
135470
136749
|
}
|
|
135471
136750
|
|
|
136751
|
+
// Cockroach-----------------------------------------------------------------------------------------------------------
|
|
136752
|
+
const resetCockroach = async (db, cockroachTables) => {
|
|
136753
|
+
const tablesToTruncate = Object.entries(cockroachTables).map(([_, table]) => {
|
|
136754
|
+
const config = getTableConfig$4(table);
|
|
136755
|
+
config.schema = config.schema === undefined ? 'public' : config.schema;
|
|
136756
|
+
return `"${config.schema}"."${config.name}"`;
|
|
136757
|
+
});
|
|
136758
|
+
await db.execute(sql.raw(`truncate ${tablesToTruncate.join(',')} cascade;`));
|
|
136759
|
+
};
|
|
136760
|
+
const filterCockroachSchema = (schema) => {
|
|
136761
|
+
const cockroachSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], CockroachTable) || is(keyValue[1], Relations)));
|
|
136762
|
+
const cockroachTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], CockroachTable)));
|
|
136763
|
+
return { cockroachSchema, cockroachTables };
|
|
136764
|
+
};
|
|
136765
|
+
const seedCockroach = async (db, schema, options = {}, refinements) => {
|
|
136766
|
+
const seedService = new SeedService();
|
|
136767
|
+
const { cockroachSchema, cockroachTables } = filterCockroachSchema(schema);
|
|
136768
|
+
const { tables, relations } = getSchemaInfo(cockroachSchema, cockroachTables, mapCockroachTable);
|
|
136769
|
+
const generatedTablesGenerators = seedService.generatePossibleGenerators('cockroach', tables, relations, refinements, options);
|
|
136770
|
+
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
136771
|
+
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, cockroachTables, { ...options, preserveCyclicTablesData });
|
|
136772
|
+
const { filteredTablesGenerators, tablesUniqueNotNullColumn } = seedService.filterCyclicTables(generatedTablesGenerators);
|
|
136773
|
+
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
136774
|
+
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, cockroachTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
136775
|
+
};
|
|
136776
|
+
const mapCockroachTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
|
|
136777
|
+
const getAllBaseColumns = (baseColumn) => {
|
|
136778
|
+
const baseColumnResult = {
|
|
136779
|
+
name: baseColumn.name,
|
|
136780
|
+
columnType: baseColumn.getSQLType(),
|
|
136781
|
+
typeParams: getTypeParams(baseColumn.getSQLType()),
|
|
136782
|
+
dataType: baseColumn.dataType.split(' ')[0],
|
|
136783
|
+
size: baseColumn.length,
|
|
136784
|
+
hasDefault: baseColumn.hasDefault,
|
|
136785
|
+
enumValues: baseColumn.enumValues,
|
|
136786
|
+
default: baseColumn.default,
|
|
136787
|
+
isUnique: baseColumn.isUnique,
|
|
136788
|
+
notNull: baseColumn.notNull,
|
|
136789
|
+
primary: baseColumn.primary,
|
|
136790
|
+
baseColumn: baseColumn.baseColumn === undefined ? undefined : getAllBaseColumns(baseColumn.baseColumn),
|
|
136791
|
+
};
|
|
136792
|
+
return baseColumnResult;
|
|
136793
|
+
};
|
|
136794
|
+
const getTypeParams = (sqlType) => {
|
|
136795
|
+
// get type params
|
|
136796
|
+
const typeParams = {};
|
|
136797
|
+
// handle dimensions
|
|
136798
|
+
if (sqlType.includes('[')) {
|
|
136799
|
+
const match = sqlType.match(/\[\w*]/g);
|
|
136800
|
+
if (match) {
|
|
136801
|
+
typeParams['dimensions'] = match.length;
|
|
136802
|
+
}
|
|
136803
|
+
}
|
|
136804
|
+
if (sqlType.startsWith('numeric')
|
|
136805
|
+
|| sqlType.startsWith('decimal')
|
|
136806
|
+
|| sqlType.startsWith('double precision')
|
|
136807
|
+
|| sqlType.startsWith('real')) {
|
|
136808
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
136809
|
+
if (match) {
|
|
136810
|
+
typeParams['precision'] = Number(match[1]);
|
|
136811
|
+
typeParams['scale'] = Number(match[2]);
|
|
136812
|
+
}
|
|
136813
|
+
}
|
|
136814
|
+
else if (sqlType.startsWith('varchar')
|
|
136815
|
+
|| sqlType.startsWith('char')
|
|
136816
|
+
|| sqlType.startsWith('bit')
|
|
136817
|
+
|| sqlType.startsWith('vector')
|
|
136818
|
+
|| sqlType.startsWith('time')
|
|
136819
|
+
|| sqlType.startsWith('timestamp')
|
|
136820
|
+
|| sqlType.startsWith('interval')) {
|
|
136821
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
136822
|
+
if (match) {
|
|
136823
|
+
typeParams['length'] = Number(match[1]);
|
|
136824
|
+
}
|
|
136825
|
+
}
|
|
136826
|
+
return typeParams;
|
|
136827
|
+
};
|
|
136828
|
+
// console.log(tableConfig.columns);
|
|
136829
|
+
return {
|
|
136830
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
136831
|
+
columns: tableConfig.columns.map((column) => ({
|
|
136832
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
136833
|
+
columnType: column.getSQLType(),
|
|
136834
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
136835
|
+
dataType: column.dataType.split(' ')[0],
|
|
136836
|
+
size: column.length,
|
|
136837
|
+
hasDefault: column.hasDefault,
|
|
136838
|
+
default: column.default,
|
|
136839
|
+
enumValues: column.enumValues,
|
|
136840
|
+
isUnique: column.isUnique,
|
|
136841
|
+
notNull: column.notNull,
|
|
136842
|
+
primary: column.primary,
|
|
136843
|
+
generatedIdentityType: column.generatedIdentity?.type,
|
|
136844
|
+
baseColumn: (column.baseColumn === undefined)
|
|
136845
|
+
? undefined
|
|
136846
|
+
: getAllBaseColumns(column.baseColumn),
|
|
136847
|
+
})),
|
|
136848
|
+
primaryKeys: tableConfig.columns
|
|
136849
|
+
.filter((column) => column.primary)
|
|
136850
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
136851
|
+
};
|
|
136852
|
+
};
|
|
136853
|
+
|
|
136854
|
+
// MySql-----------------------------------------------------------------------------------------------------
|
|
136855
|
+
const resetMsSql = async (db, schema) => {
|
|
136856
|
+
const tablesToTruncate = Object.entries(schema).map(([_tsTableName, table]) => {
|
|
136857
|
+
const tableConfig = getTableConfig$5(table);
|
|
136858
|
+
return { dbName: tableConfig.name, dbSchema: tableConfig.schema ?? 'dbo' };
|
|
136859
|
+
});
|
|
136860
|
+
const allFkConstraints = {};
|
|
136861
|
+
for (const table of tablesToTruncate) {
|
|
136862
|
+
const gatherTableRelatedFkConstraints = `
|
|
136863
|
+
DECLARE @objectId INT
|
|
136864
|
+
= OBJECT_ID( QUOTENAME('${table.dbSchema}') + '.' + QUOTENAME('${table.dbName}') );
|
|
136865
|
+
|
|
136866
|
+
SELECT
|
|
136867
|
+
fk.name AS fkName,
|
|
136868
|
+
OBJECT_SCHEMA_NAME(fk.parent_object_id) AS parentSchema,
|
|
136869
|
+
OBJECT_NAME(fk.parent_object_id) AS parentTable,
|
|
136870
|
+
OBJECT_SCHEMA_NAME(fk.referenced_object_id) AS referencedSchema,
|
|
136871
|
+
OBJECT_NAME(fk.referenced_object_id) AS referencedTable,
|
|
136872
|
+
-- fkc.constraint_column_id AS Column_Ordinal,
|
|
136873
|
+
pc.name AS parentColumn,
|
|
136874
|
+
rc.name AS referencedColumn,
|
|
136875
|
+
fk.delete_referential_action_desc AS onDeleteAction,
|
|
136876
|
+
fk.update_referential_action_desc AS onUpdateAction,
|
|
136877
|
+
CASE
|
|
136878
|
+
WHEN fk.parent_object_id = @objectId THEN 'outbound' -- your table → another table
|
|
136879
|
+
ELSE 'inbound' -- another table → your table
|
|
136880
|
+
END AS relation
|
|
136881
|
+
FROM sys.foreign_keys AS fk
|
|
136882
|
+
JOIN sys.foreign_key_columns fkc
|
|
136883
|
+
ON fk.object_id = fkc.constraint_object_id
|
|
136884
|
+
JOIN sys.columns pc
|
|
136885
|
+
ON fkc.parent_object_id = pc.object_id
|
|
136886
|
+
AND fkc.parent_column_id = pc.column_id
|
|
136887
|
+
JOIN sys.columns rc
|
|
136888
|
+
ON fkc.referenced_object_id = rc.object_id
|
|
136889
|
+
AND fkc.referenced_column_id = rc.column_id
|
|
136890
|
+
WHERE fk.parent_object_id = @objectId
|
|
136891
|
+
OR fk.referenced_object_id = @objectId
|
|
136892
|
+
ORDER BY relation, fkName;
|
|
136893
|
+
`;
|
|
136894
|
+
const rawRes = await db.execute(sql.raw(gatherTableRelatedFkConstraints));
|
|
136895
|
+
const res = rawRes.recordset;
|
|
136896
|
+
const tableRelatedFkConstraints = {};
|
|
136897
|
+
for (const fkInfo of res) {
|
|
136898
|
+
if (tableRelatedFkConstraints[fkInfo.fkName] === undefined) {
|
|
136899
|
+
const { parentColumn: _, referencedColumn: __, ...filteredFkInfo } = fkInfo;
|
|
136900
|
+
tableRelatedFkConstraints[fkInfo.fkName] = {
|
|
136901
|
+
...filteredFkInfo,
|
|
136902
|
+
parentColumns: res.filter(({ fkName }) => fkName === fkInfo.fkName).map(({ parentColumn }) => parentColumn),
|
|
136903
|
+
referencedColumns: res.filter(({ fkName }) => fkName === fkInfo.fkName).map(({ referencedColumn }) => referencedColumn),
|
|
136904
|
+
};
|
|
136905
|
+
}
|
|
136906
|
+
}
|
|
136907
|
+
allFkConstraints[`${table.dbSchema}.${table.dbName}`] = tableRelatedFkConstraints;
|
|
136908
|
+
// drop all table related fk constraints
|
|
136909
|
+
for (const fkInfo of Object.values(tableRelatedFkConstraints)) {
|
|
136910
|
+
const dropFkConstraints = `ALTER TABLE [${fkInfo.parentSchema}].[${fkInfo.parentTable}] DROP CONSTRAINT [${fkInfo.fkName}];`;
|
|
136911
|
+
await db.execute(sql.raw(dropFkConstraints));
|
|
136912
|
+
}
|
|
136913
|
+
// truncating
|
|
136914
|
+
const truncateTable = `truncate table [${table.dbSchema}].[${table.dbName}];`;
|
|
136915
|
+
await db.execute(sql.raw(truncateTable));
|
|
136916
|
+
}
|
|
136917
|
+
// add all table related fk constraints
|
|
136918
|
+
for (const table of tablesToTruncate) {
|
|
136919
|
+
const tableRelatedFkConstraints = allFkConstraints[`${table.dbSchema}.${table.dbName}`];
|
|
136920
|
+
for (const fkInfo of Object.values(tableRelatedFkConstraints)) {
|
|
136921
|
+
const addFkConstraints = `
|
|
136922
|
+
ALTER TABLE [${fkInfo.parentSchema}].[${fkInfo.parentTable}]
|
|
136923
|
+
ADD CONSTRAINT [${fkInfo.fkName}]
|
|
136924
|
+
FOREIGN KEY(${fkInfo.parentColumns.map((colName) => `[${colName}]`).join(',')})
|
|
136925
|
+
REFERENCES [${fkInfo.referencedSchema}].[${fkInfo.referencedTable}] (${fkInfo.referencedColumns.map((colName) => `[${colName}]`).join(',')})
|
|
136926
|
+
ON DELETE ${fkInfo.onDeleteAction.split('_').join(' ')}
|
|
136927
|
+
ON UPDATE ${fkInfo.onUpdateAction.split('_').join(' ')};
|
|
136928
|
+
`;
|
|
136929
|
+
await db.execute(sql.raw(addFkConstraints));
|
|
136930
|
+
}
|
|
136931
|
+
}
|
|
136932
|
+
};
|
|
136933
|
+
const filterMsSqlTables = (schema) => {
|
|
136934
|
+
const mssqlSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], MsSqlTable) || is(keyValue[1], Relations)));
|
|
136935
|
+
const mssqlTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], MsSqlTable)));
|
|
136936
|
+
return { mssqlSchema, mssqlTables };
|
|
136937
|
+
};
|
|
136938
|
+
const seedMsSql = async (db, schema, options = {}, refinements) => {
|
|
136939
|
+
const { mssqlSchema, mssqlTables } = filterMsSqlTables(schema);
|
|
136940
|
+
const { tables, relations } = getSchemaInfo(mssqlSchema, mssqlTables, mapMsSqlTable);
|
|
136941
|
+
const seedService = new SeedService();
|
|
136942
|
+
const generatedTablesGenerators = seedService.generatePossibleGenerators('mssql', tables, relations, refinements, options);
|
|
136943
|
+
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
136944
|
+
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, mssqlTables, { ...options, preserveCyclicTablesData });
|
|
136945
|
+
const { filteredTablesGenerators, tablesUniqueNotNullColumn } = seedService.filterCyclicTables(generatedTablesGenerators);
|
|
136946
|
+
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
136947
|
+
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, mssqlTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
136948
|
+
};
|
|
136949
|
+
const mapMsSqlTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
|
|
136950
|
+
// TODO: rewrite
|
|
136951
|
+
const getTypeParams = (sqlType) => {
|
|
136952
|
+
// get type params and set only type
|
|
136953
|
+
const typeParams = {};
|
|
136954
|
+
if (sqlType.startsWith('decimal')
|
|
136955
|
+
|| sqlType.startsWith('real')
|
|
136956
|
+
|| sqlType.startsWith('float')) {
|
|
136957
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
136958
|
+
if (match) {
|
|
136959
|
+
typeParams['precision'] = Number(match[1]);
|
|
136960
|
+
typeParams['scale'] = Number(match[2]);
|
|
136961
|
+
}
|
|
136962
|
+
}
|
|
136963
|
+
else if (sqlType.startsWith('char')
|
|
136964
|
+
|| sqlType.startsWith('varchar')
|
|
136965
|
+
|| sqlType.startsWith('binary')
|
|
136966
|
+
|| sqlType.startsWith('varbinary')) {
|
|
136967
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
136968
|
+
if (match) {
|
|
136969
|
+
typeParams['length'] = Number(match[1]);
|
|
136970
|
+
}
|
|
136971
|
+
}
|
|
136972
|
+
return typeParams;
|
|
136973
|
+
};
|
|
136974
|
+
return {
|
|
136975
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
136976
|
+
columns: tableConfig.columns.map((column) => ({
|
|
136977
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
136978
|
+
columnType: column.getSQLType(),
|
|
136979
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
136980
|
+
dataType: column.dataType.split(' ')[0],
|
|
136981
|
+
hasDefault: column.hasDefault,
|
|
136982
|
+
default: column.default,
|
|
136983
|
+
enumValues: column.enumValues,
|
|
136984
|
+
isUnique: column.isUnique,
|
|
136985
|
+
notNull: column.notNull,
|
|
136986
|
+
primary: column.primary,
|
|
136987
|
+
identity: column.identity ? true : false,
|
|
136988
|
+
})),
|
|
136989
|
+
primaryKeys: tableConfig.columns
|
|
136990
|
+
.filter((column) => column.primary)
|
|
136991
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
136992
|
+
};
|
|
136993
|
+
};
|
|
136994
|
+
|
|
136995
|
+
// MySql-----------------------------------------------------------------------------------------------------
|
|
136996
|
+
const resetMySql = async (db, schema) => {
|
|
136997
|
+
const tablesToTruncate = Object.entries(schema).map(([_tsTableName, table]) => {
|
|
136998
|
+
const dbTableName = getTableName(table);
|
|
136999
|
+
return dbTableName;
|
|
137000
|
+
});
|
|
137001
|
+
await db.execute(sql.raw('SET FOREIGN_KEY_CHECKS = 0;'));
|
|
137002
|
+
for (const tableName of tablesToTruncate) {
|
|
137003
|
+
const sqlQuery = `truncate \`${tableName}\`;`;
|
|
137004
|
+
await db.execute(sql.raw(sqlQuery));
|
|
137005
|
+
}
|
|
137006
|
+
await db.execute(sql.raw('SET FOREIGN_KEY_CHECKS = 1;'));
|
|
137007
|
+
};
|
|
137008
|
+
const filterMysqlTables = (schema) => {
|
|
137009
|
+
const mysqlSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], MySqlTable) || is(keyValue[1], Relations)));
|
|
137010
|
+
const mysqlTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], MySqlTable)));
|
|
137011
|
+
return { mysqlSchema, mysqlTables };
|
|
137012
|
+
};
|
|
137013
|
+
const seedMySql = async (db, schema, options = {}, refinements) => {
|
|
137014
|
+
const { mysqlSchema, mysqlTables } = filterMysqlTables(schema);
|
|
137015
|
+
const { tables, relations } = getSchemaInfo(mysqlSchema, mysqlTables, mapMySqlTable);
|
|
137016
|
+
const seedService = new SeedService();
|
|
137017
|
+
const generatedTablesGenerators = seedService.generatePossibleGenerators('mysql', tables, relations, refinements, options);
|
|
137018
|
+
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
137019
|
+
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, mysqlTables, { ...options, preserveCyclicTablesData });
|
|
137020
|
+
const { filteredTablesGenerators, tablesUniqueNotNullColumn } = seedService.filterCyclicTables(generatedTablesGenerators);
|
|
137021
|
+
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
137022
|
+
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, mysqlTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
137023
|
+
};
|
|
137024
|
+
const mapMySqlTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
|
|
137025
|
+
const getTypeParams = (sqlType) => {
|
|
137026
|
+
// get type params and set only type
|
|
137027
|
+
const typeParams = {};
|
|
137028
|
+
if (sqlType.startsWith('decimal')
|
|
137029
|
+
|| sqlType.startsWith('real')
|
|
137030
|
+
|| sqlType.startsWith('double')
|
|
137031
|
+
|| sqlType.startsWith('float')) {
|
|
137032
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137033
|
+
if (match) {
|
|
137034
|
+
typeParams['precision'] = Number(match[1]);
|
|
137035
|
+
typeParams['scale'] = Number(match[2]);
|
|
137036
|
+
}
|
|
137037
|
+
}
|
|
137038
|
+
else if (sqlType.startsWith('char')
|
|
137039
|
+
|| sqlType.startsWith('varchar')
|
|
137040
|
+
|| sqlType.startsWith('binary')
|
|
137041
|
+
|| sqlType.startsWith('varbinary')) {
|
|
137042
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137043
|
+
if (match) {
|
|
137044
|
+
typeParams['length'] = Number(match[1]);
|
|
137045
|
+
}
|
|
137046
|
+
}
|
|
137047
|
+
return typeParams;
|
|
137048
|
+
};
|
|
137049
|
+
return {
|
|
137050
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137051
|
+
columns: tableConfig.columns.map((column) => ({
|
|
137052
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137053
|
+
columnType: column.getSQLType(),
|
|
137054
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137055
|
+
dataType: column.dataType.split(' ')[0],
|
|
137056
|
+
hasDefault: column.hasDefault,
|
|
137057
|
+
default: column.default,
|
|
137058
|
+
enumValues: column.enumValues,
|
|
137059
|
+
isUnique: column.isUnique,
|
|
137060
|
+
notNull: column.notNull,
|
|
137061
|
+
primary: column.primary,
|
|
137062
|
+
})),
|
|
137063
|
+
primaryKeys: tableConfig.columns
|
|
137064
|
+
.filter((column) => column.primary)
|
|
137065
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137066
|
+
};
|
|
137067
|
+
};
|
|
137068
|
+
|
|
137069
|
+
// Postgres-----------------------------------------------------------------------------------------------------------
|
|
137070
|
+
const resetPostgres = async (db, pgTables) => {
|
|
137071
|
+
const tablesToTruncate = Object.entries(pgTables).map(([_, table]) => {
|
|
137072
|
+
const config = getTableConfig$1(table);
|
|
137073
|
+
config.schema = config.schema === undefined ? 'public' : config.schema;
|
|
137074
|
+
return `"${config.schema}"."${config.name}"`;
|
|
137075
|
+
});
|
|
137076
|
+
await db.execute(sql.raw(`truncate ${tablesToTruncate.join(',')} cascade;`));
|
|
137077
|
+
};
|
|
137078
|
+
const filterPgSchema = (schema) => {
|
|
137079
|
+
const pgSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], PgTable) || is(keyValue[1], Relations)));
|
|
137080
|
+
const pgTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], PgTable)));
|
|
137081
|
+
return { pgSchema, pgTables };
|
|
137082
|
+
};
|
|
137083
|
+
const seedPostgres = async (db, schema, options = {}, refinements) => {
|
|
137084
|
+
const seedService = new SeedService();
|
|
137085
|
+
const { pgSchema, pgTables } = filterPgSchema(schema);
|
|
137086
|
+
const { tables, relations } = getSchemaInfo(pgSchema, pgTables, mapPgTable);
|
|
137087
|
+
// const { tables, relations } = getPostgresInfo(pgSchema, pgTables);
|
|
137088
|
+
const generatedTablesGenerators = seedService.generatePossibleGenerators('postgresql', tables, relations, refinements, options);
|
|
137089
|
+
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
137090
|
+
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, pgTables, { ...options, preserveCyclicTablesData });
|
|
137091
|
+
const { filteredTablesGenerators, tablesUniqueNotNullColumn } = seedService.filterCyclicTables(generatedTablesGenerators);
|
|
137092
|
+
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
137093
|
+
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, pgTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
137094
|
+
};
|
|
137095
|
+
const mapPgTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
|
|
137096
|
+
const getAllBaseColumns = (baseColumn) => {
|
|
137097
|
+
const baseColumnResult = {
|
|
137098
|
+
name: baseColumn.name,
|
|
137099
|
+
columnType: baseColumn.getSQLType(),
|
|
137100
|
+
typeParams: getTypeParams(baseColumn.getSQLType()),
|
|
137101
|
+
dataType: baseColumn.dataType.split(' ')[0],
|
|
137102
|
+
size: baseColumn.length,
|
|
137103
|
+
hasDefault: baseColumn.hasDefault,
|
|
137104
|
+
enumValues: baseColumn.enumValues,
|
|
137105
|
+
default: baseColumn.default,
|
|
137106
|
+
isUnique: baseColumn.isUnique,
|
|
137107
|
+
notNull: baseColumn.notNull,
|
|
137108
|
+
primary: baseColumn.primary,
|
|
137109
|
+
baseColumn: baseColumn.baseColumn === undefined ? undefined : getAllBaseColumns(baseColumn.baseColumn),
|
|
137110
|
+
};
|
|
137111
|
+
return baseColumnResult;
|
|
137112
|
+
};
|
|
137113
|
+
const getTypeParams = (sqlType) => {
|
|
137114
|
+
// get type params
|
|
137115
|
+
const typeParams = {};
|
|
137116
|
+
// handle dimensions
|
|
137117
|
+
if (sqlType.includes('[')) {
|
|
137118
|
+
const match = sqlType.match(/\[\w*]/g);
|
|
137119
|
+
if (match) {
|
|
137120
|
+
typeParams['dimensions'] = match.length;
|
|
137121
|
+
}
|
|
137122
|
+
}
|
|
137123
|
+
if (sqlType.startsWith('numeric')
|
|
137124
|
+
|| sqlType.startsWith('decimal')
|
|
137125
|
+
|| sqlType.startsWith('double precision')
|
|
137126
|
+
|| sqlType.startsWith('real')) {
|
|
137127
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137128
|
+
if (match) {
|
|
137129
|
+
typeParams['precision'] = Number(match[1]);
|
|
137130
|
+
typeParams['scale'] = Number(match[2]);
|
|
137131
|
+
}
|
|
137132
|
+
}
|
|
137133
|
+
else if (sqlType.startsWith('varchar')
|
|
137134
|
+
|| sqlType.startsWith('bpchar')
|
|
137135
|
+
|| sqlType.startsWith('char')
|
|
137136
|
+
|| sqlType.startsWith('bit')
|
|
137137
|
+
|| sqlType.startsWith('vector')
|
|
137138
|
+
|| sqlType.startsWith('time')
|
|
137139
|
+
|| sqlType.startsWith('timestamp')
|
|
137140
|
+
|| sqlType.startsWith('interval')) {
|
|
137141
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137142
|
+
if (match) {
|
|
137143
|
+
typeParams['length'] = Number(match[1]);
|
|
137144
|
+
}
|
|
137145
|
+
}
|
|
137146
|
+
return typeParams;
|
|
137147
|
+
};
|
|
137148
|
+
return {
|
|
137149
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137150
|
+
columns: tableConfig.columns.map((column) => ({
|
|
137151
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137152
|
+
columnType: column.getSQLType(),
|
|
137153
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137154
|
+
dataType: column.dataType.split(' ')[0],
|
|
137155
|
+
size: column.length,
|
|
137156
|
+
hasDefault: column.hasDefault,
|
|
137157
|
+
default: column.default,
|
|
137158
|
+
enumValues: column.enumValues,
|
|
137159
|
+
isUnique: column.isUnique,
|
|
137160
|
+
notNull: column.notNull,
|
|
137161
|
+
primary: column.primary,
|
|
137162
|
+
generatedIdentityType: column.generatedIdentity?.type,
|
|
137163
|
+
baseColumn: (column.baseColumn === undefined)
|
|
137164
|
+
? undefined
|
|
137165
|
+
: getAllBaseColumns(column.baseColumn),
|
|
137166
|
+
})),
|
|
137167
|
+
primaryKeys: tableConfig.columns
|
|
137168
|
+
.filter((column) => column.primary)
|
|
137169
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137170
|
+
};
|
|
137171
|
+
};
|
|
137172
|
+
|
|
137173
|
+
// SingleStore-----------------------------------------------------------------------------------------------------
|
|
137174
|
+
const resetSingleStore = async (db, schema) => {
|
|
137175
|
+
const tablesToTruncate = Object.entries(schema).map(([_tsTableName, table]) => {
|
|
137176
|
+
const dbTableName = getTableName(table);
|
|
137177
|
+
return dbTableName;
|
|
137178
|
+
});
|
|
137179
|
+
await db.execute(sql.raw('SET FOREIGN_KEY_CHECKS = 0;'));
|
|
137180
|
+
for (const tableName of tablesToTruncate) {
|
|
137181
|
+
const sqlQuery = `truncate \`${tableName}\`;`;
|
|
137182
|
+
await db.execute(sql.raw(sqlQuery));
|
|
137183
|
+
}
|
|
137184
|
+
await db.execute(sql.raw('SET FOREIGN_KEY_CHECKS = 1;'));
|
|
137185
|
+
};
|
|
137186
|
+
const filterSingleStoreTables = (schema) => {
|
|
137187
|
+
const singleStoreSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], SingleStoreTable) || is(keyValue[1], Relations)));
|
|
137188
|
+
const singleStoreTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], SingleStoreTable)));
|
|
137189
|
+
return { singleStoreSchema, singleStoreTables };
|
|
137190
|
+
};
|
|
137191
|
+
const seedSingleStore = async (db, schema, options = {}, refinements) => {
|
|
137192
|
+
const { singleStoreSchema, singleStoreTables } = filterSingleStoreTables(schema);
|
|
137193
|
+
const { tables, relations } = getSchemaInfo(singleStoreSchema, singleStoreTables, mapSingleStoreTable);
|
|
137194
|
+
const seedService = new SeedService();
|
|
137195
|
+
const generatedTablesGenerators = seedService.generatePossibleGenerators('singlestore', tables, relations, refinements, options);
|
|
137196
|
+
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
137197
|
+
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, singleStoreTables, { ...options, preserveCyclicTablesData });
|
|
137198
|
+
const { filteredTablesGenerators, tablesUniqueNotNullColumn } = seedService.filterCyclicTables(generatedTablesGenerators);
|
|
137199
|
+
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
137200
|
+
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, singleStoreTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
137201
|
+
};
|
|
137202
|
+
const mapSingleStoreTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
|
|
137203
|
+
const getTypeParams = (sqlType) => {
|
|
137204
|
+
// get type params and set only type
|
|
137205
|
+
const typeParams = {};
|
|
137206
|
+
if (sqlType.startsWith('decimal')
|
|
137207
|
+
|| sqlType.startsWith('real')
|
|
137208
|
+
|| sqlType.startsWith('double')
|
|
137209
|
+
|| sqlType.startsWith('float')) {
|
|
137210
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137211
|
+
if (match) {
|
|
137212
|
+
typeParams['precision'] = Number(match[1]);
|
|
137213
|
+
typeParams['scale'] = Number(match[2]);
|
|
137214
|
+
}
|
|
137215
|
+
}
|
|
137216
|
+
else if (sqlType.startsWith('char')
|
|
137217
|
+
|| sqlType.startsWith('varchar')
|
|
137218
|
+
|| sqlType.startsWith('binary')
|
|
137219
|
+
|| sqlType.startsWith('varbinary')) {
|
|
137220
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137221
|
+
if (match) {
|
|
137222
|
+
typeParams['length'] = Number(match[1]);
|
|
137223
|
+
}
|
|
137224
|
+
}
|
|
137225
|
+
else if (sqlType.startsWith('vector')) {
|
|
137226
|
+
const match = sqlType.match(/\((\d+),? ?((F|I)\d{1,2})?\)/);
|
|
137227
|
+
if (match) {
|
|
137228
|
+
typeParams['length'] = Number(match[1]);
|
|
137229
|
+
typeParams['vectorValueType'] = match[2];
|
|
137230
|
+
}
|
|
137231
|
+
}
|
|
137232
|
+
return typeParams;
|
|
137233
|
+
};
|
|
137234
|
+
return {
|
|
137235
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137236
|
+
columns: tableConfig.columns.map((column) => ({
|
|
137237
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137238
|
+
columnType: column.getSQLType(),
|
|
137239
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137240
|
+
dataType: column.dataType.split(' ')[0],
|
|
137241
|
+
hasDefault: column.hasDefault,
|
|
137242
|
+
default: column.default,
|
|
137243
|
+
enumValues: column.enumValues,
|
|
137244
|
+
isUnique: column.isUnique,
|
|
137245
|
+
notNull: column.notNull,
|
|
137246
|
+
primary: column.primary,
|
|
137247
|
+
})),
|
|
137248
|
+
primaryKeys: tableConfig.columns
|
|
137249
|
+
.filter((column) => column.primary)
|
|
137250
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137251
|
+
};
|
|
137252
|
+
};
|
|
137253
|
+
|
|
137254
|
+
// Sqlite------------------------------------------------------------------------------------------------------------------------
|
|
137255
|
+
const resetSqlite = async (db, schema) => {
|
|
137256
|
+
const tablesToTruncate = Object.entries(schema).map(([_tsTableName, table]) => {
|
|
137257
|
+
const dbTableName = getTableName(table);
|
|
137258
|
+
return dbTableName;
|
|
137259
|
+
});
|
|
137260
|
+
await db.run(sql.raw('PRAGMA foreign_keys = OFF'));
|
|
137261
|
+
for (const tableName of tablesToTruncate) {
|
|
137262
|
+
const sqlQuery = `delete from \`${tableName}\`;`;
|
|
137263
|
+
await db.run(sql.raw(sqlQuery));
|
|
137264
|
+
}
|
|
137265
|
+
await db.run(sql.raw('PRAGMA foreign_keys = ON'));
|
|
137266
|
+
};
|
|
137267
|
+
const filterSqliteTables = (schema) => {
|
|
137268
|
+
const sqliteSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], SQLiteTable) || is(keyValue[1], Relations)));
|
|
137269
|
+
const sqliteTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], SQLiteTable)));
|
|
137270
|
+
return { sqliteSchema, sqliteTables };
|
|
137271
|
+
};
|
|
137272
|
+
const seedSqlite = async (db, schema, options = {}, refinements) => {
|
|
137273
|
+
const { sqliteSchema, sqliteTables } = filterSqliteTables(schema);
|
|
137274
|
+
const { tables, relations } = getSchemaInfo(sqliteSchema, sqliteTables, mapSqliteTable);
|
|
137275
|
+
const seedService = new SeedService();
|
|
137276
|
+
const generatedTablesGenerators = seedService.generatePossibleGenerators('sqlite', tables, relations, refinements, options);
|
|
137277
|
+
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
137278
|
+
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, sqliteTables, { ...options, preserveCyclicTablesData });
|
|
137279
|
+
const { filteredTablesGenerators, tablesUniqueNotNullColumn } = seedService.filterCyclicTables(generatedTablesGenerators);
|
|
137280
|
+
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
137281
|
+
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, sqliteTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
137282
|
+
};
|
|
137283
|
+
const mapSqliteTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
|
|
137284
|
+
const getTypeParams = (sqlType) => {
|
|
137285
|
+
// get type params and set only type
|
|
137286
|
+
const typeParams = {};
|
|
137287
|
+
if (sqlType.startsWith('decimal')) {
|
|
137288
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137289
|
+
if (match) {
|
|
137290
|
+
typeParams['precision'] = Number(match[1]);
|
|
137291
|
+
typeParams['scale'] = Number(match[2]);
|
|
137292
|
+
}
|
|
137293
|
+
}
|
|
137294
|
+
else if (sqlType.startsWith('char')
|
|
137295
|
+
|| sqlType.startsWith('varchar')
|
|
137296
|
+
|| sqlType.startsWith('text')) {
|
|
137297
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137298
|
+
if (match) {
|
|
137299
|
+
typeParams['length'] = Number(match[1]);
|
|
137300
|
+
}
|
|
137301
|
+
}
|
|
137302
|
+
return typeParams;
|
|
137303
|
+
};
|
|
137304
|
+
return {
|
|
137305
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137306
|
+
columns: tableConfig.columns.map((column) => ({
|
|
137307
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137308
|
+
columnType: column.getSQLType(),
|
|
137309
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137310
|
+
dataType: column.dataType.split(' ')[0],
|
|
137311
|
+
hasDefault: column.hasDefault,
|
|
137312
|
+
default: column.default,
|
|
137313
|
+
enumValues: column.enumValues,
|
|
137314
|
+
isUnique: column.isUnique,
|
|
137315
|
+
notNull: column.notNull,
|
|
137316
|
+
primary: column.primary,
|
|
137317
|
+
})),
|
|
137318
|
+
primaryKeys: tableConfig.columns
|
|
137319
|
+
.filter((column) => column.primary)
|
|
137320
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137321
|
+
};
|
|
137322
|
+
};
|
|
137323
|
+
|
|
135472
137324
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
135473
137325
|
class SeedPromise {
|
|
135474
137326
|
db;
|
|
@@ -135571,7 +137423,7 @@ async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleSt
|
|
|
135571
137423
|
* // seeding with count and seed specified
|
|
135572
137424
|
* await seed(db, schema, { count: 100000, seed: 1 });
|
|
135573
137425
|
*
|
|
135574
|
-
* //
|
|
137426
|
+
* //seeding using refine
|
|
135575
137427
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
135576
137428
|
* users: {
|
|
135577
137429
|
* columns: {
|
|
@@ -135592,17 +137444,6 @@ async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleSt
|
|
|
135592
137444
|
* },
|
|
135593
137445
|
* }));
|
|
135594
137446
|
*
|
|
135595
|
-
* // seeding while ignoring column
|
|
135596
|
-
* await seed(db, schema).refine((funcs) => ({
|
|
135597
|
-
* users: {
|
|
135598
|
-
* count: 5,
|
|
135599
|
-
* columns: {
|
|
135600
|
-
* name: funcs.fullName(),
|
|
135601
|
-
* photo: false, // the photo column will not be seeded, allowing the database to use its default value.
|
|
135602
|
-
* },
|
|
135603
|
-
* },
|
|
135604
|
-
* }));
|
|
135605
|
-
*
|
|
135606
137447
|
* ```
|
|
135607
137448
|
*/
|
|
135608
137449
|
function seed(db, schema, options) {
|
|
@@ -135622,8 +137463,17 @@ const seedFunc = async (db, schema, options = {}, refinements) => {
|
|
|
135622
137463
|
else if (is(db, (BaseSQLiteDatabase))) {
|
|
135623
137464
|
await seedSqlite(db, schema, { ...options, version }, refinements);
|
|
135624
137465
|
}
|
|
137466
|
+
else if (is(db, (MsSqlDatabase))) {
|
|
137467
|
+
await seedMsSql(db, schema, { ...options, version }, refinements);
|
|
137468
|
+
}
|
|
137469
|
+
else if (is(db, (CockroachDatabase))) {
|
|
137470
|
+
await seedCockroach(db, schema, { ...options, version }, refinements);
|
|
137471
|
+
}
|
|
137472
|
+
else if (is(db, (SingleStoreDatabase))) {
|
|
137473
|
+
await seedSingleStore(db, schema, { ...options, version }, refinements);
|
|
137474
|
+
}
|
|
135625
137475
|
else {
|
|
135626
|
-
throw new Error('The drizzle-seed package currently supports only PostgreSQL, MySQL, and
|
|
137476
|
+
throw new Error('The drizzle-seed package currently supports only PostgreSQL, MySQL, SQLite, Ms Sql, CockroachDB and SingleStore databases. Please ensure your database is one of these supported types');
|
|
135627
137477
|
}
|
|
135628
137478
|
return;
|
|
135629
137479
|
};
|
|
@@ -135665,10 +137515,6 @@ const seedFunc = async (db, schema, options = {}, refinements) => {
|
|
|
135665
137515
|
* @example
|
|
135666
137516
|
* ```ts
|
|
135667
137517
|
* await reset(db, schema);
|
|
135668
|
-
*
|
|
135669
|
-
* // Alternatively, you can provide an object containing your tables
|
|
135670
|
-
* // as the `schema` parameter when calling `reset`.
|
|
135671
|
-
* await reset(db, { users });
|
|
135672
137518
|
* ```
|
|
135673
137519
|
*/
|
|
135674
137520
|
async function reset(db, schema) {
|
|
@@ -135690,623 +137536,28 @@ async function reset(db, schema) {
|
|
|
135690
137536
|
await resetSqlite(db, sqliteTables);
|
|
135691
137537
|
}
|
|
135692
137538
|
}
|
|
135693
|
-
else {
|
|
135694
|
-
|
|
135695
|
-
|
|
135696
|
-
|
|
135697
|
-
// Postgres-----------------------------------------------------------------------------------------------------------
|
|
135698
|
-
const resetPostgres = async (db, pgTables) => {
|
|
135699
|
-
const tablesToTruncate = Object.entries(pgTables).map(([_, table]) => {
|
|
135700
|
-
const config = getTableConfig(table);
|
|
135701
|
-
config.schema = config.schema === undefined ? 'public' : config.schema;
|
|
135702
|
-
return `"${config.schema}"."${config.name}"`;
|
|
135703
|
-
});
|
|
135704
|
-
await db.execute(sql.raw(`truncate ${tablesToTruncate.join(',')} cascade;`));
|
|
135705
|
-
};
|
|
135706
|
-
const filterPgSchema = (schema) => {
|
|
135707
|
-
const pgSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], PgTable) || is(keyValue[1], Relations)));
|
|
135708
|
-
const pgTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], PgTable)));
|
|
135709
|
-
return { pgSchema, pgTables };
|
|
135710
|
-
};
|
|
135711
|
-
const seedPostgres = async (db, schema, options = {}, refinements) => {
|
|
135712
|
-
const seedService = new SeedService();
|
|
135713
|
-
const { pgSchema, pgTables } = filterPgSchema(schema);
|
|
135714
|
-
const { tables, relations } = getPostgresInfo(pgSchema, pgTables);
|
|
135715
|
-
const generatedTablesGenerators = seedService.generatePossibleGenerators('postgresql', tables, relations, refinements, options);
|
|
135716
|
-
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
135717
|
-
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, pgTables, { ...options, preserveCyclicTablesData });
|
|
135718
|
-
const { filteredTablesGenerators, tablesUniqueNotNullColumn } = seedService.filterCyclicTables(generatedTablesGenerators);
|
|
135719
|
-
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
135720
|
-
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, pgTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
135721
|
-
};
|
|
135722
|
-
const getPostgresInfo = (pgSchema, pgTables) => {
|
|
135723
|
-
let tableConfig;
|
|
135724
|
-
let dbToTsColumnNamesMap;
|
|
135725
|
-
const dbToTsTableNamesMap = Object.fromEntries(Object.entries(pgTables).map(([key, value]) => [getTableName(value), key]));
|
|
135726
|
-
const tables = [];
|
|
135727
|
-
const relations = [];
|
|
135728
|
-
const dbToTsColumnNamesMapGlobal = {};
|
|
135729
|
-
const tableRelations = {};
|
|
135730
|
-
const getDbToTsColumnNamesMap = (table) => {
|
|
135731
|
-
let dbToTsColumnNamesMap = {};
|
|
135732
|
-
const tableName = getTableName(table);
|
|
135733
|
-
if (Object.hasOwn(dbToTsColumnNamesMapGlobal, tableName)) {
|
|
135734
|
-
dbToTsColumnNamesMap = dbToTsColumnNamesMapGlobal[tableName];
|
|
135735
|
-
return dbToTsColumnNamesMap;
|
|
135736
|
-
}
|
|
135737
|
-
const tableConfig = getTableConfig(table);
|
|
135738
|
-
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
135739
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
135740
|
-
}
|
|
135741
|
-
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
135742
|
-
return dbToTsColumnNamesMap;
|
|
135743
|
-
};
|
|
135744
|
-
const transformFromDrizzleRelation = (schema, getDbToTsColumnNamesMap, tableRelations) => {
|
|
135745
|
-
const schemaConfig = extractTablesRelationalConfig(schema, createTableRelationsHelpers);
|
|
135746
|
-
const relations = [];
|
|
135747
|
-
for (const table of Object.values(schemaConfig.tables)) {
|
|
135748
|
-
if (table.relations === undefined)
|
|
135749
|
-
continue;
|
|
135750
|
-
for (const drizzleRel of Object.values(table.relations)) {
|
|
135751
|
-
if (!is(drizzleRel, One))
|
|
135752
|
-
continue;
|
|
135753
|
-
const tableConfig = getTableConfig(drizzleRel.sourceTable);
|
|
135754
|
-
const tableDbSchema = tableConfig.schema ?? 'public';
|
|
135755
|
-
const tableDbName = tableConfig.name;
|
|
135756
|
-
const tableTsName = schemaConfig.tableNamesMap[`${tableDbSchema}.${tableDbName}`] ?? tableDbName;
|
|
135757
|
-
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
135758
|
-
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
135759
|
-
?? [];
|
|
135760
|
-
const refTableConfig = getTableConfig(drizzleRel.referencedTable);
|
|
135761
|
-
const refTableDbSchema = refTableConfig.schema ?? 'public';
|
|
135762
|
-
const refTableDbName = refTableConfig.name;
|
|
135763
|
-
const refTableTsName = schemaConfig.tableNamesMap[`${refTableDbSchema}.${refTableDbName}`]
|
|
135764
|
-
?? refTableDbName;
|
|
135765
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
135766
|
-
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
135767
|
-
?? [];
|
|
135768
|
-
if (tableRelations[refTableTsName] === undefined) {
|
|
135769
|
-
tableRelations[refTableTsName] = [];
|
|
135770
|
-
}
|
|
135771
|
-
const relation = {
|
|
135772
|
-
table: tableTsName,
|
|
135773
|
-
columns,
|
|
135774
|
-
refTable: refTableTsName,
|
|
135775
|
-
refColumns,
|
|
135776
|
-
refTableRels: tableRelations[refTableTsName],
|
|
135777
|
-
type: 'one',
|
|
135778
|
-
};
|
|
135779
|
-
// do not add duplicate relation
|
|
135780
|
-
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
135781
|
-
&& rel.refTable === relation.refTable)) {
|
|
135782
|
-
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
135783
|
-
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
135784
|
-
+ `In this case, the foreign key constraint will be used.\n`);
|
|
135785
|
-
continue;
|
|
135786
|
-
}
|
|
135787
|
-
relations.push(relation);
|
|
135788
|
-
tableRelations[tableTsName].push(relation);
|
|
135789
|
-
}
|
|
135790
|
-
}
|
|
135791
|
-
return relations;
|
|
135792
|
-
};
|
|
135793
|
-
for (const table of Object.values(pgTables)) {
|
|
135794
|
-
tableConfig = getTableConfig(table);
|
|
135795
|
-
dbToTsColumnNamesMap = {};
|
|
135796
|
-
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
135797
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
135798
|
-
}
|
|
135799
|
-
// might be empty list
|
|
135800
|
-
const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
135801
|
-
const table = dbToTsTableNamesMap[tableConfig.name];
|
|
135802
|
-
const refTable = dbToTsTableNamesMap[getTableName(fk.reference().foreignTable)];
|
|
135803
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(fk.reference().foreignTable);
|
|
135804
|
-
if (tableRelations[refTable] === undefined) {
|
|
135805
|
-
tableRelations[refTable] = [];
|
|
135806
|
-
}
|
|
135807
|
-
return {
|
|
135808
|
-
table,
|
|
135809
|
-
columns: fk
|
|
135810
|
-
.reference()
|
|
135811
|
-
.columns.map((col) => dbToTsColumnNamesMap[col.name]),
|
|
135812
|
-
refTable,
|
|
135813
|
-
refColumns: fk
|
|
135814
|
-
.reference()
|
|
135815
|
-
.foreignColumns.map((fCol) => dbToTsColumnNamesMapForRefTable[fCol.name]),
|
|
135816
|
-
refTableRels: tableRelations[refTable],
|
|
135817
|
-
};
|
|
135818
|
-
});
|
|
135819
|
-
relations.push(...newRelations);
|
|
135820
|
-
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
135821
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
135822
|
-
}
|
|
135823
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]].push(...newRelations);
|
|
135824
|
-
const getAllBaseColumns = (baseColumn) => {
|
|
135825
|
-
const baseColumnResult = {
|
|
135826
|
-
name: baseColumn.name,
|
|
135827
|
-
columnType: baseColumn.getSQLType(),
|
|
135828
|
-
typeParams: getTypeParams(baseColumn.getSQLType()),
|
|
135829
|
-
dataType: baseColumn.dataType,
|
|
135830
|
-
size: baseColumn.size,
|
|
135831
|
-
hasDefault: baseColumn.hasDefault,
|
|
135832
|
-
enumValues: baseColumn.enumValues,
|
|
135833
|
-
default: baseColumn.default,
|
|
135834
|
-
isUnique: baseColumn.isUnique,
|
|
135835
|
-
notNull: baseColumn.notNull,
|
|
135836
|
-
primary: baseColumn.primary,
|
|
135837
|
-
baseColumn: baseColumn.baseColumn === undefined ? undefined : getAllBaseColumns(baseColumn.baseColumn),
|
|
135838
|
-
};
|
|
135839
|
-
return baseColumnResult;
|
|
135840
|
-
};
|
|
135841
|
-
const getTypeParams = (sqlType) => {
|
|
135842
|
-
// get type params
|
|
135843
|
-
const typeParams = {};
|
|
135844
|
-
// handle dimensions
|
|
135845
|
-
if (sqlType.includes('[')) {
|
|
135846
|
-
const match = sqlType.match(/\[\w*]/g);
|
|
135847
|
-
if (match) {
|
|
135848
|
-
typeParams['dimensions'] = match.length;
|
|
135849
|
-
}
|
|
135850
|
-
}
|
|
135851
|
-
if (sqlType.startsWith('numeric')
|
|
135852
|
-
|| sqlType.startsWith('decimal')
|
|
135853
|
-
|| sqlType.startsWith('double precision')
|
|
135854
|
-
|| sqlType.startsWith('real')) {
|
|
135855
|
-
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
135856
|
-
if (match) {
|
|
135857
|
-
typeParams['precision'] = Number(match[1]);
|
|
135858
|
-
typeParams['scale'] = Number(match[2]);
|
|
135859
|
-
}
|
|
135860
|
-
}
|
|
135861
|
-
else if (sqlType.startsWith('varchar')
|
|
135862
|
-
|| sqlType.startsWith('bpchar')
|
|
135863
|
-
|| sqlType.startsWith('char')
|
|
135864
|
-
|| sqlType.startsWith('bit')
|
|
135865
|
-
|| sqlType.startsWith('time')
|
|
135866
|
-
|| sqlType.startsWith('timestamp')
|
|
135867
|
-
|| sqlType.startsWith('interval')) {
|
|
135868
|
-
const match = sqlType.match(/\((\d+)\)/);
|
|
135869
|
-
if (match) {
|
|
135870
|
-
typeParams['length'] = Number(match[1]);
|
|
135871
|
-
}
|
|
135872
|
-
}
|
|
135873
|
-
return typeParams;
|
|
135874
|
-
};
|
|
135875
|
-
// console.log(tableConfig.columns);
|
|
135876
|
-
tables.push({
|
|
135877
|
-
name: dbToTsTableNamesMap[tableConfig.name],
|
|
135878
|
-
columns: tableConfig.columns.map((column) => ({
|
|
135879
|
-
name: dbToTsColumnNamesMap[column.name],
|
|
135880
|
-
columnType: column.getSQLType(),
|
|
135881
|
-
typeParams: getTypeParams(column.getSQLType()),
|
|
135882
|
-
dataType: column.dataType,
|
|
135883
|
-
size: column.size,
|
|
135884
|
-
hasDefault: column.hasDefault,
|
|
135885
|
-
default: column.default,
|
|
135886
|
-
enumValues: column.enumValues,
|
|
135887
|
-
isUnique: column.isUnique,
|
|
135888
|
-
notNull: column.notNull,
|
|
135889
|
-
primary: column.primary,
|
|
135890
|
-
generatedIdentityType: column.generatedIdentity?.type,
|
|
135891
|
-
baseColumn: (column.baseColumn === undefined)
|
|
135892
|
-
? undefined
|
|
135893
|
-
: getAllBaseColumns(column.baseColumn),
|
|
135894
|
-
})),
|
|
135895
|
-
primaryKeys: tableConfig.columns
|
|
135896
|
-
.filter((column) => column.primary)
|
|
135897
|
-
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
135898
|
-
});
|
|
135899
|
-
}
|
|
135900
|
-
const transformedDrizzleRelations = transformFromDrizzleRelation(pgSchema, getDbToTsColumnNamesMap, tableRelations);
|
|
135901
|
-
relations.push(...transformedDrizzleRelations);
|
|
135902
|
-
const isCyclicRelations = relations.map((relI) => {
|
|
135903
|
-
// if (relations.some((relj) => relI.table === relj.refTable && relI.refTable === relj.table)) {
|
|
135904
|
-
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
135905
|
-
if (isRelationCyclic(relI)) {
|
|
135906
|
-
tableRel['isCyclic'] = true;
|
|
135907
|
-
return { ...relI, isCyclic: true };
|
|
135908
|
-
}
|
|
135909
|
-
tableRel['isCyclic'] = false;
|
|
135910
|
-
return { ...relI, isCyclic: false };
|
|
135911
|
-
});
|
|
135912
|
-
return { tables, relations: isCyclicRelations, tableRelations };
|
|
135913
|
-
};
|
|
135914
|
-
const isRelationCyclic = (startRel) => {
|
|
135915
|
-
// self relation
|
|
135916
|
-
if (startRel.table === startRel.refTable)
|
|
135917
|
-
return false;
|
|
135918
|
-
// DFS
|
|
135919
|
-
const targetTable = startRel.table;
|
|
135920
|
-
const queue = [startRel];
|
|
135921
|
-
let path = [];
|
|
135922
|
-
while (queue.length !== 0) {
|
|
135923
|
-
const currRel = queue.shift();
|
|
135924
|
-
if (path.includes(currRel.table)) {
|
|
135925
|
-
const idx = path.indexOf(currRel.table);
|
|
135926
|
-
path = path.slice(0, idx);
|
|
135927
|
-
}
|
|
135928
|
-
path.push(currRel.table);
|
|
135929
|
-
for (const rel of currRel.refTableRels) {
|
|
135930
|
-
// self relation
|
|
135931
|
-
if (rel.table === rel.refTable)
|
|
135932
|
-
continue;
|
|
135933
|
-
if (rel.refTable === targetTable)
|
|
135934
|
-
return true;
|
|
135935
|
-
// found cycle, but not the one we are looking for
|
|
135936
|
-
if (path.includes(rel.refTable))
|
|
135937
|
-
continue;
|
|
135938
|
-
queue.unshift(rel);
|
|
137539
|
+
else if (is(db, (MsSqlDatabase))) {
|
|
137540
|
+
const { mssqlTables } = filterMsSqlTables(schema);
|
|
137541
|
+
if (Object.entries(mssqlTables).length > 0) {
|
|
137542
|
+
await resetMsSql(db, mssqlTables);
|
|
135939
137543
|
}
|
|
135940
137544
|
}
|
|
135941
|
-
|
|
135942
|
-
};
|
|
135943
|
-
|
|
135944
|
-
|
|
135945
|
-
const tablesToTruncate = Object.entries(schema).map(([_tsTableName, table]) => {
|
|
135946
|
-
const dbTableName = getTableName(table);
|
|
135947
|
-
return dbTableName;
|
|
135948
|
-
});
|
|
135949
|
-
await db.execute(sql.raw('SET FOREIGN_KEY_CHECKS = 0;'));
|
|
135950
|
-
for (const tableName of tablesToTruncate) {
|
|
135951
|
-
const sqlQuery = `truncate \`${tableName}\`;`;
|
|
135952
|
-
await db.execute(sql.raw(sqlQuery));
|
|
135953
|
-
}
|
|
135954
|
-
await db.execute(sql.raw('SET FOREIGN_KEY_CHECKS = 1;'));
|
|
135955
|
-
};
|
|
135956
|
-
const filterMysqlTables = (schema) => {
|
|
135957
|
-
const mysqlSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], MySqlTable) || is(keyValue[1], Relations)));
|
|
135958
|
-
const mysqlTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], MySqlTable)));
|
|
135959
|
-
return { mysqlSchema, mysqlTables };
|
|
135960
|
-
};
|
|
135961
|
-
const seedMySql = async (db, schema, options = {}, refinements) => {
|
|
135962
|
-
const { mysqlSchema, mysqlTables } = filterMysqlTables(schema);
|
|
135963
|
-
const { tables, relations } = getMySqlInfo(mysqlSchema, mysqlTables);
|
|
135964
|
-
const seedService = new SeedService();
|
|
135965
|
-
const generatedTablesGenerators = seedService.generatePossibleGenerators('mysql', tables, relations, refinements, options);
|
|
135966
|
-
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
135967
|
-
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, mysqlTables, { ...options, preserveCyclicTablesData });
|
|
135968
|
-
const { filteredTablesGenerators, tablesUniqueNotNullColumn } = seedService.filterCyclicTables(generatedTablesGenerators);
|
|
135969
|
-
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
135970
|
-
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, mysqlTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
135971
|
-
};
|
|
135972
|
-
const getMySqlInfo = (mysqlSchema, mysqlTables) => {
|
|
135973
|
-
let tableConfig;
|
|
135974
|
-
let dbToTsColumnNamesMap;
|
|
135975
|
-
const dbToTsTableNamesMap = Object.fromEntries(Object.entries(mysqlTables).map(([key, value]) => [getTableName(value), key]));
|
|
135976
|
-
const tables = [];
|
|
135977
|
-
const relations = [];
|
|
135978
|
-
const dbToTsColumnNamesMapGlobal = {};
|
|
135979
|
-
const tableRelations = {};
|
|
135980
|
-
const getDbToTsColumnNamesMap = (table) => {
|
|
135981
|
-
let dbToTsColumnNamesMap = {};
|
|
135982
|
-
const tableName = getTableName(table);
|
|
135983
|
-
if (Object.hasOwn(dbToTsColumnNamesMapGlobal, tableName)) {
|
|
135984
|
-
dbToTsColumnNamesMap = dbToTsColumnNamesMapGlobal[tableName];
|
|
135985
|
-
return dbToTsColumnNamesMap;
|
|
135986
|
-
}
|
|
135987
|
-
const tableConfig = getTableConfig$1(table);
|
|
135988
|
-
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
135989
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
135990
|
-
}
|
|
135991
|
-
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
135992
|
-
return dbToTsColumnNamesMap;
|
|
135993
|
-
};
|
|
135994
|
-
const transformFromDrizzleRelation = (schema, getDbToTsColumnNamesMap, tableRelations) => {
|
|
135995
|
-
const schemaConfig = extractTablesRelationalConfig(schema, createTableRelationsHelpers);
|
|
135996
|
-
const relations = [];
|
|
135997
|
-
for (const table of Object.values(schemaConfig.tables)) {
|
|
135998
|
-
if (table.relations === undefined)
|
|
135999
|
-
continue;
|
|
136000
|
-
for (const drizzleRel of Object.values(table.relations)) {
|
|
136001
|
-
if (!is(drizzleRel, One))
|
|
136002
|
-
continue;
|
|
136003
|
-
const tableConfig = getTableConfig$1(drizzleRel.sourceTable);
|
|
136004
|
-
const tableDbSchema = tableConfig.schema ?? 'public';
|
|
136005
|
-
const tableDbName = tableConfig.name;
|
|
136006
|
-
const tableTsName = schemaConfig.tableNamesMap[`${tableDbSchema}.${tableDbName}`] ?? tableDbName;
|
|
136007
|
-
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
136008
|
-
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
136009
|
-
?? [];
|
|
136010
|
-
const refTableConfig = getTableConfig$1(drizzleRel.referencedTable);
|
|
136011
|
-
const refTableDbSchema = refTableConfig.schema ?? 'public';
|
|
136012
|
-
const refTableDbName = refTableConfig.name;
|
|
136013
|
-
const refTableTsName = schemaConfig.tableNamesMap[`${refTableDbSchema}.${refTableDbName}`]
|
|
136014
|
-
?? refTableDbName;
|
|
136015
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
136016
|
-
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
136017
|
-
?? [];
|
|
136018
|
-
if (tableRelations[refTableTsName] === undefined) {
|
|
136019
|
-
tableRelations[refTableTsName] = [];
|
|
136020
|
-
}
|
|
136021
|
-
const relation = {
|
|
136022
|
-
table: tableTsName,
|
|
136023
|
-
columns,
|
|
136024
|
-
refTable: refTableTsName,
|
|
136025
|
-
refColumns,
|
|
136026
|
-
refTableRels: tableRelations[refTableTsName],
|
|
136027
|
-
type: 'one',
|
|
136028
|
-
};
|
|
136029
|
-
// do not add duplicate relation
|
|
136030
|
-
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
136031
|
-
&& rel.refTable === relation.refTable)) {
|
|
136032
|
-
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
136033
|
-
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
136034
|
-
+ `In this case, the foreign key constraint will be used.\n`);
|
|
136035
|
-
continue;
|
|
136036
|
-
}
|
|
136037
|
-
relations.push(relation);
|
|
136038
|
-
tableRelations[tableTsName].push(relation);
|
|
136039
|
-
}
|
|
136040
|
-
}
|
|
136041
|
-
return relations;
|
|
136042
|
-
};
|
|
136043
|
-
for (const table of Object.values(mysqlTables)) {
|
|
136044
|
-
tableConfig = getTableConfig$1(table);
|
|
136045
|
-
dbToTsColumnNamesMap = {};
|
|
136046
|
-
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
136047
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
136048
|
-
}
|
|
136049
|
-
const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
136050
|
-
const table = dbToTsTableNamesMap[tableConfig.name];
|
|
136051
|
-
const refTable = dbToTsTableNamesMap[getTableName(fk.reference().foreignTable)];
|
|
136052
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(fk.reference().foreignTable);
|
|
136053
|
-
if (tableRelations[refTable] === undefined) {
|
|
136054
|
-
tableRelations[refTable] = [];
|
|
136055
|
-
}
|
|
136056
|
-
return {
|
|
136057
|
-
table,
|
|
136058
|
-
columns: fk
|
|
136059
|
-
.reference()
|
|
136060
|
-
.columns.map((col) => dbToTsColumnNamesMap[col.name]),
|
|
136061
|
-
refTable,
|
|
136062
|
-
refColumns: fk
|
|
136063
|
-
.reference()
|
|
136064
|
-
.foreignColumns.map((fCol) => dbToTsColumnNamesMapForRefTable[fCol.name]),
|
|
136065
|
-
refTableRels: tableRelations[refTable],
|
|
136066
|
-
};
|
|
136067
|
-
});
|
|
136068
|
-
relations.push(...newRelations);
|
|
136069
|
-
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
136070
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
137545
|
+
else if (is(db, (CockroachDatabase))) {
|
|
137546
|
+
const { cockroachTables } = filterCockroachSchema(schema);
|
|
137547
|
+
if (Object.entries(cockroachTables).length > 0) {
|
|
137548
|
+
await resetCockroach(db, cockroachTables);
|
|
136071
137549
|
}
|
|
136072
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]].push(...newRelations);
|
|
136073
|
-
const getTypeParams = (sqlType) => {
|
|
136074
|
-
// get type params and set only type
|
|
136075
|
-
const typeParams = {};
|
|
136076
|
-
if (sqlType.startsWith('decimal')
|
|
136077
|
-
|| sqlType.startsWith('real')
|
|
136078
|
-
|| sqlType.startsWith('double')
|
|
136079
|
-
|| sqlType.startsWith('float')) {
|
|
136080
|
-
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
136081
|
-
if (match) {
|
|
136082
|
-
typeParams['precision'] = Number(match[1]);
|
|
136083
|
-
typeParams['scale'] = Number(match[2]);
|
|
136084
|
-
}
|
|
136085
|
-
}
|
|
136086
|
-
else if (sqlType.startsWith('char')
|
|
136087
|
-
|| sqlType.startsWith('varchar')
|
|
136088
|
-
|| sqlType.startsWith('binary')
|
|
136089
|
-
|| sqlType.startsWith('varbinary')) {
|
|
136090
|
-
const match = sqlType.match(/\((\d+)\)/);
|
|
136091
|
-
if (match) {
|
|
136092
|
-
typeParams['length'] = Number(match[1]);
|
|
136093
|
-
}
|
|
136094
|
-
}
|
|
136095
|
-
return typeParams;
|
|
136096
|
-
};
|
|
136097
|
-
tables.push({
|
|
136098
|
-
name: dbToTsTableNamesMap[tableConfig.name],
|
|
136099
|
-
columns: tableConfig.columns.map((column) => ({
|
|
136100
|
-
name: dbToTsColumnNamesMap[column.name],
|
|
136101
|
-
columnType: column.getSQLType(),
|
|
136102
|
-
typeParams: getTypeParams(column.getSQLType()),
|
|
136103
|
-
dataType: column.dataType,
|
|
136104
|
-
hasDefault: column.hasDefault,
|
|
136105
|
-
default: column.default,
|
|
136106
|
-
enumValues: column.enumValues,
|
|
136107
|
-
isUnique: column.isUnique,
|
|
136108
|
-
notNull: column.notNull,
|
|
136109
|
-
primary: column.primary,
|
|
136110
|
-
})),
|
|
136111
|
-
primaryKeys: tableConfig.columns
|
|
136112
|
-
.filter((column) => column.primary)
|
|
136113
|
-
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
136114
|
-
});
|
|
136115
137550
|
}
|
|
136116
|
-
|
|
136117
|
-
|
|
136118
|
-
|
|
136119
|
-
|
|
136120
|
-
if (isRelationCyclic(relI)) {
|
|
136121
|
-
tableRel['isCyclic'] = true;
|
|
136122
|
-
return { ...relI, isCyclic: true };
|
|
137551
|
+
else if (is(db, (SingleStoreDatabase))) {
|
|
137552
|
+
const { singleStoreTables } = filterSingleStoreTables(schema);
|
|
137553
|
+
if (Object.entries(singleStoreTables).length > 0) {
|
|
137554
|
+
await resetSingleStore(db, singleStoreTables);
|
|
136123
137555
|
}
|
|
136124
|
-
tableRel['isCyclic'] = false;
|
|
136125
|
-
return { ...relI, isCyclic: false };
|
|
136126
|
-
});
|
|
136127
|
-
return { tables, relations: isCyclicRelations, tableRelations };
|
|
136128
|
-
};
|
|
136129
|
-
// Sqlite------------------------------------------------------------------------------------------------------------------------
|
|
136130
|
-
const resetSqlite = async (db, schema) => {
|
|
136131
|
-
const tablesToTruncate = Object.entries(schema).map(([_tsTableName, table]) => {
|
|
136132
|
-
const dbTableName = getTableName(table);
|
|
136133
|
-
return dbTableName;
|
|
136134
|
-
});
|
|
136135
|
-
await db.run(sql.raw('PRAGMA foreign_keys = OFF'));
|
|
136136
|
-
for (const tableName of tablesToTruncate) {
|
|
136137
|
-
const sqlQuery = `delete from \`${tableName}\`;`;
|
|
136138
|
-
await db.run(sql.raw(sqlQuery));
|
|
136139
137556
|
}
|
|
136140
|
-
|
|
136141
|
-
|
|
136142
|
-
const filterSqliteTables = (schema) => {
|
|
136143
|
-
const sqliteSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], SQLiteTable) || is(keyValue[1], Relations)));
|
|
136144
|
-
const sqliteTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => is(keyValue[1], SQLiteTable)));
|
|
136145
|
-
return { sqliteSchema, sqliteTables };
|
|
136146
|
-
};
|
|
136147
|
-
const seedSqlite = async (db, schema, options = {}, refinements) => {
|
|
136148
|
-
const { sqliteSchema, sqliteTables } = filterSqliteTables(schema);
|
|
136149
|
-
const { tables, relations } = getSqliteInfo(sqliteSchema, sqliteTables);
|
|
136150
|
-
const seedService = new SeedService();
|
|
136151
|
-
const generatedTablesGenerators = seedService.generatePossibleGenerators('sqlite', tables, relations, refinements, options);
|
|
136152
|
-
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
136153
|
-
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, sqliteTables, { ...options, preserveCyclicTablesData });
|
|
136154
|
-
const { filteredTablesGenerators, tablesUniqueNotNullColumn } = seedService.filterCyclicTables(generatedTablesGenerators);
|
|
136155
|
-
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
136156
|
-
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, sqliteTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
136157
|
-
};
|
|
136158
|
-
const getSqliteInfo = (sqliteSchema, sqliteTables) => {
|
|
136159
|
-
let tableConfig;
|
|
136160
|
-
let dbToTsColumnNamesMap;
|
|
136161
|
-
const dbToTsTableNamesMap = Object.fromEntries(Object.entries(sqliteTables).map(([key, value]) => [getTableName(value), key]));
|
|
136162
|
-
const tables = [];
|
|
136163
|
-
const relations = [];
|
|
136164
|
-
const dbToTsColumnNamesMapGlobal = {};
|
|
136165
|
-
const tableRelations = {};
|
|
136166
|
-
const getDbToTsColumnNamesMap = (table) => {
|
|
136167
|
-
let dbToTsColumnNamesMap = {};
|
|
136168
|
-
const tableName = getTableName(table);
|
|
136169
|
-
if (Object.hasOwn(dbToTsColumnNamesMapGlobal, tableName)) {
|
|
136170
|
-
dbToTsColumnNamesMap = dbToTsColumnNamesMapGlobal[tableName];
|
|
136171
|
-
return dbToTsColumnNamesMap;
|
|
136172
|
-
}
|
|
136173
|
-
const tableConfig = getTableConfig$2(table);
|
|
136174
|
-
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
136175
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
136176
|
-
}
|
|
136177
|
-
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
136178
|
-
return dbToTsColumnNamesMap;
|
|
136179
|
-
};
|
|
136180
|
-
const transformFromDrizzleRelation = (schema, getDbToTsColumnNamesMap, tableRelations) => {
|
|
136181
|
-
const schemaConfig = extractTablesRelationalConfig(schema, createTableRelationsHelpers);
|
|
136182
|
-
const relations = [];
|
|
136183
|
-
for (const table of Object.values(schemaConfig.tables)) {
|
|
136184
|
-
if (table.relations === undefined)
|
|
136185
|
-
continue;
|
|
136186
|
-
for (const drizzleRel of Object.values(table.relations)) {
|
|
136187
|
-
if (!is(drizzleRel, One))
|
|
136188
|
-
continue;
|
|
136189
|
-
const tableConfig = getTableConfig$2(drizzleRel.sourceTable);
|
|
136190
|
-
const tableDbName = tableConfig.name;
|
|
136191
|
-
// TODO: tableNamesMap: have {public.customer: 'customer'} structure in sqlite
|
|
136192
|
-
const tableTsName = schemaConfig.tableNamesMap[`public.${tableDbName}`] ?? tableDbName;
|
|
136193
|
-
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
136194
|
-
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
136195
|
-
?? [];
|
|
136196
|
-
const refTableConfig = getTableConfig$2(drizzleRel.referencedTable);
|
|
136197
|
-
const refTableDbName = refTableConfig.name;
|
|
136198
|
-
const refTableTsName = schemaConfig.tableNamesMap[`public.${refTableDbName}`]
|
|
136199
|
-
?? refTableDbName;
|
|
136200
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
136201
|
-
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
136202
|
-
?? [];
|
|
136203
|
-
if (tableRelations[refTableTsName] === undefined) {
|
|
136204
|
-
tableRelations[refTableTsName] = [];
|
|
136205
|
-
}
|
|
136206
|
-
const relation = {
|
|
136207
|
-
table: tableTsName,
|
|
136208
|
-
columns,
|
|
136209
|
-
refTable: refTableTsName,
|
|
136210
|
-
refColumns,
|
|
136211
|
-
refTableRels: tableRelations[refTableTsName],
|
|
136212
|
-
type: 'one',
|
|
136213
|
-
};
|
|
136214
|
-
// do not add duplicate relation
|
|
136215
|
-
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
136216
|
-
&& rel.refTable === relation.refTable)) {
|
|
136217
|
-
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
136218
|
-
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
136219
|
-
+ `In this case, the foreign key constraint will be used.\n`);
|
|
136220
|
-
continue;
|
|
136221
|
-
}
|
|
136222
|
-
relations.push(relation);
|
|
136223
|
-
tableRelations[tableTsName].push(relation);
|
|
136224
|
-
}
|
|
136225
|
-
}
|
|
136226
|
-
return relations;
|
|
136227
|
-
};
|
|
136228
|
-
for (const table of Object.values(sqliteTables)) {
|
|
136229
|
-
tableConfig = getTableConfig$2(table);
|
|
136230
|
-
dbToTsColumnNamesMap = {};
|
|
136231
|
-
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
136232
|
-
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
136233
|
-
}
|
|
136234
|
-
const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
136235
|
-
const table = dbToTsTableNamesMap[tableConfig.name];
|
|
136236
|
-
const refTable = dbToTsTableNamesMap[getTableName(fk.reference().foreignTable)];
|
|
136237
|
-
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(fk.reference().foreignTable);
|
|
136238
|
-
if (tableRelations[refTable] === undefined) {
|
|
136239
|
-
tableRelations[refTable] = [];
|
|
136240
|
-
}
|
|
136241
|
-
return {
|
|
136242
|
-
table,
|
|
136243
|
-
columns: fk
|
|
136244
|
-
.reference()
|
|
136245
|
-
.columns.map((col) => dbToTsColumnNamesMap[col.name]),
|
|
136246
|
-
refTable,
|
|
136247
|
-
refColumns: fk
|
|
136248
|
-
.reference()
|
|
136249
|
-
.foreignColumns.map((fCol) => dbToTsColumnNamesMapForRefTable[fCol.name]),
|
|
136250
|
-
refTableRels: tableRelations[refTable],
|
|
136251
|
-
};
|
|
136252
|
-
});
|
|
136253
|
-
relations.push(...newRelations);
|
|
136254
|
-
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
136255
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
136256
|
-
}
|
|
136257
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]].push(...newRelations);
|
|
136258
|
-
const getTypeParams = (sqlType) => {
|
|
136259
|
-
// get type params and set only type
|
|
136260
|
-
const typeParams = {};
|
|
136261
|
-
if (sqlType.startsWith('decimal')) {
|
|
136262
|
-
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
136263
|
-
if (match) {
|
|
136264
|
-
typeParams['precision'] = Number(match[1]);
|
|
136265
|
-
typeParams['scale'] = Number(match[2]);
|
|
136266
|
-
}
|
|
136267
|
-
}
|
|
136268
|
-
else if (sqlType.startsWith('char')
|
|
136269
|
-
|| sqlType.startsWith('varchar')
|
|
136270
|
-
|| sqlType.startsWith('text')) {
|
|
136271
|
-
const match = sqlType.match(/\((\d+)\)/);
|
|
136272
|
-
if (match) {
|
|
136273
|
-
typeParams['length'] = Number(match[1]);
|
|
136274
|
-
}
|
|
136275
|
-
}
|
|
136276
|
-
return typeParams;
|
|
136277
|
-
};
|
|
136278
|
-
tables.push({
|
|
136279
|
-
name: dbToTsTableNamesMap[tableConfig.name],
|
|
136280
|
-
columns: tableConfig.columns.map((column) => ({
|
|
136281
|
-
name: dbToTsColumnNamesMap[column.name],
|
|
136282
|
-
columnType: column.getSQLType(),
|
|
136283
|
-
typeParams: getTypeParams(column.getSQLType()),
|
|
136284
|
-
dataType: column.dataType,
|
|
136285
|
-
hasDefault: column.hasDefault,
|
|
136286
|
-
default: column.default,
|
|
136287
|
-
enumValues: column.enumValues,
|
|
136288
|
-
isUnique: column.isUnique,
|
|
136289
|
-
notNull: column.notNull,
|
|
136290
|
-
primary: column.primary,
|
|
136291
|
-
})),
|
|
136292
|
-
primaryKeys: tableConfig.columns
|
|
136293
|
-
.filter((column) => column.primary)
|
|
136294
|
-
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
136295
|
-
});
|
|
137557
|
+
else {
|
|
137558
|
+
throw new Error('The drizzle-seed package currently supports only PostgreSQL, MySQL, SQLite, Ms Sql, CockroachDB and SingleStore databases. Please ensure your database is one of these supported types');
|
|
136296
137559
|
}
|
|
136297
|
-
|
|
136298
|
-
relations.push(...transformedDrizzleRelations);
|
|
136299
|
-
const isCyclicRelations = relations.map((relI) => {
|
|
136300
|
-
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
136301
|
-
if (isRelationCyclic(relI)) {
|
|
136302
|
-
tableRel['isCyclic'] = true;
|
|
136303
|
-
return { ...relI, isCyclic: true };
|
|
136304
|
-
}
|
|
136305
|
-
tableRel['isCyclic'] = false;
|
|
136306
|
-
return { ...relI, isCyclic: false };
|
|
136307
|
-
});
|
|
136308
|
-
return { tables, relations: isCyclicRelations, tableRelations };
|
|
136309
|
-
};
|
|
137560
|
+
}
|
|
136310
137561
|
|
|
136311
|
-
export {
|
|
137562
|
+
export { SeedService, cityNames as cities, countries, firstNames, getGeneratorsFunctions, lastNames, reset, seed, seedForDrizzleStudio };
|
|
136312
137563
|
//# sourceMappingURL=index.mjs.map
|