drizzle-seed 0.3.2-97009ae → 0.3.2-b5a9650
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/index.cjs +270 -51
- package/index.cjs.map +1 -1
- package/index.d.cts +20 -2
- package/index.d.mts +20 -2
- package/index.d.ts +20 -2
- package/index.mjs +272 -54
- package/index.mjs.map +1 -1
- package/package.json +6 -6
- package/services/GeneratorFuncs.d.cts +30 -6
- package/services/GeneratorFuncs.d.mts +30 -6
- package/services/GeneratorFuncs.d.ts +30 -6
- package/services/Generators.d.cts +27 -0
- package/services/Generators.d.mts +27 -0
- package/services/Generators.d.ts +27 -0
- package/services/SeedService.d.cts +9 -0
- package/services/SeedService.d.mts +9 -0
- package/services/SeedService.d.ts +9 -0
- package/services/utils.d.cts +5 -0
- package/services/utils.d.mts +5 -0
- package/services/utils.d.ts +5 -0
- package/types/seedService.d.cts +1 -1
- package/types/seedService.d.mts +1 -1
- package/types/seedService.d.ts +1 -1
package/index.cjs
CHANGED
|
@@ -131216,6 +131216,20 @@ const isObject = (value) => {
|
|
|
131216
131216
|
const equalSets = (set1, set2) => {
|
|
131217
131217
|
return set1.size === set2.size && [...set1].every((si) => set2.has(si));
|
|
131218
131218
|
};
|
|
131219
|
+
const isValidDate = (date) => {
|
|
131220
|
+
return !Number.isNaN(date.getTime());
|
|
131221
|
+
};
|
|
131222
|
+
const intMax = (args) => args.reduce((m, e) => e > m ? e : m);
|
|
131223
|
+
const isPostgresColumnIntLike = (column) => {
|
|
131224
|
+
return drizzleOrm.is(column, pgCore.PgSmallInt)
|
|
131225
|
+
|| drizzleOrm.is(column, pgCore.PgInteger)
|
|
131226
|
+
|| drizzleOrm.is(column, pgCore.PgBigInt53)
|
|
131227
|
+
|| drizzleOrm.is(column, pgCore.PgBigInt64)
|
|
131228
|
+
|| drizzleOrm.is(column, pgCore.PgSmallSerial)
|
|
131229
|
+
|| drizzleOrm.is(column, pgCore.PgSerial)
|
|
131230
|
+
|| drizzleOrm.is(column, pgCore.PgBigSerial53)
|
|
131231
|
+
|| drizzleOrm.is(column, pgCore.PgBigSerial64);
|
|
131232
|
+
};
|
|
131219
131233
|
|
|
131220
131234
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
131221
131235
|
class AbstractGenerator {
|
|
@@ -131787,13 +131801,20 @@ class GenerateDate extends AbstractGenerator {
|
|
|
131787
131801
|
const rng = prand.xoroshiro128plus(seed);
|
|
131788
131802
|
let { minDate, maxDate } = this.params;
|
|
131789
131803
|
const anchorDate = new Date('2024-05-08');
|
|
131804
|
+
// 4 years in milliseconds
|
|
131790
131805
|
const deltaMilliseconds = 4 * 31536000000;
|
|
131791
131806
|
if (typeof minDate === 'string') {
|
|
131792
131807
|
minDate = new Date(minDate);
|
|
131793
131808
|
}
|
|
131809
|
+
if (typeof minDate === 'object' && !isValidDate(minDate)) {
|
|
131810
|
+
throw new Error('Invalid Date was provided for the minDate parameter.');
|
|
131811
|
+
}
|
|
131794
131812
|
if (typeof maxDate === 'string') {
|
|
131795
131813
|
maxDate = new Date(maxDate);
|
|
131796
131814
|
}
|
|
131815
|
+
if (typeof maxDate === 'object' && !isValidDate(maxDate)) {
|
|
131816
|
+
throw new Error('Invalid Date was provided for the maxDate parameter.');
|
|
131817
|
+
}
|
|
131797
131818
|
if (minDate === undefined) {
|
|
131798
131819
|
if (maxDate === undefined) {
|
|
131799
131820
|
minDate = new Date(anchorDate.getTime() - deltaMilliseconds);
|
|
@@ -131806,6 +131827,9 @@ class GenerateDate extends AbstractGenerator {
|
|
|
131806
131827
|
if (maxDate === undefined) {
|
|
131807
131828
|
maxDate = new Date(minDate.getTime() + (2 * deltaMilliseconds));
|
|
131808
131829
|
}
|
|
131830
|
+
if (minDate > maxDate) {
|
|
131831
|
+
throw new Error(`The minDate parameter must be less than or equal to the maxDate parameter.`);
|
|
131832
|
+
}
|
|
131809
131833
|
this.state = { rng, minDate, maxDate };
|
|
131810
131834
|
}
|
|
131811
131835
|
generate() {
|
|
@@ -131827,18 +131851,83 @@ class GenerateTime extends AbstractGenerator {
|
|
|
131827
131851
|
init({ count, seed }) {
|
|
131828
131852
|
super.init({ count, seed });
|
|
131829
131853
|
const rng = prand.xoroshiro128plus(seed);
|
|
131830
|
-
|
|
131854
|
+
let { minTime, maxTime } = this.params;
|
|
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 };
|
|
131831
131923
|
}
|
|
131832
131924
|
generate() {
|
|
131833
131925
|
if (this.state === undefined) {
|
|
131834
131926
|
throw new Error('state is not defined.');
|
|
131835
131927
|
}
|
|
131836
|
-
const anchorDateTime = new Date('2024-05-08T12:00:00.000Z');
|
|
131837
|
-
const oneDayInMilliseconds = 86400000;
|
|
131838
|
-
let date = new Date();
|
|
131839
131928
|
let milliseconds;
|
|
131840
|
-
[milliseconds, this.state.rng] = prand.uniformIntDistribution(
|
|
131841
|
-
date = new Date(
|
|
131929
|
+
[milliseconds, this.state.rng] = prand.uniformIntDistribution(this.state.minTime.getTime(), this.state.maxTime.getTime(), this.state.rng);
|
|
131930
|
+
const date = new Date(milliseconds);
|
|
131842
131931
|
return date.toISOString().replace(/(\d{4}-\d{2}-\d{2}T)|(\.\d{3}Z)/g, '');
|
|
131843
131932
|
}
|
|
131844
131933
|
}
|
|
@@ -131848,18 +131937,46 @@ class GenerateTimestamp extends AbstractGenerator {
|
|
|
131848
131937
|
init({ count, seed }) {
|
|
131849
131938
|
super.init({ count, seed });
|
|
131850
131939
|
const rng = prand.xoroshiro128plus(seed);
|
|
131851
|
-
|
|
131940
|
+
let { minTimestamp, maxTimestamp } = this.params;
|
|
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 };
|
|
131852
131972
|
}
|
|
131853
131973
|
generate() {
|
|
131854
131974
|
if (this.state === undefined) {
|
|
131855
131975
|
throw new Error('state is not defined.');
|
|
131856
131976
|
}
|
|
131857
|
-
const anchorTimestamp = new Date('2024-05-08');
|
|
131858
|
-
const twoYearsInMilliseconds = 2 * 31536000000;
|
|
131859
|
-
let date = new Date();
|
|
131860
131977
|
let milliseconds;
|
|
131861
|
-
[milliseconds, this.state.rng] = prand.uniformIntDistribution(
|
|
131862
|
-
date = new Date(
|
|
131978
|
+
[milliseconds, this.state.rng] = prand.uniformIntDistribution(this.state.minTimestamp.getTime(), this.state.maxTimestamp.getTime(), this.state.rng);
|
|
131979
|
+
const date = new Date(milliseconds);
|
|
131863
131980
|
if (this.dataType === 'string') {
|
|
131864
131981
|
return date
|
|
131865
131982
|
.toISOString()
|
|
@@ -131875,18 +131992,46 @@ class GenerateDatetime extends AbstractGenerator {
|
|
|
131875
131992
|
init({ count, seed }) {
|
|
131876
131993
|
super.init({ count, seed });
|
|
131877
131994
|
const rng = prand.xoroshiro128plus(seed);
|
|
131878
|
-
|
|
131995
|
+
let { minDatetime, maxDatetime } = this.params;
|
|
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 };
|
|
131879
132027
|
}
|
|
131880
132028
|
generate() {
|
|
131881
132029
|
if (this.state === undefined) {
|
|
131882
132030
|
throw new Error('state is not defined.');
|
|
131883
132031
|
}
|
|
131884
|
-
const anchorDate = new Date('2024-05-08');
|
|
131885
|
-
const twoYearsInMilliseconds = 2 * 31536000000;
|
|
131886
|
-
let date = new Date();
|
|
131887
132032
|
let milliseconds;
|
|
131888
|
-
[milliseconds, this.state.rng] = prand.uniformIntDistribution(
|
|
131889
|
-
date = new Date(
|
|
132033
|
+
[milliseconds, this.state.rng] = prand.uniformIntDistribution(this.state.minDatetime.getTime(), this.state.maxDatetime.getTime(), this.state.rng);
|
|
132034
|
+
const date = new Date(milliseconds);
|
|
131890
132035
|
if (this.dataType === 'string') {
|
|
131891
132036
|
return date
|
|
131892
132037
|
.toISOString()
|
|
@@ -133559,6 +133704,8 @@ const generatorsFuncs = {
|
|
|
133559
133704
|
date: createGenerator(GenerateDate),
|
|
133560
133705
|
/**
|
|
133561
133706
|
* generates time in 24 hours style.
|
|
133707
|
+
* @param minTime - lower border of range.
|
|
133708
|
+
* @param maxTime - upper border of range.
|
|
133562
133709
|
* @param arraySize - number of elements in each one-dimensional array. (If specified, arrays will be generated.)
|
|
133563
133710
|
*
|
|
133564
133711
|
* @example
|
|
@@ -133566,7 +133713,7 @@ const generatorsFuncs = {
|
|
|
133566
133713
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
133567
133714
|
* users: {
|
|
133568
133715
|
* columns: {
|
|
133569
|
-
* birthTime: funcs.time()
|
|
133716
|
+
* birthTime: funcs.time({ minTime: "11:12:13.141", maxTime: "15:16:17.181" })
|
|
133570
133717
|
* },
|
|
133571
133718
|
* },
|
|
133572
133719
|
* }));
|
|
@@ -133576,6 +133723,8 @@ const generatorsFuncs = {
|
|
|
133576
133723
|
time: createGenerator(GenerateTime),
|
|
133577
133724
|
/**
|
|
133578
133725
|
* generates timestamps.
|
|
133726
|
+
* @param minTimestamp - lower border of range.
|
|
133727
|
+
* @param maxTimestamp - upper border of range.
|
|
133579
133728
|
* @param arraySize - number of elements in each one-dimensional array. (If specified, arrays will be generated.)
|
|
133580
133729
|
*
|
|
133581
133730
|
* @example
|
|
@@ -133583,7 +133732,7 @@ const generatorsFuncs = {
|
|
|
133583
133732
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
133584
133733
|
* orders: {
|
|
133585
133734
|
* columns: {
|
|
133586
|
-
* shippedDate: funcs.timestamp()
|
|
133735
|
+
* shippedDate: funcs.timestamp({ minTimestamp: "2025-03-07T11:12:13.141", maxTimestamp: "2025-03-08T15:16:17.181" })
|
|
133587
133736
|
* },
|
|
133588
133737
|
* },
|
|
133589
133738
|
* }));
|
|
@@ -133593,6 +133742,8 @@ const generatorsFuncs = {
|
|
|
133593
133742
|
timestamp: createGenerator(GenerateTimestamp),
|
|
133594
133743
|
/**
|
|
133595
133744
|
* generates datetime objects.
|
|
133745
|
+
* @param minDatetime - lower border of range.
|
|
133746
|
+
* @param maxDatetime - upper border of range.
|
|
133596
133747
|
* @param arraySize - number of elements in each one-dimensional array. (If specified, arrays will be generated.)
|
|
133597
133748
|
*
|
|
133598
133749
|
* @example
|
|
@@ -133600,7 +133751,7 @@ const generatorsFuncs = {
|
|
|
133600
133751
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
133601
133752
|
* orders: {
|
|
133602
133753
|
* columns: {
|
|
133603
|
-
* shippedDate: funcs.datetime()
|
|
133754
|
+
* shippedDate: funcs.datetime({ minDatetime: "2025-03-07T11:12:13.141", maxDatetime: "2025-03-08T15:16:17.181" })
|
|
133604
133755
|
* },
|
|
133605
133756
|
* },
|
|
133606
133757
|
* }));
|
|
@@ -134061,7 +134212,7 @@ const generatorsFuncs = {
|
|
|
134061
134212
|
*/
|
|
134062
134213
|
weightedRandom: createGenerator(WeightedRandomGenerator),
|
|
134063
134214
|
};
|
|
134064
|
-
// so far, version changes don’t
|
|
134215
|
+
// so far, version changes don’t change generator parameters.
|
|
134065
134216
|
const generatorsFuncsV2 = {
|
|
134066
134217
|
...generatorsFuncs,
|
|
134067
134218
|
};
|
|
@@ -134272,6 +134423,7 @@ class SeedService {
|
|
|
134272
134423
|
};
|
|
134273
134424
|
}
|
|
134274
134425
|
}
|
|
134426
|
+
// handling refinements (count, with)
|
|
134275
134427
|
if (refinements !== undefined && refinements[table.name] !== undefined) {
|
|
134276
134428
|
if (refinements[table.name].count !== undefined) {
|
|
134277
134429
|
tablesPossibleGenerators[i].count = refinements[table.name].count;
|
|
@@ -134332,11 +134484,24 @@ class SeedService {
|
|
|
134332
134484
|
wasDefinedBefore: false,
|
|
134333
134485
|
wasRefined: false,
|
|
134334
134486
|
};
|
|
134487
|
+
// handling refinements (columnGenerator)
|
|
134335
134488
|
if (refinements !== undefined
|
|
134336
134489
|
&& refinements[table.name] !== undefined
|
|
134337
134490
|
&& refinements[table.name].columns !== undefined
|
|
134338
134491
|
&& refinements[table.name].columns[col.name] !== undefined) {
|
|
134339
134492
|
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
|
+
}
|
|
134340
134505
|
if (col.columnType.match(/\[\w*]/g) !== null) {
|
|
134341
134506
|
if ((col.baseColumn?.dataType === 'array' && col.baseColumn.columnType.match(/\[\w*]/g) !== null)
|
|
134342
134507
|
// studio case
|
|
@@ -134648,8 +134813,8 @@ class SeedService {
|
|
|
134648
134813
|
// NUMBER(real, double, decimal, numeric)
|
|
134649
134814
|
if (col.columnType === 'real'
|
|
134650
134815
|
|| col.columnType === 'double precision'
|
|
134651
|
-
|| col.columnType.match(/^decimal(\(\d
|
|
134652
|
-
|| col.columnType.match(/^numeric(\(\d
|
|
134816
|
+
|| col.columnType.match(/^decimal(\(\d{1,6}(, ?-?\d{0,5})?\))?$/) !== null
|
|
134817
|
+
|| col.columnType.match(/^numeric(\(\d{1,6}(, ?-?\d{0,5})?\))?$/) !== null) {
|
|
134653
134818
|
if (col.typeParams.precision !== undefined) {
|
|
134654
134819
|
const precision = col.typeParams.precision;
|
|
134655
134820
|
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
@@ -134825,8 +134990,8 @@ class SeedService {
|
|
|
134825
134990
|
if (col.columnType === 'real'
|
|
134826
134991
|
|| col.columnType === 'double'
|
|
134827
134992
|
|| col.columnType === 'float'
|
|
134828
|
-
|| col.columnType.
|
|
134829
|
-
|| col.columnType.
|
|
134993
|
+
|| col.columnType.startsWith('decimal')
|
|
134994
|
+
|| col.columnType.startsWith('numeric')) {
|
|
134830
134995
|
if (col.typeParams.precision !== undefined) {
|
|
134831
134996
|
const precision = col.typeParams.precision;
|
|
134832
134997
|
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
@@ -134844,40 +135009,40 @@ class SeedService {
|
|
|
134844
135009
|
// STRING
|
|
134845
135010
|
if ((col.columnType === 'text'
|
|
134846
135011
|
|| col.columnType === 'blob'
|
|
134847
|
-
|| col.columnType.
|
|
134848
|
-
|| col.columnType.
|
|
134849
|
-
|| col.columnType.
|
|
134850
|
-
|| col.columnType.
|
|
135012
|
+
|| col.columnType.startsWith('char')
|
|
135013
|
+
|| col.columnType.startsWith('varchar')
|
|
135014
|
+
|| col.columnType.startsWith('binary')
|
|
135015
|
+
|| col.columnType.startsWith('varbinary'))
|
|
134851
135016
|
&& table.primaryKeys.includes(col.name)) {
|
|
134852
135017
|
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
134853
135018
|
return generator;
|
|
134854
135019
|
}
|
|
134855
135020
|
if ((col.columnType === 'text'
|
|
134856
135021
|
|| col.columnType === 'blob'
|
|
134857
|
-
|| col.columnType.
|
|
134858
|
-
|| col.columnType.
|
|
134859
|
-
|| col.columnType.
|
|
134860
|
-
|| col.columnType.
|
|
135022
|
+
|| col.columnType.startsWith('char')
|
|
135023
|
+
|| col.columnType.startsWith('varchar')
|
|
135024
|
+
|| col.columnType.startsWith('binary')
|
|
135025
|
+
|| col.columnType.startsWith('varbinary'))
|
|
134861
135026
|
&& col.name.toLowerCase().includes('name')) {
|
|
134862
135027
|
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
134863
135028
|
return generator;
|
|
134864
135029
|
}
|
|
134865
135030
|
if ((col.columnType === 'text'
|
|
134866
135031
|
|| col.columnType === 'blob'
|
|
134867
|
-
|| col.columnType.
|
|
134868
|
-
|| col.columnType.
|
|
134869
|
-
|| col.columnType.
|
|
134870
|
-
|| col.columnType.
|
|
135032
|
+
|| col.columnType.startsWith('char')
|
|
135033
|
+
|| col.columnType.startsWith('varchar')
|
|
135034
|
+
|| col.columnType.startsWith('binary')
|
|
135035
|
+
|| col.columnType.startsWith('varbinary'))
|
|
134871
135036
|
&& col.name.toLowerCase().includes('email')) {
|
|
134872
135037
|
const generator = new generatorsMap.GenerateEmail[0]();
|
|
134873
135038
|
return generator;
|
|
134874
135039
|
}
|
|
134875
135040
|
if (col.columnType === 'text'
|
|
134876
135041
|
|| col.columnType === 'blob'
|
|
134877
|
-
|| col.columnType.
|
|
134878
|
-
|| col.columnType.
|
|
134879
|
-
|| col.columnType.
|
|
134880
|
-
|| col.columnType.
|
|
135042
|
+
|| col.columnType.startsWith('char')
|
|
135043
|
+
|| col.columnType.startsWith('varchar')
|
|
135044
|
+
|| col.columnType.startsWith('binary')
|
|
135045
|
+
|| col.columnType.startsWith('varbinary')) {
|
|
134881
135046
|
const generator = new generatorsMap.GenerateString[0]();
|
|
134882
135047
|
return generator;
|
|
134883
135048
|
}
|
|
@@ -134887,7 +135052,7 @@ class SeedService {
|
|
|
134887
135052
|
return generator;
|
|
134888
135053
|
}
|
|
134889
135054
|
// DATE, TIME, TIMESTAMP, DATETIME, YEAR
|
|
134890
|
-
if (col.columnType.
|
|
135055
|
+
if (col.columnType.startsWith('datetime')) {
|
|
134891
135056
|
const generator = new generatorsMap.GenerateDatetime[0]();
|
|
134892
135057
|
return generator;
|
|
134893
135058
|
}
|
|
@@ -134899,7 +135064,7 @@ class SeedService {
|
|
|
134899
135064
|
const generator = new generatorsMap.GenerateTime[0]();
|
|
134900
135065
|
return generator;
|
|
134901
135066
|
}
|
|
134902
|
-
if (col.columnType.
|
|
135067
|
+
if (col.columnType.startsWith('timestamp')) {
|
|
134903
135068
|
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
134904
135069
|
return generator;
|
|
134905
135070
|
}
|
|
@@ -134952,8 +135117,8 @@ class SeedService {
|
|
|
134952
135117
|
return generator;
|
|
134953
135118
|
}
|
|
134954
135119
|
// number section ------------------------------------------------------------------------------------
|
|
134955
|
-
if (col.columnType.
|
|
134956
|
-
|| col.columnType.
|
|
135120
|
+
if (col.columnType.startsWith('real')
|
|
135121
|
+
|| col.columnType.startsWith('numeric')) {
|
|
134957
135122
|
if (col.typeParams.precision !== undefined) {
|
|
134958
135123
|
const precision = col.typeParams.precision;
|
|
134959
135124
|
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
@@ -135070,7 +135235,8 @@ class SeedService {
|
|
|
135070
135235
|
};
|
|
135071
135236
|
}
|
|
135072
135237
|
// get values to generate columns with foreign key
|
|
135073
|
-
// if table posts contains foreign key to table users, then rel.table === 'posts' and rel.refTable === 'users',
|
|
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.
|
|
135074
135240
|
if (filteredRelations.length !== 0) {
|
|
135075
135241
|
for (const rel of filteredRelations) {
|
|
135076
135242
|
if (table.withFromTable[rel.refTable] !== undefined
|
|
@@ -135187,9 +135353,6 @@ class SeedService {
|
|
|
135187
135353
|
columnsNumber += 1;
|
|
135188
135354
|
columnGenerator = tableGenerators[columnName];
|
|
135189
135355
|
override = tableGenerators[columnName]?.generatedIdentityType === 'always' ? true : override;
|
|
135190
|
-
if (tableName === 'evaluations' && columnName === 'gameId') {
|
|
135191
|
-
console.log();
|
|
135192
|
-
}
|
|
135193
135356
|
columnsGenerators[columnName] = columnGenerator.generator;
|
|
135194
135357
|
columnsGenerators[columnName].init({
|
|
135195
135358
|
count,
|
|
@@ -135204,6 +135367,25 @@ class SeedService {
|
|
|
135204
135367
|
// columnsGenerators[columnName] = uniqueGen;
|
|
135205
135368
|
// }
|
|
135206
135369
|
}
|
|
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
|
+
}
|
|
135207
135389
|
let maxParametersNumber;
|
|
135208
135390
|
if (drizzleOrm.is(db, (pgCore.PgDatabase))) {
|
|
135209
135391
|
// @ts-ignore
|
|
@@ -135235,6 +135417,12 @@ class SeedService {
|
|
|
135235
135417
|
// | boolean;
|
|
135236
135418
|
generatedValue = columnsGenerators[columnName].generate({ i });
|
|
135237
135419
|
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
|
+
}
|
|
135238
135426
|
}
|
|
135239
135427
|
if ((insertDataInDb === true || updateDataInDb === true)
|
|
135240
135428
|
&& ((i + 1) % batchSize === 0 || i === count - 1)) {
|
|
@@ -135281,9 +135469,28 @@ class SeedService {
|
|
|
135281
135469
|
}
|
|
135282
135470
|
}
|
|
135283
135471
|
}
|
|
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
|
+
}
|
|
135284
135481
|
}
|
|
135285
135482
|
return preserveData === true ? generatedValues : [];
|
|
135286
135483
|
};
|
|
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
|
+
};
|
|
135287
135494
|
insertInDb = async ({ generatedValues, db, schema, tableName, override, }) => {
|
|
135288
135495
|
if (drizzleOrm.is(db, (pgCore.PgDatabase))) {
|
|
135289
135496
|
const query = db.insert(schema[tableName]);
|
|
@@ -135422,7 +135629,7 @@ async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleSt
|
|
|
135422
135629
|
* // seeding with count and seed specified
|
|
135423
135630
|
* await seed(db, schema, { count: 100000, seed: 1 });
|
|
135424
135631
|
*
|
|
135425
|
-
* //seeding using refine
|
|
135632
|
+
* // seeding using refine
|
|
135426
135633
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
135427
135634
|
* users: {
|
|
135428
135635
|
* columns: {
|
|
@@ -135443,6 +135650,17 @@ async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleSt
|
|
|
135443
135650
|
* },
|
|
135444
135651
|
* }));
|
|
135445
135652
|
*
|
|
135653
|
+
* // seeding while ignoring column
|
|
135654
|
+
* await seed(db, schema).refine((funcs) => ({
|
|
135655
|
+
* users: {
|
|
135656
|
+
* count: 5,
|
|
135657
|
+
* columns: {
|
|
135658
|
+
* name: funcs.fullName(),
|
|
135659
|
+
* photo: false, // the photo column will not be seeded, allowing the database to use its default value.
|
|
135660
|
+
* },
|
|
135661
|
+
* },
|
|
135662
|
+
* }));
|
|
135663
|
+
*
|
|
135446
135664
|
* ```
|
|
135447
135665
|
*/
|
|
135448
135666
|
function seed(db, schema, options) {
|
|
@@ -136148,6 +136366,7 @@ const getSqliteInfo = (sqliteSchema, sqliteTables) => {
|
|
|
136148
136366
|
return { tables, relations: isCyclicRelations, tableRelations };
|
|
136149
136367
|
};
|
|
136150
136368
|
|
|
136369
|
+
exports.AbstractGenerator = AbstractGenerator;
|
|
136151
136370
|
exports.SeedService = SeedService;
|
|
136152
136371
|
exports.cities = cityNames;
|
|
136153
136372
|
exports.countries = countries;
|