drizzle-seed 0.4.0-c4ae133 → 0.4.0-e6bdce6

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.
Files changed (53) hide show
  1. package/SeedService.d.cts +1 -0
  2. package/SeedService.d.mts +1 -0
  3. package/SeedService.d.ts +1 -0
  4. package/cockroach-core/index.d.cts +3 -5
  5. package/cockroach-core/index.d.mts +3 -5
  6. package/cockroach-core/index.d.ts +3 -5
  7. package/common.d.cts +3 -5
  8. package/common.d.mts +3 -5
  9. package/common.d.ts +3 -5
  10. package/generators/GeneratorFuncs.d.cts +888 -28
  11. package/generators/GeneratorFuncs.d.mts +888 -28
  12. package/generators/GeneratorFuncs.d.ts +888 -28
  13. package/generators/Generators.d.cts +147 -42
  14. package/generators/Generators.d.mts +147 -42
  15. package/generators/Generators.d.ts +147 -42
  16. package/generators/apiVersion.d.cts +1 -1
  17. package/generators/apiVersion.d.mts +1 -1
  18. package/generators/apiVersion.d.ts +1 -1
  19. package/generators/versioning/v2.d.cts +11 -4
  20. package/generators/versioning/v2.d.mts +11 -4
  21. package/generators/versioning/v2.d.ts +11 -4
  22. package/generators/versioning/v3.d.cts +10 -0
  23. package/generators/versioning/v3.d.mts +10 -0
  24. package/generators/versioning/v3.d.ts +10 -0
  25. package/index.cjs +764 -282
  26. package/index.cjs.map +1 -1
  27. package/index.d.cts +8 -20
  28. package/index.d.mts +8 -20
  29. package/index.d.ts +8 -20
  30. package/index.mjs +764 -282
  31. package/index.mjs.map +1 -1
  32. package/mysql-core/index.d.cts +3 -5
  33. package/mysql-core/index.d.mts +3 -5
  34. package/mysql-core/index.d.ts +3 -5
  35. package/package.json +108 -107
  36. package/pg-core/index.d.cts +3 -5
  37. package/pg-core/index.d.mts +3 -5
  38. package/pg-core/index.d.ts +3 -5
  39. package/singlestore-core/index.d.cts +3 -5
  40. package/singlestore-core/index.d.mts +3 -5
  41. package/singlestore-core/index.d.ts +3 -5
  42. package/sqlite-core/index.d.cts +3 -5
  43. package/sqlite-core/index.d.mts +3 -5
  44. package/sqlite-core/index.d.ts +3 -5
  45. package/types/seedService.d.cts +1 -1
  46. package/types/seedService.d.mts +1 -1
  47. package/types/seedService.d.ts +1 -1
  48. package/types/tables.d.cts +9 -6
  49. package/types/tables.d.mts +9 -6
  50. package/types/tables.d.ts +9 -6
  51. package/utils.d.cts +0 -1
  52. package/utils.d.mts +0 -1
  53. package/utils.d.ts +0 -1
package/index.cjs CHANGED
@@ -39,16 +39,6 @@ const isRelationCyclic = (startRel) => {
39
39
  }
40
40
  return false;
41
41
  };
42
- const generateHashFromString = (s) => {
43
- let hash = 0;
44
- // p and m are prime numbers
45
- const p = 53;
46
- const m = 28871271685163;
47
- for (let i = 0; i < s.length; i++) {
48
- hash += ((s.codePointAt(i) || 0) * Math.pow(p, i)) % m;
49
- }
50
- return hash;
51
- };
52
42
  const equalSets = (set1, set2) => {
53
43
  return set1.size === set2.size && [...set1].every((si) => set2.has(si));
54
44
  };
@@ -116,7 +106,7 @@ const transformFromDrizzleRelation = (schema, getDbToTsColumnNamesMap, tableRela
116
106
  }
117
107
  return relations;
118
108
  };
119
- const getSchemaInfo = (drizzleTablesAndRelations, drizzleTables, mapTable) => {
109
+ const getSchemaInfo = (drizzleTablesAndRelations, drizzleTables, mapColumns) => {
120
110
  let tableConfig;
121
111
  let dbToTsColumnNamesMap;
122
112
  const dbToTsTableNamesMap = Object.fromEntries(Object.entries(drizzleTables).map(([key, value]) => [drizzleOrm.getTableName(value), key]));
@@ -167,8 +157,25 @@ const getSchemaInfo = (drizzleTablesAndRelations, drizzleTables, mapTable) => {
167
157
  tableRelations[dbToTsTableNamesMap[tableConfig.name]] = [];
168
158
  }
169
159
  tableRelations[dbToTsTableNamesMap[tableConfig.name]].push(...newRelations);
170
- // console.log(tableConfig.columns);
171
- tables.push(mapTable(tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap));
160
+ const stringsSet = [];
161
+ const uniqueConstraints = [];
162
+ for (const uniCon of tableConfig.uniqueConstraints) {
163
+ const uniConColumns = uniCon.columns.map((col) => dbToTsColumnNamesMap[col.name]);
164
+ const uniConColumnsStr = JSON.stringify(uniConColumns);
165
+ if (!stringsSet.includes(uniConColumnsStr)) {
166
+ stringsSet.push(uniConColumnsStr);
167
+ uniqueConstraints.push(uniConColumns);
168
+ }
169
+ }
170
+ const mappedTable = {
171
+ name: dbToTsTableNamesMap[tableConfig.name],
172
+ uniqueConstraints,
173
+ primaryKeys: tableConfig.columns
174
+ .filter((column) => column.primary)
175
+ .map((column) => dbToTsColumnNamesMap[column.name]),
176
+ columns: mapColumns(tableConfig, dbToTsColumnNamesMap),
177
+ };
178
+ tables.push(mappedTable);
172
179
  }
173
180
  const transformedDrizzleRelations = transformFromDrizzleRelation(drizzleTablesAndRelations, getDbToTsColumnNamesMap, tableRelations);
174
181
  relations.push(...transformedDrizzleRelations);
@@ -131450,10 +131457,12 @@ const isObject = (value) => {
131450
131457
  class AbstractGenerator {
131451
131458
  static entityKind = 'AbstractGenerator';
131452
131459
  static version = 1;
131460
+ isGeneratorUnique = false;
131453
131461
  isUnique = false;
131454
131462
  notNull = false;
131455
131463
  // param for generators which have a unique version of themselves
131456
131464
  uniqueVersionOfGen;
131465
+ maxUniqueCount = -1;
131457
131466
  dataType;
131458
131467
  timeSpent;
131459
131468
  //
@@ -131465,6 +131474,7 @@ class AbstractGenerator {
131465
131474
  weightedCountSeed;
131466
131475
  maxRepeatedValuesCount;
131467
131476
  typeParams = {};
131477
+ uniqueKey;
131468
131478
  params;
131469
131479
  constructor(params) {
131470
131480
  this.params = params === undefined ? {} : params;
@@ -131487,9 +131497,13 @@ class AbstractGenerator {
131487
131497
  const constructor = this.constructor;
131488
131498
  return constructor.entityKind;
131489
131499
  }
131500
+ getMaxUniqueCount() {
131501
+ // override if you need to initialize this.maxUniqueCount after constructor
131502
+ return this.maxUniqueCount;
131503
+ }
131490
131504
  replaceIfUnique() {
131491
131505
  this.updateParams();
131492
- if (this.uniqueVersionOfGen !== undefined
131506
+ if ((this.uniqueVersionOfGen !== undefined)
131493
131507
  && this.isUnique === true) {
131494
131508
  const uniqueGen = new this.uniqueVersionOfGen({
131495
131509
  ...this.params,
@@ -131520,6 +131534,20 @@ class AbstractGenerator {
131520
131534
  }
131521
131535
  }
131522
131536
  // Generators Classes -----------------------------------------------------------------------------------------------------------------------
131537
+ class GenerateHashFromString extends AbstractGenerator {
131538
+ static entityKind = 'GenerateHashFromString';
131539
+ init() { }
131540
+ generate({ input }) {
131541
+ let hash = 0;
131542
+ // p and m are prime numbers
131543
+ const p = 53;
131544
+ const m = 28871271685163;
131545
+ for (let i = 0; i < input.length; i++) {
131546
+ hash += ((input.codePointAt(i) || 0) * Math.pow(p, i)) % m;
131547
+ }
131548
+ return hash;
131549
+ }
131550
+ }
131523
131551
  class GenerateArray extends AbstractGenerator {
131524
131552
  static entityKind = 'GenerateArray';
131525
131553
  arraySize = 10;
@@ -131579,6 +131607,23 @@ class GenerateValuesFromArray extends AbstractGenerator {
131579
131607
  static entityKind = 'GenerateValuesFromArray';
131580
131608
  state;
131581
131609
  timeSpent = 0;
131610
+ maxUniqueCount;
131611
+ allValuesCount = 0; // TODO rewrite generator
131612
+ constructor(params) {
131613
+ super(params);
131614
+ this.allValuesCount = this.params.values.length;
131615
+ if (isObject(this.params.values[0])) {
131616
+ this.allValuesCount = this.params.values.reduce((acc, currVal) => acc + currVal.values.length, 0);
131617
+ }
131618
+ this.maxUniqueCount = this.allValuesCount;
131619
+ }
131620
+ getMaxUniqueCount() {
131621
+ this.allValuesCount = this.params.values.length;
131622
+ if (isObject(this.params.values[0])) {
131623
+ this.allValuesCount = this.params.values.reduce((acc, currVal) => acc + currVal.values.length, 0);
131624
+ }
131625
+ return this.allValuesCount;
131626
+ }
131582
131627
  checks({ count }) {
131583
131628
  const { values } = this.params;
131584
131629
  const { maxRepeatedValuesCount, notNull, isUnique } = this;
@@ -131596,16 +131641,13 @@ class GenerateValuesFromArray extends AbstractGenerator {
131596
131641
  : obj.count.every((count) => count > 0))))) {
131597
131642
  throw new Error('maxRepeatedValuesCount should be greater than zero.');
131598
131643
  }
131599
- let allValuesCount = values.length;
131600
- if (isObject(values[0])) {
131601
- allValuesCount = values.reduce((acc, currVal) => acc + currVal.values.length, 0);
131602
- }
131603
131644
  if (notNull === true
131604
131645
  && maxRepeatedValuesCount !== undefined
131605
131646
  && ((!isObject(values[0]) && typeof maxRepeatedValuesCount === 'number'
131606
131647
  && maxRepeatedValuesCount * values.length < count)
131607
131648
  || (isObject(values[0]) && typeof maxRepeatedValuesCount === 'number'
131608
- && maxRepeatedValuesCount * allValuesCount < count))) {
131649
+ // eslint-disable-next-line unicorn/consistent-destructuring
131650
+ && maxRepeatedValuesCount * this.allValuesCount < count))) {
131609
131651
  throw new Error("Can't fill notNull column with null values.");
131610
131652
  }
131611
131653
  if (isUnique === true && maxRepeatedValuesCount !== undefined && ((typeof maxRepeatedValuesCount === 'number' && maxRepeatedValuesCount > 1)
@@ -131616,7 +131658,8 @@ class GenerateValuesFromArray extends AbstractGenerator {
131616
131658
  throw new Error("Can't be greater than 1 if column is unique.");
131617
131659
  }
131618
131660
  if (isUnique === true && notNull === true && ((!isObject(values[0]) && values.length < count)
131619
- || (isObject(values[0]) && allValuesCount < count))) {
131661
+ // eslint-disable-next-line unicorn/consistent-destructuring
131662
+ || (isObject(values[0]) && this.allValuesCount < count))) {
131620
131663
  // console.log(maxRepeatedValuesCount, values.length, allValuesCount, count)
131621
131664
  throw new Error('There are no enough values to fill unique column.');
131622
131665
  }
@@ -131752,6 +131795,8 @@ class GenerateSelfRelationsValuesFromArray extends AbstractGenerator {
131752
131795
  class GenerateIntPrimaryKey extends AbstractGenerator {
131753
131796
  static entityKind = 'GenerateIntPrimaryKey';
131754
131797
  maxValue;
131798
+ maxUniqueCount = Number.POSITIVE_INFINITY;
131799
+ isGeneratorUnique = true;
131755
131800
  init({ count }) {
131756
131801
  if (this.maxValue !== undefined && count > this.maxValue) {
131757
131802
  throw new Error('count exceeds max number for this column type.');
@@ -131802,14 +131847,39 @@ class GenerateNumber extends AbstractGenerator {
131802
131847
  class GenerateUniqueNumber extends AbstractGenerator {
131803
131848
  static entityKind = 'GenerateUniqueNumber';
131804
131849
  state;
131805
- isUnique = true;
131806
- init({ count, seed }) {
131850
+ precision;
131851
+ isGeneratorUnique = true;
131852
+ maxUniqueCount;
131853
+ constructor(params) {
131854
+ super(params);
131855
+ let { minValue, maxValue } = this.params;
131856
+ const { precision } = this.params;
131857
+ this.precision = precision ?? 100;
131858
+ if (maxValue === undefined) {
131859
+ this.maxUniqueCount = Number.POSITIVE_INFINITY;
131860
+ return;
131861
+ }
131862
+ else {
131863
+ maxValue *= this.precision;
131864
+ }
131865
+ if (minValue === undefined) {
131866
+ minValue = -maxValue;
131867
+ }
131868
+ else {
131869
+ minValue *= this.precision;
131870
+ }
131871
+ this.maxUniqueCount = maxValue - minValue + 1;
131872
+ }
131873
+ getMaxUniqueCount() {
131874
+ if (this.maxUniqueCount !== undefined)
131875
+ return this.maxUniqueCount;
131807
131876
  let { minValue, maxValue, precision } = this.params;
131808
131877
  if (precision === undefined) {
131809
131878
  precision = 100;
131810
131879
  }
131811
131880
  if (maxValue === undefined) {
131812
- maxValue = count * precision;
131881
+ this.maxUniqueCount = Number.POSITIVE_INFINITY;
131882
+ return this.maxUniqueCount;
131813
131883
  }
131814
131884
  else {
131815
131885
  maxValue *= precision;
@@ -131820,9 +131890,26 @@ class GenerateUniqueNumber extends AbstractGenerator {
131820
131890
  else {
131821
131891
  minValue *= precision;
131822
131892
  }
131893
+ this.maxUniqueCount = maxValue - minValue + 1;
131894
+ return this.maxUniqueCount;
131895
+ }
131896
+ init({ count, seed }) {
131897
+ let { minValue, maxValue } = this.params;
131898
+ if (maxValue === undefined) {
131899
+ maxValue = count * this.precision;
131900
+ }
131901
+ else {
131902
+ maxValue *= this.precision;
131903
+ }
131904
+ if (minValue === undefined) {
131905
+ minValue = -maxValue;
131906
+ }
131907
+ else {
131908
+ minValue *= this.precision;
131909
+ }
131823
131910
  const genUniqueIntObj = new GenerateUniqueInt({ minValue, maxValue });
131824
131911
  genUniqueIntObj.init({ count, seed });
131825
- this.state = { genUniqueIntObj, minValue, maxValue, precision };
131912
+ this.state = { genUniqueIntObj, minValue, maxValue, precision: this.precision };
131826
131913
  }
131827
131914
  generate() {
131828
131915
  if (this.state === undefined) {
@@ -131877,8 +131964,48 @@ class GenerateUniqueInt extends AbstractGenerator {
131877
131964
  genMaxRepeatedValuesCount;
131878
131965
  skipCheck = false;
131879
131966
  state;
131880
- isUnique = true;
131967
+ isGeneratorUnique = true;
131881
131968
  timeSpent = 0;
131969
+ maxUniqueCount;
131970
+ constructor(params) {
131971
+ super(params);
131972
+ let minValue = this.params.minValue, maxValue = this.params.maxValue;
131973
+ if (maxValue === undefined) {
131974
+ this.maxUniqueCount = Number.POSITIVE_INFINITY;
131975
+ return;
131976
+ }
131977
+ if (minValue === undefined) {
131978
+ minValue = -maxValue;
131979
+ }
131980
+ if (typeof minValue === 'number' && typeof maxValue === 'number') {
131981
+ minValue = minValue >= 0 ? Math.ceil(minValue) : Math.floor(minValue);
131982
+ maxValue = maxValue >= 0 ? Math.floor(maxValue) : Math.ceil(maxValue);
131983
+ this.maxUniqueCount = Number(maxValue - minValue) + 1;
131984
+ }
131985
+ else if (typeof minValue === 'bigint' && typeof maxValue === 'bigint') {
131986
+ this.maxUniqueCount = Number(maxValue - minValue) + 1;
131987
+ }
131988
+ else
131989
+ this.maxUniqueCount = Number(Number(maxValue) - Number(minValue)) + 1; // error should be triggered in init method
131990
+ }
131991
+ getMaxUniqueCount() {
131992
+ if (this.maxUniqueCount !== undefined)
131993
+ return this.maxUniqueCount;
131994
+ let minValue = this.params.minValue, maxValue = this.params.maxValue;
131995
+ if (maxValue === undefined) {
131996
+ this.maxUniqueCount = Number.POSITIVE_INFINITY;
131997
+ return this.maxUniqueCount;
131998
+ }
131999
+ if (minValue === undefined) {
132000
+ minValue = -maxValue;
132001
+ }
132002
+ if (typeof minValue === 'number' && typeof maxValue === 'number') {
132003
+ minValue = minValue >= 0 ? Math.ceil(minValue) : Math.floor(minValue);
132004
+ maxValue = maxValue >= 0 ? Math.floor(maxValue) : Math.ceil(maxValue);
132005
+ }
132006
+ this.maxUniqueCount = Number(maxValue - minValue) + 1;
132007
+ return this.maxUniqueCount;
132008
+ }
131882
132009
  init({ count, seed }) {
131883
132010
  const rng = prand.xoroshiro128plus(seed);
131884
132011
  let { minValue, maxValue } = this.params;
@@ -132308,11 +132435,10 @@ class GenerateInterval extends AbstractGenerator {
132308
132435
  return interval;
132309
132436
  }
132310
132437
  }
132311
- // has a newer version
132312
132438
  class GenerateUniqueInterval extends AbstractGenerator {
132313
132439
  static 'entityKind' = 'GenerateUniqueInterval';
132314
132440
  state;
132315
- isUnique = true;
132441
+ isGeneratorUnique = true;
132316
132442
  config = {
132317
132443
  year: {
132318
132444
  from: 0,
@@ -132339,6 +132465,47 @@ class GenerateUniqueInterval extends AbstractGenerator {
132339
132465
  to: 60,
132340
132466
  },
132341
132467
  };
132468
+ maxUniqueCount;
132469
+ constructor(params) {
132470
+ super(params);
132471
+ const allFields = ['year', 'month', 'day', 'hour', 'minute', 'second'];
132472
+ let fieldsToGenerate = allFields;
132473
+ if (this.params.fields !== undefined && this.params.fields?.includes(' to ')) {
132474
+ const tokens = this.params.fields.split(' to ');
132475
+ const endIdx = allFields.indexOf(tokens[1]);
132476
+ fieldsToGenerate = allFields.slice(0, endIdx + 1);
132477
+ }
132478
+ else if (this.params.fields !== undefined) {
132479
+ const endIdx = allFields.indexOf(this.params.fields);
132480
+ fieldsToGenerate = allFields.slice(0, endIdx + 1);
132481
+ }
132482
+ this.maxUniqueCount = 1;
132483
+ for (const field of fieldsToGenerate) {
132484
+ const from = this.config[field].from, to = this.config[field].to;
132485
+ this.maxUniqueCount *= from - to + 1;
132486
+ }
132487
+ }
132488
+ getMaxUniqueCount() {
132489
+ if (this.maxUniqueCount !== undefined)
132490
+ return this.maxUniqueCount;
132491
+ const allFields = ['year', 'month', 'day', 'hour', 'minute', 'second'];
132492
+ let fieldsToGenerate = allFields;
132493
+ if (this.params.fields !== undefined && this.params.fields?.includes(' to ')) {
132494
+ const tokens = this.params.fields.split(' to ');
132495
+ const endIdx = allFields.indexOf(tokens[1]);
132496
+ fieldsToGenerate = allFields.slice(0, endIdx + 1);
132497
+ }
132498
+ else if (this.params.fields !== undefined) {
132499
+ const endIdx = allFields.indexOf(this.params.fields);
132500
+ fieldsToGenerate = allFields.slice(0, endIdx + 1);
132501
+ }
132502
+ this.maxUniqueCount = 1;
132503
+ for (const field of fieldsToGenerate) {
132504
+ const from = this.config[field].from, to = this.config[field].to;
132505
+ this.maxUniqueCount *= from - to + 1;
132506
+ }
132507
+ return this.maxUniqueCount;
132508
+ }
132342
132509
  init({ count, seed }) {
132343
132510
  const allFields = ['year', 'month', 'day', 'hour', 'minute', 'second'];
132344
132511
  let fieldsToGenerate = allFields;
@@ -132416,7 +132583,11 @@ class GenerateString extends AbstractGenerator {
132416
132583
  class GenerateUniqueString extends AbstractGenerator {
132417
132584
  static entityKind = 'GenerateUniqueString';
132418
132585
  state;
132419
- isUnique = true;
132586
+ isGeneratorUnique = true;
132587
+ maxUniqueCount = Number.POSITIVE_INFINITY;
132588
+ getMaxUniqueCount() {
132589
+ return Number.POSITIVE_INFINITY;
132590
+ }
132420
132591
  init({ seed }) {
132421
132592
  const rng = prand.xoroshiro128plus(seed);
132422
132593
  this.state = { rng };
@@ -132445,8 +132616,12 @@ class GenerateUniqueString extends AbstractGenerator {
132445
132616
  }
132446
132617
  class GenerateUUID extends AbstractGenerator {
132447
132618
  static entityKind = 'GenerateUUID';
132448
- isUnique = true;
132619
+ isGeneratorUnique = true;
132620
+ maxUniqueCount = Number.POSITIVE_INFINITY;
132449
132621
  state;
132622
+ getMaxUniqueCount() {
132623
+ return Number.POSITIVE_INFINITY;
132624
+ }
132450
132625
  init({ count, seed }) {
132451
132626
  super.init({ count, seed });
132452
132627
  const rng = prand.xoroshiro128plus(seed);
@@ -132501,9 +132676,16 @@ class GenerateFirstName extends AbstractGenerator {
132501
132676
  class GenerateUniqueFirstName extends AbstractGenerator {
132502
132677
  static entityKind = 'GenerateUniqueFirstName';
132503
132678
  state;
132504
- isUnique = true;
132679
+ isGeneratorUnique = true;
132680
+ maxUniqueCount = firstNames.length;
132681
+ getMaxUniqueCount() {
132682
+ if (this.maxUniqueCount !== undefined)
132683
+ return this.maxUniqueCount;
132684
+ this.maxUniqueCount = firstNames.length;
132685
+ return firstNames.length;
132686
+ }
132505
132687
  init({ count, seed }) {
132506
- if (count > firstNames.length) {
132688
+ if (count > this.getMaxUniqueCount()) {
132507
132689
  throw new Error('count exceeds max number of unique first names.');
132508
132690
  }
132509
132691
  if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$5) {
@@ -132547,9 +132729,16 @@ class GenerateLastName extends AbstractGenerator {
132547
132729
  class GenerateUniqueLastName extends AbstractGenerator {
132548
132730
  static entityKind = 'GenerateUniqueLastName';
132549
132731
  state;
132550
- isUnique = true;
132732
+ isGeneratorUnique = true;
132733
+ maxUniqueCount = lastNames.length;
132734
+ getMaxUniqueCount() {
132735
+ if (this.maxUniqueCount !== undefined)
132736
+ return this.maxUniqueCount;
132737
+ this.maxUniqueCount = lastNames.length;
132738
+ return lastNames.length;
132739
+ }
132551
132740
  init({ count, seed }) {
132552
- if (count > lastNames.length) {
132741
+ if (count > this.getMaxUniqueCount()) {
132553
132742
  throw new Error('count exceeds max number of unique last names.');
132554
132743
  }
132555
132744
  if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$3) {
@@ -132596,13 +132785,19 @@ class GenerateFullName extends AbstractGenerator {
132596
132785
  class GenerateUniqueFullName extends AbstractGenerator {
132597
132786
  static entityKind = 'GenerateUniqueFullName';
132598
132787
  state;
132599
- isUnique = true;
132788
+ isGeneratorUnique = true;
132600
132789
  timeSpent = 0;
132790
+ maxUniqueCount = firstNames.length * lastNames.length;
132791
+ getMaxUniqueCount() {
132792
+ if (this.maxUniqueCount !== undefined)
132793
+ return this.maxUniqueCount;
132794
+ this.maxUniqueCount = firstNames.length * lastNames.length;
132795
+ return this.maxUniqueCount;
132796
+ }
132601
132797
  init({ count, seed }) {
132602
132798
  const t0 = new Date();
132603
- const maxUniqueFullNamesNumber = firstNames.length * lastNames.length;
132604
- if (count > maxUniqueFullNamesNumber) {
132605
- throw new RangeError(`count exceeds max number of unique full names(${maxUniqueFullNamesNumber}).`);
132799
+ if (count > this.getMaxUniqueCount()) {
132800
+ throw new RangeError(`count exceeds max number of unique full names(${this.getMaxUniqueCount()}).`);
132606
132801
  }
132607
132802
  if (this.typeParams?.length !== undefined && this.typeParams?.length < (maxStringLength$5 + maxStringLength$3 + 1)) {
132608
132803
  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}.`);
@@ -132637,13 +132832,17 @@ class GenerateEmail extends AbstractGenerator {
132637
132832
  static entityKind = 'GenerateEmail';
132638
132833
  state;
132639
132834
  timeSpent = 0;
132640
- isUnique = true;
132835
+ isGeneratorUnique = true;
132836
+ maxUniqueCount = adjectives.length * firstNames.length * emailDomains.length;
132837
+ getMaxUniqueCount() {
132838
+ if (this.maxUniqueCount !== undefined)
132839
+ return this.maxUniqueCount;
132840
+ this.maxUniqueCount = adjectives.length * firstNames.length * emailDomains.length;
132841
+ return this.maxUniqueCount;
132842
+ }
132641
132843
  init({ count, seed }) {
132642
132844
  super.init({ count, seed });
132643
- const domainsArray = emailDomains;
132644
- const adjectivesArray = adjectives;
132645
- const namesArray = firstNames;
132646
- const maxUniqueEmailsNumber = adjectivesArray.length * namesArray.length * domainsArray.length;
132845
+ const maxUniqueEmailsNumber = adjectives.length * firstNames.length * emailDomains.length;
132647
132846
  if (count > maxUniqueEmailsNumber) {
132648
132847
  throw new RangeError(`count exceeds max number of unique emails(${maxUniqueEmailsNumber}).`);
132649
132848
  }
@@ -132651,7 +132850,7 @@ class GenerateEmail extends AbstractGenerator {
132651
132850
  if (this.typeParams?.length !== undefined && this.typeParams?.length < maxEmailLength) {
132652
132851
  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}.`);
132653
132852
  }
132654
- const arraysToGenerateFrom = [adjectivesArray, namesArray, domainsArray];
132853
+ const arraysToGenerateFrom = [adjectives, firstNames, emailDomains];
132655
132854
  const genIndicesObj = new GenerateUniqueInt({
132656
132855
  minValue: 0,
132657
132856
  maxValue: maxUniqueEmailsNumber - 1,
@@ -132675,20 +132874,75 @@ class GenerateEmail extends AbstractGenerator {
132675
132874
  class GeneratePhoneNumber extends AbstractGenerator {
132676
132875
  static entityKind = 'GeneratePhoneNumber';
132677
132876
  state;
132678
- isUnique = true;
132877
+ isGeneratorUnique = true;
132878
+ maxUniqueCount;
132879
+ constructor(params) {
132880
+ super(params);
132881
+ const { template } = this.params;
132882
+ if (template === undefined) {
132883
+ const { generatedDigitsNumbers } = this.prepareWithoutTemplate();
132884
+ this.maxUniqueCount = generatedDigitsNumbers.reduce((a, b) => a + Math.pow(10, b), 0);
132885
+ }
132886
+ else {
132887
+ const { placeholdersCount } = this.prepareWithTemplate();
132888
+ this.maxUniqueCount = Math.pow(10, placeholdersCount);
132889
+ }
132890
+ }
132891
+ prepareWithTemplate() {
132892
+ const { template } = this.params;
132893
+ const iterArray = [...template.matchAll(/#/g)];
132894
+ const placeholdersCount = iterArray.length;
132895
+ return { placeholdersCount };
132896
+ }
132897
+ prepareWithoutTemplate() {
132898
+ let { generatedDigitsNumbers, prefixes } = this.params;
132899
+ if (prefixes === undefined || prefixes.length === 0) {
132900
+ prefixes = phonesInfo.map((phoneInfo) => phoneInfo.split(',').slice(0, -1).join(' '));
132901
+ generatedDigitsNumbers = phonesInfo.map((phoneInfo) => {
132902
+ // tokens = ["380","99","9"] =
132903
+ // = ["country prefix", "operator prefix", "number length including operator prefix and excluding country prefix"]
132904
+ const tokens = phoneInfo.split(',');
132905
+ const operatorPrefixLength = tokens[1].replaceAll(' ', '').length;
132906
+ return Number(tokens[2]) - operatorPrefixLength;
132907
+ });
132908
+ }
132909
+ else {
132910
+ if (typeof generatedDigitsNumbers === 'number') {
132911
+ generatedDigitsNumbers = Array.from({ length: prefixes.length }).fill(generatedDigitsNumbers);
132912
+ }
132913
+ else if (generatedDigitsNumbers === undefined
132914
+ || generatedDigitsNumbers.length === 0) {
132915
+ generatedDigitsNumbers = Array.from({ length: prefixes.length }).fill(7);
132916
+ }
132917
+ }
132918
+ return { prefixes, generatedDigitsNumbers };
132919
+ }
132920
+ getMaxUniqueCount() {
132921
+ if (this.maxUniqueCount !== undefined)
132922
+ return this.maxUniqueCount;
132923
+ const { template } = this.params;
132924
+ if (template === undefined) {
132925
+ const { generatedDigitsNumbers } = this.prepareWithoutTemplate();
132926
+ this.maxUniqueCount = generatedDigitsNumbers.reduce((a, b) => a + Math.pow(10, b), 0);
132927
+ return this.maxUniqueCount;
132928
+ }
132929
+ else {
132930
+ const { placeholdersCount } = this.prepareWithTemplate();
132931
+ this.maxUniqueCount = Math.pow(10, placeholdersCount);
132932
+ return this.maxUniqueCount;
132933
+ }
132934
+ }
132679
132935
  init({ count, seed }) {
132680
132936
  super.init({ count, seed });
132681
- let { generatedDigitsNumbers } = this.params;
132682
- const { prefixes, template } = this.params;
132937
+ const { template } = this.params;
132683
132938
  const rng = prand.xoroshiro128plus(seed);
132684
132939
  if (template !== undefined) {
132685
132940
  if (this.typeParams?.length !== undefined && this.typeParams?.length < template.length) {
132686
132941
  throw new Error(`Length of phone number template is shorter than db column length restriction: ${this.typeParams?.length}.
132687
132942
  Set the maximum string length to at least ${template.length}.`);
132688
132943
  }
132689
- const iterArray = [...template.matchAll(/#/g)];
132690
- const placeholdersCount = iterArray.length;
132691
- const maxUniquePhoneNumbersCount = Math.pow(10, placeholdersCount);
132944
+ const { placeholdersCount } = this.prepareWithTemplate();
132945
+ const maxUniquePhoneNumbersCount = this.getMaxUniqueCount();
132692
132946
  if (maxUniquePhoneNumbersCount < count) {
132693
132947
  throw new RangeError(`count exceeds max number of unique phone numbers(${maxUniquePhoneNumbersCount}).`);
132694
132948
  }
@@ -132705,27 +132959,8 @@ class GeneratePhoneNumber extends AbstractGenerator {
132705
132959
  this.state = { rng, placeholdersCount, generatorsMap, prefixesArray, generatedDigitsNumbers, phoneNumbersSet };
132706
132960
  return;
132707
132961
  }
132708
- let prefixesArray;
132709
- if (prefixes === undefined || prefixes.length === 0) {
132710
- prefixesArray = phonesInfo.map((phoneInfo) => phoneInfo.split(',').slice(0, -1).join(' '));
132711
- generatedDigitsNumbers = phonesInfo.map((phoneInfo) => {
132712
- // tokens = ["380","99","9"] =
132713
- // = ["country prefix", "operator prefix", "number length including operator prefix and excluding country prefix"]
132714
- const tokens = phoneInfo.split(',');
132715
- const operatorPrefixLength = tokens[1].replaceAll(' ', '').length;
132716
- return Number(tokens[2]) - operatorPrefixLength;
132717
- });
132718
- }
132719
- else {
132720
- prefixesArray = prefixes;
132721
- if (typeof generatedDigitsNumbers === 'number') {
132722
- generatedDigitsNumbers = Array.from({ length: prefixes.length }).fill(generatedDigitsNumbers);
132723
- }
132724
- else if (generatedDigitsNumbers === undefined
132725
- || generatedDigitsNumbers.length === 0) {
132726
- generatedDigitsNumbers = Array.from({ length: prefixes.length }).fill(7);
132727
- }
132728
- }
132962
+ const { generatedDigitsNumbers, prefixes } = this.prepareWithoutTemplate();
132963
+ const prefixesArray = [...prefixes];
132729
132964
  const maxPrefixLength = Math.max(...prefixesArray.map((prefix) => prefix.length));
132730
132965
  const maxGeneratedDigits = Math.max(...generatedDigitsNumbers);
132731
132966
  if (this.typeParams?.length !== undefined && this.typeParams?.length < (maxPrefixLength + maxGeneratedDigits)) {
@@ -132734,7 +132969,7 @@ class GeneratePhoneNumber extends AbstractGenerator {
132734
132969
  if (new Set(prefixesArray).size !== prefixesArray.length) {
132735
132970
  throw new Error('prefixes are not unique.');
132736
132971
  }
132737
- const maxUniquePhoneNumbersCount = generatedDigitsNumbers.reduce((a, b) => a + Math.pow(10, b), 0);
132972
+ const maxUniquePhoneNumbersCount = this.getMaxUniqueCount();
132738
132973
  if (maxUniquePhoneNumbersCount < count) {
132739
132974
  throw new RangeError(`count exceeds max number of unique phone numbers(${maxUniquePhoneNumbersCount}).`);
132740
132975
  }
@@ -132824,9 +133059,16 @@ class GenerateCountry extends AbstractGenerator {
132824
133059
  class GenerateUniqueCountry extends AbstractGenerator {
132825
133060
  static entityKind = 'GenerateUniqueCountry';
132826
133061
  state;
132827
- isUnique = true;
133062
+ isGeneratorUnique = true;
133063
+ maxUniqueCount = countries.length;
133064
+ getMaxUniqueCount() {
133065
+ if (this.maxUniqueCount !== undefined)
133066
+ return this.maxUniqueCount;
133067
+ this.maxUniqueCount = countries.length;
133068
+ return this.maxUniqueCount;
133069
+ }
132828
133070
  init({ count, seed }) {
132829
- if (count > countries.length) {
133071
+ if (count > this.getMaxUniqueCount()) {
132830
133072
  throw new Error('count exceeds max number of unique countries.');
132831
133073
  }
132832
133074
  if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$7) {
@@ -132895,11 +133137,17 @@ class GenerateStreetAddress extends AbstractGenerator {
132895
133137
  class GenerateUniqueStreetAddress extends AbstractGenerator {
132896
133138
  static entityKind = 'GenerateUniqueStreetAddress';
132897
133139
  state;
132898
- isUnique = true;
133140
+ isGeneratorUnique = true;
133141
+ streetNumbersCount = 999;
133142
+ maxUniqueCount = this.streetNumbersCount * (firstNames.length + lastNames.length)
133143
+ * streetSuffix.length;
133144
+ getMaxUniqueCount() {
133145
+ return this.maxUniqueCount;
133146
+ }
132899
133147
  init({ count, seed }) {
132900
- const streetNumberStrs = Array.from({ length: 999 }, (_, i) => String(i + 1));
132901
- const maxUniqueStreetnamesNumber = streetNumberStrs.length * firstNames.length * streetSuffix.length
132902
- + streetNumberStrs.length * firstNames.length * streetSuffix.length;
133148
+ const streetNumberStrs = Array.from({ length: this.streetNumbersCount }, (_, i) => String(i + 1));
133149
+ const maxUniqueStreetnamesNumber = streetNumberStrs.length * (firstNames.length + lastNames.length)
133150
+ * streetSuffix.length;
132903
133151
  if (count > maxUniqueStreetnamesNumber) {
132904
133152
  throw new RangeError(`count exceeds max number of unique street names(${maxUniqueStreetnamesNumber}).`);
132905
133153
  }
@@ -132924,7 +133172,7 @@ class GenerateUniqueStreetAddress extends AbstractGenerator {
132924
133172
  minValue: 0,
132925
133173
  maxValue: streetNumberStrs.length * lastNames.length * streetSuffix.length - 1,
132926
133174
  }),
132927
- maxUniqueStreetNamesNumber: streetNumberStrs.length * firstNames.length * streetSuffix.length,
133175
+ maxUniqueStreetNamesNumber: streetNumberStrs.length * lastNames.length * streetSuffix.length,
132928
133176
  count: 0,
132929
133177
  arraysToChooseFrom: [streetNumberStrs, lastNames, streetSuffix],
132930
133178
  },
@@ -132978,9 +133226,10 @@ class GenerateCity extends AbstractGenerator {
132978
133226
  class GenerateUniqueCity extends AbstractGenerator {
132979
133227
  static entityKind = 'GenerateUniqueCity';
132980
133228
  state;
132981
- isUnique = true;
133229
+ isGeneratorUnique = true;
133230
+ maxUniqueCount = cityNames.length;
132982
133231
  init({ count, seed }) {
132983
- if (count > cityNames.length) {
133232
+ if (count > this.maxUniqueCount) {
132984
133233
  throw new Error('count exceeds max number of unique cities.');
132985
133234
  }
132986
133235
  if (this.typeParams?.length !== undefined && this.typeParams?.length < maxStringLength$9) {
@@ -133035,11 +133284,11 @@ class GeneratePostcode extends AbstractGenerator {
133035
133284
  class GenerateUniquePostcode extends AbstractGenerator {
133036
133285
  static entityKind = 'GenerateUniquePostcode';
133037
133286
  state;
133038
- isUnique = true;
133287
+ isGeneratorUnique = true;
133288
+ maxUniqueCount = Math.pow(10, 5) + Math.pow(10, 9);
133039
133289
  init({ count, seed }) {
133040
- const maxUniquePostcodeNumber = Math.pow(10, 5) + Math.pow(10, 9);
133041
- if (count > maxUniquePostcodeNumber) {
133042
- throw new RangeError(`count exceeds max number of unique postcodes(${maxUniquePostcodeNumber}).`);
133290
+ if (count > this.maxUniqueCount) {
133291
+ throw new RangeError(`count exceeds max number of unique postcodes(${this.maxUniqueCount}).`);
133043
133292
  }
133044
133293
  const rng = prand.xoroshiro128plus(seed);
133045
133294
  const templates = [
@@ -133161,12 +133410,12 @@ class GenerateCompanyName extends AbstractGenerator {
133161
133410
  class GenerateUniqueCompanyName extends AbstractGenerator {
133162
133411
  static entityKind = 'GenerateUniqueCompanyName';
133163
133412
  state;
133164
- isUnique = true;
133413
+ isGeneratorUnique = true;
133414
+ maxUniqueCount = lastNames.length * companyNameSuffixes.length + Math.pow(lastNames.length, 2)
133415
+ + Math.pow(lastNames.length, 2) + Math.pow(lastNames.length, 3);
133165
133416
  init({ count, seed }) {
133166
- const maxUniqueCompanyNameNumber = lastNames.length * companyNameSuffixes.length + Math.pow(lastNames.length, 2)
133167
- + Math.pow(lastNames.length, 2) + Math.pow(lastNames.length, 3);
133168
- if (count > maxUniqueCompanyNameNumber) {
133169
- throw new RangeError(`count exceeds max number of unique company names(${maxUniqueCompanyNameNumber}).`);
133417
+ if (count > this.maxUniqueCount) {
133418
+ throw new RangeError(`count exceeds max number of unique company names(${this.maxUniqueCount}).`);
133170
133419
  }
133171
133420
  // max( { template: '#', placeholdersCount: 1 }, { template: '#, # and #', placeholdersCount: 3 } )
133172
133421
  const maxCompanyNameLength = Math.max(maxStringLength$3 + maxStringLength$8 + 1, 3 * maxStringLength$3 + 7);
@@ -133352,22 +133601,29 @@ class GeneratePoint extends AbstractGenerator {
133352
133601
  class GenerateUniquePoint extends AbstractGenerator {
133353
133602
  static entityKind = 'GenerateUniquePoint';
133354
133603
  state;
133355
- isUnique = true;
133356
- init({ count, seed }) {
133357
- // TODO: rewrite the unique generator to use fastCartesianProduct for generating unique points.
133358
- const xCoordinateGen = new GenerateUniqueNumber({
133604
+ isGeneratorUnique = true;
133605
+ xCoordinateGen;
133606
+ yCoordinateGen;
133607
+ maxUniqueCount;
133608
+ constructor(params) {
133609
+ super(params);
133610
+ this.xCoordinateGen = new GenerateUniqueNumber({
133359
133611
  minValue: this.params.minXValue,
133360
133612
  maxValue: this.params.maxXValue,
133361
133613
  precision: 10,
133362
133614
  });
133363
- xCoordinateGen.init({ count, seed });
133364
- const yCoordinateGen = new GenerateUniqueNumber({
133615
+ this.yCoordinateGen = new GenerateUniqueNumber({
133365
133616
  minValue: this.params.minYValue,
133366
133617
  maxValue: this.params.maxYValue,
133367
133618
  precision: 10,
133368
133619
  });
133369
- yCoordinateGen.init({ count, seed });
133370
- this.state = { xCoordinateGen, yCoordinateGen };
133620
+ this.maxUniqueCount = Math.min(this.xCoordinateGen.maxUniqueCount, this.yCoordinateGen.maxUniqueCount);
133621
+ }
133622
+ init({ count, seed }) {
133623
+ // TODO: rewrite the unique generator to use fastCartesianProduct for generating unique points.
133624
+ this.xCoordinateGen.init({ count, seed });
133625
+ this.yCoordinateGen.init({ count, seed });
133626
+ this.state = { xCoordinateGen: this.xCoordinateGen, yCoordinateGen: this.yCoordinateGen };
133371
133627
  }
133372
133628
  generate() {
133373
133629
  if (this.state === undefined) {
@@ -133439,28 +133695,40 @@ class GenerateLine extends AbstractGenerator {
133439
133695
  class GenerateUniqueLine extends AbstractGenerator {
133440
133696
  static entityKind = 'GenerateUniqueLine';
133441
133697
  state;
133442
- isUnique = true;
133443
- init({ count, seed }) {
133444
- // TODO: rewrite the unique generator to use fastCartesianProduct for generating unique triplets(liens).
133445
- const aCoefficientGen = new GenerateUniqueNumber({
133698
+ isGeneratorUnique = true;
133699
+ maxUniqueCount;
133700
+ aCoefficientGen;
133701
+ bCoefficientGen;
133702
+ cCoefficientGen;
133703
+ constructor(params) {
133704
+ super(params);
133705
+ this.aCoefficientGen = new GenerateUniqueNumber({
133446
133706
  minValue: this.params.minAValue,
133447
133707
  maxValue: this.params.maxAValue,
133448
133708
  precision: 10,
133449
133709
  });
133450
- aCoefficientGen.init({ count, seed });
133451
- const bCoefficientGen = new GenerateUniqueNumber({
133710
+ this.bCoefficientGen = new GenerateUniqueNumber({
133452
133711
  minValue: this.params.minBValue,
133453
133712
  maxValue: this.params.maxBValue,
133454
133713
  precision: 10,
133455
133714
  });
133456
- bCoefficientGen.init({ count, seed });
133457
- const cCoefficientGen = new GenerateUniqueNumber({
133715
+ this.cCoefficientGen = new GenerateUniqueNumber({
133458
133716
  minValue: this.params.minCValue,
133459
133717
  maxValue: this.params.maxCValue,
133460
133718
  precision: 10,
133461
133719
  });
133462
- cCoefficientGen.init({ count, seed });
133463
- this.state = { aCoefficientGen, bCoefficientGen, cCoefficientGen };
133720
+ this.maxUniqueCount = Math.min(this.aCoefficientGen.maxUniqueCount, this.bCoefficientGen.maxUniqueCount, this.cCoefficientGen.maxUniqueCount);
133721
+ }
133722
+ init({ count, seed }) {
133723
+ // TODO: rewrite the unique generator to use fastCartesianProduct for generating unique triplets(liens).
133724
+ this.aCoefficientGen.init({ count, seed });
133725
+ this.bCoefficientGen.init({ count, seed });
133726
+ this.cCoefficientGen.init({ count, seed });
133727
+ this.state = {
133728
+ aCoefficientGen: this.aCoefficientGen,
133729
+ bCoefficientGen: this.bCoefficientGen,
133730
+ cCoefficientGen: this.cCoefficientGen,
133731
+ };
133464
133732
  }
133465
133733
  generate() {
133466
133734
  if (this.state === undefined) {
@@ -133518,7 +133786,15 @@ class GenerateUniqueBitString extends AbstractGenerator {
133518
133786
  static entityKind = 'GenerateUniqueBitString';
133519
133787
  dimensions = 11;
133520
133788
  state;
133521
- isUnique = true;
133789
+ isGeneratorUnique = true;
133790
+ getMaxUniqueCount() {
133791
+ if (this.maxUniqueCount >= 0)
133792
+ return this.maxUniqueCount;
133793
+ this.dimensions = this.params.dimensions ?? this.typeParams?.length ?? this.dimensions;
133794
+ this.maxUniqueCount = Math.pow(2, this.dimensions);
133795
+ // TODO revise: will work incorrect with this.dimensions > 53, due to node js number limitations
133796
+ return this.maxUniqueCount;
133797
+ }
133522
133798
  init({ count, seed }) {
133523
133799
  this.dimensions = this.params.dimensions ?? this.typeParams?.length ?? this.dimensions;
133524
133800
  let intGen;
@@ -133589,18 +133865,35 @@ class GenerateInet extends AbstractGenerator {
133589
133865
  }
133590
133866
  }
133591
133867
  }
133592
- // TODO: add defaults to js doc
133593
133868
  class GenerateUniqueInet extends AbstractGenerator {
133594
133869
  static entityKind = 'GenerateUniqueInet';
133595
133870
  ipAddress = 'ipv4';
133596
133871
  includeCidr = true;
133597
133872
  delimiter = '.';
133598
133873
  state;
133599
- isUnique = true;
133600
- init({ count, seed }) {
133874
+ isGeneratorUnique = true;
133875
+ maxUniqueCount;
133876
+ constructor(params) {
133877
+ super(params);
133601
133878
  this.ipAddress = this.params.ipAddress ?? this.ipAddress;
133602
- this.delimiter = this.ipAddress === 'ipv4' ? '.' : ':';
133603
133879
  this.includeCidr = this.params.includeCidr ?? this.includeCidr;
133880
+ if (this.ipAddress === 'ipv4') {
133881
+ this.maxUniqueCount = 256 ** 4;
133882
+ if (this.includeCidr) {
133883
+ this.maxUniqueCount *= 33;
133884
+ }
133885
+ }
133886
+ else {
133887
+ // this.ipAddress === 'ipv6'
133888
+ // TODO revise: this.maxUniqueCount can exceed Number.MAX_SAFE_INTEGER
133889
+ this.maxUniqueCount = 65535 ** 8;
133890
+ if (this.includeCidr) {
133891
+ this.maxUniqueCount *= 129;
133892
+ }
133893
+ }
133894
+ }
133895
+ init({ count, seed }) {
133896
+ this.delimiter = this.ipAddress === 'ipv4' ? '.' : ':';
133604
133897
  // maxValue - number of combinations for cartesian product: {0…255} × {0…255} × {0…255} × {0…255} × {0…32}
133605
133898
  // where pattern for ipv4 ip is {0–255}.{0–255}.{0–255}.{0–255}[/{0–32}?]
133606
133899
  // 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}
@@ -133716,11 +134009,42 @@ class GenerateUniqueGeometry extends AbstractGenerator {
133716
134009
  srid = 4326;
133717
134010
  decimalPlaces = 6;
133718
134011
  state;
133719
- isUnique = true;
133720
- init({ count, seed }) {
134012
+ isGeneratorUnique = true;
134013
+ maxUniqueCount;
134014
+ constructor(params) {
134015
+ super(params);
133721
134016
  this.type = this.params.type ?? this.type;
133722
134017
  this.srid = this.params.srid ?? this.srid;
133723
134018
  this.decimalPlaces = this.params.decimalPlaces ?? this.decimalPlaces;
134019
+ let minXValue, maxXValue, minYValue, maxYValue, denominator;
134020
+ if (this.type === 'point') {
134021
+ if (this.srid === 4326) {
134022
+ // Degrees (latitude / longitude)
134023
+ denominator = 10 ** this.decimalPlaces;
134024
+ minXValue = -180 * denominator;
134025
+ maxXValue = 180 * denominator;
134026
+ minYValue = -90 * denominator;
134027
+ maxYValue = 90 * denominator;
134028
+ }
134029
+ else {
134030
+ // this.srid === 3857
134031
+ // Meters (projected X / Y)
134032
+ denominator = 1;
134033
+ minXValue = -20026376;
134034
+ maxXValue = 20026376;
134035
+ minYValue = -20048966;
134036
+ maxYValue = 20048966;
134037
+ }
134038
+ }
134039
+ else {
134040
+ // error should be triggered in init method
134041
+ this.maxUniqueCount = -1;
134042
+ return;
134043
+ }
134044
+ // TODO revise: can lose accuracy due to exceeding Number.MAX_SAFE_INTEGER
134045
+ this.maxUniqueCount = Number(BigInt(maxXValue - minXValue + 1) * BigInt(maxYValue - minYValue + 1));
134046
+ }
134047
+ init({ count, seed }) {
133724
134048
  let minXValue, maxXValue, minYValue, maxYValue, denominator;
133725
134049
  if (this.type === 'point') {
133726
134050
  if (this.srid === 4326) {
@@ -133827,11 +134151,10 @@ class GenerateUniqueVector extends AbstractGenerator {
133827
134151
  maxValue = 1000;
133828
134152
  decimalPlaces = 2;
133829
134153
  state;
133830
- isUnique = true;
133831
- init({ count, seed }) {
133832
- this.dimensions = this.params.dimensions ?? this.typeParams.length ?? this.dimensions;
134154
+ isGeneratorUnique = true;
134155
+ constructor(params) {
134156
+ super(params);
133833
134157
  this.decimalPlaces = this.params.decimalPlaces ?? this.decimalPlaces;
133834
- const denominator = 10 ** this.decimalPlaces;
133835
134158
  this.minValue = this.params.minValue ?? this.minValue;
133836
134159
  this.maxValue = this.params.maxValue ?? this.maxValue;
133837
134160
  if (this.minValue > this.maxValue) {
@@ -133841,6 +134164,18 @@ class GenerateUniqueVector extends AbstractGenerator {
133841
134164
  if (this.decimalPlaces < 0) {
133842
134165
  throw new Error(`decimalPlaces value must be greater than or equal to zero.`);
133843
134166
  }
134167
+ }
134168
+ getMaxUniqueCount() {
134169
+ if (this.maxUniqueCount >= 0)
134170
+ return this.maxUniqueCount;
134171
+ this.dimensions = this.params.dimensions ?? this.typeParams.length ?? this.dimensions;
134172
+ const denominator = 10 ** this.decimalPlaces;
134173
+ this.maxUniqueCount = (this.maxValue * denominator - this.minValue * denominator + 1) ** this.dimensions;
134174
+ return this.maxUniqueCount;
134175
+ }
134176
+ init({ count, seed }) {
134177
+ this.dimensions = this.params.dimensions ?? this.typeParams.length ?? this.dimensions;
134178
+ const denominator = 10 ** this.decimalPlaces;
133844
134179
  if (abs(BigInt(this.minValue) * BigInt(denominator)) > Number.MAX_SAFE_INTEGER
133845
134180
  || abs(BigInt(this.maxValue) * BigInt(denominator)) > Number.MAX_SAFE_INTEGER) {
133846
134181
  console.warn(`vector generator: minValue or maxValue multiplied by 10^decimalPlaces exceeds Number.MAX_SAFE_INTEGER (2^53 -1).\n`
@@ -133876,13 +134211,80 @@ class GenerateUniqueVector extends AbstractGenerator {
133876
134211
  return vector;
133877
134212
  }
133878
134213
  }
134214
+ class GenerateCompositeUniqueKey extends AbstractGenerator {
134215
+ static entityKind = 'GenerateCompositeUniqueKey';
134216
+ columnGenerators = [];
134217
+ isInitialized = false;
134218
+ state;
134219
+ addGenerator(columnName, generator) {
134220
+ this.columnGenerators.push({ columnName, generator });
134221
+ }
134222
+ init({ count, seed }) {
134223
+ if (this.isInitialized)
134224
+ return;
134225
+ if (this.columnGenerators.length === 0) {
134226
+ throw new Error(`composite unique key generator has no generators to work with.`);
134227
+ }
134228
+ let countPerGen = Math.ceil(count ** (1 / this.columnGenerators.length));
134229
+ // const gensMaxUniqueCount: { columnName: string; count: number; maxUniqueCount: number }[] = [];
134230
+ for (const colGen of this.columnGenerators) {
134231
+ colGen.maxUniqueCount = colGen.generator.getMaxUniqueCount();
134232
+ }
134233
+ this.columnGenerators.sort((a, b) => a.maxUniqueCount - b.maxUniqueCount);
134234
+ let currCount = count;
134235
+ let canGenerate = false;
134236
+ for (const [idx, colGen] of this.columnGenerators.entries()) {
134237
+ if (colGen.maxUniqueCount < countPerGen) {
134238
+ colGen.count = colGen.maxUniqueCount;
134239
+ currCount /= colGen.count;
134240
+ countPerGen = Math.ceil(currCount ** (1 / (this.columnGenerators.length - idx - 1)));
134241
+ canGenerate = false;
134242
+ }
134243
+ else {
134244
+ colGen.count = countPerGen;
134245
+ canGenerate = true;
134246
+ }
134247
+ }
134248
+ if (!canGenerate) {
134249
+ const colGensCountInfo = this.columnGenerators.map((colGen) => `generator:${colGen.generator.getEntityKind()};count:${colGen.count}`).join('\n');
134250
+ throw new Error(`There are no enough unique values in each generator to generate ${count} values; \n${colGensCountInfo}`);
134251
+ }
134252
+ const sets = [];
134253
+ for (const colGen of this.columnGenerators) {
134254
+ colGen.generator.init({ count: colGen.count, seed });
134255
+ const setI = [];
134256
+ for (let i = 0; i < countPerGen; i++) {
134257
+ setI.push(colGen.generator.generate({ i }));
134258
+ }
134259
+ sets.push(setI);
134260
+ }
134261
+ this.state = { sets, currI: -1, currValue: {} };
134262
+ this.isInitialized = true;
134263
+ }
134264
+ generate({ i, columnName }) {
134265
+ if (this.state === undefined) {
134266
+ throw new Error('state is not defined.');
134267
+ }
134268
+ if (i > this.state.currI) {
134269
+ const rowI = fastCartesianProduct(this.state.sets, i);
134270
+ const newCurrValue = {};
134271
+ for (const [idx, colGen] of this.columnGenerators.entries()) {
134272
+ newCurrValue[colGen.columnName] = rowI[idx];
134273
+ }
134274
+ this.state.currValue = newCurrValue;
134275
+ this.state.currI = i;
134276
+ }
134277
+ return this.state.currValue[columnName];
134278
+ }
134279
+ }
133879
134280
 
133880
134281
  /* eslint-disable drizzle-internal/require-entity-kind */
133881
134282
  class GenerateUniqueIntervalV2 extends AbstractGenerator {
133882
134283
  static 'entityKind' = 'GenerateUniqueInterval';
133883
134284
  static version = 2;
133884
134285
  state;
133885
- isUnique = true;
134286
+ isGeneratorUnique = true;
134287
+ maxUniqueCount;
133886
134288
  config = {
133887
134289
  year: {
133888
134290
  from: 0,
@@ -133909,29 +134311,33 @@ class GenerateUniqueIntervalV2 extends AbstractGenerator {
133909
134311
  to: 59,
133910
134312
  },
133911
134313
  };
133912
- init({ count, seed }) {
134314
+ fieldsToGenerate;
134315
+ constructor(params) {
134316
+ super(params);
133913
134317
  const allFields = ['year', 'month', 'day', 'hour', 'minute', 'second'];
133914
- let fieldsToGenerate = allFields;
134318
+ this.fieldsToGenerate = allFields;
133915
134319
  if (this.params.fields !== undefined && this.params.fields?.includes(' to ')) {
133916
134320
  const tokens = this.params.fields.split(' to ');
133917
134321
  const endIdx = allFields.indexOf(tokens[1]);
133918
- fieldsToGenerate = allFields.slice(0, endIdx + 1);
134322
+ this.fieldsToGenerate = allFields.slice(0, endIdx + 1);
133919
134323
  }
133920
134324
  else if (this.params.fields !== undefined) {
133921
134325
  const endIdx = allFields.indexOf(this.params.fields);
133922
- fieldsToGenerate = allFields.slice(0, endIdx + 1);
134326
+ this.fieldsToGenerate = allFields.slice(0, endIdx + 1);
133923
134327
  }
133924
- let maxUniqueIntervalsNumber = 1;
133925
- for (const field of fieldsToGenerate) {
134328
+ this.maxUniqueCount = 1;
134329
+ for (const field of this.fieldsToGenerate) {
133926
134330
  const from = this.config[field].from, to = this.config[field].to;
133927
- maxUniqueIntervalsNumber *= from - to + 1;
134331
+ this.maxUniqueCount *= from - to + 1;
133928
134332
  }
133929
- if (count > maxUniqueIntervalsNumber) {
133930
- throw new RangeError(`count exceeds max number of unique intervals(${maxUniqueIntervalsNumber})`);
134333
+ }
134334
+ init({ count, seed }) {
134335
+ if (count > this.maxUniqueCount) {
134336
+ throw new RangeError(`count exceeds max number of unique intervals(${this.maxUniqueCount})`);
133931
134337
  }
133932
134338
  const rng = prand.xoroshiro128plus(seed);
133933
134339
  const intervalSet = new Set();
133934
- this.state = { rng, fieldsToGenerate, intervalSet };
134340
+ this.state = { rng, fieldsToGenerate: this.fieldsToGenerate, intervalSet };
133935
134341
  }
133936
134342
  generate() {
133937
134343
  if (this.state === undefined) {
@@ -133953,6 +134359,7 @@ class GenerateUniqueIntervalV2 extends AbstractGenerator {
133953
134359
  return interval;
133954
134360
  }
133955
134361
  }
134362
+ // TODO need to rework this generator
133956
134363
  class GenerateStringV2 extends AbstractGenerator {
133957
134364
  static 'entityKind' = 'GenerateString';
133958
134365
  static version = 2;
@@ -133994,21 +134401,27 @@ class GenerateUniqueStringV2 extends AbstractGenerator {
133994
134401
  static 'entityKind' = 'GenerateUniqueString';
133995
134402
  static version = 2;
133996
134403
  state;
133997
- isUnique = true;
134404
+ isGeneratorUnique = true;
134405
+ maxStringLength = 20;
134406
+ minStringLength = 7;
134407
+ getMaxUniqueCount() {
134408
+ if (this.maxUniqueCount >= 0)
134409
+ return this.maxUniqueCount;
134410
+ this.maxStringLength = this.typeParams?.length ?? this.maxStringLength;
134411
+ this.maxUniqueCount = Number.parseInt('f'.repeat(this.maxStringLength), 16);
134412
+ return this.maxUniqueCount;
134413
+ }
133998
134414
  init({ seed, count }) {
133999
134415
  const rng = prand.xoroshiro128plus(seed);
134000
- let minStringLength = 7;
134001
- let maxStringLength = 20;
134002
134416
  // TODO: revise later
134003
- if (this.typeParams?.length !== undefined) {
134004
- maxStringLength = this.typeParams?.length;
134005
- if (maxStringLength === 1 || maxStringLength < minStringLength)
134006
- minStringLength = maxStringLength;
134417
+ this.maxStringLength = this.typeParams?.length ?? this.maxStringLength;
134418
+ if (this.maxStringLength === 1 || this.maxStringLength < this.minStringLength) {
134419
+ this.minStringLength = this.maxStringLength;
134007
134420
  }
134008
- if (maxStringLength < count.toString(16).length) {
134009
- throw new Error(`You can't generate ${count} unique strings, with a maximum string length of ${maxStringLength}.`);
134421
+ if (count > this.getMaxUniqueCount()) {
134422
+ throw new Error(`You can't generate ${count} unique strings, with a maximum string length of ${this.maxStringLength}.`);
134010
134423
  }
134011
- this.state = { rng, minStringLength, maxStringLength };
134424
+ this.state = { rng, minStringLength: this.minStringLength, maxStringLength: this.maxStringLength };
134012
134425
  }
134013
134426
  generate({ i }) {
134014
134427
  if (this.state === undefined) {
@@ -134031,6 +134444,25 @@ class GenerateUniqueStringV2 extends AbstractGenerator {
134031
134444
  }
134032
134445
  }
134033
134446
 
134447
+ /* eslint-disable drizzle-internal/require-entity-kind */
134448
+ class GenerateHashFromStringV3 extends AbstractGenerator {
134449
+ static entityKind = 'GenerateHashFromString';
134450
+ static version = 3;
134451
+ init() { }
134452
+ generate({ input }) {
134453
+ let hash = 0n;
134454
+ // p and m are prime numbers
134455
+ const p = 53n;
134456
+ const m = 28871271685163n; // < 2^53
134457
+ let power = 1n; // will track p^i, where i is character index
134458
+ for (const ch of input) {
134459
+ hash = (hash + (BigInt(ch.codePointAt(0) || 0) * power)) % m;
134460
+ power = (power * p) % m;
134461
+ }
134462
+ return Number(hash);
134463
+ }
134464
+ }
134465
+
134034
134466
  function createGenerator(generatorConstructor) {
134035
134467
  return (...args) => {
134036
134468
  let params = args[0];
@@ -134826,7 +135258,14 @@ const generatorsFuncs = {
134826
135258
  const generatorsFuncsV2 = {
134827
135259
  ...generatorsFuncs,
134828
135260
  };
135261
+ ({
135262
+ ...generatorsFuncs,
135263
+ });
134829
135264
  const generatorsMap = {
135265
+ GenerateHashFromString: [
135266
+ GenerateHashFromString,
135267
+ GenerateHashFromStringV3,
135268
+ ],
134830
135269
  HollowGenerator: [
134831
135270
  HollowGenerator,
134832
135271
  ],
@@ -135004,6 +135443,9 @@ const generatorsMap = {
135004
135443
  GenerateUniqueVector: [
135005
135444
  GenerateUniqueVector,
135006
135445
  ],
135446
+ GenerateCompositeUniqueKey: [
135447
+ GenerateCompositeUniqueKey,
135448
+ ],
135007
135449
  };
135008
135450
 
135009
135451
  // TODO: revise serial part generators
@@ -135222,7 +135664,7 @@ const selectGeneratorForCockroachColumn = (table, col) => {
135222
135664
  return generator;
135223
135665
  };
135224
135666
 
135225
- const latestVersion = 2;
135667
+ const latestVersion = 3;
135226
135668
 
135227
135669
  const selectGeneratorForMssqlColumn = (table, col) => {
135228
135670
  const pickGenerator = (table, col) => {
@@ -136071,6 +136513,7 @@ class SeedService {
136071
136513
  sqliteMaxParametersNumber = 32766;
136072
136514
  mssqlMaxParametersNumber = 2100;
136073
136515
  version;
136516
+ hashFromStringGenerator;
136074
136517
  generatePossibleGenerators = (connectionType, tables, relations, refinements, options) => {
136075
136518
  let columnPossibleGenerator;
136076
136519
  let tablePossibleGenerators;
@@ -136079,6 +136522,7 @@ class SeedService {
136079
136522
  if (Number.isNaN(this.version) || this.version < 1 || this.version > latestVersion) {
136080
136523
  throw new Error(`Version should be in range [1, ${latestVersion}].`);
136081
136524
  }
136525
+ this.hashFromStringGenerator = this.selectVersionOfGenerator(new generatorsMap.GenerateHashFromString[0]());
136082
136526
  // sorting table in order which they will be filled up (tables with foreign keys case)
136083
136527
  const { tablesInOutRelations } = this.getInfoFromRelations(relations);
136084
136528
  const orderedTablesNames = this.getOrderedTablesList(tablesInOutRelations);
@@ -136098,6 +136542,7 @@ class SeedService {
136098
136542
  withFromTable: {},
136099
136543
  }));
136100
136544
  for (const [i, table] of tables.entries()) {
136545
+ const compositeUniqueKeyGenMap = {};
136101
136546
  // get foreignKey columns relations
136102
136547
  const foreignKeyColumns = {};
136103
136548
  for (const rel of relations
@@ -136139,7 +136584,7 @@ class SeedService {
136139
136584
  const weightedRepeatedValuesCount = refinements[table.name]
136140
136585
  .with[fkTableName];
136141
136586
  weightedCountSeed = customSeed
136142
- + generateHashFromString(`${table.name}.${fkTableName}`);
136587
+ + this.hashFromStringGenerator.generate({ input: `${table.name}.${fkTableName}` });
136143
136588
  newTableWithCount = this.getWeightedWithCount(weightedRepeatedValuesCount, (tablesPossibleGenerators[i].withCount
136144
136589
  || tablesPossibleGenerators[i].count), weightedCountSeed);
136145
136590
  }
@@ -136175,6 +136620,18 @@ class SeedService {
136175
136620
  && refinements[table.name].columns !== undefined
136176
136621
  && refinements[table.name].columns[col.name] !== undefined) {
136177
136622
  const genObj = refinements[table.name].columns[col.name];
136623
+ if (genObj === false) {
136624
+ if (col.notNull === true && col.hasDefault === false) {
136625
+ throw new Error(`You cannot set the '${col.name}' column in the '${table.name}' table to false in your refinements.`
136626
+ + `\nDoing so will result in a null value being inserted into the '${col.name}' column,`
136627
+ + `\nwhich will cause an error because the column has a not null constraint and no default value.`);
136628
+ }
136629
+ // Generating undefined as a value for a column and then inserting it via drizzle-orm
136630
+ // will result in the value not being inserted into that column.
136631
+ columnPossibleGenerator.generator = new generatorsMap.GenerateDefault[0]({ defaultValue: undefined });
136632
+ columnPossibleGenerator.wasRefined = true;
136633
+ continue;
136634
+ }
136178
136635
  if (col.columnType.match(/\[\w*]/g) !== null) {
136179
136636
  if ((col.baseColumn?.dataType === 'array' && col.baseColumn.columnType.match(/\[\w*]/g) !== null)
136180
136637
  // studio case
@@ -136246,16 +136703,66 @@ class SeedService {
136246
136703
  columnPossibleGenerator.generator = arrayGen;
136247
136704
  }
136248
136705
  columnPossibleGenerator.generator.isUnique = col.isUnique;
136706
+ // composite unique keys handling
136707
+ let compositeKeyColumnNames = table.uniqueConstraints.filter((colNames) => colNames.includes(col.name));
136708
+ if (compositeKeyColumnNames.some((colNames) => colNames.length === 1)) {
136709
+ // composite unique key contains only one column, therefore it equals to just unique column
136710
+ columnPossibleGenerator.generator.isUnique = true;
136711
+ }
136712
+ // removing column from composite unique keys if current column is unique
136713
+ if (columnPossibleGenerator.generator.isUnique && compositeKeyColumnNames.length > 0) {
136714
+ const newUniqueConstraints = [];
136715
+ for (const colNames of table.uniqueConstraints) {
136716
+ if (colNames.includes(col.name)) {
136717
+ const newColNames = colNames.filter((colName) => colName !== col.name);
136718
+ if (newColNames.length === 0)
136719
+ continue;
136720
+ newUniqueConstraints.push(newColNames);
136721
+ }
136722
+ else {
136723
+ newUniqueConstraints.push(colNames);
136724
+ }
136725
+ }
136726
+ table.uniqueConstraints = newUniqueConstraints;
136727
+ }
136728
+ compositeKeyColumnNames = table.uniqueConstraints.filter((colNames) => colNames.includes(col.name));
136729
+ if (compositeKeyColumnNames.length > 1) {
136730
+ throw new Error('Currently, multiple composite unique keys that share the same column are not supported.');
136731
+ }
136732
+ // to handle composite unique key generation, I will need a unique generator for each column in the composite key
136733
+ if (compositeKeyColumnNames.length === 1) {
136734
+ if (columnPossibleGenerator.generator.params.isUnique === false) {
136735
+ throw new Error(`To handle the composite unique key on columns: ${compositeKeyColumnNames[0]}, `
136736
+ + `column: ${col.name} should either be assigned a generator with isUnique set to true, or have isUnique omitted.`);
136737
+ }
136738
+ columnPossibleGenerator.generator.params.isUnique = true;
136739
+ }
136249
136740
  const uniqueGen = columnPossibleGenerator.generator.replaceIfUnique();
136250
136741
  if (uniqueGen !== undefined) {
136251
136742
  columnPossibleGenerator.generator = uniqueGen;
136252
136743
  }
136744
+ if (compositeKeyColumnNames.length === 1 && !columnPossibleGenerator.generator.isGeneratorUnique
136745
+ && !(columnPossibleGenerator.generator.getEntityKind() === 'GenerateValuesFromArray')) {
136746
+ throw new Error(`To handle the composite unique key on columns: ${compositeKeyColumnNames[0]}, `
136747
+ + `column: ${col.name} should be assigned a generator with its own unique version.`);
136748
+ }
136253
136749
  // selecting version of generator
136254
136750
  columnPossibleGenerator.generator = this.selectVersionOfGenerator(columnPossibleGenerator.generator);
136255
136751
  // TODO: for now only GenerateValuesFromArray support notNull property
136256
136752
  columnPossibleGenerator.generator.notNull = col.notNull;
136257
136753
  columnPossibleGenerator.generator.dataType = col.dataType;
136258
- // columnPossibleGenerator.generator.stringLength = col.typeParams.length;
136754
+ // assigning composite key generator
136755
+ if (compositeKeyColumnNames.length === 1) {
136756
+ const key = compositeKeyColumnNames[0].join('_');
136757
+ if (compositeUniqueKeyGenMap[key] === undefined) {
136758
+ let compositeUniqueKeyGen = new generatorsMap.GenerateCompositeUniqueKey[0]();
136759
+ compositeUniqueKeyGen.uniqueKey = key;
136760
+ compositeUniqueKeyGen = this.selectVersionOfGenerator(compositeUniqueKeyGen);
136761
+ compositeUniqueKeyGenMap[key] = compositeUniqueKeyGen;
136762
+ }
136763
+ compositeUniqueKeyGenMap[key].addGenerator(col.name, columnPossibleGenerator.generator);
136764
+ columnPossibleGenerator.generator = compositeUniqueKeyGenMap[key];
136765
+ }
136259
136766
  tablePossibleGenerators.columnsPossibleGenerators.push(columnPossibleGenerator);
136260
136767
  }
136261
136768
  }
@@ -136284,6 +136791,7 @@ class SeedService {
136284
136791
  newGenerator.dataType = generator.dataType;
136285
136792
  // newGenerator.stringLength = generator.stringLength;
136286
136793
  newGenerator.typeParams = generator.typeParams ?? newGenerator.typeParams;
136794
+ newGenerator.uniqueKey = generator.uniqueKey;
136287
136795
  return newGenerator;
136288
136796
  };
136289
136797
  cyclicTablesCompare = (table1, table2, relation, reverseRelation) => {
@@ -136444,8 +136952,13 @@ class SeedService {
136444
136952
  const columnRelations = filteredRelations.filter((rel) => rel.columns.includes(col.columnName));
136445
136953
  pRNGSeed = (columnRelations.length !== 0
136446
136954
  && columnRelations[0].columns.length >= 2)
136447
- ? (customSeed + generateHashFromString(`${columnRelations[0].table}.${columnRelations[0].columns.join('_')}`))
136448
- : (customSeed + generateHashFromString(`${table.tableName}.${col.columnName}`));
136955
+ ? (customSeed
136956
+ + this.hashFromStringGenerator.generate({
136957
+ input: `${columnRelations[0].table}.${columnRelations[0].columns.join('_')}`,
136958
+ }))
136959
+ : col.generator?.uniqueKey === undefined
136960
+ ? (customSeed + this.hashFromStringGenerator.generate({ input: `${table.tableName}.${col.columnName}` }))
136961
+ : (customSeed + this.hashFromStringGenerator.generate({ input: col.generator.uniqueKey }));
136449
136962
  tableGenerators[col.columnName] = {
136450
136963
  pRNGSeed,
136451
136964
  ...col,
@@ -136467,7 +136980,9 @@ class SeedService {
136467
136980
  if (rel.table === rel.refTable
136468
136981
  && tableGenerators[rel.columns[colIdx]]?.wasRefined === false) {
136469
136982
  const refColName = rel.refColumns[colIdx];
136470
- pRNGSeed = generateHashFromString(`${table.tableName}.${refColName}`);
136983
+ pRNGSeed = this.hashFromStringGenerator.generate({
136984
+ input: `${table.tableName}.${refColName}`,
136985
+ });
136471
136986
  const refColumnGenerator = {};
136472
136987
  refColumnGenerator[refColName] = {
136473
136988
  ...tableGenerators[refColName],
@@ -136616,11 +137131,7 @@ class SeedService {
136616
137131
  row = {};
136617
137132
  generatedValues.push(row);
136618
137133
  for (const columnName of Object.keys(columnsGenerators)) {
136619
- // generatedValue = columnsGenerators[columnName].next().value as
136620
- // | string
136621
- // | number
136622
- // | boolean;
136623
- generatedValue = columnsGenerators[columnName].generate({ i });
137134
+ generatedValue = columnsGenerators[columnName].generate({ i, columnName });
136624
137135
  row[columnName] = generatedValue;
136625
137136
  }
136626
137137
  if ((insertDataInDb === true || updateDataInDb === true)
@@ -136767,7 +137278,7 @@ const filterCockroachSchema = (schema) => {
136767
137278
  const seedCockroach = async (db, schema, options = {}, refinements) => {
136768
137279
  const seedService = new SeedService();
136769
137280
  const { cockroachSchema, cockroachTables } = filterCockroachSchema(schema);
136770
- const { tables, relations } = getSchemaInfo(cockroachSchema, cockroachTables, mapCockroachTable);
137281
+ const { tables, relations } = getSchemaInfo(cockroachSchema, cockroachTables, mapCockroachColumns);
136771
137282
  const generatedTablesGenerators = seedService.generatePossibleGenerators('cockroach', tables, relations, refinements, options);
136772
137283
  const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
136773
137284
  const tablesValues = await seedService.generateTablesValues(relations, generatedTablesGenerators, db, cockroachTables, { ...options, preserveCyclicTablesData });
@@ -136775,7 +137286,7 @@ const seedCockroach = async (db, schema, options = {}, refinements) => {
136775
137286
  const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
136776
137287
  await seedService.generateTablesValues(relations, filteredTablesGenerators, db, cockroachTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
136777
137288
  };
136778
- const mapCockroachTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
137289
+ const mapCockroachColumns = (tableConfig, dbToTsColumnNamesMap) => {
136779
137290
  const getAllBaseColumns = (baseColumn) => {
136780
137291
  const baseColumnResult = {
136781
137292
  name: baseColumn.name,
@@ -136827,30 +137338,24 @@ const mapCockroachTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMa
136827
137338
  }
136828
137339
  return typeParams;
136829
137340
  };
136830
- // console.log(tableConfig.columns);
136831
- return {
136832
- name: dbToTsTableNamesMap[tableConfig.name],
136833
- columns: tableConfig.columns.map((column) => ({
136834
- name: dbToTsColumnNamesMap[column.name],
136835
- columnType: column.getSQLType(),
136836
- typeParams: getTypeParams(column.getSQLType()),
136837
- dataType: column.dataType.split(' ')[0],
136838
- size: column.length,
136839
- hasDefault: column.hasDefault,
136840
- default: column.default,
136841
- enumValues: column.enumValues,
136842
- isUnique: column.isUnique,
136843
- notNull: column.notNull,
136844
- primary: column.primary,
136845
- generatedIdentityType: column.generatedIdentity?.type,
136846
- baseColumn: (column.baseColumn === undefined)
136847
- ? undefined
136848
- : getAllBaseColumns(column.baseColumn),
136849
- })),
136850
- primaryKeys: tableConfig.columns
136851
- .filter((column) => column.primary)
136852
- .map((column) => dbToTsColumnNamesMap[column.name]),
136853
- };
137341
+ const mappedColumns = tableConfig.columns.map((column) => ({
137342
+ name: dbToTsColumnNamesMap[column.name],
137343
+ columnType: column.getSQLType(),
137344
+ typeParams: getTypeParams(column.getSQLType()),
137345
+ dataType: column.dataType.split(' ')[0],
137346
+ size: column.length,
137347
+ hasDefault: column.hasDefault,
137348
+ default: column.default,
137349
+ enumValues: column.enumValues,
137350
+ isUnique: column.isUnique,
137351
+ notNull: column.notNull,
137352
+ primary: column.primary,
137353
+ generatedIdentityType: column.generatedIdentity?.type,
137354
+ baseColumn: (column.baseColumn === undefined)
137355
+ ? undefined
137356
+ : getAllBaseColumns(column.baseColumn),
137357
+ }));
137358
+ return mappedColumns;
136854
137359
  };
136855
137360
 
136856
137361
  // MySql-----------------------------------------------------------------------------------------------------
@@ -136939,7 +137444,7 @@ const filterMsSqlTables = (schema) => {
136939
137444
  };
136940
137445
  const seedMsSql = async (db, schema, options = {}, refinements) => {
136941
137446
  const { mssqlSchema, mssqlTables } = filterMsSqlTables(schema);
136942
- const { tables, relations } = getSchemaInfo(mssqlSchema, mssqlTables, mapMsSqlTable);
137447
+ const { tables, relations } = getSchemaInfo(mssqlSchema, mssqlTables, mapMsSqlColumns);
136943
137448
  const seedService = new SeedService();
136944
137449
  const generatedTablesGenerators = seedService.generatePossibleGenerators('mssql', tables, relations, refinements, options);
136945
137450
  const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
@@ -136948,7 +137453,7 @@ const seedMsSql = async (db, schema, options = {}, refinements) => {
136948
137453
  const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
136949
137454
  await seedService.generateTablesValues(relations, filteredTablesGenerators, db, mssqlTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
136950
137455
  };
136951
- const mapMsSqlTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
137456
+ const mapMsSqlColumns = (tableConfig, dbToTsColumnNamesMap) => {
136952
137457
  // TODO: rewrite
136953
137458
  const getTypeParams = (sqlType) => {
136954
137459
  // get type params and set only type
@@ -136973,25 +137478,20 @@ const mapMsSqlTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) =
136973
137478
  }
136974
137479
  return typeParams;
136975
137480
  };
136976
- return {
136977
- name: dbToTsTableNamesMap[tableConfig.name],
136978
- columns: tableConfig.columns.map((column) => ({
136979
- name: dbToTsColumnNamesMap[column.name],
136980
- columnType: column.getSQLType(),
136981
- typeParams: getTypeParams(column.getSQLType()),
136982
- dataType: column.dataType.split(' ')[0],
136983
- hasDefault: column.hasDefault,
136984
- default: column.default,
136985
- enumValues: column.enumValues,
136986
- isUnique: column.isUnique,
136987
- notNull: column.notNull,
136988
- primary: column.primary,
136989
- identity: column.identity ? true : false,
136990
- })),
136991
- primaryKeys: tableConfig.columns
136992
- .filter((column) => column.primary)
136993
- .map((column) => dbToTsColumnNamesMap[column.name]),
136994
- };
137481
+ const mappedColumns = tableConfig.columns.map((column) => ({
137482
+ name: dbToTsColumnNamesMap[column.name],
137483
+ columnType: column.getSQLType(),
137484
+ typeParams: getTypeParams(column.getSQLType()),
137485
+ dataType: column.dataType.split(' ')[0],
137486
+ hasDefault: column.hasDefault,
137487
+ default: column.default,
137488
+ enumValues: column.enumValues,
137489
+ isUnique: column.isUnique,
137490
+ notNull: column.notNull,
137491
+ primary: column.primary,
137492
+ identity: column.identity ? true : false,
137493
+ }));
137494
+ return mappedColumns;
136995
137495
  };
136996
137496
 
136997
137497
  // MySql-----------------------------------------------------------------------------------------------------
@@ -137014,7 +137514,7 @@ const filterMysqlTables = (schema) => {
137014
137514
  };
137015
137515
  const seedMySql = async (db, schema, options = {}, refinements) => {
137016
137516
  const { mysqlSchema, mysqlTables } = filterMysqlTables(schema);
137017
- const { tables, relations } = getSchemaInfo(mysqlSchema, mysqlTables, mapMySqlTable);
137517
+ const { tables, relations } = getSchemaInfo(mysqlSchema, mysqlTables, mapMySqlColumns);
137018
137518
  const seedService = new SeedService();
137019
137519
  const generatedTablesGenerators = seedService.generatePossibleGenerators('mysql', tables, relations, refinements, options);
137020
137520
  const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
@@ -137023,7 +137523,7 @@ const seedMySql = async (db, schema, options = {}, refinements) => {
137023
137523
  const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
137024
137524
  await seedService.generateTablesValues(relations, filteredTablesGenerators, db, mysqlTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
137025
137525
  };
137026
- const mapMySqlTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
137526
+ const mapMySqlColumns = (tableConfig, dbToTsColumnNamesMap) => {
137027
137527
  const getTypeParams = (sqlType) => {
137028
137528
  // get type params and set only type
137029
137529
  const typeParams = {};
@@ -137048,24 +137548,19 @@ const mapMySqlTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) =
137048
137548
  }
137049
137549
  return typeParams;
137050
137550
  };
137051
- return {
137052
- name: dbToTsTableNamesMap[tableConfig.name],
137053
- columns: tableConfig.columns.map((column) => ({
137054
- name: dbToTsColumnNamesMap[column.name],
137055
- columnType: column.getSQLType(),
137056
- typeParams: getTypeParams(column.getSQLType()),
137057
- dataType: column.dataType.split(' ')[0],
137058
- hasDefault: column.hasDefault,
137059
- default: column.default,
137060
- enumValues: column.enumValues,
137061
- isUnique: column.isUnique,
137062
- notNull: column.notNull,
137063
- primary: column.primary,
137064
- })),
137065
- primaryKeys: tableConfig.columns
137066
- .filter((column) => column.primary)
137067
- .map((column) => dbToTsColumnNamesMap[column.name]),
137068
- };
137551
+ const mappedColumns = tableConfig.columns.map((column) => ({
137552
+ name: dbToTsColumnNamesMap[column.name],
137553
+ columnType: column.getSQLType(),
137554
+ typeParams: getTypeParams(column.getSQLType()),
137555
+ dataType: column.dataType.split(' ')[0],
137556
+ hasDefault: column.hasDefault,
137557
+ default: column.default,
137558
+ enumValues: column.enumValues,
137559
+ isUnique: column.isUnique,
137560
+ notNull: column.notNull,
137561
+ primary: column.primary,
137562
+ }));
137563
+ return mappedColumns;
137069
137564
  };
137070
137565
 
137071
137566
  // Postgres-----------------------------------------------------------------------------------------------------------
@@ -137085,7 +137580,7 @@ const filterPgSchema = (schema) => {
137085
137580
  const seedPostgres = async (db, schema, options = {}, refinements) => {
137086
137581
  const seedService = new SeedService();
137087
137582
  const { pgSchema, pgTables } = filterPgSchema(schema);
137088
- const { tables, relations } = getSchemaInfo(pgSchema, pgTables, mapPgTable);
137583
+ const { tables, relations } = getSchemaInfo(pgSchema, pgTables, mapPgColumns);
137089
137584
  // const { tables, relations } = getPostgresInfo(pgSchema, pgTables);
137090
137585
  const generatedTablesGenerators = seedService.generatePossibleGenerators('postgresql', tables, relations, refinements, options);
137091
137586
  const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
@@ -137094,7 +137589,7 @@ const seedPostgres = async (db, schema, options = {}, refinements) => {
137094
137589
  const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
137095
137590
  await seedService.generateTablesValues(relations, filteredTablesGenerators, db, pgTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
137096
137591
  };
137097
- const mapPgTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
137592
+ const mapPgColumns = (tableConfig, dbToTsColumnNamesMap) => {
137098
137593
  const getAllBaseColumns = (baseColumn) => {
137099
137594
  const baseColumnResult = {
137100
137595
  name: baseColumn.name,
@@ -137147,29 +137642,24 @@ const mapPgTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
137147
137642
  }
137148
137643
  return typeParams;
137149
137644
  };
137150
- return {
137151
- name: dbToTsTableNamesMap[tableConfig.name],
137152
- columns: tableConfig.columns.map((column) => ({
137153
- name: dbToTsColumnNamesMap[column.name],
137154
- columnType: column.getSQLType(),
137155
- typeParams: getTypeParams(column.getSQLType()),
137156
- dataType: column.dataType.split(' ')[0],
137157
- size: column.length,
137158
- hasDefault: column.hasDefault,
137159
- default: column.default,
137160
- enumValues: column.enumValues,
137161
- isUnique: column.isUnique,
137162
- notNull: column.notNull,
137163
- primary: column.primary,
137164
- generatedIdentityType: column.generatedIdentity?.type,
137165
- baseColumn: (column.baseColumn === undefined)
137166
- ? undefined
137167
- : getAllBaseColumns(column.baseColumn),
137168
- })),
137169
- primaryKeys: tableConfig.columns
137170
- .filter((column) => column.primary)
137171
- .map((column) => dbToTsColumnNamesMap[column.name]),
137172
- };
137645
+ const mappedColumns = tableConfig.columns.map((column) => ({
137646
+ name: dbToTsColumnNamesMap[column.name],
137647
+ columnType: column.getSQLType(),
137648
+ typeParams: getTypeParams(column.getSQLType()),
137649
+ dataType: column.dataType.split(' ')[0],
137650
+ size: column.length,
137651
+ hasDefault: column.hasDefault,
137652
+ default: column.default,
137653
+ enumValues: column.enumValues,
137654
+ isUnique: column.isUnique,
137655
+ notNull: column.notNull,
137656
+ primary: column.primary,
137657
+ generatedIdentityType: column.generatedIdentity?.type,
137658
+ baseColumn: (column.baseColumn === undefined)
137659
+ ? undefined
137660
+ : getAllBaseColumns(column.baseColumn),
137661
+ }));
137662
+ return mappedColumns;
137173
137663
  };
137174
137664
 
137175
137665
  // SingleStore-----------------------------------------------------------------------------------------------------
@@ -137192,7 +137682,7 @@ const filterSingleStoreTables = (schema) => {
137192
137682
  };
137193
137683
  const seedSingleStore = async (db, schema, options = {}, refinements) => {
137194
137684
  const { singleStoreSchema, singleStoreTables } = filterSingleStoreTables(schema);
137195
- const { tables, relations } = getSchemaInfo(singleStoreSchema, singleStoreTables, mapSingleStoreTable);
137685
+ const { tables, relations } = getSchemaInfo(singleStoreSchema, singleStoreTables, mapSingleStoreColumns);
137196
137686
  const seedService = new SeedService();
137197
137687
  const generatedTablesGenerators = seedService.generatePossibleGenerators('singlestore', tables, relations, refinements, options);
137198
137688
  const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
@@ -137201,7 +137691,7 @@ const seedSingleStore = async (db, schema, options = {}, refinements) => {
137201
137691
  const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
137202
137692
  await seedService.generateTablesValues(relations, filteredTablesGenerators, db, singleStoreTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
137203
137693
  };
137204
- const mapSingleStoreTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
137694
+ const mapSingleStoreColumns = (tableConfig, dbToTsColumnNamesMap) => {
137205
137695
  const getTypeParams = (sqlType) => {
137206
137696
  // get type params and set only type
137207
137697
  const typeParams = {};
@@ -137217,6 +137707,7 @@ const mapSingleStoreTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNames
137217
137707
  }
137218
137708
  else if (sqlType.startsWith('char')
137219
137709
  || sqlType.startsWith('varchar')
137710
+ || sqlType.startsWith('text')
137220
137711
  || sqlType.startsWith('binary')
137221
137712
  || sqlType.startsWith('varbinary')) {
137222
137713
  const match = sqlType.match(/\((\d+)\)/);
@@ -137233,24 +137724,19 @@ const mapSingleStoreTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNames
137233
137724
  }
137234
137725
  return typeParams;
137235
137726
  };
137236
- return {
137237
- name: dbToTsTableNamesMap[tableConfig.name],
137238
- columns: tableConfig.columns.map((column) => ({
137239
- name: dbToTsColumnNamesMap[column.name],
137240
- columnType: column.getSQLType(),
137241
- typeParams: getTypeParams(column.getSQLType()),
137242
- dataType: column.dataType.split(' ')[0],
137243
- hasDefault: column.hasDefault,
137244
- default: column.default,
137245
- enumValues: column.enumValues,
137246
- isUnique: column.isUnique,
137247
- notNull: column.notNull,
137248
- primary: column.primary,
137249
- })),
137250
- primaryKeys: tableConfig.columns
137251
- .filter((column) => column.primary)
137252
- .map((column) => dbToTsColumnNamesMap[column.name]),
137253
- };
137727
+ const mappedColumns = tableConfig.columns.map((column) => ({
137728
+ name: dbToTsColumnNamesMap[column.name],
137729
+ columnType: column.getSQLType(),
137730
+ typeParams: getTypeParams(column.getSQLType()),
137731
+ dataType: column.dataType.split(' ')[0],
137732
+ hasDefault: column.hasDefault,
137733
+ default: column.default,
137734
+ enumValues: column.enumValues,
137735
+ isUnique: column.isUnique,
137736
+ notNull: column.notNull,
137737
+ primary: column.primary,
137738
+ }));
137739
+ return mappedColumns;
137254
137740
  };
137255
137741
 
137256
137742
  // Sqlite------------------------------------------------------------------------------------------------------------------------
@@ -137273,7 +137759,7 @@ const filterSqliteTables = (schema) => {
137273
137759
  };
137274
137760
  const seedSqlite = async (db, schema, options = {}, refinements) => {
137275
137761
  const { sqliteSchema, sqliteTables } = filterSqliteTables(schema);
137276
- const { tables, relations } = getSchemaInfo(sqliteSchema, sqliteTables, mapSqliteTable);
137762
+ const { tables, relations } = getSchemaInfo(sqliteSchema, sqliteTables, mapSqliteColumns);
137277
137763
  const seedService = new SeedService();
137278
137764
  const generatedTablesGenerators = seedService.generatePossibleGenerators('sqlite', tables, relations, refinements, options);
137279
137765
  const preserveCyclicTablesData = relations.some((rel) => rel.isCyclic === true);
@@ -137282,7 +137768,7 @@ const seedSqlite = async (db, schema, options = {}, refinements) => {
137282
137768
  const updateDataInDb = filteredTablesGenerators.length === 0 ? false : true;
137283
137769
  await seedService.generateTablesValues(relations, filteredTablesGenerators, db, sqliteTables, { ...options, tablesValues, updateDataInDb, tablesUniqueNotNullColumn });
137284
137770
  };
137285
- const mapSqliteTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap) => {
137771
+ const mapSqliteColumns = (tableConfig, dbToTsColumnNamesMap) => {
137286
137772
  const getTypeParams = (sqlType) => {
137287
137773
  // get type params and set only type
137288
137774
  const typeParams = {};
@@ -137303,24 +137789,19 @@ const mapSqliteTable = (tableConfig, dbToTsTableNamesMap, dbToTsColumnNamesMap)
137303
137789
  }
137304
137790
  return typeParams;
137305
137791
  };
137306
- return {
137307
- name: dbToTsTableNamesMap[tableConfig.name],
137308
- columns: tableConfig.columns.map((column) => ({
137309
- name: dbToTsColumnNamesMap[column.name],
137310
- columnType: column.getSQLType(),
137311
- typeParams: getTypeParams(column.getSQLType()),
137312
- dataType: column.dataType.split(' ')[0],
137313
- hasDefault: column.hasDefault,
137314
- default: column.default,
137315
- enumValues: column.enumValues,
137316
- isUnique: column.isUnique,
137317
- notNull: column.notNull,
137318
- primary: column.primary,
137319
- })),
137320
- primaryKeys: tableConfig.columns
137321
- .filter((column) => column.primary)
137322
- .map((column) => dbToTsColumnNamesMap[column.name]),
137323
- };
137792
+ const mappedColumns = tableConfig.columns.map((column) => ({
137793
+ name: dbToTsColumnNamesMap[column.name],
137794
+ columnType: column.getSQLType(),
137795
+ typeParams: getTypeParams(column.getSQLType()),
137796
+ dataType: column.dataType.split(' ')[0],
137797
+ hasDefault: column.hasDefault,
137798
+ default: column.default,
137799
+ enumValues: column.enumValues,
137800
+ isUnique: column.isUnique,
137801
+ notNull: column.notNull,
137802
+ primary: column.primary,
137803
+ }));
137804
+ return mappedColumns;
137324
137805
  };
137325
137806
 
137326
137807
  /* eslint-disable drizzle-internal/require-entity-kind */
@@ -137384,6 +137865,7 @@ async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleSt
137384
137865
  name: tableName,
137385
137866
  columns,
137386
137867
  primaryKeys: drizzleStudioColumns.filter((col) => col.primaryKey === true).map((col) => col.name),
137868
+ uniqueConstraints: [], // TODO change later
137387
137869
  });
137388
137870
  }
137389
137871
  relations = drizzleStudioRelations.filter((rel) => rel.schema === schemaName && rel.refSchema === schemaName);