drizzle-seed 0.3.2-b5a9650 → 0.4.0-08bb2d5
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 -35
- package/{services/SeedService.d.mts → SeedService.d.mts} +18 -35
- package/{services/SeedService.d.ts → SeedService.d.ts} +18 -35
- package/cockroach-core/index.d.cts +24 -0
- package/cockroach-core/index.d.mts +24 -0
- package/cockroach-core/index.d.ts +24 -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/{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 +149 -23
- package/{services → generators}/Generators.d.mts +149 -23
- package/{services → generators}/Generators.d.ts +149 -23
- 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 +1 -1
- package/{services → generators}/versioning/v2.d.mts +1 -1
- package/{services → generators}/versioning/v2.d.ts +1 -1
- package/index.cjs +2800 -1060
- package/index.cjs.map +1 -1
- package/index.d.cts +136 -81
- package/index.d.mts +136 -81
- package/index.d.ts +136 -81
- package/index.mjs +2813 -1072
- 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 +24 -0
- package/mysql-core/index.d.mts +24 -0
- package/mysql-core/index.d.ts +24 -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 +6 -2
- package/pg-core/index.d.cts +24 -0
- package/pg-core/index.d.mts +24 -0
- package/pg-core/index.d.ts +24 -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 +24 -0
- package/singlestore-core/index.d.mts +24 -0
- package/singlestore-core/index.d.ts +24 -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 +24 -0
- package/sqlite-core/index.d.mts +24 -0
- package/sqlite-core/index.d.ts +24 -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 +2 -0
- package/types/tables.d.mts +2 -0
- package/types/tables.d.ts +2 -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 -27
- package/services/utils.d.mts +0 -27
- package/services/utils.d.ts +0 -27
- /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.cjs
CHANGED
|
@@ -4,6 +4,9 @@ var drizzleOrm = require('drizzle-orm');
|
|
|
4
4
|
var mysqlCore = require('drizzle-orm/mysql-core');
|
|
5
5
|
var pgCore = require('drizzle-orm/pg-core');
|
|
6
6
|
var sqliteCore = require('drizzle-orm/sqlite-core');
|
|
7
|
+
var mssqlCore = require('drizzle-orm/mssql-core');
|
|
8
|
+
var cockroachCore = require('drizzle-orm/cockroach-core');
|
|
9
|
+
var singlestoreCore = require('drizzle-orm/singlestore-core');
|
|
7
10
|
var prand = require('pure-rand');
|
|
8
11
|
|
|
9
12
|
/**
|
|
@@ -131132,18 +131135,60 @@ var streetSuffix = [
|
|
|
131132
131135
|
];
|
|
131133
131136
|
const maxStringLength = 10;
|
|
131134
131137
|
|
|
131138
|
+
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
131135
131139
|
const fastCartesianProduct = (sets, index) => {
|
|
131136
131140
|
const resultList = [];
|
|
131137
131141
|
let currSet;
|
|
131138
131142
|
let element;
|
|
131139
131143
|
for (let i = sets.length - 1; i >= 0; i--) {
|
|
131140
131144
|
currSet = sets[i];
|
|
131141
|
-
element = currSet[index % currSet.length];
|
|
131145
|
+
element = currSet[index % Number(currSet.length)];
|
|
131142
131146
|
resultList.unshift(element);
|
|
131143
|
-
index = Math.floor(index / currSet.length);
|
|
131147
|
+
index = Math.floor(index / Number(currSet.length));
|
|
131144
131148
|
}
|
|
131145
131149
|
return resultList;
|
|
131146
131150
|
};
|
|
131151
|
+
const fastCartesianProductForBigint = (sets, index) => {
|
|
131152
|
+
const resultList = [];
|
|
131153
|
+
let currSet;
|
|
131154
|
+
let element;
|
|
131155
|
+
for (let i = sets.length - 1; i >= 0; i--) {
|
|
131156
|
+
currSet = sets[i];
|
|
131157
|
+
const remainder = index % BigInt(currSet.length);
|
|
131158
|
+
// TODO check how it works
|
|
131159
|
+
// remainder = remainder <= Number.MAX_SAFE_INTEGER ? Number(remainder) : remainder;
|
|
131160
|
+
element = currSet[remainder];
|
|
131161
|
+
resultList.unshift(element);
|
|
131162
|
+
index = index / BigInt(currSet.length);
|
|
131163
|
+
}
|
|
131164
|
+
return resultList;
|
|
131165
|
+
};
|
|
131166
|
+
class OrderedNumberRange {
|
|
131167
|
+
min;
|
|
131168
|
+
max;
|
|
131169
|
+
step;
|
|
131170
|
+
length;
|
|
131171
|
+
constructor(min, max, step) {
|
|
131172
|
+
this.min = min;
|
|
131173
|
+
this.max = max;
|
|
131174
|
+
this.step = step;
|
|
131175
|
+
this.length = Math.floor((this.max - this.min) / this.step) + 1;
|
|
131176
|
+
const handler = {
|
|
131177
|
+
get(target, prop, receiver) {
|
|
131178
|
+
if (typeof prop === 'string' && /^\d+$/.test(prop)) {
|
|
131179
|
+
const idx = Number(prop);
|
|
131180
|
+
if (idx >= target.length)
|
|
131181
|
+
return undefined;
|
|
131182
|
+
return (target.min + idx * target.step);
|
|
131183
|
+
}
|
|
131184
|
+
// fallback to normal lookup (and TS knows this has the right signature)
|
|
131185
|
+
return Reflect.get(target, prop, receiver);
|
|
131186
|
+
},
|
|
131187
|
+
};
|
|
131188
|
+
return new Proxy(this, handler);
|
|
131189
|
+
}
|
|
131190
|
+
}
|
|
131191
|
+
const abs = (n) => (n < 0n) ? -n : n;
|
|
131147
131192
|
const sumArray = (weights) => {
|
|
131148
131193
|
const scale = 1e10;
|
|
131149
131194
|
const scaledSum = weights.reduce((acc, currVal) => acc + Math.round(currVal * scale), 0);
|
|
@@ -131167,16 +131212,6 @@ const getWeightedIndices = (weights, accuracy = 100) => {
|
|
|
131167
131212
|
}
|
|
131168
131213
|
return weightedIndices;
|
|
131169
131214
|
};
|
|
131170
|
-
const generateHashFromString = (s) => {
|
|
131171
|
-
let hash = 0;
|
|
131172
|
-
// p and m are prime numbers
|
|
131173
|
-
const p = 53;
|
|
131174
|
-
const m = 28871271685163;
|
|
131175
|
-
for (let i = 0; i < s.length; i++) {
|
|
131176
|
-
hash += ((s.codePointAt(i) || 0) * Math.pow(p, i)) % m;
|
|
131177
|
-
}
|
|
131178
|
-
return hash;
|
|
131179
|
-
};
|
|
131180
131215
|
/**
|
|
131181
131216
|
* @param param0.template example: "#####" or "#####-####"
|
|
131182
131217
|
* @param param0.values example: ["3", "2", "h"]
|
|
@@ -131213,23 +131248,27 @@ const isObject = (value) => {
|
|
|
131213
131248
|
return true;
|
|
131214
131249
|
return false;
|
|
131215
131250
|
};
|
|
131216
|
-
const
|
|
131217
|
-
|
|
131218
|
-
|
|
131219
|
-
|
|
131220
|
-
|
|
131221
|
-
|
|
131222
|
-
|
|
131223
|
-
|
|
131224
|
-
|
|
131225
|
-
|
|
131226
|
-
|
|
131227
|
-
|
|
131228
|
-
|
|
131229
|
-
|
|
131230
|
-
|
|
131231
|
-
|
|
131232
|
-
|
|
131251
|
+
// const main = () => {
|
|
131252
|
+
// console.time('range');
|
|
131253
|
+
// const range = new OrderedBigintRange(BigInt(-10), BigInt(10), BigInt(1));
|
|
131254
|
+
// console.log(range.length);
|
|
131255
|
+
// for (let i = 0; i < Number(range.length) + 1; i++) {
|
|
131256
|
+
// console.log(range[i]);
|
|
131257
|
+
// }
|
|
131258
|
+
// console.timeEnd('range');
|
|
131259
|
+
// const list = Array.from({ length: 2e6 + 1 }, (_, idx) => idx);
|
|
131260
|
+
// console.time('list');
|
|
131261
|
+
// console.log(list.length);
|
|
131262
|
+
// for (let i = 0; i < 2e6 + 1; i++) {
|
|
131263
|
+
// list[i];
|
|
131264
|
+
// }
|
|
131265
|
+
// console.timeEnd('list');
|
|
131266
|
+
// // const n = 5;
|
|
131267
|
+
// // for (let i = 0; i < n; i++) {
|
|
131268
|
+
// // console.log(fastCartesianProduct([[1, 2], [1, 2]], i));
|
|
131269
|
+
// // }
|
|
131270
|
+
// };
|
|
131271
|
+
// main();
|
|
131233
131272
|
|
|
131234
131273
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
131235
131274
|
class AbstractGenerator {
|
|
@@ -131245,10 +131284,11 @@ class AbstractGenerator {
|
|
|
131245
131284
|
arraySize;
|
|
131246
131285
|
baseColumnDataType;
|
|
131247
131286
|
// param for text-like generators
|
|
131248
|
-
stringLength;
|
|
131287
|
+
// public stringLength?: number;
|
|
131249
131288
|
// params for GenerateValuesFromArray
|
|
131250
131289
|
weightedCountSeed;
|
|
131251
131290
|
maxRepeatedValuesCount;
|
|
131291
|
+
typeParams = {};
|
|
131252
131292
|
params;
|
|
131253
131293
|
constructor(params) {
|
|
131254
131294
|
this.params = params === undefined ? {} : params;
|
|
@@ -131280,6 +131320,7 @@ class AbstractGenerator {
|
|
|
131280
131320
|
});
|
|
131281
131321
|
uniqueGen.isUnique = this.isUnique;
|
|
131282
131322
|
uniqueGen.dataType = this.dataType;
|
|
131323
|
+
uniqueGen.typeParams = this.typeParams;
|
|
131283
131324
|
return uniqueGen;
|
|
131284
131325
|
}
|
|
131285
131326
|
return;
|
|
@@ -131290,10 +131331,13 @@ class AbstractGenerator {
|
|
|
131290
131331
|
const uniqueGen = this.replaceIfUnique();
|
|
131291
131332
|
const baseColumnGen = uniqueGen === undefined ? this : uniqueGen;
|
|
131292
131333
|
baseColumnGen.dataType = this.baseColumnDataType;
|
|
131334
|
+
const { dimensions, ...rest } = baseColumnGen.typeParams;
|
|
131335
|
+
baseColumnGen.typeParams = rest;
|
|
131293
131336
|
const arrayGen = new GenerateArray({
|
|
131294
131337
|
baseColumnGen,
|
|
131295
131338
|
size: this.arraySize,
|
|
131296
131339
|
});
|
|
131340
|
+
arrayGen.typeParams = { dimensions };
|
|
131297
131341
|
return arrayGen;
|
|
131298
131342
|
}
|
|
131299
131343
|
return;
|
|
@@ -131548,6 +131592,7 @@ class GenerateNumber extends AbstractGenerator {
|
|
|
131548
131592
|
static entityKind = 'GenerateNumber';
|
|
131549
131593
|
state;
|
|
131550
131594
|
uniqueVersionOfGen = GenerateUniqueNumber;
|
|
131595
|
+
// TODO rewrite precision to decimalPlaces
|
|
131551
131596
|
init({ count, seed }) {
|
|
131552
131597
|
super.init({ count, seed });
|
|
131553
131598
|
let { minValue, maxValue, precision } = this.params;
|
|
@@ -131801,20 +131846,13 @@ class GenerateDate extends AbstractGenerator {
|
|
|
131801
131846
|
const rng = prand.xoroshiro128plus(seed);
|
|
131802
131847
|
let { minDate, maxDate } = this.params;
|
|
131803
131848
|
const anchorDate = new Date('2024-05-08');
|
|
131804
|
-
// 4 years in milliseconds
|
|
131805
131849
|
const deltaMilliseconds = 4 * 31536000000;
|
|
131806
131850
|
if (typeof minDate === 'string') {
|
|
131807
131851
|
minDate = new Date(minDate);
|
|
131808
131852
|
}
|
|
131809
|
-
if (typeof minDate === 'object' && !isValidDate(minDate)) {
|
|
131810
|
-
throw new Error('Invalid Date was provided for the minDate parameter.');
|
|
131811
|
-
}
|
|
131812
131853
|
if (typeof maxDate === 'string') {
|
|
131813
131854
|
maxDate = new Date(maxDate);
|
|
131814
131855
|
}
|
|
131815
|
-
if (typeof maxDate === 'object' && !isValidDate(maxDate)) {
|
|
131816
|
-
throw new Error('Invalid Date was provided for the maxDate parameter.');
|
|
131817
|
-
}
|
|
131818
131856
|
if (minDate === undefined) {
|
|
131819
131857
|
if (maxDate === undefined) {
|
|
131820
131858
|
minDate = new Date(anchorDate.getTime() - deltaMilliseconds);
|
|
@@ -131827,9 +131865,6 @@ class GenerateDate extends AbstractGenerator {
|
|
|
131827
131865
|
if (maxDate === undefined) {
|
|
131828
131866
|
maxDate = new Date(minDate.getTime() + (2 * deltaMilliseconds));
|
|
131829
131867
|
}
|
|
131830
|
-
if (minDate > maxDate) {
|
|
131831
|
-
throw new Error(`The minDate parameter must be less than or equal to the maxDate parameter.`);
|
|
131832
|
-
}
|
|
131833
131868
|
this.state = { rng, minDate, maxDate };
|
|
131834
131869
|
}
|
|
131835
131870
|
generate() {
|
|
@@ -131851,83 +131886,18 @@ class GenerateTime extends AbstractGenerator {
|
|
|
131851
131886
|
init({ count, seed }) {
|
|
131852
131887
|
super.init({ count, seed });
|
|
131853
131888
|
const rng = prand.xoroshiro128plus(seed);
|
|
131854
|
-
|
|
131855
|
-
if (minTime === undefined && maxTime === undefined) {
|
|
131856
|
-
// TODO: maybe need to change in major version release
|
|
131857
|
-
// This is required to ensure that this generator remains deterministic when used without minTime, maxTime parameters.
|
|
131858
|
-
const oneDayInMilliseconds = 86400000;
|
|
131859
|
-
minTime = new Date(new Date('2024-05-08T12:00:00.000Z').getTime() - oneDayInMilliseconds);
|
|
131860
|
-
maxTime = new Date(new Date('2024-05-08T12:00:00.000Z').getTime() + oneDayInMilliseconds);
|
|
131861
|
-
this.state = { rng, minTime, maxTime };
|
|
131862
|
-
return;
|
|
131863
|
-
}
|
|
131864
|
-
if (minTime === undefined) {
|
|
131865
|
-
if (maxTime === undefined) {
|
|
131866
|
-
minTime = '00:00:00.000Z';
|
|
131867
|
-
maxTime = '23:59:59.999Z';
|
|
131868
|
-
}
|
|
131869
|
-
else {
|
|
131870
|
-
minTime = '00:00:00.000Z';
|
|
131871
|
-
}
|
|
131872
|
-
}
|
|
131873
|
-
if (maxTime === undefined) {
|
|
131874
|
-
maxTime = '23:59:59.999Z';
|
|
131875
|
-
new Date().toISOString();
|
|
131876
|
-
}
|
|
131877
|
-
const anchorDate = new Date('2024-05-08');
|
|
131878
|
-
const anchorDateString0 = anchorDate.toISOString().replace(/T\d{2}:\d{2}:\d{2}.\d{3}Z/, '');
|
|
131879
|
-
if (typeof minTime === 'string') {
|
|
131880
|
-
// const timeMatch0 = minTime.match(/^\d{2}:\d{2}:\d{2}.\d{1,3}Z?$/);
|
|
131881
|
-
const timeMatch1 = minTime.match(/^\d{2}:\d{2}:\d{2}Z?$/);
|
|
131882
|
-
const timeMatch2 = minTime.match(/^\d{2}:\d{2}Z?$/);
|
|
131883
|
-
if (
|
|
131884
|
-
// timeMatch0 === null
|
|
131885
|
-
timeMatch1 === null
|
|
131886
|
-
&& timeMatch2 === null) {
|
|
131887
|
-
throw new Error(`You're using the wrong format for the minTime parameter.`
|
|
131888
|
-
+ `\nPlease use one of these formats: 'HH:mm:ss', 'HH:mm' (with or without a trailing 'Z')`);
|
|
131889
|
-
}
|
|
131890
|
-
minTime = minTime.at(-1) === 'Z' ? minTime : minTime + 'Z';
|
|
131891
|
-
minTime = new Date(anchorDate.toISOString().replace(/\d{2}:\d{2}:\d{2}.\d{3}Z/, minTime));
|
|
131892
|
-
}
|
|
131893
|
-
if (typeof minTime === 'object') {
|
|
131894
|
-
if (!isValidDate(minTime)) {
|
|
131895
|
-
throw new Error('Invalid Date was provided for the minTime parameter.');
|
|
131896
|
-
}
|
|
131897
|
-
minTime = new Date(minTime.toISOString().replace(/\d{4}-\d{2}-\d{2}/, anchorDateString0));
|
|
131898
|
-
}
|
|
131899
|
-
if (typeof maxTime === 'string') {
|
|
131900
|
-
// const timeMatch0 = maxTime.match(/^\d{2}:\d{2}:\d{2}.\d{1,3}Z?$/);
|
|
131901
|
-
const timeMatch1 = maxTime.match(/^\d{2}:\d{2}:\d{2}Z?$/);
|
|
131902
|
-
const timeMatch2 = maxTime.match(/^\d{2}:\d{2}Z?$/);
|
|
131903
|
-
if (
|
|
131904
|
-
// timeMatch0 === null
|
|
131905
|
-
timeMatch1 === null
|
|
131906
|
-
&& timeMatch2 === null) {
|
|
131907
|
-
throw new Error(`You're using the wrong format for the maxTime parameter.`
|
|
131908
|
-
+ `\nPlease use one of these formats: 'HH:mm:ss', 'HH:mm' (with or without a trailing 'Z').`);
|
|
131909
|
-
}
|
|
131910
|
-
maxTime = maxTime.at(-1) === 'Z' ? maxTime : maxTime + 'Z';
|
|
131911
|
-
maxTime = new Date(anchorDate.toISOString().replace(/\d{2}:\d{2}:\d{2}.\d{3}Z/, maxTime));
|
|
131912
|
-
}
|
|
131913
|
-
if (typeof maxTime === 'object') {
|
|
131914
|
-
if (!isValidDate(maxTime)) {
|
|
131915
|
-
throw new Error('Invalid Date was provided for the maxTime parameter.');
|
|
131916
|
-
}
|
|
131917
|
-
maxTime = new Date(maxTime.toISOString().replace(/\d{4}-\d{2}-\d{2}/, anchorDateString0));
|
|
131918
|
-
}
|
|
131919
|
-
if (minTime > maxTime) {
|
|
131920
|
-
throw new Error(`The minTime parameter must be less than or equal to the maxTime parameter.`);
|
|
131921
|
-
}
|
|
131922
|
-
this.state = { rng, minTime, maxTime };
|
|
131889
|
+
this.state = { rng };
|
|
131923
131890
|
}
|
|
131924
131891
|
generate() {
|
|
131925
131892
|
if (this.state === undefined) {
|
|
131926
131893
|
throw new Error('state is not defined.');
|
|
131927
131894
|
}
|
|
131895
|
+
const anchorDateTime = new Date('2024-05-08T12:00:00.000Z');
|
|
131896
|
+
const oneDayInMilliseconds = 86400000;
|
|
131897
|
+
let date = new Date();
|
|
131928
131898
|
let milliseconds;
|
|
131929
|
-
[milliseconds, this.state.rng] = prand.uniformIntDistribution(
|
|
131930
|
-
|
|
131899
|
+
[milliseconds, this.state.rng] = prand.uniformIntDistribution(-oneDayInMilliseconds, oneDayInMilliseconds, this.state.rng);
|
|
131900
|
+
date = new Date(date.setTime(anchorDateTime.getTime() + milliseconds));
|
|
131931
131901
|
return date.toISOString().replace(/(\d{4}-\d{2}-\d{2}T)|(\.\d{3}Z)/g, '');
|
|
131932
131902
|
}
|
|
131933
131903
|
}
|
|
@@ -131937,46 +131907,18 @@ class GenerateTimestamp extends AbstractGenerator {
|
|
|
131937
131907
|
init({ count, seed }) {
|
|
131938
131908
|
super.init({ count, seed });
|
|
131939
131909
|
const rng = prand.xoroshiro128plus(seed);
|
|
131940
|
-
|
|
131941
|
-
const anchorDate = new Date('2024-05-08');
|
|
131942
|
-
// 2 years in milliseconds
|
|
131943
|
-
const deltaMilliseconds = 2 * 31536000000;
|
|
131944
|
-
if (typeof minTimestamp === 'string') {
|
|
131945
|
-
minTimestamp = new Date(minTimestamp);
|
|
131946
|
-
}
|
|
131947
|
-
if (typeof minTimestamp === 'object' && !isValidDate(minTimestamp)) {
|
|
131948
|
-
throw new Error('Invalid Date was provided for the minTimestamp parameter.');
|
|
131949
|
-
}
|
|
131950
|
-
if (typeof maxTimestamp === 'string') {
|
|
131951
|
-
maxTimestamp = new Date(maxTimestamp);
|
|
131952
|
-
}
|
|
131953
|
-
if (typeof maxTimestamp === 'object' && !isValidDate(maxTimestamp)) {
|
|
131954
|
-
throw new Error('Invalid Date was provided for the maxTimestamp parameter.');
|
|
131955
|
-
}
|
|
131956
|
-
if (minTimestamp === undefined) {
|
|
131957
|
-
if (maxTimestamp === undefined) {
|
|
131958
|
-
minTimestamp = new Date(anchorDate.getTime() - deltaMilliseconds);
|
|
131959
|
-
maxTimestamp = new Date(anchorDate.getTime() + deltaMilliseconds);
|
|
131960
|
-
}
|
|
131961
|
-
else {
|
|
131962
|
-
minTimestamp = new Date(maxTimestamp.getTime() - (2 * deltaMilliseconds));
|
|
131963
|
-
}
|
|
131964
|
-
}
|
|
131965
|
-
if (maxTimestamp === undefined) {
|
|
131966
|
-
maxTimestamp = new Date(minTimestamp.getTime() + (2 * deltaMilliseconds));
|
|
131967
|
-
}
|
|
131968
|
-
if (minTimestamp > maxTimestamp) {
|
|
131969
|
-
throw new Error(`The minTimestamp parameter must be less than or equal to the maxTimestamp parameter.`);
|
|
131970
|
-
}
|
|
131971
|
-
this.state = { rng, minTimestamp, maxTimestamp };
|
|
131910
|
+
this.state = { rng };
|
|
131972
131911
|
}
|
|
131973
131912
|
generate() {
|
|
131974
131913
|
if (this.state === undefined) {
|
|
131975
131914
|
throw new Error('state is not defined.');
|
|
131976
131915
|
}
|
|
131916
|
+
const anchorTimestamp = new Date('2024-05-08');
|
|
131917
|
+
const twoYearsInMilliseconds = 2 * 31536000000;
|
|
131918
|
+
let date = new Date();
|
|
131977
131919
|
let milliseconds;
|
|
131978
|
-
[milliseconds, this.state.rng] = prand.uniformIntDistribution(
|
|
131979
|
-
|
|
131920
|
+
[milliseconds, this.state.rng] = prand.uniformIntDistribution(-twoYearsInMilliseconds, twoYearsInMilliseconds, this.state.rng);
|
|
131921
|
+
date = new Date(date.setTime(anchorTimestamp.getTime() + milliseconds));
|
|
131980
131922
|
if (this.dataType === 'string') {
|
|
131981
131923
|
return date
|
|
131982
131924
|
.toISOString()
|
|
@@ -131992,46 +131934,18 @@ class GenerateDatetime extends AbstractGenerator {
|
|
|
131992
131934
|
init({ count, seed }) {
|
|
131993
131935
|
super.init({ count, seed });
|
|
131994
131936
|
const rng = prand.xoroshiro128plus(seed);
|
|
131995
|
-
|
|
131996
|
-
const anchorDate = new Date('2024-05-08');
|
|
131997
|
-
// 2 years in milliseconds
|
|
131998
|
-
const deltaMilliseconds = 2 * 31536000000;
|
|
131999
|
-
if (typeof minDatetime === 'string') {
|
|
132000
|
-
minDatetime = new Date(minDatetime);
|
|
132001
|
-
}
|
|
132002
|
-
if (typeof minDatetime === 'object' && !isValidDate(minDatetime)) {
|
|
132003
|
-
throw new Error('Invalid Date was provided for the minDatetime parameter.');
|
|
132004
|
-
}
|
|
132005
|
-
if (typeof maxDatetime === 'string') {
|
|
132006
|
-
maxDatetime = new Date(maxDatetime);
|
|
132007
|
-
}
|
|
132008
|
-
if (typeof maxDatetime === 'object' && !isValidDate(maxDatetime)) {
|
|
132009
|
-
throw new Error('Invalid Date was provided for the maxDatetime parameter.');
|
|
132010
|
-
}
|
|
132011
|
-
if (minDatetime === undefined) {
|
|
132012
|
-
if (maxDatetime === undefined) {
|
|
132013
|
-
minDatetime = new Date(anchorDate.getTime() - deltaMilliseconds);
|
|
132014
|
-
maxDatetime = new Date(anchorDate.getTime() + deltaMilliseconds);
|
|
132015
|
-
}
|
|
132016
|
-
else {
|
|
132017
|
-
minDatetime = new Date(maxDatetime.getTime() - (2 * deltaMilliseconds));
|
|
132018
|
-
}
|
|
132019
|
-
}
|
|
132020
|
-
if (maxDatetime === undefined) {
|
|
132021
|
-
maxDatetime = new Date(minDatetime.getTime() + (2 * deltaMilliseconds));
|
|
132022
|
-
}
|
|
132023
|
-
if (minDatetime > maxDatetime) {
|
|
132024
|
-
throw new Error(`The minDatetime parameter must be less than or equal to the maxDatetime parameter.`);
|
|
132025
|
-
}
|
|
132026
|
-
this.state = { rng, minDatetime, maxDatetime };
|
|
131937
|
+
this.state = { rng };
|
|
132027
131938
|
}
|
|
132028
131939
|
generate() {
|
|
132029
131940
|
if (this.state === undefined) {
|
|
132030
131941
|
throw new Error('state is not defined.');
|
|
132031
131942
|
}
|
|
131943
|
+
const anchorDate = new Date('2024-05-08');
|
|
131944
|
+
const twoYearsInMilliseconds = 2 * 31536000000;
|
|
131945
|
+
let date = new Date();
|
|
132032
131946
|
let milliseconds;
|
|
132033
|
-
[milliseconds, this.state.rng] = prand.uniformIntDistribution(
|
|
132034
|
-
|
|
131947
|
+
[milliseconds, this.state.rng] = prand.uniformIntDistribution(-twoYearsInMilliseconds, twoYearsInMilliseconds, this.state.rng);
|
|
131948
|
+
date = new Date(date.setTime(anchorDate.getTime() + milliseconds));
|
|
132035
131949
|
if (this.dataType === 'string') {
|
|
132036
131950
|
return date
|
|
132037
131951
|
.toISOString()
|
|
@@ -132387,8 +132301,8 @@ class GenerateFirstName extends AbstractGenerator {
|
|
|
132387
132301
|
init({ count, seed }) {
|
|
132388
132302
|
super.init({ count, seed });
|
|
132389
132303
|
const rng = prand.xoroshiro128plus(seed);
|
|
132390
|
-
if (this.
|
|
132391
|
-
throw new Error(`You can't use first name generator with a db column length restriction of ${this.
|
|
132304
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$5) {
|
|
132305
|
+
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}.`);
|
|
132392
132306
|
}
|
|
132393
132307
|
this.state = { rng };
|
|
132394
132308
|
}
|
|
@@ -132411,8 +132325,8 @@ class GenerateUniqueFirstName extends AbstractGenerator {
|
|
|
132411
132325
|
if (count > firstNames.length) {
|
|
132412
132326
|
throw new Error('count exceeds max number of unique first names.');
|
|
132413
132327
|
}
|
|
132414
|
-
if (this.
|
|
132415
|
-
throw new Error(`You can't use first name generator with a db column length restriction of ${this.
|
|
132328
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$5) {
|
|
132329
|
+
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}.`);
|
|
132416
132330
|
}
|
|
132417
132331
|
const genIndicesObj = new GenerateUniqueInt({ minValue: 0, maxValue: firstNames.length - 1 });
|
|
132418
132332
|
genIndicesObj.init({ count, seed });
|
|
@@ -132435,8 +132349,8 @@ class GenerateLastName extends AbstractGenerator {
|
|
|
132435
132349
|
init({ count, seed }) {
|
|
132436
132350
|
super.init({ count, seed });
|
|
132437
132351
|
const rng = prand.xoroshiro128plus(seed);
|
|
132438
|
-
if (this.
|
|
132439
|
-
throw new Error(`You can't use last name generator with a db column length restriction of ${this.
|
|
132352
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$3) {
|
|
132353
|
+
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}.`);
|
|
132440
132354
|
}
|
|
132441
132355
|
this.state = { rng };
|
|
132442
132356
|
}
|
|
@@ -132457,8 +132371,8 @@ class GenerateUniqueLastName extends AbstractGenerator {
|
|
|
132457
132371
|
if (count > lastNames.length) {
|
|
132458
132372
|
throw new Error('count exceeds max number of unique last names.');
|
|
132459
132373
|
}
|
|
132460
|
-
if (this.
|
|
132461
|
-
throw new Error(`You can't use last name generator with a db column length restriction of ${this.
|
|
132374
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$3) {
|
|
132375
|
+
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}.`);
|
|
132462
132376
|
}
|
|
132463
132377
|
const genIndicesObj = new GenerateUniqueInt({ minValue: 0, maxValue: lastNames.length - 1 });
|
|
132464
132378
|
genIndicesObj.init({ count, seed });
|
|
@@ -132480,8 +132394,8 @@ class GenerateFullName extends AbstractGenerator {
|
|
|
132480
132394
|
init({ count, seed }) {
|
|
132481
132395
|
super.init({ count, seed });
|
|
132482
132396
|
const rng = prand.xoroshiro128plus(seed);
|
|
132483
|
-
if (this.
|
|
132484
|
-
throw new Error(`You can't use full name generator with a db column length restriction of ${this.
|
|
132397
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < (maxStringLength$5 + maxStringLength$3 + 1)) {
|
|
132398
|
+
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}.`);
|
|
132485
132399
|
}
|
|
132486
132400
|
this.state = { rng };
|
|
132487
132401
|
}
|
|
@@ -132509,8 +132423,8 @@ class GenerateUniqueFullName extends AbstractGenerator {
|
|
|
132509
132423
|
if (count > maxUniqueFullNamesNumber) {
|
|
132510
132424
|
throw new RangeError(`count exceeds max number of unique full names(${maxUniqueFullNamesNumber}).`);
|
|
132511
132425
|
}
|
|
132512
|
-
if (this.
|
|
132513
|
-
throw new Error(`You can't use full name generator with a db column length restriction of ${this.
|
|
132426
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < (maxStringLength$5 + maxStringLength$3 + 1)) {
|
|
132427
|
+
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}.`);
|
|
132514
132428
|
}
|
|
132515
132429
|
const rng = prand.xoroshiro128plus(seed);
|
|
132516
132430
|
const fullnameSet = new Set();
|
|
@@ -132553,8 +132467,8 @@ class GenerateEmail extends AbstractGenerator {
|
|
|
132553
132467
|
throw new RangeError(`count exceeds max number of unique emails(${maxUniqueEmailsNumber}).`);
|
|
132554
132468
|
}
|
|
132555
132469
|
const maxEmailLength = maxStringLength$a + maxStringLength$5 + maxStringLength$6 + 2;
|
|
132556
|
-
if (this.
|
|
132557
|
-
throw new Error(`You can't use email generator with a db column length restriction of ${this.
|
|
132470
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxEmailLength) {
|
|
132471
|
+
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}.`);
|
|
132558
132472
|
}
|
|
132559
132473
|
const arraysToGenerateFrom = [adjectivesArray, namesArray, domainsArray];
|
|
132560
132474
|
const genIndicesObj = new GenerateUniqueInt({
|
|
@@ -132587,8 +132501,8 @@ class GeneratePhoneNumber extends AbstractGenerator {
|
|
|
132587
132501
|
const { prefixes, template } = this.params;
|
|
132588
132502
|
const rng = prand.xoroshiro128plus(seed);
|
|
132589
132503
|
if (template !== undefined) {
|
|
132590
|
-
if (this.
|
|
132591
|
-
throw new Error(`Length of phone number template is shorter than db column length restriction: ${this.
|
|
132504
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < template.length) {
|
|
132505
|
+
throw new Error(`Length of phone number template is shorter than db column length restriction: ${this.typeParams?.length}.
|
|
132592
132506
|
Set the maximum string length to at least ${template.length}.`);
|
|
132593
132507
|
}
|
|
132594
132508
|
const iterArray = [...template.matchAll(/#/g)];
|
|
@@ -132633,8 +132547,8 @@ class GeneratePhoneNumber extends AbstractGenerator {
|
|
|
132633
132547
|
}
|
|
132634
132548
|
const maxPrefixLength = Math.max(...prefixesArray.map((prefix) => prefix.length));
|
|
132635
132549
|
const maxGeneratedDigits = Math.max(...generatedDigitsNumbers);
|
|
132636
|
-
if (this.
|
|
132637
|
-
throw new Error(`You can't use phone number generator with a db column length restriction of ${this.
|
|
132550
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < (maxPrefixLength + maxGeneratedDigits)) {
|
|
132551
|
+
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}.`);
|
|
132638
132552
|
}
|
|
132639
132553
|
if (new Set(prefixesArray).size !== prefixesArray.length) {
|
|
132640
132554
|
throw new Error('prefixes are not unique.');
|
|
@@ -132711,8 +132625,8 @@ class GenerateCountry extends AbstractGenerator {
|
|
|
132711
132625
|
init({ count, seed }) {
|
|
132712
132626
|
super.init({ count, seed });
|
|
132713
132627
|
const rng = prand.xoroshiro128plus(seed);
|
|
132714
|
-
if (this.
|
|
132715
|
-
throw new Error(`You can't use country generator with a db column length restriction of ${this.
|
|
132628
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$7) {
|
|
132629
|
+
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}.`);
|
|
132716
132630
|
}
|
|
132717
132631
|
this.state = { rng };
|
|
132718
132632
|
}
|
|
@@ -132734,8 +132648,8 @@ class GenerateUniqueCountry extends AbstractGenerator {
|
|
|
132734
132648
|
if (count > countries.length) {
|
|
132735
132649
|
throw new Error('count exceeds max number of unique countries.');
|
|
132736
132650
|
}
|
|
132737
|
-
if (this.
|
|
132738
|
-
throw new Error(`You can't use country generator with a db column length restriction of ${this.
|
|
132651
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$7) {
|
|
132652
|
+
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}.`);
|
|
132739
132653
|
}
|
|
132740
132654
|
const genIndicesObj = new GenerateUniqueInt({ minValue: 0, maxValue: countries.length - 1 });
|
|
132741
132655
|
genIndicesObj.init({ count, seed });
|
|
@@ -132756,8 +132670,8 @@ class GenerateJobTitle extends AbstractGenerator {
|
|
|
132756
132670
|
init({ count, seed }) {
|
|
132757
132671
|
super.init({ count, seed });
|
|
132758
132672
|
const rng = prand.xoroshiro128plus(seed);
|
|
132759
|
-
if (this.
|
|
132760
|
-
throw new Error(`You can't use job title generator with a db column length restriction of ${this.
|
|
132673
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$4) {
|
|
132674
|
+
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}.`);
|
|
132761
132675
|
}
|
|
132762
132676
|
this.state = { rng };
|
|
132763
132677
|
}
|
|
@@ -132779,8 +132693,8 @@ class GenerateStreetAddress extends AbstractGenerator {
|
|
|
132779
132693
|
const rng = prand.xoroshiro128plus(seed);
|
|
132780
132694
|
const possStreetNames = [firstNames, lastNames];
|
|
132781
132695
|
const maxStreetAddressLength = 4 + Math.max(maxStringLength$5, maxStringLength$3) + 1 + maxStringLength;
|
|
132782
|
-
if (this.
|
|
132783
|
-
throw new Error(`You can't use street address generator with a db column length restriction of ${this.
|
|
132696
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStreetAddressLength) {
|
|
132697
|
+
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}.`);
|
|
132784
132698
|
}
|
|
132785
132699
|
this.state = { rng, possStreetNames };
|
|
132786
132700
|
}
|
|
@@ -132809,8 +132723,8 @@ class GenerateUniqueStreetAddress extends AbstractGenerator {
|
|
|
132809
132723
|
throw new RangeError(`count exceeds max number of unique street names(${maxUniqueStreetnamesNumber}).`);
|
|
132810
132724
|
}
|
|
132811
132725
|
const maxStreetAddressLength = 4 + Math.max(maxStringLength$5, maxStringLength$3) + 1 + maxStringLength;
|
|
132812
|
-
if (this.
|
|
132813
|
-
throw new Error(`You can't use street address generator with a db column length restriction of ${this.
|
|
132726
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStreetAddressLength) {
|
|
132727
|
+
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}.`);
|
|
132814
132728
|
}
|
|
132815
132729
|
const rng = prand.xoroshiro128plus(seed);
|
|
132816
132730
|
// ["1", "2", ..., "999"]
|
|
@@ -132866,8 +132780,8 @@ class GenerateCity extends AbstractGenerator {
|
|
|
132866
132780
|
init({ count, seed }) {
|
|
132867
132781
|
super.init({ count, seed });
|
|
132868
132782
|
const rng = prand.xoroshiro128plus(seed);
|
|
132869
|
-
if (this.
|
|
132870
|
-
throw new Error(`You can't use city generator with a db column length restriction of ${this.
|
|
132783
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$9) {
|
|
132784
|
+
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}.`);
|
|
132871
132785
|
}
|
|
132872
132786
|
this.state = { rng };
|
|
132873
132787
|
}
|
|
@@ -132888,8 +132802,8 @@ class GenerateUniqueCity extends AbstractGenerator {
|
|
|
132888
132802
|
if (count > cityNames.length) {
|
|
132889
132803
|
throw new Error('count exceeds max number of unique cities.');
|
|
132890
132804
|
}
|
|
132891
|
-
if (this.
|
|
132892
|
-
throw new Error(`You can't use city generator with a db column length restriction of ${this.
|
|
132805
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$9) {
|
|
132806
|
+
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}.`);
|
|
132893
132807
|
}
|
|
132894
132808
|
const genIndicesObj = new GenerateUniqueInt({ minValue: 0, maxValue: cityNames.length - 1 });
|
|
132895
132809
|
genIndicesObj.init({ count, seed });
|
|
@@ -132913,8 +132827,8 @@ class GeneratePostcode extends AbstractGenerator {
|
|
|
132913
132827
|
const rng = prand.xoroshiro128plus(seed);
|
|
132914
132828
|
const templates = ['#####', '#####-####'];
|
|
132915
132829
|
const maxPostcodeLength = Math.max(...templates.map((template) => template.length));
|
|
132916
|
-
if (this.
|
|
132917
|
-
throw new Error(`You can't use postcode generator with a db column length restriction of ${this.
|
|
132830
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxPostcodeLength) {
|
|
132831
|
+
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}.`);
|
|
132918
132832
|
}
|
|
132919
132833
|
this.state = { rng, templates };
|
|
132920
132834
|
}
|
|
@@ -132964,8 +132878,8 @@ class GenerateUniquePostcode extends AbstractGenerator {
|
|
|
132964
132878
|
},
|
|
132965
132879
|
];
|
|
132966
132880
|
const maxPostcodeLength = Math.max(...templates.map((template) => template.template.length));
|
|
132967
|
-
if (this.
|
|
132968
|
-
throw new Error(`You can't use postcode generator with a db column length restriction of ${this.
|
|
132881
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxPostcodeLength) {
|
|
132882
|
+
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}.`);
|
|
132969
132883
|
}
|
|
132970
132884
|
for (const templateObj of templates) {
|
|
132971
132885
|
templateObj.indicesGen.skipCheck = true;
|
|
@@ -133001,8 +132915,8 @@ class GenerateState extends AbstractGenerator {
|
|
|
133001
132915
|
init({ count, seed }) {
|
|
133002
132916
|
super.init({ count, seed });
|
|
133003
132917
|
const rng = prand.xoroshiro128plus(seed);
|
|
133004
|
-
if (this.
|
|
133005
|
-
throw new Error(`You can't use state generator with a db column length restriction of ${this.
|
|
132918
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$1) {
|
|
132919
|
+
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}.`);
|
|
133006
132920
|
}
|
|
133007
132921
|
this.state = { rng };
|
|
133008
132922
|
}
|
|
@@ -133030,8 +132944,8 @@ class GenerateCompanyName extends AbstractGenerator {
|
|
|
133030
132944
|
];
|
|
133031
132945
|
// max( { template: '#', placeholdersCount: 1 }, { template: '#, # and #', placeholdersCount: 3 } )
|
|
133032
132946
|
const maxCompanyNameLength = Math.max(maxStringLength$3 + maxStringLength$8 + 1, 3 * maxStringLength$3 + 7);
|
|
133033
|
-
if (this.
|
|
133034
|
-
throw new Error(`You can't use company name generator with a db column length restriction of ${this.
|
|
132947
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxCompanyNameLength) {
|
|
132948
|
+
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}.`);
|
|
133035
132949
|
}
|
|
133036
132950
|
this.state = { rng, templates };
|
|
133037
132951
|
}
|
|
@@ -133075,8 +132989,8 @@ class GenerateUniqueCompanyName extends AbstractGenerator {
|
|
|
133075
132989
|
}
|
|
133076
132990
|
// max( { template: '#', placeholdersCount: 1 }, { template: '#, # and #', placeholdersCount: 3 } )
|
|
133077
132991
|
const maxCompanyNameLength = Math.max(maxStringLength$3 + maxStringLength$8 + 1, 3 * maxStringLength$3 + 7);
|
|
133078
|
-
if (this.
|
|
133079
|
-
throw new Error(`You can't use company name generator with a db column length restriction of ${this.
|
|
132992
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxCompanyNameLength) {
|
|
132993
|
+
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}.`);
|
|
133080
132994
|
}
|
|
133081
132995
|
const rng = prand.xoroshiro128plus(seed);
|
|
133082
132996
|
// when count reach maxUniqueCompanyNameNumber template will be deleted from array
|
|
@@ -133152,8 +133066,8 @@ class GenerateLoremIpsum extends AbstractGenerator {
|
|
|
133152
133066
|
this.params.sentencesCount = 1;
|
|
133153
133067
|
const maxLoremIpsumSentencesLength = maxStringLength$2 * this.params.sentencesCount + this.params.sentencesCount
|
|
133154
133068
|
- 1;
|
|
133155
|
-
if (this.
|
|
133156
|
-
throw new Error(`You can't use lorem ipsum generator with a db column length restriction of ${this.
|
|
133069
|
+
if (this.typeParams?.length !== undefined && this.typeParams?.length < maxLoremIpsumSentencesLength) {
|
|
133070
|
+
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}.`);
|
|
133157
133071
|
}
|
|
133158
133072
|
this.state = { rng };
|
|
133159
133073
|
}
|
|
@@ -133259,6 +133173,7 @@ class GenerateUniquePoint extends AbstractGenerator {
|
|
|
133259
133173
|
state;
|
|
133260
133174
|
isUnique = true;
|
|
133261
133175
|
init({ count, seed }) {
|
|
133176
|
+
// TODO: rewrite the unique generator to use fastCartesianProduct for generating unique points.
|
|
133262
133177
|
const xCoordinateGen = new GenerateUniqueNumber({
|
|
133263
133178
|
minValue: this.params.minXValue,
|
|
133264
133179
|
maxValue: this.params.maxXValue,
|
|
@@ -133345,6 +133260,7 @@ class GenerateUniqueLine extends AbstractGenerator {
|
|
|
133345
133260
|
state;
|
|
133346
133261
|
isUnique = true;
|
|
133347
133262
|
init({ count, seed }) {
|
|
133263
|
+
// TODO: rewrite the unique generator to use fastCartesianProduct for generating unique triplets(liens).
|
|
133348
133264
|
const aCoefficientGen = new GenerateUniqueNumber({
|
|
133349
133265
|
minValue: this.params.minAValue,
|
|
133350
133266
|
maxValue: this.params.maxAValue,
|
|
@@ -133388,6 +133304,397 @@ class GenerateUniqueLine extends AbstractGenerator {
|
|
|
133388
133304
|
}
|
|
133389
133305
|
}
|
|
133390
133306
|
}
|
|
133307
|
+
class GenerateBitString extends AbstractGenerator {
|
|
133308
|
+
static entityKind = 'GenerateBitString';
|
|
133309
|
+
dimensions = 11;
|
|
133310
|
+
state;
|
|
133311
|
+
uniqueVersionOfGen = GenerateUniqueBitString;
|
|
133312
|
+
init({ count, seed }) {
|
|
133313
|
+
super.init({ count, seed });
|
|
133314
|
+
this.dimensions = this.params.dimensions ?? this.typeParams?.length ?? this.dimensions;
|
|
133315
|
+
let intGen;
|
|
133316
|
+
if (this.dimensions > 53) {
|
|
133317
|
+
const maxValue = (BigInt(2) ** BigInt(this.dimensions)) - BigInt(1);
|
|
133318
|
+
intGen = new GenerateInt({ minValue: BigInt(0), maxValue });
|
|
133319
|
+
}
|
|
133320
|
+
else {
|
|
133321
|
+
// dimensions <= 53
|
|
133322
|
+
const maxValue = Math.pow(2, this.dimensions) - 1;
|
|
133323
|
+
intGen = new GenerateInt({ minValue: 0, maxValue });
|
|
133324
|
+
}
|
|
133325
|
+
intGen.init({ count, seed });
|
|
133326
|
+
this.state = { intGen };
|
|
133327
|
+
}
|
|
133328
|
+
generate() {
|
|
133329
|
+
if (this.state === undefined) {
|
|
133330
|
+
throw new Error('state is not defined.');
|
|
133331
|
+
}
|
|
133332
|
+
const bitString = this.state.intGen.generate().toString(2);
|
|
133333
|
+
return bitString.padStart(this.dimensions, '0');
|
|
133334
|
+
}
|
|
133335
|
+
}
|
|
133336
|
+
class GenerateUniqueBitString extends AbstractGenerator {
|
|
133337
|
+
static entityKind = 'GenerateUniqueBitString';
|
|
133338
|
+
dimensions = 11;
|
|
133339
|
+
state;
|
|
133340
|
+
isUnique = true;
|
|
133341
|
+
init({ count, seed }) {
|
|
133342
|
+
this.dimensions = this.params.dimensions ?? this.typeParams?.length ?? this.dimensions;
|
|
133343
|
+
let intGen;
|
|
133344
|
+
if (this.dimensions > 53) {
|
|
133345
|
+
const maxValue = (BigInt(2) ** BigInt(this.dimensions)) - BigInt(1);
|
|
133346
|
+
intGen = new GenerateUniqueInt({ minValue: BigInt(0), maxValue });
|
|
133347
|
+
}
|
|
133348
|
+
else {
|
|
133349
|
+
// dimensions <= 53
|
|
133350
|
+
const maxValue = Math.pow(2, this.dimensions) - 1;
|
|
133351
|
+
intGen = new GenerateUniqueInt({ minValue: 0, maxValue });
|
|
133352
|
+
}
|
|
133353
|
+
intGen.init({ count, seed });
|
|
133354
|
+
this.state = { intGen };
|
|
133355
|
+
}
|
|
133356
|
+
generate() {
|
|
133357
|
+
if (this.state === undefined) {
|
|
133358
|
+
throw new Error('state is not defined.');
|
|
133359
|
+
}
|
|
133360
|
+
const bitString = this.state.intGen.generate().toString(2);
|
|
133361
|
+
return bitString.padStart(this.dimensions, '0');
|
|
133362
|
+
}
|
|
133363
|
+
}
|
|
133364
|
+
class GenerateInet extends AbstractGenerator {
|
|
133365
|
+
static entityKind = 'GenerateInet';
|
|
133366
|
+
ipAddress = 'ipv4';
|
|
133367
|
+
includeCidr = true;
|
|
133368
|
+
state;
|
|
133369
|
+
uniqueVersionOfGen = GenerateUniqueInet;
|
|
133370
|
+
init({ count, seed }) {
|
|
133371
|
+
super.init({ count, seed });
|
|
133372
|
+
this.ipAddress = this.params.ipAddress ?? this.ipAddress;
|
|
133373
|
+
this.includeCidr = this.params.includeCidr ?? this.includeCidr;
|
|
133374
|
+
const rng = prand.xoroshiro128plus(seed);
|
|
133375
|
+
this.state = { rng };
|
|
133376
|
+
}
|
|
133377
|
+
generate() {
|
|
133378
|
+
if (this.state === undefined) {
|
|
133379
|
+
throw new Error('state is not defined.');
|
|
133380
|
+
}
|
|
133381
|
+
let value;
|
|
133382
|
+
const values = [];
|
|
133383
|
+
let inetVal = '';
|
|
133384
|
+
if (this.ipAddress === 'ipv4') {
|
|
133385
|
+
for (let octet = 0; octet < 4; octet++) {
|
|
133386
|
+
[value, this.state.rng] = prand.uniformIntDistribution(0, 255, this.state.rng);
|
|
133387
|
+
values.push(value.toString());
|
|
133388
|
+
}
|
|
133389
|
+
inetVal += values.join('.');
|
|
133390
|
+
if (this.includeCidr) {
|
|
133391
|
+
[value, this.state.rng] = prand.uniformIntDistribution(0, 32, this.state.rng);
|
|
133392
|
+
inetVal += `/${value}`;
|
|
133393
|
+
}
|
|
133394
|
+
return inetVal;
|
|
133395
|
+
}
|
|
133396
|
+
else {
|
|
133397
|
+
// this.ipAddress === 'ipv6'
|
|
133398
|
+
for (let hextet = 0; hextet < 8; hextet++) {
|
|
133399
|
+
[value, this.state.rng] = prand.uniformIntDistribution(0, 65535, this.state.rng);
|
|
133400
|
+
values.push(value.toString(16));
|
|
133401
|
+
}
|
|
133402
|
+
inetVal += values.join(':');
|
|
133403
|
+
if (this.includeCidr) {
|
|
133404
|
+
[value, this.state.rng] = prand.uniformIntDistribution(0, 128, this.state.rng);
|
|
133405
|
+
inetVal += `/${value}`;
|
|
133406
|
+
}
|
|
133407
|
+
return inetVal;
|
|
133408
|
+
}
|
|
133409
|
+
}
|
|
133410
|
+
}
|
|
133411
|
+
// TODO: add defaults to js doc
|
|
133412
|
+
class GenerateUniqueInet extends AbstractGenerator {
|
|
133413
|
+
static entityKind = 'GenerateUniqueInet';
|
|
133414
|
+
ipAddress = 'ipv4';
|
|
133415
|
+
includeCidr = true;
|
|
133416
|
+
delimiter = '.';
|
|
133417
|
+
state;
|
|
133418
|
+
isUnique = true;
|
|
133419
|
+
init({ count, seed }) {
|
|
133420
|
+
this.ipAddress = this.params.ipAddress ?? this.ipAddress;
|
|
133421
|
+
this.delimiter = this.ipAddress === 'ipv4' ? '.' : ':';
|
|
133422
|
+
this.includeCidr = this.params.includeCidr ?? this.includeCidr;
|
|
133423
|
+
// maxValue - number of combinations for cartesian product: {0…255} × {0…255} × {0…255} × {0…255} × {0…32}
|
|
133424
|
+
// where pattern for ipv4 ip is {0–255}.{0–255}.{0–255}.{0–255}[/{0–32}?]
|
|
133425
|
+
// 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}
|
|
133426
|
+
// where pattern for ipv6 ip is {0-65535}:{0-65535}:{0-65535}:{0-65535}:{0-65535}:{0-65535}:{0-65535}:{0-65535}[/0-128?]
|
|
133427
|
+
let minValue, maxValue;
|
|
133428
|
+
if (this.ipAddress === 'ipv4') {
|
|
133429
|
+
minValue = 0;
|
|
133430
|
+
maxValue = 256 ** 4;
|
|
133431
|
+
if (this.includeCidr) {
|
|
133432
|
+
maxValue = maxValue * 33;
|
|
133433
|
+
}
|
|
133434
|
+
}
|
|
133435
|
+
else {
|
|
133436
|
+
// this.ipAddress === 'ipv6'
|
|
133437
|
+
minValue = BigInt(0);
|
|
133438
|
+
maxValue = BigInt(65535) ** BigInt(8);
|
|
133439
|
+
if (this.includeCidr) {
|
|
133440
|
+
maxValue = maxValue * BigInt(129);
|
|
133441
|
+
}
|
|
133442
|
+
}
|
|
133443
|
+
const indexGen = new GenerateUniqueInt({ minValue, maxValue });
|
|
133444
|
+
indexGen.init({ count, seed });
|
|
133445
|
+
const octetSet = Array.from({ length: 256 }, (_, i) => i.toString());
|
|
133446
|
+
const ipv4PrefixSet = Array.from({ length: 33 }, (_, i) => i.toString());
|
|
133447
|
+
const hextetSet = Array.from({ length: 65536 }, (_, i) => i.toString(16));
|
|
133448
|
+
const ipv6PrefixSet = Array.from({ length: 129 }, (_, i) => i.toString());
|
|
133449
|
+
this.state = { indexGen, octetSet, ipv4PrefixSet, hextetSet, ipv6PrefixSet };
|
|
133450
|
+
}
|
|
133451
|
+
generate() {
|
|
133452
|
+
if (this.state === undefined) {
|
|
133453
|
+
throw new Error('state is not defined.');
|
|
133454
|
+
}
|
|
133455
|
+
let inetVal = '';
|
|
133456
|
+
let tokens = [];
|
|
133457
|
+
if (this.ipAddress === 'ipv4') {
|
|
133458
|
+
const sets = Array.from({ length: 4 }).fill(this.state.octetSet);
|
|
133459
|
+
if (this.includeCidr)
|
|
133460
|
+
sets.push(this.state.ipv4PrefixSet);
|
|
133461
|
+
const index = this.state.indexGen.generate();
|
|
133462
|
+
tokens = fastCartesianProduct(sets, index);
|
|
133463
|
+
}
|
|
133464
|
+
else {
|
|
133465
|
+
// this.ipAddress === 'ipv6'
|
|
133466
|
+
const sets = Array.from({ length: 8 }).fill(this.state.hextetSet);
|
|
133467
|
+
if (this.includeCidr)
|
|
133468
|
+
sets.push(this.state.ipv6PrefixSet);
|
|
133469
|
+
const idx = this.state.indexGen.generate();
|
|
133470
|
+
tokens = fastCartesianProductForBigint(sets, idx);
|
|
133471
|
+
}
|
|
133472
|
+
inetVal = this.includeCidr
|
|
133473
|
+
? tokens.slice(0, -1).join(this.delimiter) + `/${tokens.at(-1)}`
|
|
133474
|
+
: tokens.join(this.delimiter);
|
|
133475
|
+
return inetVal;
|
|
133476
|
+
}
|
|
133477
|
+
}
|
|
133478
|
+
class GenerateGeometry extends AbstractGenerator {
|
|
133479
|
+
static entityKind = 'GenerateGeometry';
|
|
133480
|
+
type = 'point';
|
|
133481
|
+
srid = 4326;
|
|
133482
|
+
decimalPlaces = 6;
|
|
133483
|
+
state;
|
|
133484
|
+
uniqueVersionOfGen = GenerateUniqueGeometry;
|
|
133485
|
+
init({ count, seed }) {
|
|
133486
|
+
super.init({ count, seed });
|
|
133487
|
+
this.type = this.params.type ?? this.type;
|
|
133488
|
+
this.srid = this.params.srid ?? this.srid;
|
|
133489
|
+
this.decimalPlaces = this.params.decimalPlaces ?? this.decimalPlaces;
|
|
133490
|
+
let minXValue, maxXValue, minYValue, maxYValue, denominator;
|
|
133491
|
+
if (this.type === 'point') {
|
|
133492
|
+
if (this.srid === 4326) {
|
|
133493
|
+
// Degrees (latitude / longitude)
|
|
133494
|
+
denominator = 10 ** this.decimalPlaces;
|
|
133495
|
+
minXValue = -180 * denominator;
|
|
133496
|
+
maxXValue = 180 * denominator;
|
|
133497
|
+
minYValue = -90 * denominator;
|
|
133498
|
+
maxYValue = 90 * denominator;
|
|
133499
|
+
}
|
|
133500
|
+
else {
|
|
133501
|
+
// this.srid === 3857
|
|
133502
|
+
// Meters (projected X / Y)
|
|
133503
|
+
denominator = 1;
|
|
133504
|
+
minXValue = -20026376;
|
|
133505
|
+
maxXValue = 20026376;
|
|
133506
|
+
minYValue = -20048966;
|
|
133507
|
+
maxYValue = 20048966;
|
|
133508
|
+
}
|
|
133509
|
+
}
|
|
133510
|
+
else {
|
|
133511
|
+
throw new Error('geometry generator currently supports only the point type.');
|
|
133512
|
+
}
|
|
133513
|
+
const rng = prand.xoroshiro128plus(seed);
|
|
133514
|
+
this.state = { rng, minXValue, maxXValue, minYValue, maxYValue, denominator };
|
|
133515
|
+
}
|
|
133516
|
+
generate() {
|
|
133517
|
+
if (this.state === undefined) {
|
|
133518
|
+
throw new Error('state is not defined.');
|
|
133519
|
+
}
|
|
133520
|
+
let x, y;
|
|
133521
|
+
[x, this.state.rng] = prand.uniformIntDistribution(this.state.minXValue, this.state.maxXValue, this.state.rng);
|
|
133522
|
+
x = x / this.state.denominator;
|
|
133523
|
+
[y, this.state.rng] = prand.uniformIntDistribution(this.state.minYValue, this.state.maxYValue, this.state.rng);
|
|
133524
|
+
y = y / this.state.denominator;
|
|
133525
|
+
if (this.dataType === 'array') {
|
|
133526
|
+
return [x, y];
|
|
133527
|
+
}
|
|
133528
|
+
// this.dataType === 'object'
|
|
133529
|
+
return { x, y };
|
|
133530
|
+
}
|
|
133531
|
+
}
|
|
133532
|
+
class GenerateUniqueGeometry extends AbstractGenerator {
|
|
133533
|
+
static entityKind = 'GenerateUniqueGeometry';
|
|
133534
|
+
type = 'point';
|
|
133535
|
+
srid = 4326;
|
|
133536
|
+
decimalPlaces = 6;
|
|
133537
|
+
state;
|
|
133538
|
+
isUnique = true;
|
|
133539
|
+
init({ count, seed }) {
|
|
133540
|
+
this.type = this.params.type ?? this.type;
|
|
133541
|
+
this.srid = this.params.srid ?? this.srid;
|
|
133542
|
+
this.decimalPlaces = this.params.decimalPlaces ?? this.decimalPlaces;
|
|
133543
|
+
let minXValue, maxXValue, minYValue, maxYValue, denominator;
|
|
133544
|
+
if (this.type === 'point') {
|
|
133545
|
+
if (this.srid === 4326) {
|
|
133546
|
+
// Degrees (latitude / longitude)
|
|
133547
|
+
denominator = 10 ** this.decimalPlaces;
|
|
133548
|
+
minXValue = -180 * denominator;
|
|
133549
|
+
maxXValue = 180 * denominator;
|
|
133550
|
+
minYValue = -90 * denominator;
|
|
133551
|
+
maxYValue = 90 * denominator;
|
|
133552
|
+
}
|
|
133553
|
+
else {
|
|
133554
|
+
// this.srid === 3857
|
|
133555
|
+
// Meters (projected X / Y)
|
|
133556
|
+
denominator = 1;
|
|
133557
|
+
minXValue = -20026376;
|
|
133558
|
+
maxXValue = 20026376;
|
|
133559
|
+
minYValue = -20048966;
|
|
133560
|
+
maxYValue = 20048966;
|
|
133561
|
+
}
|
|
133562
|
+
}
|
|
133563
|
+
else {
|
|
133564
|
+
throw new Error('geometry generator currently supports only the point type.');
|
|
133565
|
+
}
|
|
133566
|
+
const xRange = new OrderedNumberRange(minXValue, maxXValue, 1);
|
|
133567
|
+
const yRange = new OrderedNumberRange(minYValue, maxYValue, 1);
|
|
133568
|
+
const xySets = [xRange, yRange];
|
|
133569
|
+
const maxCombIdx = BigInt(maxXValue - minXValue + 1) * BigInt(maxYValue - minYValue + 1) - BigInt(1);
|
|
133570
|
+
const indexGen = maxCombIdx <= 2 ** 53
|
|
133571
|
+
? new GenerateUniqueInt({ minValue: 0, maxValue: Number(maxCombIdx) })
|
|
133572
|
+
: new GenerateUniqueInt({ minValue: BigInt(0), maxValue: maxCombIdx });
|
|
133573
|
+
indexGen.init({ count, seed });
|
|
133574
|
+
this.state = { denominator, indexGen, xySets };
|
|
133575
|
+
}
|
|
133576
|
+
generate() {
|
|
133577
|
+
if (this.state === undefined) {
|
|
133578
|
+
throw new Error('state is not defined.');
|
|
133579
|
+
}
|
|
133580
|
+
const idx = this.state.indexGen.generate();
|
|
133581
|
+
let x, y;
|
|
133582
|
+
if (typeof idx === 'number') {
|
|
133583
|
+
[x, y] = fastCartesianProduct(this.state.xySets, idx);
|
|
133584
|
+
}
|
|
133585
|
+
else {
|
|
133586
|
+
// typeof idx === 'bigint'
|
|
133587
|
+
[x, y] = fastCartesianProductForBigint(this.state.xySets, idx);
|
|
133588
|
+
}
|
|
133589
|
+
if (this.dataType === 'array') {
|
|
133590
|
+
return [x, y];
|
|
133591
|
+
}
|
|
133592
|
+
// this.dataType === 'object'
|
|
133593
|
+
return { x, y };
|
|
133594
|
+
}
|
|
133595
|
+
}
|
|
133596
|
+
class GenerateVector extends AbstractGenerator {
|
|
133597
|
+
static entityKind = 'GenerateVector';
|
|
133598
|
+
// property below should be overridden in init
|
|
133599
|
+
dimensions = 3;
|
|
133600
|
+
minValue = -1000;
|
|
133601
|
+
maxValue = 1000;
|
|
133602
|
+
decimalPlaces = 2;
|
|
133603
|
+
state;
|
|
133604
|
+
uniqueVersionOfGen = GenerateUniqueVector;
|
|
133605
|
+
init({ count, seed }) {
|
|
133606
|
+
super.init({ count, seed });
|
|
133607
|
+
this.dimensions = this.params.dimensions ?? this.typeParams.length ?? this.dimensions;
|
|
133608
|
+
this.decimalPlaces = this.params.decimalPlaces ?? this.decimalPlaces;
|
|
133609
|
+
this.minValue = this.params.minValue ?? this.minValue;
|
|
133610
|
+
this.maxValue = this.params.maxValue ?? this.maxValue;
|
|
133611
|
+
if (this.minValue > this.maxValue) {
|
|
133612
|
+
throw new Error(`minValue ( ${this.minValue} ) cannot be greater than maxValue ( ${this.maxValue} ).\n`
|
|
133613
|
+
+ `Did you forget to pass both minValue and maxValue to the generator's properties?`);
|
|
133614
|
+
}
|
|
133615
|
+
if (this.decimalPlaces < 0) {
|
|
133616
|
+
throw new Error(`decimalPlaces value must be greater than or equal to zero.`);
|
|
133617
|
+
}
|
|
133618
|
+
if (abs(BigInt(this.minValue) * BigInt(10 ** this.decimalPlaces)) > Number.MAX_SAFE_INTEGER
|
|
133619
|
+
|| abs(BigInt(this.maxValue) * BigInt(10 ** this.decimalPlaces)) > Number.MAX_SAFE_INTEGER) {
|
|
133620
|
+
console.warn(`vector generator: minValue or maxValue multiplied by 10^decimalPlaces exceeds Number.MAX_SAFE_INTEGER (2^53 -1).\n`
|
|
133621
|
+
+ `This overflow may result in less accurate values being generated.`);
|
|
133622
|
+
}
|
|
133623
|
+
// `numberGen` is initialized in the `init` method of `GenerateArray`
|
|
133624
|
+
const numberGen = new GenerateNumber({
|
|
133625
|
+
minValue: this.minValue,
|
|
133626
|
+
maxValue: this.maxValue,
|
|
133627
|
+
precision: 10 ** this.decimalPlaces,
|
|
133628
|
+
});
|
|
133629
|
+
const vectorGen = new GenerateArray({ baseColumnGen: numberGen, size: this.dimensions });
|
|
133630
|
+
vectorGen.init({ count, seed });
|
|
133631
|
+
this.state = { vectorGen };
|
|
133632
|
+
}
|
|
133633
|
+
generate() {
|
|
133634
|
+
if (this.state === undefined) {
|
|
133635
|
+
throw new Error('state is not defined.');
|
|
133636
|
+
}
|
|
133637
|
+
const vectorVal = this.state.vectorGen.generate();
|
|
133638
|
+
return vectorVal;
|
|
133639
|
+
}
|
|
133640
|
+
}
|
|
133641
|
+
class GenerateUniqueVector extends AbstractGenerator {
|
|
133642
|
+
static entityKind = 'GenerateUniqueVector';
|
|
133643
|
+
// property below should be overridden in init
|
|
133644
|
+
dimensions = 3;
|
|
133645
|
+
minValue = -1000;
|
|
133646
|
+
maxValue = 1000;
|
|
133647
|
+
decimalPlaces = 2;
|
|
133648
|
+
state;
|
|
133649
|
+
isUnique = true;
|
|
133650
|
+
init({ count, seed }) {
|
|
133651
|
+
this.dimensions = this.params.dimensions ?? this.typeParams.length ?? this.dimensions;
|
|
133652
|
+
this.decimalPlaces = this.params.decimalPlaces ?? this.decimalPlaces;
|
|
133653
|
+
const denominator = 10 ** this.decimalPlaces;
|
|
133654
|
+
this.minValue = this.params.minValue ?? this.minValue;
|
|
133655
|
+
this.maxValue = this.params.maxValue ?? this.maxValue;
|
|
133656
|
+
if (this.minValue > this.maxValue) {
|
|
133657
|
+
throw new Error(`minValue ( ${this.minValue} ) cannot be greater than maxValue ( ${this.maxValue} ).\n`
|
|
133658
|
+
+ `Did you forget to pass both minValue and maxValue to the generator's properties?`);
|
|
133659
|
+
}
|
|
133660
|
+
if (this.decimalPlaces < 0) {
|
|
133661
|
+
throw new Error(`decimalPlaces value must be greater than or equal to zero.`);
|
|
133662
|
+
}
|
|
133663
|
+
if (abs(BigInt(this.minValue) * BigInt(denominator)) > Number.MAX_SAFE_INTEGER
|
|
133664
|
+
|| abs(BigInt(this.maxValue) * BigInt(denominator)) > Number.MAX_SAFE_INTEGER) {
|
|
133665
|
+
console.warn(`vector generator: minValue or maxValue multiplied by 10^decimalPlaces exceeds Number.MAX_SAFE_INTEGER (2^53 -1).\n`
|
|
133666
|
+
+ `This overflow may result in less accurate values being generated.`);
|
|
133667
|
+
}
|
|
133668
|
+
const dimensionRange = new OrderedNumberRange(this.minValue * denominator, this.maxValue * denominator, 1);
|
|
133669
|
+
const vectorSets = Array.from({ length: this.dimensions }).fill(dimensionRange);
|
|
133670
|
+
const maxCombIdx = vectorSets.reduce((acc, curr) => acc * BigInt(curr.length), BigInt(1)) - BigInt(1);
|
|
133671
|
+
const indexGen = maxCombIdx <= Number.MAX_SAFE_INTEGER
|
|
133672
|
+
? new GenerateUniqueInt({ minValue: 0, maxValue: Number(maxCombIdx) })
|
|
133673
|
+
: new GenerateUniqueInt({ minValue: BigInt(0), maxValue: maxCombIdx });
|
|
133674
|
+
indexGen.init({ count, seed });
|
|
133675
|
+
const transformVector = denominator === 1
|
|
133676
|
+
? (_vector, _denominator) => { }
|
|
133677
|
+
: (vector, denominator) => {
|
|
133678
|
+
for (let i = 0; i < vector.length; i++) {
|
|
133679
|
+
vector[i] = vector[i] / denominator;
|
|
133680
|
+
}
|
|
133681
|
+
return;
|
|
133682
|
+
};
|
|
133683
|
+
this.state = { indexGen, vectorSets, denominator, transformVector };
|
|
133684
|
+
}
|
|
133685
|
+
generate() {
|
|
133686
|
+
if (this.state === undefined) {
|
|
133687
|
+
throw new Error('state is not defined.');
|
|
133688
|
+
}
|
|
133689
|
+
const idx = this.state.indexGen.generate();
|
|
133690
|
+
const vector = typeof idx === 'number'
|
|
133691
|
+
? fastCartesianProduct(this.state.vectorSets, idx)
|
|
133692
|
+
// typeof idx === 'bigint'
|
|
133693
|
+
: fastCartesianProductForBigint(this.state.vectorSets, idx);
|
|
133694
|
+
this.state.transformVector(vector, this.state.denominator);
|
|
133695
|
+
return vector;
|
|
133696
|
+
}
|
|
133697
|
+
}
|
|
133391
133698
|
|
|
133392
133699
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
133393
133700
|
class GenerateUniqueIntervalV2 extends AbstractGenerator {
|
|
@@ -133474,8 +133781,8 @@ class GenerateStringV2 extends AbstractGenerator {
|
|
|
133474
133781
|
super.init({ count, seed });
|
|
133475
133782
|
let minStringLength = 7;
|
|
133476
133783
|
let maxStringLength = 20;
|
|
133477
|
-
if (this.
|
|
133478
|
-
maxStringLength = this.
|
|
133784
|
+
if (this.typeParams?.length !== undefined) {
|
|
133785
|
+
maxStringLength = this.typeParams?.length;
|
|
133479
133786
|
if (maxStringLength === 1)
|
|
133480
133787
|
minStringLength = maxStringLength;
|
|
133481
133788
|
if (maxStringLength < minStringLength)
|
|
@@ -133497,6 +133804,8 @@ class GenerateStringV2 extends AbstractGenerator {
|
|
|
133497
133804
|
[idx, this.state.rng] = prand.uniformIntDistribution(0, stringChars.length - 1, this.state.rng);
|
|
133498
133805
|
currStr += stringChars[idx];
|
|
133499
133806
|
}
|
|
133807
|
+
if (this.dataType === 'buffer')
|
|
133808
|
+
return Buffer.from(currStr);
|
|
133500
133809
|
return currStr;
|
|
133501
133810
|
}
|
|
133502
133811
|
}
|
|
@@ -133510,8 +133819,8 @@ class GenerateUniqueStringV2 extends AbstractGenerator {
|
|
|
133510
133819
|
let minStringLength = 7;
|
|
133511
133820
|
let maxStringLength = 20;
|
|
133512
133821
|
// TODO: revise later
|
|
133513
|
-
if (this.
|
|
133514
|
-
maxStringLength = this.
|
|
133822
|
+
if (this.typeParams?.length !== undefined) {
|
|
133823
|
+
maxStringLength = this.typeParams?.length;
|
|
133515
133824
|
if (maxStringLength === 1 || maxStringLength < minStringLength)
|
|
133516
133825
|
minStringLength = maxStringLength;
|
|
133517
133826
|
}
|
|
@@ -133704,8 +134013,6 @@ const generatorsFuncs = {
|
|
|
133704
134013
|
date: createGenerator(GenerateDate),
|
|
133705
134014
|
/**
|
|
133706
134015
|
* generates time in 24 hours style.
|
|
133707
|
-
* @param minTime - lower border of range.
|
|
133708
|
-
* @param maxTime - upper border of range.
|
|
133709
134016
|
* @param arraySize - number of elements in each one-dimensional array. (If specified, arrays will be generated.)
|
|
133710
134017
|
*
|
|
133711
134018
|
* @example
|
|
@@ -133713,7 +134020,7 @@ const generatorsFuncs = {
|
|
|
133713
134020
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
133714
134021
|
* users: {
|
|
133715
134022
|
* columns: {
|
|
133716
|
-
* birthTime: funcs.time(
|
|
134023
|
+
* birthTime: funcs.time()
|
|
133717
134024
|
* },
|
|
133718
134025
|
* },
|
|
133719
134026
|
* }));
|
|
@@ -133723,8 +134030,6 @@ const generatorsFuncs = {
|
|
|
133723
134030
|
time: createGenerator(GenerateTime),
|
|
133724
134031
|
/**
|
|
133725
134032
|
* generates timestamps.
|
|
133726
|
-
* @param minTimestamp - lower border of range.
|
|
133727
|
-
* @param maxTimestamp - upper border of range.
|
|
133728
134033
|
* @param arraySize - number of elements in each one-dimensional array. (If specified, arrays will be generated.)
|
|
133729
134034
|
*
|
|
133730
134035
|
* @example
|
|
@@ -133732,7 +134037,7 @@ const generatorsFuncs = {
|
|
|
133732
134037
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
133733
134038
|
* orders: {
|
|
133734
134039
|
* columns: {
|
|
133735
|
-
* shippedDate: funcs.timestamp(
|
|
134040
|
+
* shippedDate: funcs.timestamp()
|
|
133736
134041
|
* },
|
|
133737
134042
|
* },
|
|
133738
134043
|
* }));
|
|
@@ -133742,8 +134047,6 @@ const generatorsFuncs = {
|
|
|
133742
134047
|
timestamp: createGenerator(GenerateTimestamp),
|
|
133743
134048
|
/**
|
|
133744
134049
|
* generates datetime objects.
|
|
133745
|
-
* @param minDatetime - lower border of range.
|
|
133746
|
-
* @param maxDatetime - upper border of range.
|
|
133747
134050
|
* @param arraySize - number of elements in each one-dimensional array. (If specified, arrays will be generated.)
|
|
133748
134051
|
*
|
|
133749
134052
|
* @example
|
|
@@ -133751,7 +134054,7 @@ const generatorsFuncs = {
|
|
|
133751
134054
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
133752
134055
|
* orders: {
|
|
133753
134056
|
* columns: {
|
|
133754
|
-
* shippedDate: funcs.datetime(
|
|
134057
|
+
* shippedDate: funcs.datetime()
|
|
133755
134058
|
* },
|
|
133756
134059
|
* },
|
|
133757
134060
|
* }));
|
|
@@ -134211,8 +134514,132 @@ const generatorsFuncs = {
|
|
|
134211
134514
|
* ```
|
|
134212
134515
|
*/
|
|
134213
134516
|
weightedRandom: createGenerator(WeightedRandomGenerator),
|
|
134517
|
+
/**
|
|
134518
|
+
* generates bit strings based on specified parameters
|
|
134519
|
+
*
|
|
134520
|
+
* @param isUnique - property that controls if generated values gonna be unique or not.
|
|
134521
|
+
* @param arraySize - number of elements in each one-dimensional array (If specified, arrays will be generated).
|
|
134522
|
+
* @param dimensions - desired length of each bit string (e.g., `dimensions = 3` produces values like `'010'`).
|
|
134523
|
+
*
|
|
134524
|
+
* Defaults to the value of the database column bit-length.
|
|
134525
|
+
*
|
|
134526
|
+
* @example
|
|
134527
|
+
* ```ts
|
|
134528
|
+
* await seed(db, { bitStringTable: schema.bitStringTable }).refine((funcs) => ({
|
|
134529
|
+
* bitStringTable: {
|
|
134530
|
+
* count,
|
|
134531
|
+
* columns: {
|
|
134532
|
+
* bit: funcs.bitString({
|
|
134533
|
+
* dimensions: 12,
|
|
134534
|
+
* isUnique: true
|
|
134535
|
+
* }),
|
|
134536
|
+
* },
|
|
134537
|
+
* },
|
|
134538
|
+
* }));
|
|
134539
|
+
* ```
|
|
134540
|
+
*/
|
|
134541
|
+
bitString: createGenerator(GenerateBitString),
|
|
134542
|
+
/**
|
|
134543
|
+
* generates ip addresses based on specified parameters
|
|
134544
|
+
*
|
|
134545
|
+
* @param isUnique - property that controls if generated values gonna be unique or not.
|
|
134546
|
+
* @param arraySize - number of elements in each one-dimensional array (If specified, arrays will be generated).
|
|
134547
|
+
* @param ipAddress - type of IP address to generate — either "ipv4" or "ipv6".
|
|
134548
|
+
*
|
|
134549
|
+
* Defaults to `'ipv4'`.
|
|
134550
|
+
* @param includeCidr - determines whether generated IPs include a CIDR suffix.
|
|
134551
|
+
*
|
|
134552
|
+
* Defaults to `true`.
|
|
134553
|
+
*
|
|
134554
|
+
* @example
|
|
134555
|
+
* ```ts
|
|
134556
|
+
* await seed(db, { inetTable: schema.inetTable }).refine((funcs) => ({
|
|
134557
|
+
* inetTable: {
|
|
134558
|
+
* count,
|
|
134559
|
+
* columns: {
|
|
134560
|
+
* inet: funcs.inet({
|
|
134561
|
+
* ipAddress: 'ipv4',
|
|
134562
|
+
* includeCidr: true,
|
|
134563
|
+
* isUnique: true
|
|
134564
|
+
* }),
|
|
134565
|
+
* },
|
|
134566
|
+
* },
|
|
134567
|
+
* }));
|
|
134568
|
+
* ```
|
|
134569
|
+
*/
|
|
134570
|
+
inet: createGenerator(GenerateInet),
|
|
134571
|
+
/**
|
|
134572
|
+
* generates PostGIS geometry objects based on the given parameters.
|
|
134573
|
+
*
|
|
134574
|
+
* @param isUnique - property that controls if generated values gonna be unique or not.
|
|
134575
|
+
* @param arraySize - number of elements in each one-dimensional array (If specified, arrays will be generated).
|
|
134576
|
+
* @param type - geometry type to generate; currently only `'point'` is supported.
|
|
134577
|
+
*
|
|
134578
|
+
* Defaults to `'point'`.
|
|
134579
|
+
* @param srid - Spatial Reference System Identifier: determines what type of point will be generated - either `4326` or `3857`.
|
|
134580
|
+
*
|
|
134581
|
+
* Defaults to `4326`.
|
|
134582
|
+
* @param decimalPlaces - number of decimal places for points when `srid` is `4326` (e.g., `decimalPlaces = 3` produces values like `'point(30.723 46.482)'`).
|
|
134583
|
+
*
|
|
134584
|
+
* Defaults to `6`.
|
|
134585
|
+
*
|
|
134586
|
+
* @example
|
|
134587
|
+
* ```ts
|
|
134588
|
+
* await seed(db, { geometryTable: schema.geometryTable }).refine((funcs) => ({
|
|
134589
|
+
* geometryTable: {
|
|
134590
|
+
* count,
|
|
134591
|
+
* columns: {
|
|
134592
|
+
* geometryPointTuple: funcs.geometry({
|
|
134593
|
+
* type: 'point',
|
|
134594
|
+
* srid: 4326,
|
|
134595
|
+
* decimalPlaces: 5,
|
|
134596
|
+
* isUnique: true
|
|
134597
|
+
* })
|
|
134598
|
+
* },
|
|
134599
|
+
* },
|
|
134600
|
+
* }));
|
|
134601
|
+
* ```
|
|
134602
|
+
*/
|
|
134603
|
+
geometry: createGenerator(GenerateGeometry),
|
|
134604
|
+
/**
|
|
134605
|
+
* generates vectors based on the provided parameters.
|
|
134606
|
+
*
|
|
134607
|
+
* @param isUnique - property that controls if generated values gonna be unique or not.
|
|
134608
|
+
* @param arraySize - number of elements in each one-dimensional array (If specified, arrays will be generated).
|
|
134609
|
+
* @param decimalPlaces - number of decimal places for each vector element (e.g., `decimalPlaces = 3` produces values like `1.123`).
|
|
134610
|
+
*
|
|
134611
|
+
* Defaults to `2`.
|
|
134612
|
+
* @param dimensions - number of elements in each generated vector (e.g., `dimensions = 3` produces values like `[1,2,3]`).
|
|
134613
|
+
*
|
|
134614
|
+
* Defaults to the value of the database column’s dimensions.
|
|
134615
|
+
* @param minValue - minimum allowed value for each vector element.
|
|
134616
|
+
*
|
|
134617
|
+
* Defaults to `-1000`.
|
|
134618
|
+
* @param maxValue - maximum allowed value for each vector element.
|
|
134619
|
+
*
|
|
134620
|
+
* Defaults to `1000`.
|
|
134621
|
+
*
|
|
134622
|
+
* @example
|
|
134623
|
+
* ```ts
|
|
134624
|
+
* await seed(db, { vectorTable: schema.vectorTable }).refine((funcs) => ({
|
|
134625
|
+
* vectorTable: {
|
|
134626
|
+
* count,
|
|
134627
|
+
* columns: {
|
|
134628
|
+
* vector: funcs.vector({
|
|
134629
|
+
* decimalPlaces: 5,
|
|
134630
|
+
* dimensions: 12,
|
|
134631
|
+
* minValue: -100,
|
|
134632
|
+
* maxValue: 100,
|
|
134633
|
+
* isUnique: true
|
|
134634
|
+
* }),
|
|
134635
|
+
* },
|
|
134636
|
+
* },
|
|
134637
|
+
* }));
|
|
134638
|
+
* ```
|
|
134639
|
+
*/
|
|
134640
|
+
vector: createGenerator(GenerateVector),
|
|
134214
134641
|
};
|
|
134215
|
-
// so far, version changes don’t
|
|
134642
|
+
// so far, version changes don’t affect generator parameters.
|
|
134216
134643
|
const generatorsFuncsV2 = {
|
|
134217
134644
|
...generatorsFuncs,
|
|
134218
134645
|
};
|
|
@@ -134370,10 +134797,1128 @@ const generatorsMap = {
|
|
|
134370
134797
|
GenerateWeightedCount: [
|
|
134371
134798
|
GenerateWeightedCount,
|
|
134372
134799
|
],
|
|
134800
|
+
GenerateBitString: [
|
|
134801
|
+
GenerateBitString,
|
|
134802
|
+
],
|
|
134803
|
+
GenerateUniqueBitString: [
|
|
134804
|
+
GenerateUniqueBitString,
|
|
134805
|
+
],
|
|
134806
|
+
GenerateInet: [
|
|
134807
|
+
GenerateInet,
|
|
134808
|
+
],
|
|
134809
|
+
GenerateUniqueInet: [
|
|
134810
|
+
GenerateUniqueInet,
|
|
134811
|
+
],
|
|
134812
|
+
GenerateGeometry: [
|
|
134813
|
+
GenerateGeometry,
|
|
134814
|
+
],
|
|
134815
|
+
GenerateUniqueGeometry: [
|
|
134816
|
+
GenerateUniqueGeometry,
|
|
134817
|
+
],
|
|
134818
|
+
GenerateVector: [
|
|
134819
|
+
GenerateVector,
|
|
134820
|
+
],
|
|
134821
|
+
GenerateUniqueVector: [
|
|
134822
|
+
GenerateUniqueVector,
|
|
134823
|
+
],
|
|
134824
|
+
};
|
|
134825
|
+
|
|
134826
|
+
// TODO: revise serial part generators
|
|
134827
|
+
const selectGeneratorForCockroachColumn = (table, col) => {
|
|
134828
|
+
const pickGenerator = (table, col) => {
|
|
134829
|
+
// ARRAY
|
|
134830
|
+
if (col.columnType.match(/\[\w*]/g) !== null && col.baseColumn !== undefined) {
|
|
134831
|
+
const baseColumnGen = selectGeneratorForCockroachColumn(table, col.baseColumn);
|
|
134832
|
+
if (baseColumnGen === undefined) {
|
|
134833
|
+
throw new Error(`column with type ${col.baseColumn.columnType} is not supported for now.`);
|
|
134834
|
+
}
|
|
134835
|
+
// const getBaseColumnDataType = (baseColumn: Column) => {
|
|
134836
|
+
// if (baseColumn.baseColumn !== undefined) {
|
|
134837
|
+
// return getBaseColumnDataType(baseColumn.baseColumn);
|
|
134838
|
+
// }
|
|
134839
|
+
// return baseColumn.dataType;
|
|
134840
|
+
// };
|
|
134841
|
+
// const baseColumnDataType = getBaseColumnDataType(col.baseColumn);
|
|
134842
|
+
const generator = new generatorsMap.GenerateArray[0]({ baseColumnGen, size: col.size });
|
|
134843
|
+
// generator.baseColumnDataType = baseColumnDataType;
|
|
134844
|
+
return generator;
|
|
134845
|
+
}
|
|
134846
|
+
// ARRAY for studio
|
|
134847
|
+
if (col.columnType.match(/\[\w*]/g) !== null) {
|
|
134848
|
+
// remove dimensions from type
|
|
134849
|
+
const baseColumnType = col.columnType.replace(/\[\w*]/g, '');
|
|
134850
|
+
const baseColumn = {
|
|
134851
|
+
...col,
|
|
134852
|
+
};
|
|
134853
|
+
baseColumn.columnType = baseColumnType;
|
|
134854
|
+
const baseColumnGen = selectGeneratorForCockroachColumn(table, baseColumn);
|
|
134855
|
+
if (baseColumnGen === undefined) {
|
|
134856
|
+
throw new Error(`column with type ${col.baseColumn.columnType} is not supported for now.`);
|
|
134857
|
+
}
|
|
134858
|
+
let generator = new generatorsMap.GenerateArray[0]({ baseColumnGen });
|
|
134859
|
+
for (let i = 0; i < col.typeParams.dimensions - 1; i++) {
|
|
134860
|
+
generator = new generatorsMap.GenerateArray[0]({ baseColumnGen: generator });
|
|
134861
|
+
}
|
|
134862
|
+
return generator;
|
|
134863
|
+
}
|
|
134864
|
+
// INT ------------------------------------------------------------------------------------------------------------
|
|
134865
|
+
if ((col.columnType === 'int2'
|
|
134866
|
+
|| col.columnType === 'int4'
|
|
134867
|
+
|| col.columnType.includes('int8'))
|
|
134868
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
134869
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
134870
|
+
return generator;
|
|
134871
|
+
}
|
|
134872
|
+
let minValue;
|
|
134873
|
+
let maxValue;
|
|
134874
|
+
if (col.columnType.startsWith('int')) {
|
|
134875
|
+
if (col.columnType === 'int2') {
|
|
134876
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
134877
|
+
minValue = -32768;
|
|
134878
|
+
maxValue = 32767;
|
|
134879
|
+
}
|
|
134880
|
+
else if (col.columnType === 'int4') {
|
|
134881
|
+
// 2^32 / 2 - 1, 4 bytes
|
|
134882
|
+
minValue = -2147483648;
|
|
134883
|
+
maxValue = 2147483647;
|
|
134884
|
+
}
|
|
134885
|
+
else if (col.columnType.includes('int8')) {
|
|
134886
|
+
if (col.dataType === 'bigint') {
|
|
134887
|
+
// 2^64 / 2 - 1, 8 bytes
|
|
134888
|
+
minValue = BigInt('-9223372036854775808');
|
|
134889
|
+
maxValue = BigInt('9223372036854775807');
|
|
134890
|
+
}
|
|
134891
|
+
else {
|
|
134892
|
+
// if (col.dataType === 'number')
|
|
134893
|
+
// if you’re expecting values above 2^31 but below 2^53
|
|
134894
|
+
minValue = -9007199254740991;
|
|
134895
|
+
maxValue = 9007199254740991;
|
|
134896
|
+
}
|
|
134897
|
+
}
|
|
134898
|
+
}
|
|
134899
|
+
if (col.columnType.startsWith('int')
|
|
134900
|
+
&& !col.columnType.includes('interval')) {
|
|
134901
|
+
const generator = new generatorsMap.GenerateInt[0]({
|
|
134902
|
+
minValue,
|
|
134903
|
+
maxValue,
|
|
134904
|
+
});
|
|
134905
|
+
return generator;
|
|
134906
|
+
}
|
|
134907
|
+
// NUMBER(real, double, decimal, numeric)
|
|
134908
|
+
if (col.columnType.startsWith('real')
|
|
134909
|
+
|| col.columnType.startsWith('float')
|
|
134910
|
+
|| col.columnType.startsWith('decimal')
|
|
134911
|
+
|| col.columnType.startsWith('numeric')) {
|
|
134912
|
+
if (col.typeParams.precision !== undefined) {
|
|
134913
|
+
const precision = col.typeParams.precision;
|
|
134914
|
+
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
134915
|
+
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
134916
|
+
const generator = new generatorsMap.GenerateNumber[0]({
|
|
134917
|
+
minValue: -maxAbsoluteValue,
|
|
134918
|
+
maxValue: maxAbsoluteValue,
|
|
134919
|
+
precision: Math.pow(10, scale),
|
|
134920
|
+
});
|
|
134921
|
+
return generator;
|
|
134922
|
+
}
|
|
134923
|
+
const generator = new generatorsMap.GenerateNumber[0]();
|
|
134924
|
+
return generator;
|
|
134925
|
+
}
|
|
134926
|
+
// STRING
|
|
134927
|
+
if ((col.columnType === 'string'
|
|
134928
|
+
|| col.columnType.startsWith('varchar')
|
|
134929
|
+
|| col.columnType.startsWith('char'))
|
|
134930
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
134931
|
+
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
134932
|
+
return generator;
|
|
134933
|
+
}
|
|
134934
|
+
if ((col.columnType === 'string'
|
|
134935
|
+
|| col.columnType.startsWith('varchar')
|
|
134936
|
+
|| col.columnType.startsWith('char'))
|
|
134937
|
+
&& col.name.toLowerCase().includes('name')) {
|
|
134938
|
+
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
134939
|
+
return generator;
|
|
134940
|
+
}
|
|
134941
|
+
if ((col.columnType === 'string'
|
|
134942
|
+
|| col.columnType.startsWith('varchar')
|
|
134943
|
+
|| col.columnType.startsWith('char'))
|
|
134944
|
+
&& col.name.toLowerCase().includes('email')) {
|
|
134945
|
+
const generator = new generatorsMap.GenerateEmail[0]();
|
|
134946
|
+
return generator;
|
|
134947
|
+
}
|
|
134948
|
+
if (col.columnType === 'string'
|
|
134949
|
+
|| col.columnType.startsWith('varchar')
|
|
134950
|
+
|| col.columnType.startsWith('char')) {
|
|
134951
|
+
const generator = new generatorsMap.GenerateString[0]();
|
|
134952
|
+
return generator;
|
|
134953
|
+
}
|
|
134954
|
+
// BIT
|
|
134955
|
+
if (col.columnType.startsWith('bit')) {
|
|
134956
|
+
const generator = new generatorsMap.GenerateBitString[0]();
|
|
134957
|
+
return generator;
|
|
134958
|
+
}
|
|
134959
|
+
// INET
|
|
134960
|
+
if (col.columnType === 'inet') {
|
|
134961
|
+
const generator = new generatorsMap.GenerateInet[0]();
|
|
134962
|
+
return generator;
|
|
134963
|
+
}
|
|
134964
|
+
// geometry(point)
|
|
134965
|
+
if (col.columnType.startsWith('geometry')) {
|
|
134966
|
+
const generator = new generatorsMap.GenerateGeometry[0]();
|
|
134967
|
+
return generator;
|
|
134968
|
+
}
|
|
134969
|
+
// vector
|
|
134970
|
+
if (col.columnType.startsWith('vector')) {
|
|
134971
|
+
const generator = new generatorsMap.GenerateVector[0]();
|
|
134972
|
+
return generator;
|
|
134973
|
+
}
|
|
134974
|
+
// UUID
|
|
134975
|
+
if (col.columnType === 'uuid') {
|
|
134976
|
+
const generator = new generatorsMap.GenerateUUID[0]();
|
|
134977
|
+
return generator;
|
|
134978
|
+
}
|
|
134979
|
+
// BOOLEAN
|
|
134980
|
+
if (col.columnType === 'boolean') {
|
|
134981
|
+
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
134982
|
+
return generator;
|
|
134983
|
+
}
|
|
134984
|
+
// DATE, TIME, TIMESTAMP
|
|
134985
|
+
if (col.columnType.includes('date')) {
|
|
134986
|
+
const generator = new generatorsMap.GenerateDate[0]();
|
|
134987
|
+
return generator;
|
|
134988
|
+
}
|
|
134989
|
+
if (col.columnType === 'time') {
|
|
134990
|
+
const generator = new generatorsMap.GenerateTime[0]();
|
|
134991
|
+
return generator;
|
|
134992
|
+
}
|
|
134993
|
+
if (col.columnType.includes('timestamp')) {
|
|
134994
|
+
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
134995
|
+
return generator;
|
|
134996
|
+
}
|
|
134997
|
+
// JSON, JSONB
|
|
134998
|
+
if (col.columnType === 'json' || col.columnType === 'jsonb') {
|
|
134999
|
+
const generator = new generatorsMap.GenerateJson[0]();
|
|
135000
|
+
return generator;
|
|
135001
|
+
}
|
|
135002
|
+
// if (col.columnType === "jsonb") {
|
|
135003
|
+
// const generator = new GenerateJsonb({});
|
|
135004
|
+
// return generator;
|
|
135005
|
+
// }
|
|
135006
|
+
// ENUM
|
|
135007
|
+
if (col.enumValues !== undefined) {
|
|
135008
|
+
const generator = new generatorsMap.GenerateEnum[0]({
|
|
135009
|
+
enumValues: col.enumValues,
|
|
135010
|
+
});
|
|
135011
|
+
return generator;
|
|
135012
|
+
}
|
|
135013
|
+
// INTERVAL
|
|
135014
|
+
if (col.columnType.startsWith('interval')) {
|
|
135015
|
+
if (col.columnType === 'interval') {
|
|
135016
|
+
const generator = new generatorsMap.GenerateInterval[0]();
|
|
135017
|
+
return generator;
|
|
135018
|
+
}
|
|
135019
|
+
const fields = col.columnType.replace('interval ', '');
|
|
135020
|
+
const generator = new generatorsMap.GenerateInterval[0]({ fields });
|
|
135021
|
+
return generator;
|
|
135022
|
+
}
|
|
135023
|
+
if (col.hasDefault && col.default !== undefined) {
|
|
135024
|
+
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135025
|
+
defaultValue: col.default,
|
|
135026
|
+
});
|
|
135027
|
+
return generator;
|
|
135028
|
+
}
|
|
135029
|
+
return;
|
|
135030
|
+
};
|
|
135031
|
+
const generator = pickGenerator(table, col);
|
|
135032
|
+
// set params for base column
|
|
135033
|
+
if (generator !== undefined) {
|
|
135034
|
+
generator.isUnique = col.isUnique;
|
|
135035
|
+
generator.dataType = col.dataType;
|
|
135036
|
+
// generator.stringLength = col.typeParams.length;
|
|
135037
|
+
generator.typeParams = col.typeParams;
|
|
135038
|
+
}
|
|
135039
|
+
return generator;
|
|
134373
135040
|
};
|
|
134374
135041
|
|
|
134375
135042
|
const latestVersion = 2;
|
|
134376
135043
|
|
|
135044
|
+
const selectGeneratorForMssqlColumn = (table, col) => {
|
|
135045
|
+
const pickGenerator = (table, col) => {
|
|
135046
|
+
// INT ------------------------------------------------------------------------------------------------------------
|
|
135047
|
+
if (col.columnType.includes('int') && table.primaryKeys.includes(col.name)) {
|
|
135048
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135049
|
+
return generator;
|
|
135050
|
+
}
|
|
135051
|
+
let minValue;
|
|
135052
|
+
let maxValue;
|
|
135053
|
+
if (col.columnType.includes('int')) {
|
|
135054
|
+
if (col.columnType === 'tinyint') {
|
|
135055
|
+
// 2^8 / 2 - 1, 1 bytes
|
|
135056
|
+
// more like unsigned tinyint
|
|
135057
|
+
minValue = 0;
|
|
135058
|
+
maxValue = 255;
|
|
135059
|
+
}
|
|
135060
|
+
else if (col.columnType === 'smallint') {
|
|
135061
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135062
|
+
minValue = -32768;
|
|
135063
|
+
maxValue = 32767;
|
|
135064
|
+
}
|
|
135065
|
+
else if (col.columnType === 'int') {
|
|
135066
|
+
// 2^32 / 2 - 1, 4 bytes
|
|
135067
|
+
minValue = -2147483648;
|
|
135068
|
+
maxValue = 2147483647;
|
|
135069
|
+
}
|
|
135070
|
+
else if (col.columnType === 'bigint') {
|
|
135071
|
+
// 2^64 / 2 - 1, 8 bytes
|
|
135072
|
+
minValue = BigInt('-9223372036854775808');
|
|
135073
|
+
maxValue = BigInt('9223372036854775807');
|
|
135074
|
+
}
|
|
135075
|
+
const generator = new generatorsMap.GenerateInt[0]({
|
|
135076
|
+
minValue,
|
|
135077
|
+
maxValue,
|
|
135078
|
+
});
|
|
135079
|
+
return generator;
|
|
135080
|
+
}
|
|
135081
|
+
// NUMBER(real, decimal, numeric, float)
|
|
135082
|
+
if (col.columnType.startsWith('real')
|
|
135083
|
+
|| col.columnType.startsWith('decimal')
|
|
135084
|
+
|| col.columnType.startsWith('float')
|
|
135085
|
+
|| col.columnType.startsWith('numeric')) {
|
|
135086
|
+
if (col.typeParams.precision !== undefined) {
|
|
135087
|
+
const precision = col.typeParams.precision;
|
|
135088
|
+
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
135089
|
+
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
135090
|
+
const generator = new generatorsMap.GenerateNumber[0]({
|
|
135091
|
+
minValue: -maxAbsoluteValue,
|
|
135092
|
+
maxValue: maxAbsoluteValue,
|
|
135093
|
+
precision: Math.pow(10, scale),
|
|
135094
|
+
});
|
|
135095
|
+
return generator;
|
|
135096
|
+
}
|
|
135097
|
+
const generator = new generatorsMap.GenerateNumber[0]();
|
|
135098
|
+
return generator;
|
|
135099
|
+
}
|
|
135100
|
+
// STRING
|
|
135101
|
+
if ((col.columnType === 'text'
|
|
135102
|
+
|| col.columnType.startsWith('char')
|
|
135103
|
+
|| col.columnType.startsWith('varchar')
|
|
135104
|
+
|| col.columnType.startsWith('binary')
|
|
135105
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135106
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135107
|
+
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135108
|
+
return generator;
|
|
135109
|
+
}
|
|
135110
|
+
if ((col.columnType === 'text'
|
|
135111
|
+
|| col.columnType.startsWith('char')
|
|
135112
|
+
|| col.columnType.startsWith('varchar')
|
|
135113
|
+
|| col.columnType.startsWith('binary')
|
|
135114
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135115
|
+
&& col.name.toLowerCase().includes('name')) {
|
|
135116
|
+
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135117
|
+
return generator;
|
|
135118
|
+
}
|
|
135119
|
+
if ((col.columnType === 'text'
|
|
135120
|
+
|| col.columnType.startsWith('char')
|
|
135121
|
+
|| col.columnType.startsWith('varchar')
|
|
135122
|
+
|| col.columnType.startsWith('binary')
|
|
135123
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135124
|
+
&& col.name.toLowerCase().includes('email')) {
|
|
135125
|
+
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135126
|
+
return generator;
|
|
135127
|
+
}
|
|
135128
|
+
if (col.columnType === 'text'
|
|
135129
|
+
|| col.columnType.startsWith('char')
|
|
135130
|
+
|| col.columnType.startsWith('varchar')
|
|
135131
|
+
|| col.columnType.startsWith('binary')
|
|
135132
|
+
|| col.columnType.startsWith('varbinary')) {
|
|
135133
|
+
const generator = new generatorsMap.GenerateString[0]();
|
|
135134
|
+
return generator;
|
|
135135
|
+
}
|
|
135136
|
+
// bit
|
|
135137
|
+
if (col.columnType === 'bit') {
|
|
135138
|
+
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135139
|
+
return generator;
|
|
135140
|
+
}
|
|
135141
|
+
// DATE, TIME, TIMESTAMP, DATETIME, YEAR
|
|
135142
|
+
if (col.columnType.includes('datetime')) {
|
|
135143
|
+
const generator = new generatorsMap.GenerateDatetime[0]();
|
|
135144
|
+
return generator;
|
|
135145
|
+
}
|
|
135146
|
+
if (col.columnType.includes('date')) {
|
|
135147
|
+
const generator = new generatorsMap.GenerateDate[0]();
|
|
135148
|
+
return generator;
|
|
135149
|
+
}
|
|
135150
|
+
if (col.columnType === 'time') {
|
|
135151
|
+
const generator = new generatorsMap.GenerateTime[0]();
|
|
135152
|
+
return generator;
|
|
135153
|
+
}
|
|
135154
|
+
// JSON
|
|
135155
|
+
if (col.columnType === 'json') {
|
|
135156
|
+
const generator = new generatorsMap.GenerateJson[0]();
|
|
135157
|
+
return generator;
|
|
135158
|
+
}
|
|
135159
|
+
if (col.hasDefault && col.default !== undefined) {
|
|
135160
|
+
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135161
|
+
defaultValue: col.default,
|
|
135162
|
+
});
|
|
135163
|
+
return generator;
|
|
135164
|
+
}
|
|
135165
|
+
return;
|
|
135166
|
+
};
|
|
135167
|
+
const generator = pickGenerator(table, col);
|
|
135168
|
+
return generator;
|
|
135169
|
+
};
|
|
135170
|
+
|
|
135171
|
+
const selectGeneratorForMysqlColumn = (table, col) => {
|
|
135172
|
+
const pickGenerator = (table, col) => {
|
|
135173
|
+
// INT ------------------------------------------------------------------------------------------------------------
|
|
135174
|
+
if ((col.columnType.includes('serial') || col.columnType.includes('int'))
|
|
135175
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135176
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135177
|
+
return generator;
|
|
135178
|
+
}
|
|
135179
|
+
let minValue;
|
|
135180
|
+
let maxValue;
|
|
135181
|
+
if (col.columnType === 'serial') {
|
|
135182
|
+
// 2^64 % 2 - 1, 8 bytes
|
|
135183
|
+
minValue = BigInt(0);
|
|
135184
|
+
maxValue = BigInt('9223372036854775807');
|
|
135185
|
+
}
|
|
135186
|
+
else if (col.columnType.includes('int')) {
|
|
135187
|
+
if (col.columnType === 'tinyint') {
|
|
135188
|
+
// 2^8 / 2 - 1, 1 bytes
|
|
135189
|
+
minValue = -128;
|
|
135190
|
+
maxValue = 127;
|
|
135191
|
+
}
|
|
135192
|
+
else if (col.columnType === 'smallint') {
|
|
135193
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135194
|
+
minValue = -32768;
|
|
135195
|
+
maxValue = 32767;
|
|
135196
|
+
}
|
|
135197
|
+
else if (col.columnType === 'mediumint') {
|
|
135198
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135199
|
+
minValue = -8388608;
|
|
135200
|
+
maxValue = 8388607;
|
|
135201
|
+
}
|
|
135202
|
+
else if (col.columnType === 'int') {
|
|
135203
|
+
// 2^32 / 2 - 1, 4 bytes
|
|
135204
|
+
minValue = -2147483648;
|
|
135205
|
+
maxValue = 2147483647;
|
|
135206
|
+
}
|
|
135207
|
+
else if (col.columnType === 'bigint') {
|
|
135208
|
+
// 2^64 / 2 - 1, 8 bytes
|
|
135209
|
+
minValue = BigInt('-9223372036854775808');
|
|
135210
|
+
maxValue = BigInt('9223372036854775807');
|
|
135211
|
+
}
|
|
135212
|
+
}
|
|
135213
|
+
if (col.columnType.includes('int')) {
|
|
135214
|
+
const generator = new generatorsMap.GenerateInt[0]({
|
|
135215
|
+
minValue,
|
|
135216
|
+
maxValue,
|
|
135217
|
+
});
|
|
135218
|
+
return generator;
|
|
135219
|
+
}
|
|
135220
|
+
if (col.columnType.includes('serial')) {
|
|
135221
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135222
|
+
generator.maxValue = maxValue;
|
|
135223
|
+
return generator;
|
|
135224
|
+
}
|
|
135225
|
+
// NUMBER(real, double, decimal, float)
|
|
135226
|
+
if (col.columnType.startsWith('real')
|
|
135227
|
+
|| col.columnType.startsWith('double')
|
|
135228
|
+
|| col.columnType.startsWith('decimal')
|
|
135229
|
+
|| col.columnType.startsWith('float')
|
|
135230
|
+
|| col.columnType.startsWith('numeric')) {
|
|
135231
|
+
if (col.typeParams.precision !== undefined) {
|
|
135232
|
+
const precision = col.typeParams.precision;
|
|
135233
|
+
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
135234
|
+
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
135235
|
+
const generator = new generatorsMap.GenerateNumber[0]({
|
|
135236
|
+
minValue: -maxAbsoluteValue,
|
|
135237
|
+
maxValue: maxAbsoluteValue,
|
|
135238
|
+
precision: Math.pow(10, scale),
|
|
135239
|
+
});
|
|
135240
|
+
return generator;
|
|
135241
|
+
}
|
|
135242
|
+
const generator = new generatorsMap.GenerateNumber[0]();
|
|
135243
|
+
return generator;
|
|
135244
|
+
}
|
|
135245
|
+
// STRING
|
|
135246
|
+
if ((col.columnType === 'text'
|
|
135247
|
+
|| col.columnType === 'blob'
|
|
135248
|
+
|| col.columnType.startsWith('char')
|
|
135249
|
+
|| col.columnType.startsWith('varchar')
|
|
135250
|
+
|| col.columnType.startsWith('binary')
|
|
135251
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135252
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135253
|
+
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135254
|
+
return generator;
|
|
135255
|
+
}
|
|
135256
|
+
if ((col.columnType === 'text'
|
|
135257
|
+
|| col.columnType === 'blob'
|
|
135258
|
+
|| col.columnType.startsWith('char')
|
|
135259
|
+
|| col.columnType.startsWith('varchar')
|
|
135260
|
+
|| col.columnType.startsWith('binary')
|
|
135261
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135262
|
+
&& col.name.toLowerCase().includes('name')) {
|
|
135263
|
+
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135264
|
+
return generator;
|
|
135265
|
+
}
|
|
135266
|
+
if ((col.columnType === 'text'
|
|
135267
|
+
|| col.columnType === 'blob'
|
|
135268
|
+
|| col.columnType.startsWith('char')
|
|
135269
|
+
|| col.columnType.startsWith('varchar')
|
|
135270
|
+
|| col.columnType.startsWith('binary')
|
|
135271
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135272
|
+
&& col.name.toLowerCase().includes('email')) {
|
|
135273
|
+
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135274
|
+
return generator;
|
|
135275
|
+
}
|
|
135276
|
+
if (col.columnType === 'text'
|
|
135277
|
+
|| col.columnType === 'blob'
|
|
135278
|
+
|| col.columnType.startsWith('char')
|
|
135279
|
+
|| col.columnType.startsWith('varchar')
|
|
135280
|
+
|| col.columnType.startsWith('binary')
|
|
135281
|
+
|| col.columnType.startsWith('varbinary')) {
|
|
135282
|
+
const generator = new generatorsMap.GenerateString[0]();
|
|
135283
|
+
return generator;
|
|
135284
|
+
}
|
|
135285
|
+
// BOOLEAN
|
|
135286
|
+
if (col.columnType === 'boolean') {
|
|
135287
|
+
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135288
|
+
return generator;
|
|
135289
|
+
}
|
|
135290
|
+
// DATE, TIME, TIMESTAMP, DATETIME, YEAR
|
|
135291
|
+
if (col.columnType.includes('datetime')) {
|
|
135292
|
+
const generator = new generatorsMap.GenerateDatetime[0]();
|
|
135293
|
+
return generator;
|
|
135294
|
+
}
|
|
135295
|
+
if (col.columnType.includes('date')) {
|
|
135296
|
+
const generator = new generatorsMap.GenerateDate[0]();
|
|
135297
|
+
return generator;
|
|
135298
|
+
}
|
|
135299
|
+
if (col.columnType === 'time') {
|
|
135300
|
+
const generator = new generatorsMap.GenerateTime[0]();
|
|
135301
|
+
return generator;
|
|
135302
|
+
}
|
|
135303
|
+
if (col.columnType.includes('timestamp')) {
|
|
135304
|
+
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135305
|
+
return generator;
|
|
135306
|
+
}
|
|
135307
|
+
if (col.columnType === 'year') {
|
|
135308
|
+
const generator = new generatorsMap.GenerateYear[0]();
|
|
135309
|
+
return generator;
|
|
135310
|
+
}
|
|
135311
|
+
// JSON
|
|
135312
|
+
if (col.columnType === 'json') {
|
|
135313
|
+
const generator = new generatorsMap.GenerateJson[0]();
|
|
135314
|
+
return generator;
|
|
135315
|
+
}
|
|
135316
|
+
// ENUM
|
|
135317
|
+
if (col.enumValues !== undefined) {
|
|
135318
|
+
const generator = new generatorsMap.GenerateEnum[0]({
|
|
135319
|
+
enumValues: col.enumValues,
|
|
135320
|
+
});
|
|
135321
|
+
return generator;
|
|
135322
|
+
}
|
|
135323
|
+
if (col.hasDefault && col.default !== undefined) {
|
|
135324
|
+
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135325
|
+
defaultValue: col.default,
|
|
135326
|
+
});
|
|
135327
|
+
return generator;
|
|
135328
|
+
}
|
|
135329
|
+
return;
|
|
135330
|
+
};
|
|
135331
|
+
const generator = pickGenerator(table, col);
|
|
135332
|
+
return generator;
|
|
135333
|
+
};
|
|
135334
|
+
|
|
135335
|
+
// TODO: revise serial part generators
|
|
135336
|
+
const selectGeneratorForPostgresColumn = (table, col) => {
|
|
135337
|
+
const pickGenerator = (table, col) => {
|
|
135338
|
+
// ARRAY
|
|
135339
|
+
if (col.columnType.match(/\[\w*]/g) !== null && col.baseColumn !== undefined) {
|
|
135340
|
+
const baseColumnGen = selectGeneratorForPostgresColumn(table, col.baseColumn);
|
|
135341
|
+
if (baseColumnGen === undefined) {
|
|
135342
|
+
throw new Error(`column with type ${col.baseColumn.columnType} is not supported for now.`);
|
|
135343
|
+
}
|
|
135344
|
+
// const getBaseColumnDataType = (baseColumn: Column) => {
|
|
135345
|
+
// if (baseColumn.baseColumn !== undefined) {
|
|
135346
|
+
// return getBaseColumnDataType(baseColumn.baseColumn);
|
|
135347
|
+
// }
|
|
135348
|
+
// return baseColumn.dataType;
|
|
135349
|
+
// };
|
|
135350
|
+
// const baseColumnDataType = getBaseColumnDataType(col.baseColumn);
|
|
135351
|
+
const generator = new generatorsMap.GenerateArray[0]({ baseColumnGen, size: col.size });
|
|
135352
|
+
// generator.baseColumnDataType = baseColumnDataType;
|
|
135353
|
+
return generator;
|
|
135354
|
+
}
|
|
135355
|
+
// ARRAY for studio
|
|
135356
|
+
if (col.columnType.match(/\[\w*]/g) !== null) {
|
|
135357
|
+
// remove dimensions from type
|
|
135358
|
+
const baseColumnType = col.columnType.replace(/\[\w*]/g, '');
|
|
135359
|
+
const baseColumn = {
|
|
135360
|
+
...col,
|
|
135361
|
+
};
|
|
135362
|
+
baseColumn.columnType = baseColumnType;
|
|
135363
|
+
const baseColumnGen = selectGeneratorForPostgresColumn(table, baseColumn);
|
|
135364
|
+
if (baseColumnGen === undefined) {
|
|
135365
|
+
throw new Error(`column with type ${col.baseColumn.columnType} is not supported for now.`);
|
|
135366
|
+
}
|
|
135367
|
+
let generator = new generatorsMap.GenerateArray[0]({ baseColumnGen });
|
|
135368
|
+
for (let i = 0; i < col.typeParams.dimensions - 1; i++) {
|
|
135369
|
+
generator = new generatorsMap.GenerateArray[0]({ baseColumnGen: generator });
|
|
135370
|
+
}
|
|
135371
|
+
return generator;
|
|
135372
|
+
}
|
|
135373
|
+
// INT ------------------------------------------------------------------------------------------------------------
|
|
135374
|
+
if ((col.columnType.includes('serial')
|
|
135375
|
+
|| col.columnType === 'integer'
|
|
135376
|
+
|| col.columnType === 'smallint'
|
|
135377
|
+
|| col.columnType.includes('bigint'))
|
|
135378
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135379
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135380
|
+
return generator;
|
|
135381
|
+
}
|
|
135382
|
+
let minValue;
|
|
135383
|
+
let maxValue;
|
|
135384
|
+
if (col.columnType.includes('serial')) {
|
|
135385
|
+
minValue = 1;
|
|
135386
|
+
if (col.columnType === 'smallserial') {
|
|
135387
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135388
|
+
maxValue = 32767;
|
|
135389
|
+
}
|
|
135390
|
+
else if (col.columnType === 'serial') {
|
|
135391
|
+
// 2^32 / 2 - 1, 4 bytes
|
|
135392
|
+
maxValue = 2147483647;
|
|
135393
|
+
}
|
|
135394
|
+
else if (col.columnType === 'bigserial') {
|
|
135395
|
+
// 2^64 / 2 - 1, 8 bytes
|
|
135396
|
+
minValue = BigInt(1);
|
|
135397
|
+
maxValue = BigInt('9223372036854775807');
|
|
135398
|
+
}
|
|
135399
|
+
}
|
|
135400
|
+
else if (col.columnType.includes('int')) {
|
|
135401
|
+
if (col.columnType === 'smallint') {
|
|
135402
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135403
|
+
minValue = -32768;
|
|
135404
|
+
maxValue = 32767;
|
|
135405
|
+
}
|
|
135406
|
+
else if (col.columnType === 'integer') {
|
|
135407
|
+
// 2^32 / 2 - 1, 4 bytes
|
|
135408
|
+
minValue = -2147483648;
|
|
135409
|
+
maxValue = 2147483647;
|
|
135410
|
+
}
|
|
135411
|
+
else if (col.columnType.includes('bigint')) {
|
|
135412
|
+
if (col.dataType === 'bigint') {
|
|
135413
|
+
// 2^64 / 2 - 1, 8 bytes
|
|
135414
|
+
minValue = BigInt('-9223372036854775808');
|
|
135415
|
+
maxValue = BigInt('9223372036854775807');
|
|
135416
|
+
}
|
|
135417
|
+
else {
|
|
135418
|
+
// if (col.dataType === 'number')
|
|
135419
|
+
// if you’re expecting values above 2^31 but below 2^53
|
|
135420
|
+
minValue = -9007199254740991;
|
|
135421
|
+
maxValue = 9007199254740991;
|
|
135422
|
+
}
|
|
135423
|
+
}
|
|
135424
|
+
}
|
|
135425
|
+
if (col.columnType.includes('int')
|
|
135426
|
+
&& !col.columnType.includes('interval')
|
|
135427
|
+
&& !col.columnType.includes('point')) {
|
|
135428
|
+
const generator = new generatorsMap.GenerateInt[0]({
|
|
135429
|
+
minValue,
|
|
135430
|
+
maxValue,
|
|
135431
|
+
});
|
|
135432
|
+
return generator;
|
|
135433
|
+
}
|
|
135434
|
+
if (col.columnType.includes('serial')) {
|
|
135435
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135436
|
+
generator.maxValue = maxValue;
|
|
135437
|
+
return generator;
|
|
135438
|
+
}
|
|
135439
|
+
// NUMBER(real, double, decimal, numeric)
|
|
135440
|
+
if (col.columnType.startsWith('real')
|
|
135441
|
+
|| col.columnType.startsWith('double precision')
|
|
135442
|
+
|| col.columnType.startsWith('decimal')
|
|
135443
|
+
|| col.columnType.startsWith('numeric')) {
|
|
135444
|
+
if (col.typeParams.precision !== undefined) {
|
|
135445
|
+
const precision = col.typeParams.precision;
|
|
135446
|
+
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
135447
|
+
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
135448
|
+
const generator = new generatorsMap.GenerateNumber[0]({
|
|
135449
|
+
minValue: -maxAbsoluteValue,
|
|
135450
|
+
maxValue: maxAbsoluteValue,
|
|
135451
|
+
precision: Math.pow(10, scale),
|
|
135452
|
+
});
|
|
135453
|
+
return generator;
|
|
135454
|
+
}
|
|
135455
|
+
const generator = new generatorsMap.GenerateNumber[0]();
|
|
135456
|
+
return generator;
|
|
135457
|
+
}
|
|
135458
|
+
// STRING
|
|
135459
|
+
if ((col.columnType === 'text'
|
|
135460
|
+
|| col.columnType.startsWith('varchar')
|
|
135461
|
+
|| col.columnType.startsWith('char'))
|
|
135462
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135463
|
+
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135464
|
+
return generator;
|
|
135465
|
+
}
|
|
135466
|
+
if ((col.columnType === 'text'
|
|
135467
|
+
|| col.columnType.startsWith('varchar')
|
|
135468
|
+
|| col.columnType.startsWith('char'))
|
|
135469
|
+
&& col.name.toLowerCase().includes('name')) {
|
|
135470
|
+
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135471
|
+
return generator;
|
|
135472
|
+
}
|
|
135473
|
+
if ((col.columnType === 'text'
|
|
135474
|
+
|| col.columnType.startsWith('varchar')
|
|
135475
|
+
|| col.columnType.startsWith('char'))
|
|
135476
|
+
&& col.name.toLowerCase().includes('email')) {
|
|
135477
|
+
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135478
|
+
return generator;
|
|
135479
|
+
}
|
|
135480
|
+
if (col.columnType === 'text'
|
|
135481
|
+
|| col.columnType.startsWith('varchar')
|
|
135482
|
+
|| col.columnType.startsWith('char')) {
|
|
135483
|
+
const generator = new generatorsMap.GenerateString[0]();
|
|
135484
|
+
return generator;
|
|
135485
|
+
}
|
|
135486
|
+
// BIT
|
|
135487
|
+
if (col.columnType.startsWith('bit')) {
|
|
135488
|
+
const generator = new generatorsMap.GenerateBitString[0]();
|
|
135489
|
+
return generator;
|
|
135490
|
+
}
|
|
135491
|
+
// INET
|
|
135492
|
+
if (col.columnType === 'inet') {
|
|
135493
|
+
const generator = new generatorsMap.GenerateInet[0]();
|
|
135494
|
+
return generator;
|
|
135495
|
+
}
|
|
135496
|
+
// geometry(point)
|
|
135497
|
+
if (col.columnType.startsWith('geometry')) {
|
|
135498
|
+
const generator = new generatorsMap.GenerateGeometry[0]();
|
|
135499
|
+
return generator;
|
|
135500
|
+
}
|
|
135501
|
+
// vector
|
|
135502
|
+
if (col.columnType.startsWith('vector')) {
|
|
135503
|
+
const generator = new generatorsMap.GenerateVector[0]();
|
|
135504
|
+
return generator;
|
|
135505
|
+
}
|
|
135506
|
+
// UUID
|
|
135507
|
+
if (col.columnType === 'uuid') {
|
|
135508
|
+
const generator = new generatorsMap.GenerateUUID[0]();
|
|
135509
|
+
return generator;
|
|
135510
|
+
}
|
|
135511
|
+
// BOOLEAN
|
|
135512
|
+
if (col.columnType === 'boolean') {
|
|
135513
|
+
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135514
|
+
return generator;
|
|
135515
|
+
}
|
|
135516
|
+
// DATE, TIME, TIMESTAMP
|
|
135517
|
+
if (col.columnType.includes('date')) {
|
|
135518
|
+
const generator = new generatorsMap.GenerateDate[0]();
|
|
135519
|
+
return generator;
|
|
135520
|
+
}
|
|
135521
|
+
if (col.columnType === 'time') {
|
|
135522
|
+
const generator = new generatorsMap.GenerateTime[0]();
|
|
135523
|
+
return generator;
|
|
135524
|
+
}
|
|
135525
|
+
if (col.columnType.includes('timestamp')) {
|
|
135526
|
+
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135527
|
+
return generator;
|
|
135528
|
+
}
|
|
135529
|
+
// JSON, JSONB
|
|
135530
|
+
if (col.columnType === 'json' || col.columnType === 'jsonb') {
|
|
135531
|
+
const generator = new generatorsMap.GenerateJson[0]();
|
|
135532
|
+
return generator;
|
|
135533
|
+
}
|
|
135534
|
+
// if (col.columnType === "jsonb") {
|
|
135535
|
+
// const generator = new GenerateJsonb({});
|
|
135536
|
+
// return generator;
|
|
135537
|
+
// }
|
|
135538
|
+
// ENUM
|
|
135539
|
+
if (col.enumValues !== undefined) {
|
|
135540
|
+
const generator = new generatorsMap.GenerateEnum[0]({
|
|
135541
|
+
enumValues: col.enumValues,
|
|
135542
|
+
});
|
|
135543
|
+
return generator;
|
|
135544
|
+
}
|
|
135545
|
+
// INTERVAL
|
|
135546
|
+
if (col.columnType.startsWith('interval')) {
|
|
135547
|
+
if (col.columnType === 'interval') {
|
|
135548
|
+
const generator = new generatorsMap.GenerateInterval[0]();
|
|
135549
|
+
return generator;
|
|
135550
|
+
}
|
|
135551
|
+
const fields = col.columnType.replace('interval ', '');
|
|
135552
|
+
const generator = new generatorsMap.GenerateInterval[0]({ fields });
|
|
135553
|
+
return generator;
|
|
135554
|
+
}
|
|
135555
|
+
// POINT, LINE
|
|
135556
|
+
if (col.columnType.includes('point')) {
|
|
135557
|
+
const generator = new generatorsMap.GeneratePoint[0]();
|
|
135558
|
+
return generator;
|
|
135559
|
+
}
|
|
135560
|
+
if (col.columnType.includes('line')) {
|
|
135561
|
+
const generator = new generatorsMap.GenerateLine[0]();
|
|
135562
|
+
return generator;
|
|
135563
|
+
}
|
|
135564
|
+
if (col.hasDefault && col.default !== undefined) {
|
|
135565
|
+
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135566
|
+
defaultValue: col.default,
|
|
135567
|
+
});
|
|
135568
|
+
return generator;
|
|
135569
|
+
}
|
|
135570
|
+
return;
|
|
135571
|
+
};
|
|
135572
|
+
const generator = pickGenerator(table, col);
|
|
135573
|
+
if (generator !== undefined) {
|
|
135574
|
+
generator.isUnique = col.isUnique;
|
|
135575
|
+
generator.dataType = col.dataType;
|
|
135576
|
+
generator.typeParams = col.typeParams;
|
|
135577
|
+
// generator.stringLength = col.typeParams.length;
|
|
135578
|
+
}
|
|
135579
|
+
return generator;
|
|
135580
|
+
};
|
|
135581
|
+
|
|
135582
|
+
const selectGeneratorForSingleStoreColumn = (table, col) => {
|
|
135583
|
+
const pickGenerator = (table, col) => {
|
|
135584
|
+
// INT ------------------------------------------------------------------------------------------------------------
|
|
135585
|
+
if ((col.columnType.includes('serial') || col.columnType.includes('int'))
|
|
135586
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135587
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135588
|
+
return generator;
|
|
135589
|
+
}
|
|
135590
|
+
let minValue;
|
|
135591
|
+
let maxValue;
|
|
135592
|
+
if (col.columnType === 'serial') {
|
|
135593
|
+
// 2^64 % 2 - 1, 8 bytes
|
|
135594
|
+
minValue = BigInt(0);
|
|
135595
|
+
maxValue = BigInt('9223372036854775807');
|
|
135596
|
+
}
|
|
135597
|
+
else if (col.columnType.includes('int')) {
|
|
135598
|
+
if (col.columnType === 'tinyint') {
|
|
135599
|
+
// 2^8 / 2 - 1, 1 bytes
|
|
135600
|
+
minValue = -128;
|
|
135601
|
+
maxValue = 127;
|
|
135602
|
+
}
|
|
135603
|
+
else if (col.columnType === 'smallint') {
|
|
135604
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135605
|
+
minValue = -32768;
|
|
135606
|
+
maxValue = 32767;
|
|
135607
|
+
}
|
|
135608
|
+
else if (col.columnType === 'mediumint') {
|
|
135609
|
+
// 2^16 / 2 - 1, 2 bytes
|
|
135610
|
+
minValue = -8388608;
|
|
135611
|
+
maxValue = 8388607;
|
|
135612
|
+
}
|
|
135613
|
+
else if (col.columnType === 'int') {
|
|
135614
|
+
// 2^32 / 2 - 1, 4 bytes
|
|
135615
|
+
minValue = -2147483648;
|
|
135616
|
+
maxValue = 2147483647;
|
|
135617
|
+
}
|
|
135618
|
+
else if (col.columnType === 'bigint') {
|
|
135619
|
+
// 2^64 / 2 - 1, 8 bytes
|
|
135620
|
+
minValue = BigInt('-9223372036854775808');
|
|
135621
|
+
maxValue = BigInt('9223372036854775807');
|
|
135622
|
+
}
|
|
135623
|
+
}
|
|
135624
|
+
if (col.columnType.includes('int')) {
|
|
135625
|
+
const generator = new generatorsMap.GenerateInt[0]({
|
|
135626
|
+
minValue,
|
|
135627
|
+
maxValue,
|
|
135628
|
+
});
|
|
135629
|
+
return generator;
|
|
135630
|
+
}
|
|
135631
|
+
if (col.columnType.includes('serial')) {
|
|
135632
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135633
|
+
generator.maxValue = maxValue;
|
|
135634
|
+
return generator;
|
|
135635
|
+
}
|
|
135636
|
+
// NUMBER(real, double, decimal, float)
|
|
135637
|
+
if (col.columnType.startsWith('real')
|
|
135638
|
+
|| col.columnType.startsWith('double')
|
|
135639
|
+
|| col.columnType.startsWith('decimal')
|
|
135640
|
+
|| col.columnType.startsWith('float')
|
|
135641
|
+
|| col.columnType.startsWith('numeric')) {
|
|
135642
|
+
if (col.typeParams.precision !== undefined) {
|
|
135643
|
+
const precision = col.typeParams.precision;
|
|
135644
|
+
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
135645
|
+
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
135646
|
+
const generator = new generatorsMap.GenerateNumber[0]({
|
|
135647
|
+
minValue: -maxAbsoluteValue,
|
|
135648
|
+
maxValue: maxAbsoluteValue,
|
|
135649
|
+
precision: Math.pow(10, scale),
|
|
135650
|
+
});
|
|
135651
|
+
return generator;
|
|
135652
|
+
}
|
|
135653
|
+
const generator = new generatorsMap.GenerateNumber[0]();
|
|
135654
|
+
return generator;
|
|
135655
|
+
}
|
|
135656
|
+
// STRING
|
|
135657
|
+
if ((col.columnType === 'tinytext'
|
|
135658
|
+
|| col.columnType === 'mediumtext'
|
|
135659
|
+
|| col.columnType === 'text'
|
|
135660
|
+
|| col.columnType === 'longtext'
|
|
135661
|
+
|| col.columnType === 'blob'
|
|
135662
|
+
|| col.columnType.startsWith('char')
|
|
135663
|
+
|| col.columnType.startsWith('varchar')
|
|
135664
|
+
|| col.columnType.startsWith('binary')
|
|
135665
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135666
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135667
|
+
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135668
|
+
return generator;
|
|
135669
|
+
}
|
|
135670
|
+
if ((col.columnType === 'tinytext'
|
|
135671
|
+
|| col.columnType === 'mediumtext'
|
|
135672
|
+
|| col.columnType === 'text'
|
|
135673
|
+
|| col.columnType === 'longtext'
|
|
135674
|
+
|| col.columnType === 'blob'
|
|
135675
|
+
|| col.columnType.startsWith('char')
|
|
135676
|
+
|| col.columnType.startsWith('varchar')
|
|
135677
|
+
|| col.columnType.startsWith('binary')
|
|
135678
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135679
|
+
&& col.name.toLowerCase().includes('name')) {
|
|
135680
|
+
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135681
|
+
return generator;
|
|
135682
|
+
}
|
|
135683
|
+
if ((col.columnType === 'tinytext'
|
|
135684
|
+
|| col.columnType === 'mediumtext'
|
|
135685
|
+
|| col.columnType === 'text'
|
|
135686
|
+
|| col.columnType === 'longtext'
|
|
135687
|
+
|| col.columnType === 'blob'
|
|
135688
|
+
|| col.columnType.startsWith('char')
|
|
135689
|
+
|| col.columnType.startsWith('varchar')
|
|
135690
|
+
|| col.columnType.startsWith('binary')
|
|
135691
|
+
|| col.columnType.startsWith('varbinary'))
|
|
135692
|
+
&& col.name.toLowerCase().includes('email')) {
|
|
135693
|
+
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135694
|
+
return generator;
|
|
135695
|
+
}
|
|
135696
|
+
if (col.columnType === 'tinytext'
|
|
135697
|
+
|| col.columnType === 'mediumtext'
|
|
135698
|
+
|| col.columnType === 'text'
|
|
135699
|
+
|| col.columnType === 'longtext'
|
|
135700
|
+
|| col.columnType === 'blob'
|
|
135701
|
+
|| col.columnType.startsWith('char')
|
|
135702
|
+
|| col.columnType.startsWith('varchar')
|
|
135703
|
+
|| col.columnType.startsWith('binary')
|
|
135704
|
+
|| col.columnType.startsWith('varbinary')) {
|
|
135705
|
+
const generator = new generatorsMap.GenerateString[0]();
|
|
135706
|
+
return generator;
|
|
135707
|
+
}
|
|
135708
|
+
// BOOLEAN
|
|
135709
|
+
if (col.columnType === 'boolean') {
|
|
135710
|
+
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135711
|
+
return generator;
|
|
135712
|
+
}
|
|
135713
|
+
// DATE, TIME, TIMESTAMP, DATETIME, YEAR
|
|
135714
|
+
if (col.columnType.includes('datetime')) {
|
|
135715
|
+
const generator = new generatorsMap.GenerateDatetime[0]();
|
|
135716
|
+
return generator;
|
|
135717
|
+
}
|
|
135718
|
+
if (col.columnType.includes('date')) {
|
|
135719
|
+
const generator = new generatorsMap.GenerateDate[0]();
|
|
135720
|
+
return generator;
|
|
135721
|
+
}
|
|
135722
|
+
if (col.columnType === 'time') {
|
|
135723
|
+
const generator = new generatorsMap.GenerateTime[0]();
|
|
135724
|
+
return generator;
|
|
135725
|
+
}
|
|
135726
|
+
if (col.columnType.includes('timestamp')) {
|
|
135727
|
+
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135728
|
+
return generator;
|
|
135729
|
+
}
|
|
135730
|
+
if (col.columnType === 'year') {
|
|
135731
|
+
const generator = new generatorsMap.GenerateYear[0]();
|
|
135732
|
+
return generator;
|
|
135733
|
+
}
|
|
135734
|
+
// JSON
|
|
135735
|
+
if (col.columnType === 'json') {
|
|
135736
|
+
const generator = new generatorsMap.GenerateJson[0]();
|
|
135737
|
+
return generator;
|
|
135738
|
+
}
|
|
135739
|
+
// ENUM
|
|
135740
|
+
if (col.enumValues !== undefined) {
|
|
135741
|
+
const generator = new generatorsMap.GenerateEnum[0]({
|
|
135742
|
+
enumValues: col.enumValues,
|
|
135743
|
+
});
|
|
135744
|
+
return generator;
|
|
135745
|
+
}
|
|
135746
|
+
// vector
|
|
135747
|
+
if (col.columnType.startsWith('vector')) {
|
|
135748
|
+
let minValue, maxValue, decimalPlaces;
|
|
135749
|
+
if (col.typeParams.vectorValueType === 'I8') {
|
|
135750
|
+
minValue = -128;
|
|
135751
|
+
maxValue = 127;
|
|
135752
|
+
decimalPlaces = 0;
|
|
135753
|
+
}
|
|
135754
|
+
else if (col.typeParams.vectorValueType === 'I16') {
|
|
135755
|
+
minValue = -32768;
|
|
135756
|
+
maxValue = 32767;
|
|
135757
|
+
decimalPlaces = 0;
|
|
135758
|
+
}
|
|
135759
|
+
else if (col.typeParams.vectorValueType === 'I32') {
|
|
135760
|
+
minValue = -2147483648;
|
|
135761
|
+
maxValue = 2147483647;
|
|
135762
|
+
decimalPlaces = 0;
|
|
135763
|
+
}
|
|
135764
|
+
else if (col.typeParams.vectorValueType === 'I64') {
|
|
135765
|
+
minValue = Number.MIN_SAFE_INTEGER;
|
|
135766
|
+
maxValue = Number.MAX_SAFE_INTEGER;
|
|
135767
|
+
// minValue = -BigInt('9223372036854775808');
|
|
135768
|
+
// maxValue = BigInt('9223372036854775807');
|
|
135769
|
+
decimalPlaces = 0;
|
|
135770
|
+
}
|
|
135771
|
+
else if (col.typeParams.vectorValueType === 'F32') {
|
|
135772
|
+
minValue = -2147483648;
|
|
135773
|
+
maxValue = 2147483647;
|
|
135774
|
+
decimalPlaces = 6;
|
|
135775
|
+
}
|
|
135776
|
+
else if (col.typeParams.vectorValueType === 'F64') {
|
|
135777
|
+
minValue = -524288;
|
|
135778
|
+
maxValue = 524287;
|
|
135779
|
+
decimalPlaces = 10;
|
|
135780
|
+
}
|
|
135781
|
+
const generator = new generatorsMap.GenerateVector[0]({ minValue, maxValue, decimalPlaces });
|
|
135782
|
+
return generator;
|
|
135783
|
+
}
|
|
135784
|
+
if (col.hasDefault && col.default !== undefined) {
|
|
135785
|
+
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135786
|
+
defaultValue: col.default,
|
|
135787
|
+
});
|
|
135788
|
+
return generator;
|
|
135789
|
+
}
|
|
135790
|
+
return;
|
|
135791
|
+
};
|
|
135792
|
+
const generator = pickGenerator(table, col);
|
|
135793
|
+
return generator;
|
|
135794
|
+
};
|
|
135795
|
+
|
|
135796
|
+
const selectGeneratorForSqlite = (table, col) => {
|
|
135797
|
+
const pickGenerator = (table, col) => {
|
|
135798
|
+
// int section ---------------------------------------------------------------------------------------
|
|
135799
|
+
if ((col.columnType === 'integer' || col.columnType === 'numeric')
|
|
135800
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135801
|
+
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135802
|
+
return generator;
|
|
135803
|
+
}
|
|
135804
|
+
if (col.columnType === 'integer' && col.dataType === 'boolean') {
|
|
135805
|
+
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135806
|
+
return generator;
|
|
135807
|
+
}
|
|
135808
|
+
if ((col.columnType === 'integer' && col.dataType === 'date')) {
|
|
135809
|
+
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135810
|
+
return generator;
|
|
135811
|
+
}
|
|
135812
|
+
if (col.columnType === 'integer'
|
|
135813
|
+
|| (col.dataType === 'bigint' && col.columnType === 'blob')) {
|
|
135814
|
+
const generator = new generatorsMap.GenerateInt[0]();
|
|
135815
|
+
return generator;
|
|
135816
|
+
}
|
|
135817
|
+
// number section ------------------------------------------------------------------------------------
|
|
135818
|
+
if (col.columnType.startsWith('real') || col.columnType.startsWith('numeric')) {
|
|
135819
|
+
if (col.typeParams.precision !== undefined) {
|
|
135820
|
+
const precision = col.typeParams.precision;
|
|
135821
|
+
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
135822
|
+
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
135823
|
+
const generator = new generatorsMap.GenerateNumber[0]({
|
|
135824
|
+
minValue: -maxAbsoluteValue,
|
|
135825
|
+
maxValue: maxAbsoluteValue,
|
|
135826
|
+
precision: Math.pow(10, scale),
|
|
135827
|
+
});
|
|
135828
|
+
return generator;
|
|
135829
|
+
}
|
|
135830
|
+
const generator = new generatorsMap.GenerateNumber[0]();
|
|
135831
|
+
return generator;
|
|
135832
|
+
}
|
|
135833
|
+
// string section ------------------------------------------------------------------------------------
|
|
135834
|
+
if ((col.columnType.startsWith('text')
|
|
135835
|
+
|| col.columnType.startsWith('numeric')
|
|
135836
|
+
|| col.columnType.startsWith('blob'))
|
|
135837
|
+
&& table.primaryKeys.includes(col.name)) {
|
|
135838
|
+
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135839
|
+
return generator;
|
|
135840
|
+
}
|
|
135841
|
+
if ((col.columnType.startsWith('text')
|
|
135842
|
+
|| col.columnType.startsWith('numeric')
|
|
135843
|
+
|| col.columnType.startsWith('blob'))
|
|
135844
|
+
&& col.name.toLowerCase().includes('name')) {
|
|
135845
|
+
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135846
|
+
return generator;
|
|
135847
|
+
}
|
|
135848
|
+
if ((col.columnType.startsWith('text')
|
|
135849
|
+
|| col.columnType.startsWith('numeric')
|
|
135850
|
+
|| col.columnType.startsWith('blob'))
|
|
135851
|
+
&& col.name.toLowerCase().includes('email')) {
|
|
135852
|
+
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135853
|
+
return generator;
|
|
135854
|
+
}
|
|
135855
|
+
if (col.columnType.startsWith('text')
|
|
135856
|
+
|| col.columnType.startsWith('numeric')
|
|
135857
|
+
|| col.columnType.startsWith('blob')
|
|
135858
|
+
|| col.columnType.startsWith('blobbuffer')) {
|
|
135859
|
+
const generator = new generatorsMap.GenerateString[0]();
|
|
135860
|
+
return generator;
|
|
135861
|
+
}
|
|
135862
|
+
if ((col.columnType.startsWith('text') && col.dataType === 'json')
|
|
135863
|
+
|| (col.columnType.startsWith('blob') && col.dataType === 'json')) {
|
|
135864
|
+
const generator = new generatorsMap.GenerateJson[0]();
|
|
135865
|
+
return generator;
|
|
135866
|
+
}
|
|
135867
|
+
if (col.hasDefault && col.default !== undefined) {
|
|
135868
|
+
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135869
|
+
defaultValue: col.default,
|
|
135870
|
+
});
|
|
135871
|
+
return generator;
|
|
135872
|
+
}
|
|
135873
|
+
return;
|
|
135874
|
+
};
|
|
135875
|
+
const generator = pickGenerator(table, col);
|
|
135876
|
+
return generator;
|
|
135877
|
+
};
|
|
135878
|
+
|
|
135879
|
+
const isRelationCyclic = (startRel) => {
|
|
135880
|
+
// self relation
|
|
135881
|
+
if (startRel.table === startRel.refTable)
|
|
135882
|
+
return false;
|
|
135883
|
+
// DFS
|
|
135884
|
+
const targetTable = startRel.table;
|
|
135885
|
+
const queue = [startRel];
|
|
135886
|
+
let path = [];
|
|
135887
|
+
while (queue.length !== 0) {
|
|
135888
|
+
const currRel = queue.shift();
|
|
135889
|
+
if (path.includes(currRel.table)) {
|
|
135890
|
+
const idx = path.indexOf(currRel.table);
|
|
135891
|
+
path = path.slice(0, idx);
|
|
135892
|
+
}
|
|
135893
|
+
path.push(currRel.table);
|
|
135894
|
+
for (const rel of currRel.refTableRels) {
|
|
135895
|
+
// self relation
|
|
135896
|
+
if (rel.table === rel.refTable)
|
|
135897
|
+
continue;
|
|
135898
|
+
if (rel.refTable === targetTable)
|
|
135899
|
+
return true;
|
|
135900
|
+
// found cycle, but not the one we are looking for
|
|
135901
|
+
if (path.includes(rel.refTable))
|
|
135902
|
+
continue;
|
|
135903
|
+
queue.unshift(rel);
|
|
135904
|
+
}
|
|
135905
|
+
}
|
|
135906
|
+
return false;
|
|
135907
|
+
};
|
|
135908
|
+
const generateHashFromString = (s) => {
|
|
135909
|
+
let hash = 0;
|
|
135910
|
+
// p and m are prime numbers
|
|
135911
|
+
const p = 53;
|
|
135912
|
+
const m = 28871271685163;
|
|
135913
|
+
for (let i = 0; i < s.length; i++) {
|
|
135914
|
+
hash += ((s.codePointAt(i) || 0) * Math.pow(p, i)) % m;
|
|
135915
|
+
}
|
|
135916
|
+
return hash;
|
|
135917
|
+
};
|
|
135918
|
+
const equalSets = (set1, set2) => {
|
|
135919
|
+
return set1.size === set2.size && [...set1].every((si) => set2.has(si));
|
|
135920
|
+
};
|
|
135921
|
+
|
|
134377
135922
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
134378
135923
|
class SeedService {
|
|
134379
135924
|
static entityKind = 'SeedService';
|
|
@@ -134384,6 +135929,7 @@ class SeedService {
|
|
|
134384
135929
|
mysqlMaxParametersNumber = 100000;
|
|
134385
135930
|
// 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.
|
|
134386
135931
|
sqliteMaxParametersNumber = 32766;
|
|
135932
|
+
mssqlMaxParametersNumber = 2100;
|
|
134387
135933
|
version;
|
|
134388
135934
|
generatePossibleGenerators = (connectionType, tables, relations, refinements, options) => {
|
|
134389
135935
|
let columnPossibleGenerator;
|
|
@@ -134423,7 +135969,6 @@ class SeedService {
|
|
|
134423
135969
|
};
|
|
134424
135970
|
}
|
|
134425
135971
|
}
|
|
134426
|
-
// handling refinements (count, with)
|
|
134427
135972
|
if (refinements !== undefined && refinements[table.name] !== undefined) {
|
|
134428
135973
|
if (refinements[table.name].count !== undefined) {
|
|
134429
135974
|
tablesPossibleGenerators[i].count = refinements[table.name].count;
|
|
@@ -134479,29 +136024,17 @@ class SeedService {
|
|
|
134479
136024
|
notNull: col.notNull,
|
|
134480
136025
|
primary: col.primary,
|
|
134481
136026
|
generatedIdentityType: col.generatedIdentityType,
|
|
136027
|
+
identity: col.identity,
|
|
134482
136028
|
generator: undefined,
|
|
134483
136029
|
isCyclic: false,
|
|
134484
136030
|
wasDefinedBefore: false,
|
|
134485
136031
|
wasRefined: false,
|
|
134486
136032
|
};
|
|
134487
|
-
// handling refinements (columnGenerator)
|
|
134488
136033
|
if (refinements !== undefined
|
|
134489
136034
|
&& refinements[table.name] !== undefined
|
|
134490
136035
|
&& refinements[table.name].columns !== undefined
|
|
134491
136036
|
&& refinements[table.name].columns[col.name] !== undefined) {
|
|
134492
136037
|
const genObj = refinements[table.name].columns[col.name];
|
|
134493
|
-
if (genObj === false) {
|
|
134494
|
-
if (col.notNull === true && col.hasDefault === false) {
|
|
134495
|
-
throw new Error(`You cannot set the '${col.name}' column in the '${table.name}' table to false in your refinements.`
|
|
134496
|
-
+ `\nDoing so will result in a null value being inserted into the '${col.name}' column,`
|
|
134497
|
-
+ `\nwhich will cause an error because the column has a not null constraint and no default value.`);
|
|
134498
|
-
}
|
|
134499
|
-
// Generating undefined as a value for a column and then inserting it via drizzle-orm
|
|
134500
|
-
// will result in the value not being inserted into that column.
|
|
134501
|
-
columnPossibleGenerator.generator = new generatorsMap.GenerateDefault[0]({ defaultValue: undefined });
|
|
134502
|
-
columnPossibleGenerator.wasRefined = true;
|
|
134503
|
-
continue;
|
|
134504
|
-
}
|
|
134505
136038
|
if (col.columnType.match(/\[\w*]/g) !== null) {
|
|
134506
136039
|
if ((col.baseColumn?.dataType === 'array' && col.baseColumn.columnType.match(/\[\w*]/g) !== null)
|
|
134507
136040
|
// studio case
|
|
@@ -134547,17 +136080,27 @@ class SeedService {
|
|
|
134547
136080
|
}
|
|
134548
136081
|
} // TODO: rewrite pickGeneratorFor... using new col properties: isUnique and notNull
|
|
134549
136082
|
else if (connectionType === 'postgresql') {
|
|
134550
|
-
columnPossibleGenerator.generator =
|
|
136083
|
+
columnPossibleGenerator.generator = selectGeneratorForPostgresColumn(table, col);
|
|
134551
136084
|
}
|
|
134552
136085
|
else if (connectionType === 'mysql') {
|
|
134553
|
-
columnPossibleGenerator.generator =
|
|
136086
|
+
columnPossibleGenerator.generator = selectGeneratorForMysqlColumn(table, col);
|
|
134554
136087
|
}
|
|
134555
136088
|
else if (connectionType === 'sqlite') {
|
|
134556
|
-
columnPossibleGenerator.generator =
|
|
136089
|
+
columnPossibleGenerator.generator = selectGeneratorForSqlite(table, col);
|
|
136090
|
+
}
|
|
136091
|
+
else if (connectionType === 'mssql') {
|
|
136092
|
+
columnPossibleGenerator.generator = selectGeneratorForMssqlColumn(table, col);
|
|
136093
|
+
}
|
|
136094
|
+
else if (connectionType === 'cockroach') {
|
|
136095
|
+
columnPossibleGenerator.generator = selectGeneratorForCockroachColumn(table, col);
|
|
136096
|
+
}
|
|
136097
|
+
else if (connectionType === 'singlestore') {
|
|
136098
|
+
columnPossibleGenerator.generator = selectGeneratorForSingleStoreColumn(table, col);
|
|
134557
136099
|
}
|
|
134558
136100
|
if (columnPossibleGenerator.generator === undefined) {
|
|
134559
136101
|
throw new Error(`column with type ${col.columnType} is not supported for now.`);
|
|
134560
136102
|
}
|
|
136103
|
+
columnPossibleGenerator.generator.typeParams = col.typeParams ?? columnPossibleGenerator.generator.typeParams;
|
|
134561
136104
|
const arrayGen = columnPossibleGenerator.generator.replaceIfArray();
|
|
134562
136105
|
if (arrayGen !== undefined) {
|
|
134563
136106
|
columnPossibleGenerator.generator = arrayGen;
|
|
@@ -134572,7 +136115,7 @@ class SeedService {
|
|
|
134572
136115
|
// TODO: for now only GenerateValuesFromArray support notNull property
|
|
134573
136116
|
columnPossibleGenerator.generator.notNull = col.notNull;
|
|
134574
136117
|
columnPossibleGenerator.generator.dataType = col.dataType;
|
|
134575
|
-
columnPossibleGenerator.generator.stringLength = col.typeParams.length;
|
|
136118
|
+
// columnPossibleGenerator.generator.stringLength = col.typeParams.length;
|
|
134576
136119
|
tablePossibleGenerators.columnsPossibleGenerators.push(columnPossibleGenerator);
|
|
134577
136120
|
}
|
|
134578
136121
|
}
|
|
@@ -134583,7 +136126,7 @@ class SeedService {
|
|
|
134583
136126
|
if (entityKind === 'GenerateArray') {
|
|
134584
136127
|
const oldBaseColumnGen = generator.params.baseColumnGen;
|
|
134585
136128
|
const newBaseColumnGen = this.selectVersionOfGenerator(oldBaseColumnGen);
|
|
134586
|
-
|
|
136129
|
+
newBaseColumnGen.typeParams = oldBaseColumnGen.typeParams;
|
|
134587
136130
|
generator.params.baseColumnGen = newBaseColumnGen;
|
|
134588
136131
|
}
|
|
134589
136132
|
const possibleGeneratorConstructors = generatorsMap[entityKind];
|
|
@@ -134599,7 +136142,8 @@ class SeedService {
|
|
|
134599
136142
|
// TODO: for now only GenerateValuesFromArray support notNull property
|
|
134600
136143
|
newGenerator.notNull = generator.notNull;
|
|
134601
136144
|
newGenerator.dataType = generator.dataType;
|
|
134602
|
-
newGenerator.stringLength = generator.stringLength;
|
|
136145
|
+
// newGenerator.stringLength = generator.stringLength;
|
|
136146
|
+
newGenerator.typeParams = generator.typeParams ?? newGenerator.typeParams;
|
|
134603
136147
|
return newGenerator;
|
|
134604
136148
|
};
|
|
134605
136149
|
cyclicTablesCompare = (table1, table2, relation, reverseRelation) => {
|
|
@@ -134710,474 +136254,6 @@ class SeedService {
|
|
|
134710
136254
|
}
|
|
134711
136255
|
return weightedWithCount;
|
|
134712
136256
|
};
|
|
134713
|
-
// TODO: revise serial part generators
|
|
134714
|
-
selectGeneratorForPostgresColumn = (table, col) => {
|
|
134715
|
-
const pickGenerator = (table, col) => {
|
|
134716
|
-
// ARRAY
|
|
134717
|
-
if (col.columnType.match(/\[\w*]/g) !== null && col.baseColumn !== undefined) {
|
|
134718
|
-
const baseColumnGen = this.selectGeneratorForPostgresColumn(table, col.baseColumn);
|
|
134719
|
-
if (baseColumnGen === undefined) {
|
|
134720
|
-
throw new Error(`column with type ${col.baseColumn.columnType} is not supported for now.`);
|
|
134721
|
-
}
|
|
134722
|
-
// const getBaseColumnDataType = (baseColumn: Column) => {
|
|
134723
|
-
// if (baseColumn.baseColumn !== undefined) {
|
|
134724
|
-
// return getBaseColumnDataType(baseColumn.baseColumn);
|
|
134725
|
-
// }
|
|
134726
|
-
// return baseColumn.dataType;
|
|
134727
|
-
// };
|
|
134728
|
-
// const baseColumnDataType = getBaseColumnDataType(col.baseColumn);
|
|
134729
|
-
const generator = new generatorsMap.GenerateArray[0]({ baseColumnGen, size: col.size });
|
|
134730
|
-
// generator.baseColumnDataType = baseColumnDataType;
|
|
134731
|
-
return generator;
|
|
134732
|
-
}
|
|
134733
|
-
// ARRAY for studio
|
|
134734
|
-
if (col.columnType.match(/\[\w*]/g) !== null) {
|
|
134735
|
-
// remove dimensions from type
|
|
134736
|
-
const baseColumnType = col.columnType.replace(/\[\w*]/g, '');
|
|
134737
|
-
const baseColumn = {
|
|
134738
|
-
...col,
|
|
134739
|
-
};
|
|
134740
|
-
baseColumn.columnType = baseColumnType;
|
|
134741
|
-
const baseColumnGen = this.selectGeneratorForPostgresColumn(table, baseColumn);
|
|
134742
|
-
if (baseColumnGen === undefined) {
|
|
134743
|
-
throw new Error(`column with type ${col.baseColumn.columnType} is not supported for now.`);
|
|
134744
|
-
}
|
|
134745
|
-
let generator = new generatorsMap.GenerateArray[0]({ baseColumnGen });
|
|
134746
|
-
for (let i = 0; i < col.typeParams.dimensions - 1; i++) {
|
|
134747
|
-
generator = new generatorsMap.GenerateArray[0]({ baseColumnGen: generator });
|
|
134748
|
-
}
|
|
134749
|
-
return generator;
|
|
134750
|
-
}
|
|
134751
|
-
// INT ------------------------------------------------------------------------------------------------------------
|
|
134752
|
-
if ((['smallserial', 'serial', 'bigserial'].includes(col.columnType)
|
|
134753
|
-
|| ['smallint', 'integer', 'bigint'].includes(col.columnType))
|
|
134754
|
-
&& table.primaryKeys.includes(col.name)) {
|
|
134755
|
-
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
134756
|
-
return generator;
|
|
134757
|
-
}
|
|
134758
|
-
let minValue;
|
|
134759
|
-
let maxValue;
|
|
134760
|
-
if (['smallserial', 'serial', 'bigserial'].includes(col.columnType)) {
|
|
134761
|
-
minValue = 1;
|
|
134762
|
-
if (col.columnType === 'smallserial') {
|
|
134763
|
-
// 2^16 / 2 - 1, 2 bytes
|
|
134764
|
-
maxValue = 32767;
|
|
134765
|
-
}
|
|
134766
|
-
else if (col.columnType === 'serial') {
|
|
134767
|
-
// 2^32 / 2 - 1, 4 bytes
|
|
134768
|
-
maxValue = 2147483647;
|
|
134769
|
-
}
|
|
134770
|
-
else if (col.columnType === 'bigserial') {
|
|
134771
|
-
// 2^64 / 2 - 1, 8 bytes
|
|
134772
|
-
minValue = BigInt(1);
|
|
134773
|
-
maxValue = BigInt('9223372036854775807');
|
|
134774
|
-
}
|
|
134775
|
-
}
|
|
134776
|
-
else if (['smallint', 'integer', 'bigint'].includes(col.columnType)) {
|
|
134777
|
-
if (col.columnType === 'smallint') {
|
|
134778
|
-
// 2^16 / 2 - 1, 2 bytes
|
|
134779
|
-
minValue = -32768;
|
|
134780
|
-
maxValue = 32767;
|
|
134781
|
-
}
|
|
134782
|
-
else if (col.columnType === 'integer') {
|
|
134783
|
-
// 2^32 / 2 - 1, 4 bytes
|
|
134784
|
-
minValue = -2147483648;
|
|
134785
|
-
maxValue = 2147483647;
|
|
134786
|
-
}
|
|
134787
|
-
else if (col.columnType.includes('bigint')) {
|
|
134788
|
-
if (col.dataType === 'bigint') {
|
|
134789
|
-
// 2^64 / 2 - 1, 8 bytes
|
|
134790
|
-
minValue = BigInt('-9223372036854775808');
|
|
134791
|
-
maxValue = BigInt('9223372036854775807');
|
|
134792
|
-
}
|
|
134793
|
-
else {
|
|
134794
|
-
// if (col.dataType === 'number')
|
|
134795
|
-
// if you’re expecting values above 2^31 but below 2^53
|
|
134796
|
-
minValue = -9007199254740991;
|
|
134797
|
-
maxValue = 9007199254740991;
|
|
134798
|
-
}
|
|
134799
|
-
}
|
|
134800
|
-
}
|
|
134801
|
-
if (['smallint', 'integer', 'bigint'].includes(col.columnType)) {
|
|
134802
|
-
const generator = new generatorsMap.GenerateInt[0]({
|
|
134803
|
-
minValue,
|
|
134804
|
-
maxValue,
|
|
134805
|
-
});
|
|
134806
|
-
return generator;
|
|
134807
|
-
}
|
|
134808
|
-
if (['smallserial', 'serial', 'bigserial'].includes(col.columnType)) {
|
|
134809
|
-
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
134810
|
-
generator.maxValue = maxValue;
|
|
134811
|
-
return generator;
|
|
134812
|
-
}
|
|
134813
|
-
// NUMBER(real, double, decimal, numeric)
|
|
134814
|
-
if (col.columnType === 'real'
|
|
134815
|
-
|| col.columnType === 'double precision'
|
|
134816
|
-
|| col.columnType.match(/^decimal(\(\d{1,6}(, ?-?\d{0,5})?\))?$/) !== null
|
|
134817
|
-
|| col.columnType.match(/^numeric(\(\d{1,6}(, ?-?\d{0,5})?\))?$/) !== null) {
|
|
134818
|
-
if (col.typeParams.precision !== undefined) {
|
|
134819
|
-
const precision = col.typeParams.precision;
|
|
134820
|
-
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
134821
|
-
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
134822
|
-
const generator = new generatorsMap.GenerateNumber[0]({
|
|
134823
|
-
minValue: -maxAbsoluteValue,
|
|
134824
|
-
maxValue: maxAbsoluteValue,
|
|
134825
|
-
precision: Math.pow(10, scale),
|
|
134826
|
-
});
|
|
134827
|
-
return generator;
|
|
134828
|
-
}
|
|
134829
|
-
const generator = new generatorsMap.GenerateNumber[0]();
|
|
134830
|
-
return generator;
|
|
134831
|
-
}
|
|
134832
|
-
// STRING
|
|
134833
|
-
if ((col.columnType === 'text'
|
|
134834
|
-
|| col.columnType.match(/^varchar(\(\d+\))?$/) !== null
|
|
134835
|
-
|| col.columnType.match(/^char(\(\d+\))?$/) !== null)
|
|
134836
|
-
&& table.primaryKeys.includes(col.name)) {
|
|
134837
|
-
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
134838
|
-
return generator;
|
|
134839
|
-
}
|
|
134840
|
-
if ((col.columnType === 'text'
|
|
134841
|
-
|| col.columnType.match(/^varchar(\(\d+\))?$/) !== null
|
|
134842
|
-
|| col.columnType.match(/^char(\(\d+\))?$/) !== null)
|
|
134843
|
-
&& col.name.toLowerCase().includes('name')) {
|
|
134844
|
-
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
134845
|
-
return generator;
|
|
134846
|
-
}
|
|
134847
|
-
if ((col.columnType === 'text'
|
|
134848
|
-
|| col.columnType.match(/^varchar(\(\d+\))?$/) !== null
|
|
134849
|
-
|| col.columnType.match(/^char(\(\d+\))?$/) !== null)
|
|
134850
|
-
&& col.name.toLowerCase().includes('email')) {
|
|
134851
|
-
const generator = new generatorsMap.GenerateEmail[0]();
|
|
134852
|
-
return generator;
|
|
134853
|
-
}
|
|
134854
|
-
if (col.columnType === 'text'
|
|
134855
|
-
|| col.columnType.match(/^varchar(\(\d+\))?$/) !== null
|
|
134856
|
-
|| col.columnType.match(/^char(\(\d+\))?$/) !== null) {
|
|
134857
|
-
const generator = new generatorsMap.GenerateString[0]();
|
|
134858
|
-
return generator;
|
|
134859
|
-
}
|
|
134860
|
-
// UUID
|
|
134861
|
-
if (col.columnType === 'uuid') {
|
|
134862
|
-
const generator = new generatorsMap.GenerateUUID[0]();
|
|
134863
|
-
return generator;
|
|
134864
|
-
}
|
|
134865
|
-
// BOOLEAN
|
|
134866
|
-
if (col.columnType === 'boolean') {
|
|
134867
|
-
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
134868
|
-
return generator;
|
|
134869
|
-
}
|
|
134870
|
-
// DATE, TIME, TIMESTAMP
|
|
134871
|
-
if (col.columnType === 'date') {
|
|
134872
|
-
const generator = new generatorsMap.GenerateDate[0]();
|
|
134873
|
-
return generator;
|
|
134874
|
-
}
|
|
134875
|
-
if (col.columnType.match(/^time((\(\d+\))|( with time zone))?$/) !== null) {
|
|
134876
|
-
const generator = new generatorsMap.GenerateTime[0]();
|
|
134877
|
-
return generator;
|
|
134878
|
-
}
|
|
134879
|
-
if (col.columnType.match(/^timestamp((\(\d+\))|( with time zone))?$/) !== null) {
|
|
134880
|
-
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
134881
|
-
return generator;
|
|
134882
|
-
}
|
|
134883
|
-
// JSON, JSONB
|
|
134884
|
-
if (col.columnType === 'json' || col.columnType === 'jsonb') {
|
|
134885
|
-
const generator = new generatorsMap.GenerateJson[0]();
|
|
134886
|
-
return generator;
|
|
134887
|
-
}
|
|
134888
|
-
// if (col.columnType === "jsonb") {
|
|
134889
|
-
// const generator = new GenerateJsonb({});
|
|
134890
|
-
// return generator;
|
|
134891
|
-
// }
|
|
134892
|
-
// ENUM
|
|
134893
|
-
if (col.enumValues !== undefined) {
|
|
134894
|
-
const generator = new generatorsMap.GenerateEnum[0]({
|
|
134895
|
-
enumValues: col.enumValues,
|
|
134896
|
-
});
|
|
134897
|
-
return generator;
|
|
134898
|
-
}
|
|
134899
|
-
// INTERVAL
|
|
134900
|
-
if (col.columnType.match(/^interval( .+)?$/) !== null) {
|
|
134901
|
-
if (col.columnType === 'interval') {
|
|
134902
|
-
const generator = new generatorsMap.GenerateInterval[0]();
|
|
134903
|
-
return generator;
|
|
134904
|
-
}
|
|
134905
|
-
const fields = col.columnType.replace('interval ', '');
|
|
134906
|
-
const generator = new generatorsMap.GenerateInterval[0]({ fields });
|
|
134907
|
-
return generator;
|
|
134908
|
-
}
|
|
134909
|
-
// POINT, LINE
|
|
134910
|
-
if (col.columnType === 'point') {
|
|
134911
|
-
const generator = new generatorsMap.GeneratePoint[0]();
|
|
134912
|
-
return generator;
|
|
134913
|
-
}
|
|
134914
|
-
if (col.columnType === 'line') {
|
|
134915
|
-
const generator = new generatorsMap.GenerateLine[0]();
|
|
134916
|
-
return generator;
|
|
134917
|
-
}
|
|
134918
|
-
if (col.hasDefault && col.default !== undefined) {
|
|
134919
|
-
const generator = new generatorsMap.GenerateDefault[0]({
|
|
134920
|
-
defaultValue: col.default,
|
|
134921
|
-
});
|
|
134922
|
-
return generator;
|
|
134923
|
-
}
|
|
134924
|
-
return;
|
|
134925
|
-
};
|
|
134926
|
-
const generator = pickGenerator(table, col);
|
|
134927
|
-
if (generator !== undefined) {
|
|
134928
|
-
generator.isUnique = col.isUnique;
|
|
134929
|
-
generator.dataType = col.dataType;
|
|
134930
|
-
generator.stringLength = col.typeParams.length;
|
|
134931
|
-
}
|
|
134932
|
-
return generator;
|
|
134933
|
-
};
|
|
134934
|
-
selectGeneratorForMysqlColumn = (table, col) => {
|
|
134935
|
-
const pickGenerator = (table, col) => {
|
|
134936
|
-
// INT ------------------------------------------------------------------------------------------------------------
|
|
134937
|
-
if ((col.columnType === 'serial'
|
|
134938
|
-
|| ['tinyint', 'smallint', 'mediumint', 'int', 'bigint'].includes(col.columnType))
|
|
134939
|
-
&& table.primaryKeys.includes(col.name)) {
|
|
134940
|
-
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
134941
|
-
return generator;
|
|
134942
|
-
}
|
|
134943
|
-
let minValue;
|
|
134944
|
-
let maxValue;
|
|
134945
|
-
if (col.columnType === 'serial') {
|
|
134946
|
-
// 2^64 % 2 - 1, 8 bytes
|
|
134947
|
-
minValue = BigInt(0);
|
|
134948
|
-
maxValue = BigInt('9223372036854775807');
|
|
134949
|
-
}
|
|
134950
|
-
else if (['tinyint', 'smallint', 'mediumint', 'int', 'bigint'].includes(col.columnType)) {
|
|
134951
|
-
if (col.columnType === 'tinyint') {
|
|
134952
|
-
// 2^8 / 2 - 1, 1 bytes
|
|
134953
|
-
minValue = -128;
|
|
134954
|
-
maxValue = 127;
|
|
134955
|
-
}
|
|
134956
|
-
else if (col.columnType === 'smallint') {
|
|
134957
|
-
// 2^16 / 2 - 1, 2 bytes
|
|
134958
|
-
minValue = -32768;
|
|
134959
|
-
maxValue = 32767;
|
|
134960
|
-
}
|
|
134961
|
-
else if (col.columnType === 'mediumint') {
|
|
134962
|
-
// 2^16 / 2 - 1, 2 bytes
|
|
134963
|
-
minValue = -8388608;
|
|
134964
|
-
maxValue = 8388607;
|
|
134965
|
-
}
|
|
134966
|
-
else if (col.columnType === 'int') {
|
|
134967
|
-
// 2^32 / 2 - 1, 4 bytes
|
|
134968
|
-
minValue = -2147483648;
|
|
134969
|
-
maxValue = 2147483647;
|
|
134970
|
-
}
|
|
134971
|
-
else if (col.columnType === 'bigint') {
|
|
134972
|
-
// 2^64 / 2 - 1, 8 bytes
|
|
134973
|
-
minValue = BigInt('-9223372036854775808');
|
|
134974
|
-
maxValue = BigInt('9223372036854775807');
|
|
134975
|
-
}
|
|
134976
|
-
}
|
|
134977
|
-
if (['tinyint', 'smallint', 'mediumint', 'int', 'bigint'].includes(col.columnType)) {
|
|
134978
|
-
const generator = new generatorsMap.GenerateInt[0]({
|
|
134979
|
-
minValue,
|
|
134980
|
-
maxValue,
|
|
134981
|
-
});
|
|
134982
|
-
return generator;
|
|
134983
|
-
}
|
|
134984
|
-
if (col.columnType === 'serial') {
|
|
134985
|
-
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
134986
|
-
generator.maxValue = maxValue;
|
|
134987
|
-
return generator;
|
|
134988
|
-
}
|
|
134989
|
-
// NUMBER(real, double, decimal, float)
|
|
134990
|
-
if (col.columnType === 'real'
|
|
134991
|
-
|| col.columnType === 'double'
|
|
134992
|
-
|| col.columnType === 'float'
|
|
134993
|
-
|| col.columnType.startsWith('decimal')
|
|
134994
|
-
|| col.columnType.startsWith('numeric')) {
|
|
134995
|
-
if (col.typeParams.precision !== undefined) {
|
|
134996
|
-
const precision = col.typeParams.precision;
|
|
134997
|
-
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
134998
|
-
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
134999
|
-
const generator = new generatorsMap.GenerateNumber[0]({
|
|
135000
|
-
minValue: -maxAbsoluteValue,
|
|
135001
|
-
maxValue: maxAbsoluteValue,
|
|
135002
|
-
precision: Math.pow(10, scale),
|
|
135003
|
-
});
|
|
135004
|
-
return generator;
|
|
135005
|
-
}
|
|
135006
|
-
const generator = new generatorsMap.GenerateNumber[0]();
|
|
135007
|
-
return generator;
|
|
135008
|
-
}
|
|
135009
|
-
// STRING
|
|
135010
|
-
if ((col.columnType === 'text'
|
|
135011
|
-
|| col.columnType === 'blob'
|
|
135012
|
-
|| col.columnType.startsWith('char')
|
|
135013
|
-
|| col.columnType.startsWith('varchar')
|
|
135014
|
-
|| col.columnType.startsWith('binary')
|
|
135015
|
-
|| col.columnType.startsWith('varbinary'))
|
|
135016
|
-
&& table.primaryKeys.includes(col.name)) {
|
|
135017
|
-
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135018
|
-
return generator;
|
|
135019
|
-
}
|
|
135020
|
-
if ((col.columnType === 'text'
|
|
135021
|
-
|| col.columnType === 'blob'
|
|
135022
|
-
|| col.columnType.startsWith('char')
|
|
135023
|
-
|| col.columnType.startsWith('varchar')
|
|
135024
|
-
|| col.columnType.startsWith('binary')
|
|
135025
|
-
|| col.columnType.startsWith('varbinary'))
|
|
135026
|
-
&& col.name.toLowerCase().includes('name')) {
|
|
135027
|
-
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135028
|
-
return generator;
|
|
135029
|
-
}
|
|
135030
|
-
if ((col.columnType === 'text'
|
|
135031
|
-
|| col.columnType === 'blob'
|
|
135032
|
-
|| col.columnType.startsWith('char')
|
|
135033
|
-
|| col.columnType.startsWith('varchar')
|
|
135034
|
-
|| col.columnType.startsWith('binary')
|
|
135035
|
-
|| col.columnType.startsWith('varbinary'))
|
|
135036
|
-
&& col.name.toLowerCase().includes('email')) {
|
|
135037
|
-
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135038
|
-
return generator;
|
|
135039
|
-
}
|
|
135040
|
-
if (col.columnType === 'text'
|
|
135041
|
-
|| col.columnType === 'blob'
|
|
135042
|
-
|| col.columnType.startsWith('char')
|
|
135043
|
-
|| col.columnType.startsWith('varchar')
|
|
135044
|
-
|| col.columnType.startsWith('binary')
|
|
135045
|
-
|| col.columnType.startsWith('varbinary')) {
|
|
135046
|
-
const generator = new generatorsMap.GenerateString[0]();
|
|
135047
|
-
return generator;
|
|
135048
|
-
}
|
|
135049
|
-
// BOOLEAN
|
|
135050
|
-
if (col.columnType === 'boolean') {
|
|
135051
|
-
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135052
|
-
return generator;
|
|
135053
|
-
}
|
|
135054
|
-
// DATE, TIME, TIMESTAMP, DATETIME, YEAR
|
|
135055
|
-
if (col.columnType.startsWith('datetime')) {
|
|
135056
|
-
const generator = new generatorsMap.GenerateDatetime[0]();
|
|
135057
|
-
return generator;
|
|
135058
|
-
}
|
|
135059
|
-
if (col.columnType === 'date') {
|
|
135060
|
-
const generator = new generatorsMap.GenerateDate[0]();
|
|
135061
|
-
return generator;
|
|
135062
|
-
}
|
|
135063
|
-
if (col.columnType === 'time') {
|
|
135064
|
-
const generator = new generatorsMap.GenerateTime[0]();
|
|
135065
|
-
return generator;
|
|
135066
|
-
}
|
|
135067
|
-
if (col.columnType.startsWith('timestamp')) {
|
|
135068
|
-
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135069
|
-
return generator;
|
|
135070
|
-
}
|
|
135071
|
-
if (col.columnType === 'year') {
|
|
135072
|
-
const generator = new generatorsMap.GenerateYear[0]();
|
|
135073
|
-
return generator;
|
|
135074
|
-
}
|
|
135075
|
-
// JSON
|
|
135076
|
-
if (col.columnType === 'json') {
|
|
135077
|
-
const generator = new generatorsMap.GenerateJson[0]();
|
|
135078
|
-
return generator;
|
|
135079
|
-
}
|
|
135080
|
-
// ENUM
|
|
135081
|
-
if (col.enumValues !== undefined) {
|
|
135082
|
-
const generator = new generatorsMap.GenerateEnum[0]({
|
|
135083
|
-
enumValues: col.enumValues,
|
|
135084
|
-
});
|
|
135085
|
-
return generator;
|
|
135086
|
-
}
|
|
135087
|
-
if (col.hasDefault && col.default !== undefined) {
|
|
135088
|
-
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135089
|
-
defaultValue: col.default,
|
|
135090
|
-
});
|
|
135091
|
-
return generator;
|
|
135092
|
-
}
|
|
135093
|
-
return;
|
|
135094
|
-
};
|
|
135095
|
-
const generator = pickGenerator(table, col);
|
|
135096
|
-
return generator;
|
|
135097
|
-
};
|
|
135098
|
-
selectGeneratorForSqlite = (table, col) => {
|
|
135099
|
-
const pickGenerator = (table, col) => {
|
|
135100
|
-
// int section ---------------------------------------------------------------------------------------
|
|
135101
|
-
if ((col.columnType === 'integer' || col.columnType === 'numeric')
|
|
135102
|
-
&& table.primaryKeys.includes(col.name)) {
|
|
135103
|
-
const generator = new generatorsMap.GenerateIntPrimaryKey[0]();
|
|
135104
|
-
return generator;
|
|
135105
|
-
}
|
|
135106
|
-
if (col.columnType === 'integer' && col.dataType === 'boolean') {
|
|
135107
|
-
const generator = new generatorsMap.GenerateBoolean[0]();
|
|
135108
|
-
return generator;
|
|
135109
|
-
}
|
|
135110
|
-
if ((col.columnType === 'integer' && col.dataType === 'date')) {
|
|
135111
|
-
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
135112
|
-
return generator;
|
|
135113
|
-
}
|
|
135114
|
-
if (col.columnType === 'integer'
|
|
135115
|
-
|| (col.dataType === 'bigint' && col.columnType === 'blob')) {
|
|
135116
|
-
const generator = new generatorsMap.GenerateInt[0]();
|
|
135117
|
-
return generator;
|
|
135118
|
-
}
|
|
135119
|
-
// number section ------------------------------------------------------------------------------------
|
|
135120
|
-
if (col.columnType.startsWith('real')
|
|
135121
|
-
|| col.columnType.startsWith('numeric')) {
|
|
135122
|
-
if (col.typeParams.precision !== undefined) {
|
|
135123
|
-
const precision = col.typeParams.precision;
|
|
135124
|
-
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
135125
|
-
const maxAbsoluteValue = Math.pow(10, precision - scale) - Math.pow(10, -scale);
|
|
135126
|
-
const generator = new generatorsMap.GenerateNumber[0]({
|
|
135127
|
-
minValue: -maxAbsoluteValue,
|
|
135128
|
-
maxValue: maxAbsoluteValue,
|
|
135129
|
-
precision: Math.pow(10, scale),
|
|
135130
|
-
});
|
|
135131
|
-
return generator;
|
|
135132
|
-
}
|
|
135133
|
-
const generator = new generatorsMap.GenerateNumber[0]();
|
|
135134
|
-
return generator;
|
|
135135
|
-
}
|
|
135136
|
-
// string section ------------------------------------------------------------------------------------
|
|
135137
|
-
if ((col.columnType.startsWith('text')
|
|
135138
|
-
|| col.columnType.startsWith('numeric')
|
|
135139
|
-
|| col.columnType.startsWith('blob'))
|
|
135140
|
-
&& table.primaryKeys.includes(col.name)) {
|
|
135141
|
-
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
135142
|
-
return generator;
|
|
135143
|
-
}
|
|
135144
|
-
if ((col.columnType.startsWith('text')
|
|
135145
|
-
|| col.columnType.startsWith('numeric')
|
|
135146
|
-
|| col.columnType.startsWith('blob'))
|
|
135147
|
-
&& col.name.toLowerCase().includes('name')) {
|
|
135148
|
-
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
135149
|
-
return generator;
|
|
135150
|
-
}
|
|
135151
|
-
if ((col.columnType.startsWith('text')
|
|
135152
|
-
|| col.columnType.startsWith('numeric')
|
|
135153
|
-
|| col.columnType.startsWith('blob'))
|
|
135154
|
-
&& col.name.toLowerCase().includes('email')) {
|
|
135155
|
-
const generator = new generatorsMap.GenerateEmail[0]();
|
|
135156
|
-
return generator;
|
|
135157
|
-
}
|
|
135158
|
-
if (col.columnType.startsWith('text')
|
|
135159
|
-
|| col.columnType.startsWith('numeric')
|
|
135160
|
-
|| col.columnType.startsWith('blob')
|
|
135161
|
-
|| col.columnType.startsWith('blobbuffer')) {
|
|
135162
|
-
const generator = new generatorsMap.GenerateString[0]();
|
|
135163
|
-
return generator;
|
|
135164
|
-
}
|
|
135165
|
-
if ((col.columnType.startsWith('text') && col.dataType === 'json')
|
|
135166
|
-
|| (col.columnType.startsWith('blob') && col.dataType === 'json')) {
|
|
135167
|
-
const generator = new generatorsMap.GenerateJson[0]();
|
|
135168
|
-
return generator;
|
|
135169
|
-
}
|
|
135170
|
-
if (col.hasDefault && col.default !== undefined) {
|
|
135171
|
-
const generator = new generatorsMap.GenerateDefault[0]({
|
|
135172
|
-
defaultValue: col.default,
|
|
135173
|
-
});
|
|
135174
|
-
return generator;
|
|
135175
|
-
}
|
|
135176
|
-
return;
|
|
135177
|
-
};
|
|
135178
|
-
const generator = pickGenerator(table, col);
|
|
135179
|
-
return generator;
|
|
135180
|
-
};
|
|
135181
136257
|
filterCyclicTables = (tablesGenerators) => {
|
|
135182
136258
|
const filteredTablesGenerators = tablesGenerators.filter((tableGen) => tableGen.columnsPossibleGenerators.some((columnGen) => columnGen.isCyclic === true && columnGen.wasDefinedBefore === true));
|
|
135183
136259
|
const tablesUniqueNotNullColumn = {};
|
|
@@ -135189,7 +136265,8 @@ class SeedService {
|
|
|
135189
136265
|
throw new Error(`Table '${tableGen.tableName}' does not have primary or (unique and notNull) column. Can't seed table with cyclic relation.`);
|
|
135190
136266
|
}
|
|
135191
136267
|
tablesUniqueNotNullColumn[tableGen.tableName] = { uniqueNotNullColName };
|
|
135192
|
-
filteredTablesGenerators[idx].columnsPossibleGenerators = tableGen.columnsPossibleGenerators.filter((colGen) => (colGen.isCyclic === true && colGen.wasDefinedBefore === true) || colGen.columnName === uniqueNotNullColName)
|
|
136268
|
+
filteredTablesGenerators[idx].columnsPossibleGenerators = tableGen.columnsPossibleGenerators.filter((colGen) => (colGen.isCyclic === true && colGen.wasDefinedBefore === true) || colGen.columnName === uniqueNotNullColName)
|
|
136269
|
+
.map((colGen) => {
|
|
135193
136270
|
const newColGen = { ...colGen };
|
|
135194
136271
|
newColGen.wasDefinedBefore = false;
|
|
135195
136272
|
return newColGen;
|
|
@@ -135235,8 +136312,7 @@ class SeedService {
|
|
|
135235
136312
|
};
|
|
135236
136313
|
}
|
|
135237
136314
|
// get values to generate columns with foreign key
|
|
135238
|
-
// if table posts contains foreign key to table users, then rel.table === 'posts' and rel.refTable === 'users',
|
|
135239
|
-
// because table posts has reference to table users.
|
|
136315
|
+
// if table posts contains foreign key to table users, then rel.table === 'posts' and rel.refTable === 'users', because table posts has reference to table users.
|
|
135240
136316
|
if (filteredRelations.length !== 0) {
|
|
135241
136317
|
for (const rel of filteredRelations) {
|
|
135242
136318
|
if (table.withFromTable[rel.refTable] !== undefined
|
|
@@ -135284,7 +136360,9 @@ class SeedService {
|
|
|
135284
136360
|
weightedCountSeed = table.withFromTable[rel.refTable].weightedCountSeed;
|
|
135285
136361
|
}
|
|
135286
136362
|
// TODO: revise maybe need to select version of generator here too
|
|
135287
|
-
genObj = new generatorsMap.GenerateValuesFromArray[0]({
|
|
136363
|
+
genObj = new generatorsMap.GenerateValuesFromArray[0]({
|
|
136364
|
+
values: refColumnValues,
|
|
136365
|
+
});
|
|
135288
136366
|
genObj.notNull = tableGenerators[rel.columns[colIdx]].notNull;
|
|
135289
136367
|
genObj.weightedCountSeed = weightedCountSeed;
|
|
135290
136368
|
genObj.maxRepeatedValuesCount = repeatedValuesCount;
|
|
@@ -135352,7 +136430,10 @@ class SeedService {
|
|
|
135352
136430
|
for (const columnName of Object.keys(tableGenerators)) {
|
|
135353
136431
|
columnsNumber += 1;
|
|
135354
136432
|
columnGenerator = tableGenerators[columnName];
|
|
136433
|
+
// postgres identity columns
|
|
135355
136434
|
override = tableGenerators[columnName]?.generatedIdentityType === 'always' ? true : override;
|
|
136435
|
+
// mssql identity columns
|
|
136436
|
+
override = tableGenerators[columnName]?.identity === true ? true : override;
|
|
135356
136437
|
columnsGenerators[columnName] = columnGenerator.generator;
|
|
135357
136438
|
columnsGenerators[columnName].init({
|
|
135358
136439
|
count,
|
|
@@ -135367,25 +136448,6 @@ class SeedService {
|
|
|
135367
136448
|
// columnsGenerators[columnName] = uniqueGen;
|
|
135368
136449
|
// }
|
|
135369
136450
|
}
|
|
135370
|
-
// sequence updates will only be performed for PostgreSQL, since MySQL and SQLite already update their sequences correctly on their own.
|
|
135371
|
-
const columnsToUpdateSeq = new Map();
|
|
135372
|
-
if (count > 0 && drizzleOrm.is(db, pgCore.PgDatabase) && schema !== undefined && tableName !== undefined
|
|
135373
|
-
&& schema[tableName] !== undefined) {
|
|
135374
|
-
const tableConfig = pgCore.getTableConfig(schema[tableName]);
|
|
135375
|
-
for (const column of tableConfig.columns) {
|
|
135376
|
-
// TODO should I filter only primary key columns?
|
|
135377
|
-
// should I filter column by dataType or by column drizzle type?
|
|
135378
|
-
// column.dataType === 'number' || column.dataType === 'bigint'
|
|
135379
|
-
if (isPostgresColumnIntLike(column)) {
|
|
135380
|
-
columnsToUpdateSeq.set(column.name, {
|
|
135381
|
-
schemaName: tableConfig.schema,
|
|
135382
|
-
tableName: tableConfig.name,
|
|
135383
|
-
columnName: column.name,
|
|
135384
|
-
valueToUpdate: undefined,
|
|
135385
|
-
});
|
|
135386
|
-
}
|
|
135387
|
-
}
|
|
135388
|
-
}
|
|
135389
136451
|
let maxParametersNumber;
|
|
135390
136452
|
if (drizzleOrm.is(db, (pgCore.PgDatabase))) {
|
|
135391
136453
|
// @ts-ignore
|
|
@@ -135396,10 +136458,13 @@ class SeedService {
|
|
|
135396
136458
|
else if (drizzleOrm.is(db, (mysqlCore.MySqlDatabase))) {
|
|
135397
136459
|
maxParametersNumber = this.mysqlMaxParametersNumber;
|
|
135398
136460
|
}
|
|
135399
|
-
else {
|
|
135400
|
-
// is(db, BaseSQLiteDatabase<any, any>)
|
|
136461
|
+
else if (drizzleOrm.is(db, (sqliteCore.BaseSQLiteDatabase))) {
|
|
135401
136462
|
maxParametersNumber = this.sqliteMaxParametersNumber;
|
|
135402
136463
|
}
|
|
136464
|
+
else {
|
|
136465
|
+
// is(db, MsSqlDatabase<any, any>)
|
|
136466
|
+
maxParametersNumber = this.mssqlMaxParametersNumber;
|
|
136467
|
+
}
|
|
135403
136468
|
const maxBatchSize = Math.floor(maxParametersNumber / columnsNumber);
|
|
135404
136469
|
batchSize = batchSize > maxBatchSize ? maxBatchSize : batchSize;
|
|
135405
136470
|
if ((insertDataInDb === true || updateDataInDb === true)
|
|
@@ -135417,12 +136482,6 @@ class SeedService {
|
|
|
135417
136482
|
// | boolean;
|
|
135418
136483
|
generatedValue = columnsGenerators[columnName].generate({ i });
|
|
135419
136484
|
row[columnName] = generatedValue;
|
|
135420
|
-
const colToUpdateSeq = columnsToUpdateSeq.get(columnName);
|
|
135421
|
-
if (columnsToUpdateSeq.size !== 0 && colToUpdateSeq !== undefined) {
|
|
135422
|
-
colToUpdateSeq.valueToUpdate = colToUpdateSeq?.valueToUpdate === undefined
|
|
135423
|
-
? generatedValue
|
|
135424
|
-
: intMax([colToUpdateSeq.valueToUpdate, generatedValue]);
|
|
135425
|
-
}
|
|
135426
136485
|
}
|
|
135427
136486
|
if ((insertDataInDb === true || updateDataInDb === true)
|
|
135428
136487
|
&& ((i + 1) % batchSize === 0 || i === count - 1)) {
|
|
@@ -135469,28 +136528,9 @@ class SeedService {
|
|
|
135469
136528
|
}
|
|
135470
136529
|
}
|
|
135471
136530
|
}
|
|
135472
|
-
const columnsToUpdateSeqFiltered = [...columnsToUpdateSeq.values()].filter((col) => col.valueToUpdate !== undefined);
|
|
135473
|
-
if (i === count - 1
|
|
135474
|
-
&& columnsToUpdateSeqFiltered.length !== 0 && db !== undefined) {
|
|
135475
|
-
for (const columnConfig of columnsToUpdateSeq.values()) {
|
|
135476
|
-
if (columnConfig) {
|
|
135477
|
-
await this.updateColumnSequence({ db, columnConfig });
|
|
135478
|
-
}
|
|
135479
|
-
}
|
|
135480
|
-
}
|
|
135481
136531
|
}
|
|
135482
136532
|
return preserveData === true ? generatedValues : [];
|
|
135483
136533
|
};
|
|
135484
|
-
updateColumnSequence = async ({ db, columnConfig: { schemaName, tableName, columnName, valueToUpdate } }) => {
|
|
135485
|
-
if (drizzleOrm.is(db, pgCore.PgDatabase)) {
|
|
135486
|
-
const fullTableName = schemaName ? `"${schemaName}"."${tableName}"` : `"${tableName}"`;
|
|
135487
|
-
const rawQuery = `SELECT setval(pg_get_serial_sequence('${fullTableName}', '${columnName}'), ${(valueToUpdate ?? 'null').toString()}, true);`;
|
|
135488
|
-
await db.execute(drizzleOrm.sql.raw(rawQuery));
|
|
135489
|
-
}
|
|
135490
|
-
// mysql updates auto_increment or serial column by itself
|
|
135491
|
-
// sqlite updates autoincrement column by itself
|
|
135492
|
-
return;
|
|
135493
|
-
};
|
|
135494
136534
|
insertInDb = async ({ generatedValues, db, schema, tableName, override, }) => {
|
|
135495
136535
|
if (drizzleOrm.is(db, (pgCore.PgDatabase))) {
|
|
135496
136536
|
const query = db.insert(schema[tableName]);
|
|
@@ -135509,249 +136549,726 @@ class SeedService {
|
|
|
135509
136549
|
.insert(schema[tableName])
|
|
135510
136550
|
.values(generatedValues);
|
|
135511
136551
|
}
|
|
136552
|
+
else if (drizzleOrm.is(db, (mssqlCore.MsSqlDatabase))) {
|
|
136553
|
+
let schemaDbName;
|
|
136554
|
+
let tableDbName;
|
|
136555
|
+
if (override === true) {
|
|
136556
|
+
const tableConfig = mssqlCore.getTableConfig(schema[tableName]);
|
|
136557
|
+
schemaDbName = tableConfig.schema ?? 'dbo';
|
|
136558
|
+
tableDbName = tableConfig.name;
|
|
136559
|
+
await db.execute(drizzleOrm.sql.raw(`SET IDENTITY_INSERT [${schemaDbName}].[${tableDbName}] ON;`));
|
|
136560
|
+
}
|
|
136561
|
+
await db
|
|
136562
|
+
.insert(schema[tableName])
|
|
136563
|
+
.values(generatedValues);
|
|
136564
|
+
if (override === true) {
|
|
136565
|
+
await db.execute(drizzleOrm.sql.raw(`SET IDENTITY_INSERT [${schemaDbName}].[${tableDbName}] OFF;`));
|
|
136566
|
+
}
|
|
136567
|
+
}
|
|
136568
|
+
else if (drizzleOrm.is(db, (cockroachCore.CockroachDatabase))) {
|
|
136569
|
+
const query = db
|
|
136570
|
+
.insert(schema[tableName])
|
|
136571
|
+
.values(generatedValues);
|
|
136572
|
+
await query;
|
|
136573
|
+
}
|
|
136574
|
+
else if (drizzleOrm.is(db, (singlestoreCore.SingleStoreDatabase))) {
|
|
136575
|
+
const query = db
|
|
136576
|
+
.insert(schema[tableName])
|
|
136577
|
+
.values(generatedValues);
|
|
136578
|
+
await query;
|
|
136579
|
+
}
|
|
135512
136580
|
};
|
|
135513
136581
|
updateDb = async ({ generatedValues, db, schema, tableName, uniqueNotNullColName, }) => {
|
|
136582
|
+
let values = generatedValues[0];
|
|
136583
|
+
const uniqueNotNullColValue = values[uniqueNotNullColName];
|
|
136584
|
+
values = Object.fromEntries(Object.entries(values).filter(([colName]) => colName !== uniqueNotNullColName));
|
|
135514
136585
|
if (drizzleOrm.is(db, (pgCore.PgDatabase))) {
|
|
135515
136586
|
const table = schema[tableName];
|
|
135516
136587
|
const uniqueNotNullCol = table[uniqueNotNullColName];
|
|
135517
|
-
await db.update(table).set(
|
|
136588
|
+
await db.update(table).set(values).where(drizzleOrm.eq(uniqueNotNullCol, uniqueNotNullColValue));
|
|
135518
136589
|
}
|
|
135519
136590
|
else if (drizzleOrm.is(db, (mysqlCore.MySqlDatabase))) {
|
|
135520
136591
|
const table = schema[tableName];
|
|
135521
|
-
await db.update(table).set(
|
|
136592
|
+
await db.update(table).set(values).where(drizzleOrm.eq(table[uniqueNotNullColName], uniqueNotNullColValue));
|
|
135522
136593
|
}
|
|
135523
136594
|
else if (drizzleOrm.is(db, (sqliteCore.BaseSQLiteDatabase))) {
|
|
135524
136595
|
const table = schema[tableName];
|
|
135525
|
-
await db.update(table).set(
|
|
136596
|
+
await db.update(table).set(values).where(drizzleOrm.eq(table[uniqueNotNullColName], uniqueNotNullColValue));
|
|
136597
|
+
}
|
|
136598
|
+
else if (drizzleOrm.is(db, (mssqlCore.MsSqlDatabase))) {
|
|
136599
|
+
const table = schema[tableName];
|
|
136600
|
+
await db.update(table).set(values).where(drizzleOrm.eq(table[uniqueNotNullColName], uniqueNotNullColValue));
|
|
136601
|
+
}
|
|
136602
|
+
else if (drizzleOrm.is(db, (cockroachCore.CockroachDatabase))) {
|
|
136603
|
+
const table = schema[tableName];
|
|
136604
|
+
await db.update(table).set(values).where(drizzleOrm.eq(table[uniqueNotNullColName], uniqueNotNullColValue));
|
|
136605
|
+
}
|
|
136606
|
+
else if (drizzleOrm.is(db, (singlestoreCore.SingleStoreDatabase))) {
|
|
136607
|
+
const table = schema[tableName];
|
|
136608
|
+
await db.update(table).set(values).where(drizzleOrm.eq(table[uniqueNotNullColName], uniqueNotNullColValue));
|
|
135526
136609
|
}
|
|
135527
136610
|
};
|
|
135528
136611
|
}
|
|
135529
136612
|
|
|
135530
|
-
|
|
135531
|
-
|
|
135532
|
-
|
|
135533
|
-
|
|
135534
|
-
|
|
135535
|
-
|
|
135536
|
-
|
|
135537
|
-
|
|
135538
|
-
|
|
135539
|
-
|
|
135540
|
-
|
|
135541
|
-
|
|
135542
|
-
|
|
135543
|
-
|
|
135544
|
-
|
|
135545
|
-
|
|
135546
|
-
|
|
135547
|
-
}
|
|
135548
|
-
|
|
135549
|
-
|
|
135550
|
-
|
|
135551
|
-
|
|
135552
|
-
|
|
135553
|
-
|
|
135554
|
-
|
|
136613
|
+
// Cockroach-----------------------------------------------------------------------------------------------------------
|
|
136614
|
+
const resetCockroach = async (db, cockroachTables) => {
|
|
136615
|
+
const tablesToTruncate = Object.entries(cockroachTables).map(([_, table]) => {
|
|
136616
|
+
const config = cockroachCore.getTableConfig(table);
|
|
136617
|
+
config.schema = config.schema === undefined ? 'public' : config.schema;
|
|
136618
|
+
return `"${config.schema}"."${config.name}"`;
|
|
136619
|
+
});
|
|
136620
|
+
await db.execute(drizzleOrm.sql.raw(`truncate ${tablesToTruncate.join(',')} cascade;`));
|
|
136621
|
+
};
|
|
136622
|
+
const filterCockroachSchema = (schema) => {
|
|
136623
|
+
const cockroachSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], cockroachCore.CockroachTable) || drizzleOrm.is(keyValue[1], drizzleOrm.Relations)));
|
|
136624
|
+
const cockroachTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], cockroachCore.CockroachTable)));
|
|
136625
|
+
return { cockroachSchema, cockroachTables };
|
|
136626
|
+
};
|
|
136627
|
+
const seedCockroach = async (db, schema, options = {}, refinements) => {
|
|
136628
|
+
const seedService = new SeedService();
|
|
136629
|
+
const { cockroachSchema, cockroachTables } = filterCockroachSchema(schema);
|
|
136630
|
+
const { tables, relations } = getCockroachInfo(cockroachSchema, cockroachTables);
|
|
136631
|
+
const generatedTablesGenerators = seedService.generatePossibleGenerators('cockroach', tables, relations, refinements, options);
|
|
136632
|
+
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
136633
|
+
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, cockroachTables, { ...options, preserveCyclicTablesData });
|
|
136634
|
+
const { filteredTablesGenerators, tablesUniqueNotNullColumn } = seedService.filterCyclicTables(generatedTablesGenerators);
|
|
136635
|
+
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
136636
|
+
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, cockroachTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
136637
|
+
};
|
|
136638
|
+
const getCockroachInfo = (cockroachSchema, cockroachTables) => {
|
|
136639
|
+
let tableConfig;
|
|
136640
|
+
let dbToTsColumnNamesMap;
|
|
136641
|
+
const dbToTsTableNamesMap = Object.fromEntries(Object.entries(cockroachTables).map(([key, value]) => [drizzleOrm.getTableName(value), key]));
|
|
136642
|
+
const tables = [];
|
|
136643
|
+
const relations = [];
|
|
136644
|
+
const dbToTsColumnNamesMapGlobal = {};
|
|
136645
|
+
const tableRelations = {};
|
|
136646
|
+
const getDbToTsColumnNamesMap = (table) => {
|
|
136647
|
+
let dbToTsColumnNamesMap = {};
|
|
136648
|
+
const tableName = drizzleOrm.getTableName(table);
|
|
136649
|
+
if (Object.hasOwn(dbToTsColumnNamesMapGlobal, tableName)) {
|
|
136650
|
+
dbToTsColumnNamesMap = dbToTsColumnNamesMapGlobal[tableName];
|
|
136651
|
+
return dbToTsColumnNamesMap;
|
|
136652
|
+
}
|
|
136653
|
+
const tableConfig = cockroachCore.getTableConfig(table);
|
|
136654
|
+
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
136655
|
+
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
136656
|
+
}
|
|
136657
|
+
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
136658
|
+
return dbToTsColumnNamesMap;
|
|
136659
|
+
};
|
|
136660
|
+
const transformFromDrizzleRelation = (schema, getDbToTsColumnNamesMap, tableRelations) => {
|
|
136661
|
+
const schemaConfig = drizzleOrm.extractTablesRelationalConfig(schema, drizzleOrm.createTableRelationsHelpers);
|
|
136662
|
+
const relations = [];
|
|
136663
|
+
for (const table of Object.values(schemaConfig.tables)) {
|
|
136664
|
+
if (table.relations === undefined)
|
|
136665
|
+
continue;
|
|
136666
|
+
for (const drizzleRel of Object.values(table.relations)) {
|
|
136667
|
+
if (!drizzleOrm.is(drizzleRel, drizzleOrm.One))
|
|
136668
|
+
continue;
|
|
136669
|
+
const tableConfig = cockroachCore.getTableConfig(drizzleRel.sourceTable);
|
|
136670
|
+
const tableDbSchema = tableConfig.schema ?? 'public';
|
|
136671
|
+
const tableDbName = tableConfig.name;
|
|
136672
|
+
const tableTsName = schemaConfig.tableNamesMap[`${tableDbSchema}.${tableDbName}`] ?? tableDbName;
|
|
136673
|
+
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
136674
|
+
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
136675
|
+
?? [];
|
|
136676
|
+
const refTableConfig = cockroachCore.getTableConfig(drizzleRel.referencedTable);
|
|
136677
|
+
const refTableDbSchema = refTableConfig.schema ?? 'public';
|
|
136678
|
+
const refTableDbName = refTableConfig.name;
|
|
136679
|
+
const refTableTsName = schemaConfig.tableNamesMap[`${refTableDbSchema}.${refTableDbName}`]
|
|
136680
|
+
?? refTableDbName;
|
|
136681
|
+
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
136682
|
+
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
136683
|
+
?? [];
|
|
136684
|
+
if (tableRelations[refTableTsName] === undefined) {
|
|
136685
|
+
tableRelations[refTableTsName] = [];
|
|
136686
|
+
}
|
|
136687
|
+
const relation = {
|
|
136688
|
+
table: tableTsName,
|
|
136689
|
+
columns,
|
|
136690
|
+
refTable: refTableTsName,
|
|
136691
|
+
refColumns,
|
|
136692
|
+
refTableRels: tableRelations[refTableTsName],
|
|
136693
|
+
type: 'one',
|
|
136694
|
+
};
|
|
136695
|
+
// do not add duplicate relation
|
|
136696
|
+
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
136697
|
+
&& rel.refTable === relation.refTable)) {
|
|
136698
|
+
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
136699
|
+
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
136700
|
+
+ `In this case, the foreign key constraint will be used.\n`);
|
|
136701
|
+
continue;
|
|
136702
|
+
}
|
|
136703
|
+
relations.push(relation);
|
|
136704
|
+
tableRelations[tableTsName].push(relation);
|
|
136705
|
+
}
|
|
136706
|
+
}
|
|
136707
|
+
return relations;
|
|
136708
|
+
};
|
|
136709
|
+
for (const table of Object.values(cockroachTables)) {
|
|
136710
|
+
tableConfig = cockroachCore.getTableConfig(table);
|
|
136711
|
+
dbToTsColumnNamesMap = {};
|
|
136712
|
+
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
136713
|
+
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
136714
|
+
}
|
|
136715
|
+
// might be empty list
|
|
136716
|
+
const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
136717
|
+
const table = dbToTsTableNamesMap[tableConfig.name];
|
|
136718
|
+
const refTable = dbToTsTableNamesMap[drizzleOrm.getTableName(fk.reference().foreignTable)];
|
|
136719
|
+
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(fk.reference().foreignTable);
|
|
136720
|
+
if (tableRelations[refTable] === undefined) {
|
|
136721
|
+
tableRelations[refTable] = [];
|
|
136722
|
+
}
|
|
136723
|
+
return {
|
|
136724
|
+
table,
|
|
136725
|
+
columns: fk
|
|
136726
|
+
.reference()
|
|
136727
|
+
.columns.map((col) => dbToTsColumnNamesMap[col.name]),
|
|
136728
|
+
refTable,
|
|
136729
|
+
refColumns: fk
|
|
136730
|
+
.reference()
|
|
136731
|
+
.foreignColumns.map((fCol) => dbToTsColumnNamesMapForRefTable[fCol.name]),
|
|
136732
|
+
refTableRels: tableRelations[refTable],
|
|
136733
|
+
};
|
|
135555
136734
|
});
|
|
135556
|
-
|
|
135557
|
-
|
|
135558
|
-
|
|
135559
|
-
? callback(generatorsFuncsV2)
|
|
135560
|
-
: callback(generatorsFuncs);
|
|
135561
|
-
await seedFunc(this.db, this.schema, this.options, refinements);
|
|
135562
|
-
}
|
|
135563
|
-
}
|
|
135564
|
-
function getGeneratorsFunctions() {
|
|
135565
|
-
return generatorsFuncs;
|
|
135566
|
-
}
|
|
135567
|
-
async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleStudioRelations, schemasRefinements, options }) {
|
|
135568
|
-
const generatedSchemas = {};
|
|
135569
|
-
let tables, relations, refinements;
|
|
135570
|
-
drizzleStudioRelations = drizzleStudioRelations.filter((rel) => rel.type === 'one');
|
|
135571
|
-
for (const [schemaName, { tables: drizzleStudioTables }] of Object.entries(drizzleStudioObject)) {
|
|
135572
|
-
tables = [];
|
|
135573
|
-
for (const [tableName, table] of Object.entries(drizzleStudioTables)) {
|
|
135574
|
-
const drizzleStudioColumns = Object.values(table.columns);
|
|
135575
|
-
const columns = drizzleStudioColumns.map((col) => ({
|
|
135576
|
-
name: col.name,
|
|
135577
|
-
dataType: 'string',
|
|
135578
|
-
columnType: col.type,
|
|
135579
|
-
// TODO: revise later
|
|
135580
|
-
typeParams: {},
|
|
135581
|
-
default: col.default,
|
|
135582
|
-
hasDefault: col.default === undefined ? false : true,
|
|
135583
|
-
isUnique: col.isUnique === undefined ? false : col.isUnique,
|
|
135584
|
-
notNull: col.notNull,
|
|
135585
|
-
primary: col.primaryKey,
|
|
135586
|
-
}));
|
|
135587
|
-
tables.push({
|
|
135588
|
-
name: tableName,
|
|
135589
|
-
columns,
|
|
135590
|
-
primaryKeys: drizzleStudioColumns.filter((col) => col.primaryKey === true).map((col) => col.name),
|
|
135591
|
-
});
|
|
136735
|
+
relations.push(...newRelations);
|
|
136736
|
+
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
136737
|
+
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
135592
136738
|
}
|
|
135593
|
-
|
|
135594
|
-
const
|
|
135595
|
-
|
|
135596
|
-
|
|
136739
|
+
tableRelations[dbToTsTableNamesMap[tableConfig.name]].push(...newRelations);
|
|
136740
|
+
const getAllBaseColumns = (baseColumn) => {
|
|
136741
|
+
const baseColumnResult = {
|
|
136742
|
+
name: baseColumn.name,
|
|
136743
|
+
columnType: baseColumn.getSQLType(),
|
|
136744
|
+
typeParams: getTypeParams(baseColumn.getSQLType()),
|
|
136745
|
+
dataType: baseColumn.dataType,
|
|
136746
|
+
size: baseColumn.size,
|
|
136747
|
+
hasDefault: baseColumn.hasDefault,
|
|
136748
|
+
enumValues: baseColumn.enumValues,
|
|
136749
|
+
default: baseColumn.default,
|
|
136750
|
+
isUnique: baseColumn.isUnique,
|
|
136751
|
+
notNull: baseColumn.notNull,
|
|
136752
|
+
primary: baseColumn.primary,
|
|
136753
|
+
baseColumn: baseColumn.baseColumn === undefined ? undefined : getAllBaseColumns(baseColumn.baseColumn),
|
|
136754
|
+
};
|
|
136755
|
+
return baseColumnResult;
|
|
136756
|
+
};
|
|
136757
|
+
const getTypeParams = (sqlType) => {
|
|
136758
|
+
// get type params
|
|
136759
|
+
const typeParams = {};
|
|
136760
|
+
// handle dimensions
|
|
136761
|
+
if (sqlType.includes('[')) {
|
|
136762
|
+
const match = sqlType.match(/\[\w*]/g);
|
|
136763
|
+
if (match) {
|
|
136764
|
+
typeParams['dimensions'] = match.length;
|
|
136765
|
+
}
|
|
135597
136766
|
}
|
|
135598
|
-
|
|
136767
|
+
if (sqlType.startsWith('numeric')
|
|
136768
|
+
|| sqlType.startsWith('decimal')
|
|
136769
|
+
|| sqlType.startsWith('double precision')
|
|
136770
|
+
|| sqlType.startsWith('real')) {
|
|
136771
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
136772
|
+
if (match) {
|
|
136773
|
+
typeParams['precision'] = Number(match[1]);
|
|
136774
|
+
typeParams['scale'] = Number(match[2]);
|
|
136775
|
+
}
|
|
136776
|
+
}
|
|
136777
|
+
else if (sqlType.startsWith('varchar')
|
|
136778
|
+
|| sqlType.startsWith('char')
|
|
136779
|
+
|| sqlType.startsWith('bit')
|
|
136780
|
+
|| sqlType.startsWith('vector')
|
|
136781
|
+
|| sqlType.startsWith('time')
|
|
136782
|
+
|| sqlType.startsWith('timestamp')
|
|
136783
|
+
|| sqlType.startsWith('interval')) {
|
|
136784
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
136785
|
+
if (match) {
|
|
136786
|
+
typeParams['length'] = Number(match[1]);
|
|
136787
|
+
}
|
|
136788
|
+
}
|
|
136789
|
+
return typeParams;
|
|
136790
|
+
};
|
|
136791
|
+
// console.log(tableConfig.columns);
|
|
136792
|
+
tables.push({
|
|
136793
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
136794
|
+
columns: tableConfig.columns.map((column) => ({
|
|
136795
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
136796
|
+
columnType: column.getSQLType(),
|
|
136797
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
136798
|
+
dataType: column.dataType,
|
|
136799
|
+
size: column.size,
|
|
136800
|
+
hasDefault: column.hasDefault,
|
|
136801
|
+
default: column.default,
|
|
136802
|
+
enumValues: column.enumValues,
|
|
136803
|
+
isUnique: column.isUnique,
|
|
136804
|
+
notNull: column.notNull,
|
|
136805
|
+
primary: column.primary,
|
|
136806
|
+
generatedIdentityType: column.generatedIdentity?.type,
|
|
136807
|
+
baseColumn: (column.baseColumn === undefined)
|
|
136808
|
+
? undefined
|
|
136809
|
+
: getAllBaseColumns(column.baseColumn),
|
|
136810
|
+
})),
|
|
136811
|
+
primaryKeys: tableConfig.columns
|
|
136812
|
+
.filter((column) => column.primary)
|
|
136813
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
135599
136814
|
});
|
|
135600
|
-
refinements = schemasRefinements !== undefined && schemasRefinements[schemaName] !== undefined
|
|
135601
|
-
? schemasRefinements[schemaName]
|
|
135602
|
-
: undefined;
|
|
135603
|
-
const seedService = new SeedService();
|
|
135604
|
-
const generatedTablesGenerators = seedService.generatePossibleGenerators(sqlDialect, tables, isCyclicRelations, refinements, options);
|
|
135605
|
-
const generatedTables = await seedService.generateTablesValues(isCyclicRelations, generatedTablesGenerators, undefined, undefined, { ...options, preserveData: true, insertDataInDb: false });
|
|
135606
|
-
generatedSchemas[schemaName] = { tables: generatedTables };
|
|
135607
136815
|
}
|
|
135608
|
-
|
|
135609
|
-
|
|
135610
|
-
|
|
135611
|
-
|
|
135612
|
-
|
|
135613
|
-
|
|
135614
|
-
|
|
135615
|
-
|
|
135616
|
-
|
|
135617
|
-
|
|
135618
|
-
|
|
135619
|
-
|
|
135620
|
-
|
|
135621
|
-
|
|
135622
|
-
|
|
135623
|
-
|
|
135624
|
-
|
|
135625
|
-
|
|
135626
|
-
|
|
135627
|
-
|
|
135628
|
-
|
|
135629
|
-
|
|
135630
|
-
|
|
135631
|
-
|
|
135632
|
-
|
|
135633
|
-
|
|
135634
|
-
|
|
135635
|
-
|
|
135636
|
-
|
|
135637
|
-
|
|
135638
|
-
|
|
135639
|
-
|
|
135640
|
-
|
|
135641
|
-
|
|
135642
|
-
|
|
135643
|
-
|
|
135644
|
-
|
|
135645
|
-
|
|
135646
|
-
|
|
135647
|
-
|
|
135648
|
-
|
|
135649
|
-
|
|
135650
|
-
|
|
135651
|
-
|
|
135652
|
-
|
|
135653
|
-
|
|
135654
|
-
|
|
135655
|
-
|
|
135656
|
-
|
|
135657
|
-
|
|
135658
|
-
|
|
135659
|
-
|
|
135660
|
-
|
|
135661
|
-
|
|
135662
|
-
|
|
135663
|
-
|
|
135664
|
-
|
|
135665
|
-
|
|
135666
|
-
|
|
135667
|
-
|
|
135668
|
-
}
|
|
135669
|
-
|
|
135670
|
-
|
|
135671
|
-
|
|
135672
|
-
|
|
135673
|
-
|
|
135674
|
-
|
|
135675
|
-
|
|
135676
|
-
|
|
135677
|
-
|
|
135678
|
-
|
|
135679
|
-
|
|
135680
|
-
|
|
135681
|
-
|
|
136816
|
+
const transformedDrizzleRelations = transformFromDrizzleRelation(cockroachSchema, getDbToTsColumnNamesMap, tableRelations);
|
|
136817
|
+
relations.push(...transformedDrizzleRelations);
|
|
136818
|
+
const isCyclicRelations = relations.map((relI) => {
|
|
136819
|
+
// if (relations.some((relj) => relI.table === relj.refTable && relI.refTable === relj.table)) {
|
|
136820
|
+
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
136821
|
+
if (isRelationCyclic(relI)) {
|
|
136822
|
+
tableRel['isCyclic'] = true;
|
|
136823
|
+
return { ...relI, isCyclic: true };
|
|
136824
|
+
}
|
|
136825
|
+
tableRel['isCyclic'] = false;
|
|
136826
|
+
return { ...relI, isCyclic: false };
|
|
136827
|
+
});
|
|
136828
|
+
return { tables, relations: isCyclicRelations, tableRelations };
|
|
136829
|
+
};
|
|
136830
|
+
|
|
136831
|
+
// MySql-----------------------------------------------------------------------------------------------------
|
|
136832
|
+
const resetMsSql = async (db, schema) => {
|
|
136833
|
+
const tablesToTruncate = Object.entries(schema).map(([_tsTableName, table]) => {
|
|
136834
|
+
const tableConfig = mssqlCore.getTableConfig(table);
|
|
136835
|
+
return { dbName: tableConfig.name, dbSchema: tableConfig.schema ?? 'dbo' };
|
|
136836
|
+
});
|
|
136837
|
+
const allFkConstraints = {};
|
|
136838
|
+
for (const table of tablesToTruncate) {
|
|
136839
|
+
const gatherTableRelatedFkConstraints = `
|
|
136840
|
+
DECLARE @objectId INT
|
|
136841
|
+
= OBJECT_ID( QUOTENAME('${table.dbSchema}') + '.' + QUOTENAME('${table.dbName}') );
|
|
136842
|
+
|
|
136843
|
+
SELECT
|
|
136844
|
+
fk.name AS fkName,
|
|
136845
|
+
OBJECT_SCHEMA_NAME(fk.parent_object_id) AS parentSchema,
|
|
136846
|
+
OBJECT_NAME(fk.parent_object_id) AS parentTable,
|
|
136847
|
+
OBJECT_SCHEMA_NAME(fk.referenced_object_id) AS referencedSchema,
|
|
136848
|
+
OBJECT_NAME(fk.referenced_object_id) AS referencedTable,
|
|
136849
|
+
-- fkc.constraint_column_id AS Column_Ordinal,
|
|
136850
|
+
pc.name AS parentColumn,
|
|
136851
|
+
rc.name AS referencedColumn,
|
|
136852
|
+
fk.delete_referential_action_desc AS onDeleteAction,
|
|
136853
|
+
fk.update_referential_action_desc AS onUpdateAction,
|
|
136854
|
+
CASE
|
|
136855
|
+
WHEN fk.parent_object_id = @objectId THEN 'outbound' -- your table → another table
|
|
136856
|
+
ELSE 'inbound' -- another table → your table
|
|
136857
|
+
END AS relation
|
|
136858
|
+
FROM sys.foreign_keys AS fk
|
|
136859
|
+
JOIN sys.foreign_key_columns fkc
|
|
136860
|
+
ON fk.object_id = fkc.constraint_object_id
|
|
136861
|
+
JOIN sys.columns pc
|
|
136862
|
+
ON fkc.parent_object_id = pc.object_id
|
|
136863
|
+
AND fkc.parent_column_id = pc.column_id
|
|
136864
|
+
JOIN sys.columns rc
|
|
136865
|
+
ON fkc.referenced_object_id = rc.object_id
|
|
136866
|
+
AND fkc.referenced_column_id = rc.column_id
|
|
136867
|
+
WHERE fk.parent_object_id = @objectId
|
|
136868
|
+
OR fk.referenced_object_id = @objectId
|
|
136869
|
+
ORDER BY relation, fkName;
|
|
136870
|
+
`;
|
|
136871
|
+
const rawRes = await db.execute(drizzleOrm.sql.raw(gatherTableRelatedFkConstraints));
|
|
136872
|
+
const res = rawRes.recordset;
|
|
136873
|
+
const tableRelatedFkConstraints = {};
|
|
136874
|
+
for (const fkInfo of res) {
|
|
136875
|
+
if (tableRelatedFkConstraints[fkInfo.fkName] === undefined) {
|
|
136876
|
+
const { parentColumn: _, referencedColumn: __, ...filteredFkInfo } = fkInfo;
|
|
136877
|
+
tableRelatedFkConstraints[fkInfo.fkName] = {
|
|
136878
|
+
...filteredFkInfo,
|
|
136879
|
+
parentColumns: res.filter(({ fkName }) => fkName === fkInfo.fkName).map(({ parentColumn }) => parentColumn),
|
|
136880
|
+
referencedColumns: res.filter(({ fkName }) => fkName === fkInfo.fkName).map(({ referencedColumn }) => referencedColumn),
|
|
136881
|
+
};
|
|
136882
|
+
}
|
|
136883
|
+
}
|
|
136884
|
+
allFkConstraints[`${table.dbSchema}.${table.dbName}`] = tableRelatedFkConstraints;
|
|
136885
|
+
// drop all table related fk constraints
|
|
136886
|
+
for (const fkInfo of Object.values(tableRelatedFkConstraints)) {
|
|
136887
|
+
const dropFkConstraints = `ALTER TABLE [${fkInfo.parentSchema}].[${fkInfo.parentTable}] DROP CONSTRAINT [${fkInfo.fkName}];`;
|
|
136888
|
+
await db.execute(drizzleOrm.sql.raw(dropFkConstraints));
|
|
136889
|
+
}
|
|
136890
|
+
// truncating
|
|
136891
|
+
const truncateTable = `truncate table [${table.dbSchema}].[${table.dbName}];`;
|
|
136892
|
+
await db.execute(drizzleOrm.sql.raw(truncateTable));
|
|
135682
136893
|
}
|
|
135683
|
-
|
|
135684
|
-
|
|
136894
|
+
// add all table related fk constraints
|
|
136895
|
+
for (const table of tablesToTruncate) {
|
|
136896
|
+
const tableRelatedFkConstraints = allFkConstraints[`${table.dbSchema}.${table.dbName}`];
|
|
136897
|
+
for (const fkInfo of Object.values(tableRelatedFkConstraints)) {
|
|
136898
|
+
const addFkConstraints = `
|
|
136899
|
+
ALTER TABLE [${fkInfo.parentSchema}].[${fkInfo.parentTable}]
|
|
136900
|
+
ADD CONSTRAINT [${fkInfo.fkName}]
|
|
136901
|
+
FOREIGN KEY(${fkInfo.parentColumns.map((colName) => `[${colName}]`).join(',')})
|
|
136902
|
+
REFERENCES [${fkInfo.referencedSchema}].[${fkInfo.referencedTable}] (${fkInfo.referencedColumns.map((colName) => `[${colName}]`).join(',')})
|
|
136903
|
+
ON DELETE ${fkInfo.onDeleteAction.split('_').join(' ')}
|
|
136904
|
+
ON UPDATE ${fkInfo.onUpdateAction.split('_').join(' ')};
|
|
136905
|
+
`;
|
|
136906
|
+
await db.execute(drizzleOrm.sql.raw(addFkConstraints));
|
|
136907
|
+
}
|
|
135685
136908
|
}
|
|
135686
|
-
return;
|
|
135687
136909
|
};
|
|
135688
|
-
|
|
135689
|
-
|
|
135690
|
-
|
|
135691
|
-
|
|
135692
|
-
|
|
135693
|
-
|
|
135694
|
-
|
|
135695
|
-
|
|
135696
|
-
|
|
135697
|
-
|
|
135698
|
-
|
|
135699
|
-
|
|
135700
|
-
|
|
135701
|
-
|
|
135702
|
-
|
|
135703
|
-
|
|
135704
|
-
|
|
135705
|
-
|
|
135706
|
-
|
|
135707
|
-
|
|
135708
|
-
|
|
135709
|
-
|
|
135710
|
-
|
|
135711
|
-
|
|
135712
|
-
|
|
135713
|
-
|
|
135714
|
-
|
|
135715
|
-
|
|
135716
|
-
|
|
135717
|
-
|
|
135718
|
-
* .
|
|
135719
|
-
*
|
|
135720
|
-
* PRAGMA foreign_keys = ON;
|
|
135721
|
-
* ```
|
|
135722
|
-
*
|
|
135723
|
-
* @example
|
|
135724
|
-
* ```ts
|
|
135725
|
-
* await reset(db, schema);
|
|
135726
|
-
*
|
|
135727
|
-
* // Alternatively, you can provide an object containing your tables
|
|
135728
|
-
* // as the `schema` parameter when calling `reset`.
|
|
135729
|
-
* await reset(db, { users });
|
|
135730
|
-
* ```
|
|
135731
|
-
*/
|
|
135732
|
-
async function reset(db, schema) {
|
|
135733
|
-
if (drizzleOrm.is(db, (pgCore.PgDatabase))) {
|
|
135734
|
-
const { pgTables } = filterPgSchema(schema);
|
|
135735
|
-
if (Object.entries(pgTables).length > 0) {
|
|
135736
|
-
await resetPostgres(db, pgTables);
|
|
136910
|
+
const filterMsSqlTables = (schema) => {
|
|
136911
|
+
const mssqlSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mssqlCore.MsSqlTable) || drizzleOrm.is(keyValue[1], drizzleOrm.Relations)));
|
|
136912
|
+
const mssqlTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mssqlCore.MsSqlTable)));
|
|
136913
|
+
return { mssqlSchema, mssqlTables };
|
|
136914
|
+
};
|
|
136915
|
+
const seedMsSql = async (db, schema, options = {}, refinements) => {
|
|
136916
|
+
const { mssqlSchema, mssqlTables } = filterMsSqlTables(schema);
|
|
136917
|
+
const { tables, relations } = getMsSqlInfo(mssqlSchema, mssqlTables);
|
|
136918
|
+
const seedService = new SeedService();
|
|
136919
|
+
const generatedTablesGenerators = seedService.generatePossibleGenerators('mssql', tables, relations, refinements, options);
|
|
136920
|
+
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
136921
|
+
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, mssqlTables, { ...options, preserveCyclicTablesData });
|
|
136922
|
+
const { filteredTablesGenerators, tablesUniqueNotNullColumn } = seedService.filterCyclicTables(generatedTablesGenerators);
|
|
136923
|
+
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
136924
|
+
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, mssqlTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
136925
|
+
};
|
|
136926
|
+
const getMsSqlInfo = (mssqlSchema, mssqlTables) => {
|
|
136927
|
+
let tableConfig;
|
|
136928
|
+
let dbToTsColumnNamesMap;
|
|
136929
|
+
const dbToTsTableNamesMap = Object.fromEntries(Object.entries(mssqlTables).map(([key, value]) => [drizzleOrm.getTableName(value), key]));
|
|
136930
|
+
const tables = [];
|
|
136931
|
+
const relations = [];
|
|
136932
|
+
const dbToTsColumnNamesMapGlobal = {};
|
|
136933
|
+
const tableRelations = {};
|
|
136934
|
+
const getDbToTsColumnNamesMap = (table) => {
|
|
136935
|
+
let dbToTsColumnNamesMap = {};
|
|
136936
|
+
const tableName = drizzleOrm.getTableName(table);
|
|
136937
|
+
if (Object.hasOwn(dbToTsColumnNamesMapGlobal, tableName)) {
|
|
136938
|
+
dbToTsColumnNamesMap = dbToTsColumnNamesMapGlobal[tableName];
|
|
136939
|
+
return dbToTsColumnNamesMap;
|
|
135737
136940
|
}
|
|
135738
|
-
|
|
135739
|
-
|
|
135740
|
-
|
|
135741
|
-
if (Object.entries(mysqlTables).length > 0) {
|
|
135742
|
-
await resetMySql(db, mysqlTables);
|
|
136941
|
+
const tableConfig = mssqlCore.getTableConfig(table);
|
|
136942
|
+
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
136943
|
+
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
135743
136944
|
}
|
|
136945
|
+
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
136946
|
+
return dbToTsColumnNamesMap;
|
|
136947
|
+
};
|
|
136948
|
+
const transformFromDrizzleRelation = (schema, getDbToTsColumnNamesMap, tableRelations) => {
|
|
136949
|
+
const schemaConfig = drizzleOrm.extractTablesRelationalConfig(schema, drizzleOrm.createTableRelationsHelpers);
|
|
136950
|
+
const relations = [];
|
|
136951
|
+
for (const table of Object.values(schemaConfig.tables)) {
|
|
136952
|
+
if (table.relations === undefined)
|
|
136953
|
+
continue;
|
|
136954
|
+
for (const drizzleRel of Object.values(table.relations)) {
|
|
136955
|
+
if (!drizzleOrm.is(drizzleRel, drizzleOrm.One))
|
|
136956
|
+
continue;
|
|
136957
|
+
const tableConfig = mssqlCore.getTableConfig(drizzleRel.sourceTable);
|
|
136958
|
+
const tableDbSchema = tableConfig.schema ?? 'public';
|
|
136959
|
+
const tableDbName = tableConfig.name;
|
|
136960
|
+
const tableTsName = schemaConfig.tableNamesMap[`${tableDbSchema}.${tableDbName}`] ?? tableDbName;
|
|
136961
|
+
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
136962
|
+
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
136963
|
+
?? [];
|
|
136964
|
+
const refTableConfig = mssqlCore.getTableConfig(drizzleRel.referencedTable);
|
|
136965
|
+
const refTableDbSchema = refTableConfig.schema ?? 'public';
|
|
136966
|
+
const refTableDbName = refTableConfig.name;
|
|
136967
|
+
const refTableTsName = schemaConfig.tableNamesMap[`${refTableDbSchema}.${refTableDbName}`]
|
|
136968
|
+
?? refTableDbName;
|
|
136969
|
+
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
136970
|
+
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
136971
|
+
?? [];
|
|
136972
|
+
if (tableRelations[refTableTsName] === undefined) {
|
|
136973
|
+
tableRelations[refTableTsName] = [];
|
|
136974
|
+
}
|
|
136975
|
+
const relation = {
|
|
136976
|
+
table: tableTsName,
|
|
136977
|
+
columns,
|
|
136978
|
+
refTable: refTableTsName,
|
|
136979
|
+
refColumns,
|
|
136980
|
+
refTableRels: tableRelations[refTableTsName],
|
|
136981
|
+
type: 'one',
|
|
136982
|
+
};
|
|
136983
|
+
// do not add duplicate relation
|
|
136984
|
+
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
136985
|
+
&& rel.refTable === relation.refTable)) {
|
|
136986
|
+
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
136987
|
+
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
136988
|
+
+ `In this case, the foreign key constraint will be used.\n`);
|
|
136989
|
+
continue;
|
|
136990
|
+
}
|
|
136991
|
+
relations.push(relation);
|
|
136992
|
+
tableRelations[tableTsName].push(relation);
|
|
136993
|
+
}
|
|
136994
|
+
}
|
|
136995
|
+
return relations;
|
|
136996
|
+
};
|
|
136997
|
+
for (const table of Object.values(mssqlTables)) {
|
|
136998
|
+
tableConfig = mssqlCore.getTableConfig(table);
|
|
136999
|
+
dbToTsColumnNamesMap = {};
|
|
137000
|
+
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
137001
|
+
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
137002
|
+
}
|
|
137003
|
+
const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
137004
|
+
const table = dbToTsTableNamesMap[tableConfig.name];
|
|
137005
|
+
const refTable = dbToTsTableNamesMap[drizzleOrm.getTableName(fk.reference().foreignTable)];
|
|
137006
|
+
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(fk.reference().foreignTable);
|
|
137007
|
+
if (tableRelations[refTable] === undefined) {
|
|
137008
|
+
tableRelations[refTable] = [];
|
|
137009
|
+
}
|
|
137010
|
+
return {
|
|
137011
|
+
table,
|
|
137012
|
+
columns: fk
|
|
137013
|
+
.reference()
|
|
137014
|
+
.columns.map((col) => dbToTsColumnNamesMap[col.name]),
|
|
137015
|
+
refTable,
|
|
137016
|
+
refColumns: fk
|
|
137017
|
+
.reference()
|
|
137018
|
+
.foreignColumns.map((fCol) => dbToTsColumnNamesMapForRefTable[fCol.name]),
|
|
137019
|
+
refTableRels: tableRelations[refTable],
|
|
137020
|
+
};
|
|
137021
|
+
});
|
|
137022
|
+
relations.push(...newRelations);
|
|
137023
|
+
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
137024
|
+
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
137025
|
+
}
|
|
137026
|
+
tableRelations[dbToTsTableNamesMap[tableConfig.name]].push(...newRelations);
|
|
137027
|
+
// TODO: rewrite
|
|
137028
|
+
const getTypeParams = (sqlType) => {
|
|
137029
|
+
// get type params and set only type
|
|
137030
|
+
const typeParams = {};
|
|
137031
|
+
if (sqlType.startsWith('decimal')
|
|
137032
|
+
|| sqlType.startsWith('real')
|
|
137033
|
+
|| sqlType.startsWith('float')) {
|
|
137034
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137035
|
+
if (match) {
|
|
137036
|
+
typeParams['precision'] = Number(match[1]);
|
|
137037
|
+
typeParams['scale'] = Number(match[2]);
|
|
137038
|
+
}
|
|
137039
|
+
}
|
|
137040
|
+
else if (sqlType.startsWith('char')
|
|
137041
|
+
|| sqlType.startsWith('varchar')
|
|
137042
|
+
|| sqlType.startsWith('binary')
|
|
137043
|
+
|| sqlType.startsWith('varbinary')) {
|
|
137044
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137045
|
+
if (match) {
|
|
137046
|
+
typeParams['length'] = Number(match[1]);
|
|
137047
|
+
}
|
|
137048
|
+
}
|
|
137049
|
+
return typeParams;
|
|
137050
|
+
};
|
|
137051
|
+
tables.push({
|
|
137052
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137053
|
+
columns: tableConfig.columns.map((column) => ({
|
|
137054
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137055
|
+
columnType: column.getSQLType(),
|
|
137056
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137057
|
+
dataType: column.dataType,
|
|
137058
|
+
hasDefault: column.hasDefault,
|
|
137059
|
+
default: column.default,
|
|
137060
|
+
enumValues: column.enumValues,
|
|
137061
|
+
isUnique: column.isUnique,
|
|
137062
|
+
notNull: column.notNull,
|
|
137063
|
+
primary: column.primary,
|
|
137064
|
+
identity: column.identity ? true : false,
|
|
137065
|
+
})),
|
|
137066
|
+
primaryKeys: tableConfig.columns
|
|
137067
|
+
.filter((column) => column.primary)
|
|
137068
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137069
|
+
});
|
|
135744
137070
|
}
|
|
135745
|
-
|
|
135746
|
-
|
|
135747
|
-
|
|
135748
|
-
|
|
137071
|
+
const transformedDrizzleRelations = transformFromDrizzleRelation(mssqlSchema, getDbToTsColumnNamesMap, tableRelations);
|
|
137072
|
+
relations.push(...transformedDrizzleRelations);
|
|
137073
|
+
const modifiedRelations = relations.map((relI) => {
|
|
137074
|
+
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
137075
|
+
if (isRelationCyclic(relI)) {
|
|
137076
|
+
tableRel['isCyclic'] = true;
|
|
137077
|
+
return { ...relI, isCyclic: true };
|
|
135749
137078
|
}
|
|
137079
|
+
tableRel['isCyclic'] = false;
|
|
137080
|
+
return { ...relI, isCyclic: false };
|
|
137081
|
+
});
|
|
137082
|
+
return { tables, relations: modifiedRelations, tableRelations };
|
|
137083
|
+
};
|
|
137084
|
+
|
|
137085
|
+
// MySql-----------------------------------------------------------------------------------------------------
|
|
137086
|
+
const resetMySql = async (db, schema) => {
|
|
137087
|
+
const tablesToTruncate = Object.entries(schema).map(([_tsTableName, table]) => {
|
|
137088
|
+
const dbTableName = drizzleOrm.getTableName(table);
|
|
137089
|
+
return dbTableName;
|
|
137090
|
+
});
|
|
137091
|
+
await db.execute(drizzleOrm.sql.raw('SET FOREIGN_KEY_CHECKS = 0;'));
|
|
137092
|
+
for (const tableName of tablesToTruncate) {
|
|
137093
|
+
const sqlQuery = `truncate \`${tableName}\`;`;
|
|
137094
|
+
await db.execute(drizzleOrm.sql.raw(sqlQuery));
|
|
135750
137095
|
}
|
|
135751
|
-
|
|
135752
|
-
|
|
137096
|
+
await db.execute(drizzleOrm.sql.raw('SET FOREIGN_KEY_CHECKS = 1;'));
|
|
137097
|
+
};
|
|
137098
|
+
const filterMysqlTables = (schema) => {
|
|
137099
|
+
const mysqlSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mysqlCore.MySqlTable) || drizzleOrm.is(keyValue[1], drizzleOrm.Relations)));
|
|
137100
|
+
const mysqlTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], mysqlCore.MySqlTable)));
|
|
137101
|
+
return { mysqlSchema, mysqlTables };
|
|
137102
|
+
};
|
|
137103
|
+
const seedMySql = async (db, schema, options = {}, refinements) => {
|
|
137104
|
+
const { mysqlSchema, mysqlTables } = filterMysqlTables(schema);
|
|
137105
|
+
const { tables, relations } = getMySqlInfo(mysqlSchema, mysqlTables);
|
|
137106
|
+
const seedService = new SeedService();
|
|
137107
|
+
const generatedTablesGenerators = seedService.generatePossibleGenerators('mysql', tables, relations, refinements, options);
|
|
137108
|
+
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
137109
|
+
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, mysqlTables, { ...options, preserveCyclicTablesData });
|
|
137110
|
+
const { filteredTablesGenerators, tablesUniqueNotNullColumn } = seedService.filterCyclicTables(generatedTablesGenerators);
|
|
137111
|
+
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
137112
|
+
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, mysqlTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
137113
|
+
};
|
|
137114
|
+
const getMySqlInfo = (mysqlSchema, mysqlTables) => {
|
|
137115
|
+
let tableConfig;
|
|
137116
|
+
let dbToTsColumnNamesMap;
|
|
137117
|
+
const dbToTsTableNamesMap = Object.fromEntries(Object.entries(mysqlTables).map(([key, value]) => [drizzleOrm.getTableName(value), key]));
|
|
137118
|
+
const tables = [];
|
|
137119
|
+
const relations = [];
|
|
137120
|
+
const dbToTsColumnNamesMapGlobal = {};
|
|
137121
|
+
const tableRelations = {};
|
|
137122
|
+
const getDbToTsColumnNamesMap = (table) => {
|
|
137123
|
+
let dbToTsColumnNamesMap = {};
|
|
137124
|
+
const tableName = drizzleOrm.getTableName(table);
|
|
137125
|
+
if (Object.hasOwn(dbToTsColumnNamesMapGlobal, tableName)) {
|
|
137126
|
+
dbToTsColumnNamesMap = dbToTsColumnNamesMapGlobal[tableName];
|
|
137127
|
+
return dbToTsColumnNamesMap;
|
|
137128
|
+
}
|
|
137129
|
+
const tableConfig = mysqlCore.getTableConfig(table);
|
|
137130
|
+
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
137131
|
+
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
137132
|
+
}
|
|
137133
|
+
dbToTsColumnNamesMapGlobal[tableName] = dbToTsColumnNamesMap;
|
|
137134
|
+
return dbToTsColumnNamesMap;
|
|
137135
|
+
};
|
|
137136
|
+
const transformFromDrizzleRelation = (schema, getDbToTsColumnNamesMap, tableRelations) => {
|
|
137137
|
+
const schemaConfig = drizzleOrm.extractTablesRelationalConfig(schema, drizzleOrm.createTableRelationsHelpers);
|
|
137138
|
+
const relations = [];
|
|
137139
|
+
for (const table of Object.values(schemaConfig.tables)) {
|
|
137140
|
+
if (table.relations === undefined)
|
|
137141
|
+
continue;
|
|
137142
|
+
for (const drizzleRel of Object.values(table.relations)) {
|
|
137143
|
+
if (!drizzleOrm.is(drizzleRel, drizzleOrm.One))
|
|
137144
|
+
continue;
|
|
137145
|
+
const tableConfig = mysqlCore.getTableConfig(drizzleRel.sourceTable);
|
|
137146
|
+
const tableDbSchema = tableConfig.schema ?? 'public';
|
|
137147
|
+
const tableDbName = tableConfig.name;
|
|
137148
|
+
const tableTsName = schemaConfig.tableNamesMap[`${tableDbSchema}.${tableDbName}`] ?? tableDbName;
|
|
137149
|
+
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
137150
|
+
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
137151
|
+
?? [];
|
|
137152
|
+
const refTableConfig = mysqlCore.getTableConfig(drizzleRel.referencedTable);
|
|
137153
|
+
const refTableDbSchema = refTableConfig.schema ?? 'public';
|
|
137154
|
+
const refTableDbName = refTableConfig.name;
|
|
137155
|
+
const refTableTsName = schemaConfig.tableNamesMap[`${refTableDbSchema}.${refTableDbName}`]
|
|
137156
|
+
?? refTableDbName;
|
|
137157
|
+
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(drizzleRel.referencedTable);
|
|
137158
|
+
const refColumns = drizzleRel.config?.references.map((ref) => dbToTsColumnNamesMapForRefTable[ref.name])
|
|
137159
|
+
?? [];
|
|
137160
|
+
if (tableRelations[refTableTsName] === undefined) {
|
|
137161
|
+
tableRelations[refTableTsName] = [];
|
|
137162
|
+
}
|
|
137163
|
+
const relation = {
|
|
137164
|
+
table: tableTsName,
|
|
137165
|
+
columns,
|
|
137166
|
+
refTable: refTableTsName,
|
|
137167
|
+
refColumns,
|
|
137168
|
+
refTableRels: tableRelations[refTableTsName],
|
|
137169
|
+
type: 'one',
|
|
137170
|
+
};
|
|
137171
|
+
// do not add duplicate relation
|
|
137172
|
+
if (tableRelations[tableTsName]?.some((rel) => rel.table === relation.table
|
|
137173
|
+
&& rel.refTable === relation.refTable)) {
|
|
137174
|
+
console.warn(`You are providing a one-to-many relation between the '${relation.refTable}' and '${relation.table}' tables,\n`
|
|
137175
|
+
+ `while the '${relation.table}' table object already has foreign key constraint in the schema referencing '${relation.refTable}' table.\n`
|
|
137176
|
+
+ `In this case, the foreign key constraint will be used.\n`);
|
|
137177
|
+
continue;
|
|
137178
|
+
}
|
|
137179
|
+
relations.push(relation);
|
|
137180
|
+
tableRelations[tableTsName].push(relation);
|
|
137181
|
+
}
|
|
137182
|
+
}
|
|
137183
|
+
return relations;
|
|
137184
|
+
};
|
|
137185
|
+
for (const table of Object.values(mysqlTables)) {
|
|
137186
|
+
tableConfig = mysqlCore.getTableConfig(table);
|
|
137187
|
+
dbToTsColumnNamesMap = {};
|
|
137188
|
+
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
137189
|
+
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
137190
|
+
}
|
|
137191
|
+
const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
137192
|
+
const table = dbToTsTableNamesMap[tableConfig.name];
|
|
137193
|
+
const refTable = dbToTsTableNamesMap[drizzleOrm.getTableName(fk.reference().foreignTable)];
|
|
137194
|
+
const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(fk.reference().foreignTable);
|
|
137195
|
+
if (tableRelations[refTable] === undefined) {
|
|
137196
|
+
tableRelations[refTable] = [];
|
|
137197
|
+
}
|
|
137198
|
+
return {
|
|
137199
|
+
table,
|
|
137200
|
+
columns: fk
|
|
137201
|
+
.reference()
|
|
137202
|
+
.columns.map((col) => dbToTsColumnNamesMap[col.name]),
|
|
137203
|
+
refTable,
|
|
137204
|
+
refColumns: fk
|
|
137205
|
+
.reference()
|
|
137206
|
+
.foreignColumns.map((fCol) => dbToTsColumnNamesMapForRefTable[fCol.name]),
|
|
137207
|
+
refTableRels: tableRelations[refTable],
|
|
137208
|
+
};
|
|
137209
|
+
});
|
|
137210
|
+
relations.push(...newRelations);
|
|
137211
|
+
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
137212
|
+
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
137213
|
+
}
|
|
137214
|
+
tableRelations[dbToTsTableNamesMap[tableConfig.name]].push(...newRelations);
|
|
137215
|
+
const getTypeParams = (sqlType) => {
|
|
137216
|
+
// get type params and set only type
|
|
137217
|
+
const typeParams = {};
|
|
137218
|
+
if (sqlType.startsWith('decimal')
|
|
137219
|
+
|| sqlType.startsWith('real')
|
|
137220
|
+
|| sqlType.startsWith('double')
|
|
137221
|
+
|| sqlType.startsWith('float')) {
|
|
137222
|
+
const match = sqlType.match(/\((\d+), *(\d+)\)/);
|
|
137223
|
+
if (match) {
|
|
137224
|
+
typeParams['precision'] = Number(match[1]);
|
|
137225
|
+
typeParams['scale'] = Number(match[2]);
|
|
137226
|
+
}
|
|
137227
|
+
}
|
|
137228
|
+
else if (sqlType.startsWith('char')
|
|
137229
|
+
|| sqlType.startsWith('varchar')
|
|
137230
|
+
|| sqlType.startsWith('binary')
|
|
137231
|
+
|| sqlType.startsWith('varbinary')) {
|
|
137232
|
+
const match = sqlType.match(/\((\d+)\)/);
|
|
137233
|
+
if (match) {
|
|
137234
|
+
typeParams['length'] = Number(match[1]);
|
|
137235
|
+
}
|
|
137236
|
+
}
|
|
137237
|
+
return typeParams;
|
|
137238
|
+
};
|
|
137239
|
+
tables.push({
|
|
137240
|
+
name: dbToTsTableNamesMap[tableConfig.name],
|
|
137241
|
+
columns: tableConfig.columns.map((column) => ({
|
|
137242
|
+
name: dbToTsColumnNamesMap[column.name],
|
|
137243
|
+
columnType: column.getSQLType(),
|
|
137244
|
+
typeParams: getTypeParams(column.getSQLType()),
|
|
137245
|
+
dataType: column.dataType,
|
|
137246
|
+
hasDefault: column.hasDefault,
|
|
137247
|
+
default: column.default,
|
|
137248
|
+
enumValues: column.enumValues,
|
|
137249
|
+
isUnique: column.isUnique,
|
|
137250
|
+
notNull: column.notNull,
|
|
137251
|
+
primary: column.primary,
|
|
137252
|
+
})),
|
|
137253
|
+
primaryKeys: tableConfig.columns
|
|
137254
|
+
.filter((column) => column.primary)
|
|
137255
|
+
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
137256
|
+
});
|
|
135753
137257
|
}
|
|
135754
|
-
|
|
137258
|
+
const transformedDrizzleRelations = transformFromDrizzleRelation(mysqlSchema, getDbToTsColumnNamesMap, tableRelations);
|
|
137259
|
+
relations.push(...transformedDrizzleRelations);
|
|
137260
|
+
const isCyclicRelations = relations.map((relI) => {
|
|
137261
|
+
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
137262
|
+
if (isRelationCyclic(relI)) {
|
|
137263
|
+
tableRel['isCyclic'] = true;
|
|
137264
|
+
return { ...relI, isCyclic: true };
|
|
137265
|
+
}
|
|
137266
|
+
tableRel['isCyclic'] = false;
|
|
137267
|
+
return { ...relI, isCyclic: false };
|
|
137268
|
+
});
|
|
137269
|
+
return { tables, relations: isCyclicRelations, tableRelations };
|
|
137270
|
+
};
|
|
137271
|
+
|
|
135755
137272
|
// Postgres-----------------------------------------------------------------------------------------------------------
|
|
135756
137273
|
const resetPostgres = async (db, pgTables) => {
|
|
135757
137274
|
const tablesToTruncate = Object.entries(pgTables).map(([_, table]) => {
|
|
@@ -135920,6 +137437,7 @@ const getPostgresInfo = (pgSchema, pgTables) => {
|
|
|
135920
137437
|
|| sqlType.startsWith('bpchar')
|
|
135921
137438
|
|| sqlType.startsWith('char')
|
|
135922
137439
|
|| sqlType.startsWith('bit')
|
|
137440
|
+
|| sqlType.startsWith('vector')
|
|
135923
137441
|
|| sqlType.startsWith('time')
|
|
135924
137442
|
|| sqlType.startsWith('timestamp')
|
|
135925
137443
|
|| sqlType.startsWith('interval')) {
|
|
@@ -135969,37 +137487,9 @@ const getPostgresInfo = (pgSchema, pgTables) => {
|
|
|
135969
137487
|
});
|
|
135970
137488
|
return { tables, relations: isCyclicRelations, tableRelations };
|
|
135971
137489
|
};
|
|
135972
|
-
|
|
135973
|
-
|
|
135974
|
-
|
|
135975
|
-
return false;
|
|
135976
|
-
// DFS
|
|
135977
|
-
const targetTable = startRel.table;
|
|
135978
|
-
const queue = [startRel];
|
|
135979
|
-
let path = [];
|
|
135980
|
-
while (queue.length !== 0) {
|
|
135981
|
-
const currRel = queue.shift();
|
|
135982
|
-
if (path.includes(currRel.table)) {
|
|
135983
|
-
const idx = path.indexOf(currRel.table);
|
|
135984
|
-
path = path.slice(0, idx);
|
|
135985
|
-
}
|
|
135986
|
-
path.push(currRel.table);
|
|
135987
|
-
for (const rel of currRel.refTableRels) {
|
|
135988
|
-
// self relation
|
|
135989
|
-
if (rel.table === rel.refTable)
|
|
135990
|
-
continue;
|
|
135991
|
-
if (rel.refTable === targetTable)
|
|
135992
|
-
return true;
|
|
135993
|
-
// found cycle, but not the one we are looking for
|
|
135994
|
-
if (path.includes(rel.refTable))
|
|
135995
|
-
continue;
|
|
135996
|
-
queue.unshift(rel);
|
|
135997
|
-
}
|
|
135998
|
-
}
|
|
135999
|
-
return false;
|
|
136000
|
-
};
|
|
136001
|
-
// MySql-----------------------------------------------------------------------------------------------------
|
|
136002
|
-
const resetMySql = async (db, schema) => {
|
|
137490
|
+
|
|
137491
|
+
// SingleStore-----------------------------------------------------------------------------------------------------
|
|
137492
|
+
const resetSingleStore = async (db, schema) => {
|
|
136003
137493
|
const tablesToTruncate = Object.entries(schema).map(([_tsTableName, table]) => {
|
|
136004
137494
|
const dbTableName = drizzleOrm.getTableName(table);
|
|
136005
137495
|
return dbTableName;
|
|
@@ -136011,26 +137501,26 @@ const resetMySql = async (db, schema) => {
|
|
|
136011
137501
|
}
|
|
136012
137502
|
await db.execute(drizzleOrm.sql.raw('SET FOREIGN_KEY_CHECKS = 1;'));
|
|
136013
137503
|
};
|
|
136014
|
-
const
|
|
136015
|
-
const
|
|
136016
|
-
const
|
|
136017
|
-
return {
|
|
137504
|
+
const filterSingleStoreTables = (schema) => {
|
|
137505
|
+
const singleStoreSchema = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], singlestoreCore.SingleStoreTable) || drizzleOrm.is(keyValue[1], drizzleOrm.Relations)));
|
|
137506
|
+
const singleStoreTables = Object.fromEntries(Object.entries(schema).filter((keyValue) => drizzleOrm.is(keyValue[1], singlestoreCore.SingleStoreTable)));
|
|
137507
|
+
return { singleStoreSchema, singleStoreTables };
|
|
136018
137508
|
};
|
|
136019
|
-
const
|
|
136020
|
-
const {
|
|
136021
|
-
const { tables, relations } =
|
|
137509
|
+
const seedSingleStore = async (db, schema, options = {}, refinements) => {
|
|
137510
|
+
const { singleStoreSchema, singleStoreTables } = filterSingleStoreTables(schema);
|
|
137511
|
+
const { tables, relations } = getSingleStoreInfo(singleStoreSchema, singleStoreTables);
|
|
136022
137512
|
const seedService = new SeedService();
|
|
136023
|
-
const generatedTablesGenerators = seedService.generatePossibleGenerators('
|
|
137513
|
+
const generatedTablesGenerators = seedService.generatePossibleGenerators('singlestore', tables, relations, refinements, options);
|
|
136024
137514
|
const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
|
|
136025
|
-
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db,
|
|
137515
|
+
const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, singleStoreTables, { ...options, preserveCyclicTablesData });
|
|
136026
137516
|
const { filteredTablesGenerators, tablesUniqueNotNullColumn } = seedService.filterCyclicTables(generatedTablesGenerators);
|
|
136027
137517
|
const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
|
|
136028
|
-
await seedService.generateTablesValues(relations, filteredTablesGenerators, db,
|
|
137518
|
+
await seedService.generateTablesValues(relations, filteredTablesGenerators, db, singleStoreTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
|
|
136029
137519
|
};
|
|
136030
|
-
const
|
|
137520
|
+
const getSingleStoreInfo = (singleStoreSchema, singleStoreTables) => {
|
|
136031
137521
|
let tableConfig;
|
|
136032
137522
|
let dbToTsColumnNamesMap;
|
|
136033
|
-
const dbToTsTableNamesMap = Object.fromEntries(Object.entries(
|
|
137523
|
+
const dbToTsTableNamesMap = Object.fromEntries(Object.entries(singleStoreTables).map(([key, value]) => [drizzleOrm.getTableName(value), key]));
|
|
136034
137524
|
const tables = [];
|
|
136035
137525
|
const relations = [];
|
|
136036
137526
|
const dbToTsColumnNamesMapGlobal = {};
|
|
@@ -136042,7 +137532,7 @@ const getMySqlInfo = (mysqlSchema, mysqlTables) => {
|
|
|
136042
137532
|
dbToTsColumnNamesMap = dbToTsColumnNamesMapGlobal[tableName];
|
|
136043
137533
|
return dbToTsColumnNamesMap;
|
|
136044
137534
|
}
|
|
136045
|
-
const tableConfig =
|
|
137535
|
+
const tableConfig = singlestoreCore.getTableConfig(table);
|
|
136046
137536
|
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
136047
137537
|
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
136048
137538
|
}
|
|
@@ -136058,14 +137548,14 @@ const getMySqlInfo = (mysqlSchema, mysqlTables) => {
|
|
|
136058
137548
|
for (const drizzleRel of Object.values(table.relations)) {
|
|
136059
137549
|
if (!drizzleOrm.is(drizzleRel, drizzleOrm.One))
|
|
136060
137550
|
continue;
|
|
136061
|
-
const tableConfig =
|
|
137551
|
+
const tableConfig = singlestoreCore.getTableConfig(drizzleRel.sourceTable);
|
|
136062
137552
|
const tableDbSchema = tableConfig.schema ?? 'public';
|
|
136063
137553
|
const tableDbName = tableConfig.name;
|
|
136064
137554
|
const tableTsName = schemaConfig.tableNamesMap[`${tableDbSchema}.${tableDbName}`] ?? tableDbName;
|
|
136065
137555
|
const dbToTsColumnNamesMap = getDbToTsColumnNamesMap(drizzleRel.sourceTable);
|
|
136066
137556
|
const columns = drizzleRel.config?.fields.map((field) => dbToTsColumnNamesMap[field.name])
|
|
136067
137557
|
?? [];
|
|
136068
|
-
const refTableConfig =
|
|
137558
|
+
const refTableConfig = singlestoreCore.getTableConfig(drizzleRel.referencedTable);
|
|
136069
137559
|
const refTableDbSchema = refTableConfig.schema ?? 'public';
|
|
136070
137560
|
const refTableDbName = refTableConfig.name;
|
|
136071
137561
|
const refTableTsName = schemaConfig.tableNamesMap[`${refTableDbSchema}.${refTableDbName}`]
|
|
@@ -136098,36 +137588,42 @@ const getMySqlInfo = (mysqlSchema, mysqlTables) => {
|
|
|
136098
137588
|
}
|
|
136099
137589
|
return relations;
|
|
136100
137590
|
};
|
|
136101
|
-
for (const table of Object.values(
|
|
136102
|
-
tableConfig =
|
|
137591
|
+
for (const table of Object.values(singleStoreTables)) {
|
|
137592
|
+
tableConfig = singlestoreCore.getTableConfig(table);
|
|
136103
137593
|
dbToTsColumnNamesMap = {};
|
|
136104
137594
|
for (const [tsCol, col] of Object.entries(tableConfig.columns[0].table)) {
|
|
136105
137595
|
dbToTsColumnNamesMap[col.name] = tsCol;
|
|
136106
137596
|
}
|
|
136107
|
-
const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
136108
|
-
|
|
136109
|
-
|
|
136110
|
-
|
|
136111
|
-
|
|
136112
|
-
|
|
136113
|
-
|
|
136114
|
-
|
|
136115
|
-
|
|
136116
|
-
|
|
136117
|
-
|
|
136118
|
-
|
|
136119
|
-
|
|
136120
|
-
|
|
136121
|
-
|
|
136122
|
-
|
|
136123
|
-
|
|
136124
|
-
|
|
136125
|
-
|
|
136126
|
-
|
|
137597
|
+
// const newRelations = tableConfig.foreignKeys.map((fk) => {
|
|
137598
|
+
// const table = dbToTsTableNamesMap[tableConfig.name] as string;
|
|
137599
|
+
// const refTable = dbToTsTableNamesMap[getTableName(fk.reference().foreignTable)] as string;
|
|
137600
|
+
// const dbToTsColumnNamesMapForRefTable = getDbToTsColumnNamesMap(
|
|
137601
|
+
// fk.reference().foreignTable,
|
|
137602
|
+
// );
|
|
137603
|
+
// if (tableRelations[refTable] === undefined) {
|
|
137604
|
+
// tableRelations[refTable] = [];
|
|
137605
|
+
// }
|
|
137606
|
+
// return {
|
|
137607
|
+
// table,
|
|
137608
|
+
// columns: fk
|
|
137609
|
+
// .reference()
|
|
137610
|
+
// .columns.map((col) => dbToTsColumnNamesMap[col.name] as string),
|
|
137611
|
+
// refTable,
|
|
137612
|
+
// refColumns: fk
|
|
137613
|
+
// .reference()
|
|
137614
|
+
// .foreignColumns.map(
|
|
137615
|
+
// (fCol) => dbToTsColumnNamesMapForRefTable[fCol.name] as string,
|
|
137616
|
+
// ),
|
|
137617
|
+
// refTableRels: tableRelations[refTable],
|
|
137618
|
+
// };
|
|
137619
|
+
// });
|
|
137620
|
+
// relations.push(
|
|
137621
|
+
// ...newRelations,
|
|
137622
|
+
// );
|
|
136127
137623
|
if (tableRelations[dbToTsTableNamesMap[tableConfig.name]] === undefined) {
|
|
136128
137624
|
tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
|
|
136129
137625
|
}
|
|
136130
|
-
tableRelations[dbToTsTableNamesMap[tableConfig.name]]
|
|
137626
|
+
// tableRelations[dbToTsTableNamesMap[tableConfig.name] as string]!.push(...newRelations);
|
|
136131
137627
|
const getTypeParams = (sqlType) => {
|
|
136132
137628
|
// get type params and set only type
|
|
136133
137629
|
const typeParams = {};
|
|
@@ -136150,6 +137646,13 @@ const getMySqlInfo = (mysqlSchema, mysqlTables) => {
|
|
|
136150
137646
|
typeParams['length'] = Number(match[1]);
|
|
136151
137647
|
}
|
|
136152
137648
|
}
|
|
137649
|
+
else if (sqlType.startsWith('vector')) {
|
|
137650
|
+
const match = sqlType.match(/\((\d+),? ?((F|I)\d{1,2})?\)/);
|
|
137651
|
+
if (match) {
|
|
137652
|
+
typeParams['length'] = Number(match[1]);
|
|
137653
|
+
typeParams['vectorValueType'] = match[2];
|
|
137654
|
+
}
|
|
137655
|
+
}
|
|
136153
137656
|
return typeParams;
|
|
136154
137657
|
};
|
|
136155
137658
|
tables.push({
|
|
@@ -136171,7 +137674,7 @@ const getMySqlInfo = (mysqlSchema, mysqlTables) => {
|
|
|
136171
137674
|
.map((column) => dbToTsColumnNamesMap[column.name]),
|
|
136172
137675
|
});
|
|
136173
137676
|
}
|
|
136174
|
-
const transformedDrizzleRelations = transformFromDrizzleRelation(
|
|
137677
|
+
const transformedDrizzleRelations = transformFromDrizzleRelation(singleStoreSchema, getDbToTsColumnNamesMap, tableRelations);
|
|
136175
137678
|
relations.push(...transformedDrizzleRelations);
|
|
136176
137679
|
const isCyclicRelations = relations.map((relI) => {
|
|
136177
137680
|
const tableRel = tableRelations[relI.table].find((relJ) => relJ.refTable === relI.refTable);
|
|
@@ -136184,6 +137687,7 @@ const getMySqlInfo = (mysqlSchema, mysqlTables) => {
|
|
|
136184
137687
|
});
|
|
136185
137688
|
return { tables, relations: isCyclicRelations, tableRelations };
|
|
136186
137689
|
};
|
|
137690
|
+
|
|
136187
137691
|
// Sqlite------------------------------------------------------------------------------------------------------------------------
|
|
136188
137692
|
const resetSqlite = async (db, schema) => {
|
|
136189
137693
|
const tablesToTruncate = Object.entries(schema).map(([_tsTableName, table]) => {
|
|
@@ -136366,7 +137870,243 @@ const getSqliteInfo = (sqliteSchema, sqliteTables) => {
|
|
|
136366
137870
|
return { tables, relations: isCyclicRelations, tableRelations };
|
|
136367
137871
|
};
|
|
136368
137872
|
|
|
136369
|
-
|
|
137873
|
+
class SeedPromise {
|
|
137874
|
+
db;
|
|
137875
|
+
schema;
|
|
137876
|
+
options;
|
|
137877
|
+
static entityKind = 'SeedPromise';
|
|
137878
|
+
[Symbol.toStringTag] = 'SeedPromise';
|
|
137879
|
+
constructor(db, schema, options) {
|
|
137880
|
+
this.db = db;
|
|
137881
|
+
this.schema = schema;
|
|
137882
|
+
this.options = options;
|
|
137883
|
+
}
|
|
137884
|
+
then(onfulfilled, onrejected) {
|
|
137885
|
+
return seedFunc(this.db, this.schema, this.options).then(onfulfilled, onrejected);
|
|
137886
|
+
}
|
|
137887
|
+
catch(onrejected) {
|
|
137888
|
+
return this.then(undefined, onrejected);
|
|
137889
|
+
}
|
|
137890
|
+
finally(onfinally) {
|
|
137891
|
+
return this.then((value) => {
|
|
137892
|
+
onfinally?.();
|
|
137893
|
+
return value;
|
|
137894
|
+
}, (reason) => {
|
|
137895
|
+
onfinally?.();
|
|
137896
|
+
throw reason;
|
|
137897
|
+
});
|
|
137898
|
+
}
|
|
137899
|
+
async refine(callback) {
|
|
137900
|
+
const refinements = this.options?.version === undefined || this.options.version === '2'
|
|
137901
|
+
? callback(generatorsFuncsV2)
|
|
137902
|
+
: callback(generatorsFuncs);
|
|
137903
|
+
await seedFunc(this.db, this.schema, this.options, refinements);
|
|
137904
|
+
}
|
|
137905
|
+
}
|
|
137906
|
+
function getGeneratorsFunctions() {
|
|
137907
|
+
return generatorsFuncs;
|
|
137908
|
+
}
|
|
137909
|
+
async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleStudioRelations, schemasRefinements, options }) {
|
|
137910
|
+
const generatedSchemas = {};
|
|
137911
|
+
let tables, relations, refinements;
|
|
137912
|
+
drizzleStudioRelations = drizzleStudioRelations.filter((rel) => rel.type === 'one');
|
|
137913
|
+
for (const [schemaName, { tables: drizzleStudioTables }] of Object.entries(drizzleStudioObject)) {
|
|
137914
|
+
tables = [];
|
|
137915
|
+
for (const [tableName, table] of Object.entries(drizzleStudioTables)) {
|
|
137916
|
+
const drizzleStudioColumns = Object.values(table.columns);
|
|
137917
|
+
const columns = drizzleStudioColumns.map((col) => ({
|
|
137918
|
+
name: col.name,
|
|
137919
|
+
dataType: 'string',
|
|
137920
|
+
columnType: col.type,
|
|
137921
|
+
// TODO: revise later
|
|
137922
|
+
typeParams: {},
|
|
137923
|
+
default: col.default,
|
|
137924
|
+
hasDefault: col.default === undefined ? false : true,
|
|
137925
|
+
isUnique: col.isUnique === undefined ? false : col.isUnique,
|
|
137926
|
+
notNull: col.notNull,
|
|
137927
|
+
primary: col.primaryKey,
|
|
137928
|
+
}));
|
|
137929
|
+
tables.push({
|
|
137930
|
+
name: tableName,
|
|
137931
|
+
columns,
|
|
137932
|
+
primaryKeys: drizzleStudioColumns.filter((col) => col.primaryKey === true).map((col) => col.name),
|
|
137933
|
+
});
|
|
137934
|
+
}
|
|
137935
|
+
relations = drizzleStudioRelations.filter((rel) => rel.schema === schemaName && rel.refSchema === schemaName);
|
|
137936
|
+
const isCyclicRelations = relations.map((reli) => {
|
|
137937
|
+
if (relations.some((relj) => reli.table === relj.refTable && reli.refTable === relj.table)) {
|
|
137938
|
+
return { ...reli, isCyclic: true };
|
|
137939
|
+
}
|
|
137940
|
+
return { ...reli, isCyclic: false };
|
|
137941
|
+
});
|
|
137942
|
+
refinements = schemasRefinements !== undefined && schemasRefinements[schemaName] !== undefined
|
|
137943
|
+
? schemasRefinements[schemaName]
|
|
137944
|
+
: undefined;
|
|
137945
|
+
const seedService = new SeedService();
|
|
137946
|
+
const generatedTablesGenerators = seedService.generatePossibleGenerators(sqlDialect, tables, isCyclicRelations, refinements, options);
|
|
137947
|
+
const generatedTables = await seedService.generateTablesValues(isCyclicRelations, generatedTablesGenerators, undefined, undefined, { ...options, preserveData: true, insertDataInDb: false });
|
|
137948
|
+
generatedSchemas[schemaName] = { tables: generatedTables };
|
|
137949
|
+
}
|
|
137950
|
+
return generatedSchemas;
|
|
137951
|
+
}
|
|
137952
|
+
/**
|
|
137953
|
+
* @param db - database you would like to seed.
|
|
137954
|
+
* @param schema - object that contains all your database tables you would like to seed.
|
|
137955
|
+
* @param options - object that contains properties `count` and `seed`:
|
|
137956
|
+
*
|
|
137957
|
+
* `count` - number of rows you want to generate.
|
|
137958
|
+
*
|
|
137959
|
+
* `seed` - a number that controls the state of generated data. (if the `seed` number is the same and nothing is changed in the seeding script, generated data will remain the same each time you seed database)
|
|
137960
|
+
*
|
|
137961
|
+
* @returns SeedPromise - a class object that has a refine method that is used to change generators for columns.
|
|
137962
|
+
*
|
|
137963
|
+
* @example
|
|
137964
|
+
* ```ts
|
|
137965
|
+
* // base seeding
|
|
137966
|
+
* await seed(db, schema);
|
|
137967
|
+
*
|
|
137968
|
+
* // seeding with count specified
|
|
137969
|
+
* await seed(db, schema, { count: 100000 });
|
|
137970
|
+
*
|
|
137971
|
+
* // seeding with count and seed specified
|
|
137972
|
+
* await seed(db, schema, { count: 100000, seed: 1 });
|
|
137973
|
+
*
|
|
137974
|
+
* //seeding using refine
|
|
137975
|
+
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
137976
|
+
* users: {
|
|
137977
|
+
* columns: {
|
|
137978
|
+
* name: funcs.firstName({ isUnique: true }),
|
|
137979
|
+
* email: funcs.email(),
|
|
137980
|
+
* phone: funcs.phoneNumber({ template: "+380 99 ###-##-##" }),
|
|
137981
|
+
* password: funcs.string({ isUnique: true }),
|
|
137982
|
+
* },
|
|
137983
|
+
* count: 100000,
|
|
137984
|
+
* },
|
|
137985
|
+
* posts: {
|
|
137986
|
+
* columns: {
|
|
137987
|
+
* title: funcs.valuesFromArray({
|
|
137988
|
+
* values: ["Title1", "Title2", "Title3", "Title4", "Title5"],
|
|
137989
|
+
* }),
|
|
137990
|
+
* content: funcs.loremIpsum({ sentencesCount: 3 }),
|
|
137991
|
+
* },
|
|
137992
|
+
* },
|
|
137993
|
+
* }));
|
|
137994
|
+
*
|
|
137995
|
+
* ```
|
|
137996
|
+
*/
|
|
137997
|
+
function seed(db, schema, options) {
|
|
137998
|
+
return new SeedPromise(db, schema, options);
|
|
137999
|
+
}
|
|
138000
|
+
const seedFunc = async (db, schema, options = {}, refinements) => {
|
|
138001
|
+
let version;
|
|
138002
|
+
if (options?.version !== undefined) {
|
|
138003
|
+
version = Number(options?.version);
|
|
138004
|
+
}
|
|
138005
|
+
if (drizzleOrm.is(db, (pgCore.PgDatabase))) {
|
|
138006
|
+
await seedPostgres(db, schema, { ...options, version }, refinements);
|
|
138007
|
+
}
|
|
138008
|
+
else if (drizzleOrm.is(db, (mysqlCore.MySqlDatabase))) {
|
|
138009
|
+
await seedMySql(db, schema, { ...options, version }, refinements);
|
|
138010
|
+
}
|
|
138011
|
+
else if (drizzleOrm.is(db, (sqliteCore.BaseSQLiteDatabase))) {
|
|
138012
|
+
await seedSqlite(db, schema, { ...options, version }, refinements);
|
|
138013
|
+
}
|
|
138014
|
+
else if (drizzleOrm.is(db, (mssqlCore.MsSqlDatabase))) {
|
|
138015
|
+
await seedMsSql(db, schema, { ...options, version }, refinements);
|
|
138016
|
+
}
|
|
138017
|
+
else if (drizzleOrm.is(db, (cockroachCore.CockroachDatabase))) {
|
|
138018
|
+
await seedCockroach(db, schema, { ...options, version }, refinements);
|
|
138019
|
+
}
|
|
138020
|
+
else if (drizzleOrm.is(db, (singlestoreCore.SingleStoreDatabase))) {
|
|
138021
|
+
await seedSingleStore(db, schema, { ...options, version }, refinements);
|
|
138022
|
+
}
|
|
138023
|
+
else {
|
|
138024
|
+
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');
|
|
138025
|
+
}
|
|
138026
|
+
return;
|
|
138027
|
+
};
|
|
138028
|
+
/**
|
|
138029
|
+
* deletes all data from specified tables
|
|
138030
|
+
*
|
|
138031
|
+
* @param db - database you would like to reset.
|
|
138032
|
+
* @param schema - object that contains all your database tables you would like to delete data from.
|
|
138033
|
+
*
|
|
138034
|
+
* `If db is a PgDatabase object`, we will execute sql query and delete data from your tables the following way:
|
|
138035
|
+
* ```sql
|
|
138036
|
+
* truncate tableName1, tableName2, ... cascade;
|
|
138037
|
+
* ```
|
|
138038
|
+
*
|
|
138039
|
+
* `If db is a MySqlDatabase object`, we will execute sql queries and delete data from your tables the following way:
|
|
138040
|
+
* ```sql
|
|
138041
|
+
* SET FOREIGN_KEY_CHECKS = 0;
|
|
138042
|
+
* truncate tableName1;
|
|
138043
|
+
* truncate tableName2;
|
|
138044
|
+
* .
|
|
138045
|
+
* .
|
|
138046
|
+
* .
|
|
138047
|
+
*
|
|
138048
|
+
* SET FOREIGN_KEY_CHECKS = 1;
|
|
138049
|
+
* ```
|
|
138050
|
+
*
|
|
138051
|
+
* `If db is a BaseSQLiteDatabase object`, we will execute sql queries and delete data from your tables the following way:
|
|
138052
|
+
* ```sql
|
|
138053
|
+
* PRAGMA foreign_keys = OFF;
|
|
138054
|
+
* delete from tableName1;
|
|
138055
|
+
* delete from tableName2;
|
|
138056
|
+
* .
|
|
138057
|
+
* .
|
|
138058
|
+
* .
|
|
138059
|
+
*
|
|
138060
|
+
* PRAGMA foreign_keys = ON;
|
|
138061
|
+
* ```
|
|
138062
|
+
*
|
|
138063
|
+
* @example
|
|
138064
|
+
* ```ts
|
|
138065
|
+
* await reset(db, schema);
|
|
138066
|
+
* ```
|
|
138067
|
+
*/
|
|
138068
|
+
async function reset(db, schema) {
|
|
138069
|
+
if (drizzleOrm.is(db, (pgCore.PgDatabase))) {
|
|
138070
|
+
const { pgTables } = filterPgSchema(schema);
|
|
138071
|
+
if (Object.entries(pgTables).length > 0) {
|
|
138072
|
+
await resetPostgres(db, pgTables);
|
|
138073
|
+
}
|
|
138074
|
+
}
|
|
138075
|
+
else if (drizzleOrm.is(db, (mysqlCore.MySqlDatabase))) {
|
|
138076
|
+
const { mysqlTables } = filterMysqlTables(schema);
|
|
138077
|
+
if (Object.entries(mysqlTables).length > 0) {
|
|
138078
|
+
await resetMySql(db, mysqlTables);
|
|
138079
|
+
}
|
|
138080
|
+
}
|
|
138081
|
+
else if (drizzleOrm.is(db, (sqliteCore.BaseSQLiteDatabase))) {
|
|
138082
|
+
const { sqliteTables } = filterSqliteTables(schema);
|
|
138083
|
+
if (Object.entries(sqliteTables).length > 0) {
|
|
138084
|
+
await resetSqlite(db, sqliteTables);
|
|
138085
|
+
}
|
|
138086
|
+
}
|
|
138087
|
+
else if (drizzleOrm.is(db, (mssqlCore.MsSqlDatabase))) {
|
|
138088
|
+
const { mssqlTables } = filterMsSqlTables(schema);
|
|
138089
|
+
if (Object.entries(mssqlTables).length > 0) {
|
|
138090
|
+
await resetMsSql(db, mssqlTables);
|
|
138091
|
+
}
|
|
138092
|
+
}
|
|
138093
|
+
else if (drizzleOrm.is(db, (cockroachCore.CockroachDatabase))) {
|
|
138094
|
+
const { cockroachTables } = filterCockroachSchema(schema);
|
|
138095
|
+
if (Object.entries(cockroachTables).length > 0) {
|
|
138096
|
+
await resetCockroach(db, cockroachTables);
|
|
138097
|
+
}
|
|
138098
|
+
}
|
|
138099
|
+
else if (drizzleOrm.is(db, (singlestoreCore.SingleStoreDatabase))) {
|
|
138100
|
+
const { singleStoreTables } = filterSingleStoreTables(schema);
|
|
138101
|
+
if (Object.entries(singleStoreTables).length > 0) {
|
|
138102
|
+
await resetSingleStore(db, singleStoreTables);
|
|
138103
|
+
}
|
|
138104
|
+
}
|
|
138105
|
+
else {
|
|
138106
|
+
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');
|
|
138107
|
+
}
|
|
138108
|
+
}
|
|
138109
|
+
|
|
136370
138110
|
exports.SeedService = SeedService;
|
|
136371
138111
|
exports.cities = cityNames;
|
|
136372
138112
|
exports.countries = countries;
|