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.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { is, entityKind,
|
|
1
|
+
import { is, entityKind, sql, eq, Relations, getTableName, extractTablesRelationalConfig, createTableRelationsHelpers, One } from 'drizzle-orm';
|
|
2
2
|
import { MySqlDatabase, MySqlTable, getTableConfig as getTableConfig$1 } from 'drizzle-orm/mysql-core';
|
|
3
|
-
import { PgDatabase, getTableConfig, PgTable } from 'drizzle-orm/pg-core';
|
|
3
|
+
import { PgSmallInt, PgInteger, PgBigInt53, PgBigInt64, PgSmallSerial, PgSerial, PgBigSerial53, PgBigSerial64, PgDatabase, getTableConfig, PgTable } from 'drizzle-orm/pg-core';
|
|
4
4
|
import { BaseSQLiteDatabase, SQLiteTable, getTableConfig as getTableConfig$2 } from 'drizzle-orm/sqlite-core';
|
|
5
5
|
import prand from 'pure-rand';
|
|
6
6
|
|
|
@@ -131214,6 +131214,20 @@ const isObject = (value) => {
|
|
|
131214
131214
|
const equalSets = (set1, set2) => {
|
|
131215
131215
|
return set1.size === set2.size && [...set1].every((si) => set2.has(si));
|
|
131216
131216
|
};
|
|
131217
|
+
const isValidDate = (date) => {
|
|
131218
|
+
return !Number.isNaN(date.getTime());
|
|
131219
|
+
};
|
|
131220
|
+
const intMax = (args) => args.reduce((m, e) => e > m ? e : m);
|
|
131221
|
+
const isPostgresColumnIntLike = (column) => {
|
|
131222
|
+
return is(column, PgSmallInt)
|
|
131223
|
+
|| is(column, PgInteger)
|
|
131224
|
+
|| is(column, PgBigInt53)
|
|
131225
|
+
|| is(column, PgBigInt64)
|
|
131226
|
+
|| is(column, PgSmallSerial)
|
|
131227
|
+
|| is(column, PgSerial)
|
|
131228
|
+
|| is(column, PgBigSerial53)
|
|
131229
|
+
|| is(column, PgBigSerial64);
|
|
131230
|
+
};
|
|
131217
131231
|
|
|
131218
131232
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
131219
131233
|
class AbstractGenerator {
|
|
@@ -131785,13 +131799,20 @@ class GenerateDate extends AbstractGenerator {
|
|
|
131785
131799
|
const rng = prand.xoroshiro128plus(seed);
|
|
131786
131800
|
let { minDate, maxDate } = this.params;
|
|
131787
131801
|
const anchorDate = new Date('2024-05-08');
|
|
131802
|
+
// 4 years in milliseconds
|
|
131788
131803
|
const deltaMilliseconds = 4 * 31536000000;
|
|
131789
131804
|
if (typeof minDate === 'string') {
|
|
131790
131805
|
minDate = new Date(minDate);
|
|
131791
131806
|
}
|
|
131807
|
+
if (typeof minDate === 'object' && !isValidDate(minDate)) {
|
|
131808
|
+
throw new Error('Invalid Date was provided for the minDate parameter.');
|
|
131809
|
+
}
|
|
131792
131810
|
if (typeof maxDate === 'string') {
|
|
131793
131811
|
maxDate = new Date(maxDate);
|
|
131794
131812
|
}
|
|
131813
|
+
if (typeof maxDate === 'object' && !isValidDate(maxDate)) {
|
|
131814
|
+
throw new Error('Invalid Date was provided for the maxDate parameter.');
|
|
131815
|
+
}
|
|
131795
131816
|
if (minDate === undefined) {
|
|
131796
131817
|
if (maxDate === undefined) {
|
|
131797
131818
|
minDate = new Date(anchorDate.getTime() - deltaMilliseconds);
|
|
@@ -131804,6 +131825,9 @@ class GenerateDate extends AbstractGenerator {
|
|
|
131804
131825
|
if (maxDate === undefined) {
|
|
131805
131826
|
maxDate = new Date(minDate.getTime() + (2 * deltaMilliseconds));
|
|
131806
131827
|
}
|
|
131828
|
+
if (minDate > maxDate) {
|
|
131829
|
+
throw new Error(`The minDate parameter must be less than or equal to the maxDate parameter.`);
|
|
131830
|
+
}
|
|
131807
131831
|
this.state = { rng, minDate, maxDate };
|
|
131808
131832
|
}
|
|
131809
131833
|
generate() {
|
|
@@ -131825,18 +131849,83 @@ class GenerateTime extends AbstractGenerator {
|
|
|
131825
131849
|
init({ count, seed }) {
|
|
131826
131850
|
super.init({ count, seed });
|
|
131827
131851
|
const rng = prand.xoroshiro128plus(seed);
|
|
131828
|
-
|
|
131852
|
+
let { minTime, maxTime } = this.params;
|
|
131853
|
+
if (minTime === undefined && maxTime === undefined) {
|
|
131854
|
+
// TODO: maybe need to change in major version release
|
|
131855
|
+
// This is required to ensure that this generator remains deterministic when used without minTime, maxTime parameters.
|
|
131856
|
+
const oneDayInMilliseconds = 86400000;
|
|
131857
|
+
minTime = new Date(new Date('2024-05-08T12:00:00.000Z').getTime() - oneDayInMilliseconds);
|
|
131858
|
+
maxTime = new Date(new Date('2024-05-08T12:00:00.000Z').getTime() + oneDayInMilliseconds);
|
|
131859
|
+
this.state = { rng, minTime, maxTime };
|
|
131860
|
+
return;
|
|
131861
|
+
}
|
|
131862
|
+
if (minTime === undefined) {
|
|
131863
|
+
if (maxTime === undefined) {
|
|
131864
|
+
minTime = '00:00:00.000Z';
|
|
131865
|
+
maxTime = '23:59:59.999Z';
|
|
131866
|
+
}
|
|
131867
|
+
else {
|
|
131868
|
+
minTime = '00:00:00.000Z';
|
|
131869
|
+
}
|
|
131870
|
+
}
|
|
131871
|
+
if (maxTime === undefined) {
|
|
131872
|
+
maxTime = '23:59:59.999Z';
|
|
131873
|
+
new Date().toISOString();
|
|
131874
|
+
}
|
|
131875
|
+
const anchorDate = new Date('2024-05-08');
|
|
131876
|
+
const anchorDateString0 = anchorDate.toISOString().replace(/T\d{2}:\d{2}:\d{2}.\d{3}Z/, '');
|
|
131877
|
+
if (typeof minTime === 'string') {
|
|
131878
|
+
// const timeMatch0 = minTime.match(/^\d{2}:\d{2}:\d{2}.\d{1,3}Z?$/);
|
|
131879
|
+
const timeMatch1 = minTime.match(/^\d{2}:\d{2}:\d{2}Z?$/);
|
|
131880
|
+
const timeMatch2 = minTime.match(/^\d{2}:\d{2}Z?$/);
|
|
131881
|
+
if (
|
|
131882
|
+
// timeMatch0 === null
|
|
131883
|
+
timeMatch1 === null
|
|
131884
|
+
&& timeMatch2 === null) {
|
|
131885
|
+
throw new Error(`You're using the wrong format for the minTime parameter.`
|
|
131886
|
+
+ `\nPlease use one of these formats: 'HH:mm:ss', 'HH:mm' (with or without a trailing 'Z')`);
|
|
131887
|
+
}
|
|
131888
|
+
minTime = minTime.at(-1) === 'Z' ? minTime : minTime + 'Z';
|
|
131889
|
+
minTime = new Date(anchorDate.toISOString().replace(/\d{2}:\d{2}:\d{2}.\d{3}Z/, minTime));
|
|
131890
|
+
}
|
|
131891
|
+
if (typeof minTime === 'object') {
|
|
131892
|
+
if (!isValidDate(minTime)) {
|
|
131893
|
+
throw new Error('Invalid Date was provided for the minTime parameter.');
|
|
131894
|
+
}
|
|
131895
|
+
minTime = new Date(minTime.toISOString().replace(/\d{4}-\d{2}-\d{2}/, anchorDateString0));
|
|
131896
|
+
}
|
|
131897
|
+
if (typeof maxTime === 'string') {
|
|
131898
|
+
// const timeMatch0 = maxTime.match(/^\d{2}:\d{2}:\d{2}.\d{1,3}Z?$/);
|
|
131899
|
+
const timeMatch1 = maxTime.match(/^\d{2}:\d{2}:\d{2}Z?$/);
|
|
131900
|
+
const timeMatch2 = maxTime.match(/^\d{2}:\d{2}Z?$/);
|
|
131901
|
+
if (
|
|
131902
|
+
// timeMatch0 === null
|
|
131903
|
+
timeMatch1 === null
|
|
131904
|
+
&& timeMatch2 === null) {
|
|
131905
|
+
throw new Error(`You're using the wrong format for the maxTime parameter.`
|
|
131906
|
+
+ `\nPlease use one of these formats: 'HH:mm:ss', 'HH:mm' (with or without a trailing 'Z').`);
|
|
131907
|
+
}
|
|
131908
|
+
maxTime = maxTime.at(-1) === 'Z' ? maxTime : maxTime + 'Z';
|
|
131909
|
+
maxTime = new Date(anchorDate.toISOString().replace(/\d{2}:\d{2}:\d{2}.\d{3}Z/, maxTime));
|
|
131910
|
+
}
|
|
131911
|
+
if (typeof maxTime === 'object') {
|
|
131912
|
+
if (!isValidDate(maxTime)) {
|
|
131913
|
+
throw new Error('Invalid Date was provided for the maxTime parameter.');
|
|
131914
|
+
}
|
|
131915
|
+
maxTime = new Date(maxTime.toISOString().replace(/\d{4}-\d{2}-\d{2}/, anchorDateString0));
|
|
131916
|
+
}
|
|
131917
|
+
if (minTime > maxTime) {
|
|
131918
|
+
throw new Error(`The minTime parameter must be less than or equal to the maxTime parameter.`);
|
|
131919
|
+
}
|
|
131920
|
+
this.state = { rng, minTime, maxTime };
|
|
131829
131921
|
}
|
|
131830
131922
|
generate() {
|
|
131831
131923
|
if (this.state === undefined) {
|
|
131832
131924
|
throw new Error('state is not defined.');
|
|
131833
131925
|
}
|
|
131834
|
-
const anchorDateTime = new Date('2024-05-08T12:00:00.000Z');
|
|
131835
|
-
const oneDayInMilliseconds = 86400000;
|
|
131836
|
-
let date = new Date();
|
|
131837
131926
|
let milliseconds;
|
|
131838
|
-
[milliseconds, this.state.rng] = prand.uniformIntDistribution(
|
|
131839
|
-
date = new Date(
|
|
131927
|
+
[milliseconds, this.state.rng] = prand.uniformIntDistribution(this.state.minTime.getTime(), this.state.maxTime.getTime(), this.state.rng);
|
|
131928
|
+
const date = new Date(milliseconds);
|
|
131840
131929
|
return date.toISOString().replace(/(\d{4}-\d{2}-\d{2}T)|(\.\d{3}Z)/g, '');
|
|
131841
131930
|
}
|
|
131842
131931
|
}
|
|
@@ -131846,18 +131935,46 @@ class GenerateTimestamp extends AbstractGenerator {
|
|
|
131846
131935
|
init({ count, seed }) {
|
|
131847
131936
|
super.init({ count, seed });
|
|
131848
131937
|
const rng = prand.xoroshiro128plus(seed);
|
|
131849
|
-
|
|
131938
|
+
let { minTimestamp, maxTimestamp } = this.params;
|
|
131939
|
+
const anchorDate = new Date('2024-05-08');
|
|
131940
|
+
// 2 years in milliseconds
|
|
131941
|
+
const deltaMilliseconds = 2 * 31536000000;
|
|
131942
|
+
if (typeof minTimestamp === 'string') {
|
|
131943
|
+
minTimestamp = new Date(minTimestamp);
|
|
131944
|
+
}
|
|
131945
|
+
if (typeof minTimestamp === 'object' && !isValidDate(minTimestamp)) {
|
|
131946
|
+
throw new Error('Invalid Date was provided for the minTimestamp parameter.');
|
|
131947
|
+
}
|
|
131948
|
+
if (typeof maxTimestamp === 'string') {
|
|
131949
|
+
maxTimestamp = new Date(maxTimestamp);
|
|
131950
|
+
}
|
|
131951
|
+
if (typeof maxTimestamp === 'object' && !isValidDate(maxTimestamp)) {
|
|
131952
|
+
throw new Error('Invalid Date was provided for the maxTimestamp parameter.');
|
|
131953
|
+
}
|
|
131954
|
+
if (minTimestamp === undefined) {
|
|
131955
|
+
if (maxTimestamp === undefined) {
|
|
131956
|
+
minTimestamp = new Date(anchorDate.getTime() - deltaMilliseconds);
|
|
131957
|
+
maxTimestamp = new Date(anchorDate.getTime() + deltaMilliseconds);
|
|
131958
|
+
}
|
|
131959
|
+
else {
|
|
131960
|
+
minTimestamp = new Date(maxTimestamp.getTime() - (2 * deltaMilliseconds));
|
|
131961
|
+
}
|
|
131962
|
+
}
|
|
131963
|
+
if (maxTimestamp === undefined) {
|
|
131964
|
+
maxTimestamp = new Date(minTimestamp.getTime() + (2 * deltaMilliseconds));
|
|
131965
|
+
}
|
|
131966
|
+
if (minTimestamp > maxTimestamp) {
|
|
131967
|
+
throw new Error(`The minTimestamp parameter must be less than or equal to the maxTimestamp parameter.`);
|
|
131968
|
+
}
|
|
131969
|
+
this.state = { rng, minTimestamp, maxTimestamp };
|
|
131850
131970
|
}
|
|
131851
131971
|
generate() {
|
|
131852
131972
|
if (this.state === undefined) {
|
|
131853
131973
|
throw new Error('state is not defined.');
|
|
131854
131974
|
}
|
|
131855
|
-
const anchorTimestamp = new Date('2024-05-08');
|
|
131856
|
-
const twoYearsInMilliseconds = 2 * 31536000000;
|
|
131857
|
-
let date = new Date();
|
|
131858
131975
|
let milliseconds;
|
|
131859
|
-
[milliseconds, this.state.rng] = prand.uniformIntDistribution(
|
|
131860
|
-
date = new Date(
|
|
131976
|
+
[milliseconds, this.state.rng] = prand.uniformIntDistribution(this.state.minTimestamp.getTime(), this.state.maxTimestamp.getTime(), this.state.rng);
|
|
131977
|
+
const date = new Date(milliseconds);
|
|
131861
131978
|
if (this.dataType === 'string') {
|
|
131862
131979
|
return date
|
|
131863
131980
|
.toISOString()
|
|
@@ -131873,18 +131990,46 @@ class GenerateDatetime extends AbstractGenerator {
|
|
|
131873
131990
|
init({ count, seed }) {
|
|
131874
131991
|
super.init({ count, seed });
|
|
131875
131992
|
const rng = prand.xoroshiro128plus(seed);
|
|
131876
|
-
|
|
131993
|
+
let { minDatetime, maxDatetime } = this.params;
|
|
131994
|
+
const anchorDate = new Date('2024-05-08');
|
|
131995
|
+
// 2 years in milliseconds
|
|
131996
|
+
const deltaMilliseconds = 2 * 31536000000;
|
|
131997
|
+
if (typeof minDatetime === 'string') {
|
|
131998
|
+
minDatetime = new Date(minDatetime);
|
|
131999
|
+
}
|
|
132000
|
+
if (typeof minDatetime === 'object' && !isValidDate(minDatetime)) {
|
|
132001
|
+
throw new Error('Invalid Date was provided for the minDatetime parameter.');
|
|
132002
|
+
}
|
|
132003
|
+
if (typeof maxDatetime === 'string') {
|
|
132004
|
+
maxDatetime = new Date(maxDatetime);
|
|
132005
|
+
}
|
|
132006
|
+
if (typeof maxDatetime === 'object' && !isValidDate(maxDatetime)) {
|
|
132007
|
+
throw new Error('Invalid Date was provided for the maxDatetime parameter.');
|
|
132008
|
+
}
|
|
132009
|
+
if (minDatetime === undefined) {
|
|
132010
|
+
if (maxDatetime === undefined) {
|
|
132011
|
+
minDatetime = new Date(anchorDate.getTime() - deltaMilliseconds);
|
|
132012
|
+
maxDatetime = new Date(anchorDate.getTime() + deltaMilliseconds);
|
|
132013
|
+
}
|
|
132014
|
+
else {
|
|
132015
|
+
minDatetime = new Date(maxDatetime.getTime() - (2 * deltaMilliseconds));
|
|
132016
|
+
}
|
|
132017
|
+
}
|
|
132018
|
+
if (maxDatetime === undefined) {
|
|
132019
|
+
maxDatetime = new Date(minDatetime.getTime() + (2 * deltaMilliseconds));
|
|
132020
|
+
}
|
|
132021
|
+
if (minDatetime > maxDatetime) {
|
|
132022
|
+
throw new Error(`The minDatetime parameter must be less than or equal to the maxDatetime parameter.`);
|
|
132023
|
+
}
|
|
132024
|
+
this.state = { rng, minDatetime, maxDatetime };
|
|
131877
132025
|
}
|
|
131878
132026
|
generate() {
|
|
131879
132027
|
if (this.state === undefined) {
|
|
131880
132028
|
throw new Error('state is not defined.');
|
|
131881
132029
|
}
|
|
131882
|
-
const anchorDate = new Date('2024-05-08');
|
|
131883
|
-
const twoYearsInMilliseconds = 2 * 31536000000;
|
|
131884
|
-
let date = new Date();
|
|
131885
132030
|
let milliseconds;
|
|
131886
|
-
[milliseconds, this.state.rng] = prand.uniformIntDistribution(
|
|
131887
|
-
date = new Date(
|
|
132031
|
+
[milliseconds, this.state.rng] = prand.uniformIntDistribution(this.state.minDatetime.getTime(), this.state.maxDatetime.getTime(), this.state.rng);
|
|
132032
|
+
const date = new Date(milliseconds);
|
|
131888
132033
|
if (this.dataType === 'string') {
|
|
131889
132034
|
return date
|
|
131890
132035
|
.toISOString()
|
|
@@ -133557,6 +133702,8 @@ const generatorsFuncs = {
|
|
|
133557
133702
|
date: createGenerator(GenerateDate),
|
|
133558
133703
|
/**
|
|
133559
133704
|
* generates time in 24 hours style.
|
|
133705
|
+
* @param minTime - lower border of range.
|
|
133706
|
+
* @param maxTime - upper border of range.
|
|
133560
133707
|
* @param arraySize - number of elements in each one-dimensional array. (If specified, arrays will be generated.)
|
|
133561
133708
|
*
|
|
133562
133709
|
* @example
|
|
@@ -133564,7 +133711,7 @@ const generatorsFuncs = {
|
|
|
133564
133711
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
133565
133712
|
* users: {
|
|
133566
133713
|
* columns: {
|
|
133567
|
-
* birthTime: funcs.time()
|
|
133714
|
+
* birthTime: funcs.time({ minTime: "11:12:13.141", maxTime: "15:16:17.181" })
|
|
133568
133715
|
* },
|
|
133569
133716
|
* },
|
|
133570
133717
|
* }));
|
|
@@ -133574,6 +133721,8 @@ const generatorsFuncs = {
|
|
|
133574
133721
|
time: createGenerator(GenerateTime),
|
|
133575
133722
|
/**
|
|
133576
133723
|
* generates timestamps.
|
|
133724
|
+
* @param minTimestamp - lower border of range.
|
|
133725
|
+
* @param maxTimestamp - upper border of range.
|
|
133577
133726
|
* @param arraySize - number of elements in each one-dimensional array. (If specified, arrays will be generated.)
|
|
133578
133727
|
*
|
|
133579
133728
|
* @example
|
|
@@ -133581,7 +133730,7 @@ const generatorsFuncs = {
|
|
|
133581
133730
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
133582
133731
|
* orders: {
|
|
133583
133732
|
* columns: {
|
|
133584
|
-
* shippedDate: funcs.timestamp()
|
|
133733
|
+
* shippedDate: funcs.timestamp({ minTimestamp: "2025-03-07T11:12:13.141", maxTimestamp: "2025-03-08T15:16:17.181" })
|
|
133585
133734
|
* },
|
|
133586
133735
|
* },
|
|
133587
133736
|
* }));
|
|
@@ -133591,6 +133740,8 @@ const generatorsFuncs = {
|
|
|
133591
133740
|
timestamp: createGenerator(GenerateTimestamp),
|
|
133592
133741
|
/**
|
|
133593
133742
|
* generates datetime objects.
|
|
133743
|
+
* @param minDatetime - lower border of range.
|
|
133744
|
+
* @param maxDatetime - upper border of range.
|
|
133594
133745
|
* @param arraySize - number of elements in each one-dimensional array. (If specified, arrays will be generated.)
|
|
133595
133746
|
*
|
|
133596
133747
|
* @example
|
|
@@ -133598,7 +133749,7 @@ const generatorsFuncs = {
|
|
|
133598
133749
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
133599
133750
|
* orders: {
|
|
133600
133751
|
* columns: {
|
|
133601
|
-
* shippedDate: funcs.datetime()
|
|
133752
|
+
* shippedDate: funcs.datetime({ minDatetime: "2025-03-07T11:12:13.141", maxDatetime: "2025-03-08T15:16:17.181" })
|
|
133602
133753
|
* },
|
|
133603
133754
|
* },
|
|
133604
133755
|
* }));
|
|
@@ -134059,7 +134210,7 @@ const generatorsFuncs = {
|
|
|
134059
134210
|
*/
|
|
134060
134211
|
weightedRandom: createGenerator(WeightedRandomGenerator),
|
|
134061
134212
|
};
|
|
134062
|
-
// so far, version changes don’t
|
|
134213
|
+
// so far, version changes don’t change generator parameters.
|
|
134063
134214
|
const generatorsFuncsV2 = {
|
|
134064
134215
|
...generatorsFuncs,
|
|
134065
134216
|
};
|
|
@@ -134270,6 +134421,7 @@ class SeedService {
|
|
|
134270
134421
|
};
|
|
134271
134422
|
}
|
|
134272
134423
|
}
|
|
134424
|
+
// handling refinements (count, with)
|
|
134273
134425
|
if (refinements !== undefined && refinements[table.name] !== undefined) {
|
|
134274
134426
|
if (refinements[table.name].count !== undefined) {
|
|
134275
134427
|
tablesPossibleGenerators[i].count = refinements[table.name].count;
|
|
@@ -134330,11 +134482,24 @@ class SeedService {
|
|
|
134330
134482
|
wasDefinedBefore: false,
|
|
134331
134483
|
wasRefined: false,
|
|
134332
134484
|
};
|
|
134485
|
+
// handling refinements (columnGenerator)
|
|
134333
134486
|
if (refinements !== undefined
|
|
134334
134487
|
&& refinements[table.name] !== undefined
|
|
134335
134488
|
&& refinements[table.name].columns !== undefined
|
|
134336
134489
|
&& refinements[table.name].columns[col.name] !== undefined) {
|
|
134337
134490
|
const genObj = refinements[table.name].columns[col.name];
|
|
134491
|
+
if (genObj === false) {
|
|
134492
|
+
if (col.notNull === true && col.hasDefault === false) {
|
|
134493
|
+
throw new Error(`You cannot set the '${col.name}' column in the '${table.name}' table to false in your refinements.`
|
|
134494
|
+
+ `\nDoing so will result in a null value being inserted into the '${col.name}' column,`
|
|
134495
|
+
+ `\nwhich will cause an error because the column has a not null constraint and no default value.`);
|
|
134496
|
+
}
|
|
134497
|
+
// Generating undefined as a value for a column and then inserting it via drizzle-orm
|
|
134498
|
+
// will result in the value not being inserted into that column.
|
|
134499
|
+
columnPossibleGenerator.generator = new generatorsMap.GenerateDefault[0]({ defaultValue: undefined });
|
|
134500
|
+
columnPossibleGenerator.wasRefined = true;
|
|
134501
|
+
continue;
|
|
134502
|
+
}
|
|
134338
134503
|
if (col.columnType.match(/\[\w*]/g) !== null) {
|
|
134339
134504
|
if ((col.baseColumn?.dataType === 'array' && col.baseColumn.columnType.match(/\[\w*]/g) !== null)
|
|
134340
134505
|
// studio case
|
|
@@ -134646,8 +134811,8 @@ class SeedService {
|
|
|
134646
134811
|
// NUMBER(real, double, decimal, numeric)
|
|
134647
134812
|
if (col.columnType === 'real'
|
|
134648
134813
|
|| col.columnType === 'double precision'
|
|
134649
|
-
|| col.columnType.match(/^decimal(\(\d
|
|
134650
|
-
|| col.columnType.match(/^numeric(\(\d
|
|
134814
|
+
|| col.columnType.match(/^decimal(\(\d{1,6}(, ?-?\d{0,5})?\))?$/) !== null
|
|
134815
|
+
|| col.columnType.match(/^numeric(\(\d{1,6}(, ?-?\d{0,5})?\))?$/) !== null) {
|
|
134651
134816
|
if (col.typeParams.precision !== undefined) {
|
|
134652
134817
|
const precision = col.typeParams.precision;
|
|
134653
134818
|
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
@@ -134823,8 +134988,8 @@ class SeedService {
|
|
|
134823
134988
|
if (col.columnType === 'real'
|
|
134824
134989
|
|| col.columnType === 'double'
|
|
134825
134990
|
|| col.columnType === 'float'
|
|
134826
|
-
|| col.columnType.
|
|
134827
|
-
|| col.columnType.
|
|
134991
|
+
|| col.columnType.startsWith('decimal')
|
|
134992
|
+
|| col.columnType.startsWith('numeric')) {
|
|
134828
134993
|
if (col.typeParams.precision !== undefined) {
|
|
134829
134994
|
const precision = col.typeParams.precision;
|
|
134830
134995
|
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
@@ -134842,40 +135007,40 @@ class SeedService {
|
|
|
134842
135007
|
// STRING
|
|
134843
135008
|
if ((col.columnType === 'text'
|
|
134844
135009
|
|| col.columnType === 'blob'
|
|
134845
|
-
|| col.columnType.
|
|
134846
|
-
|| col.columnType.
|
|
134847
|
-
|| col.columnType.
|
|
134848
|
-
|| col.columnType.
|
|
135010
|
+
|| col.columnType.startsWith('char')
|
|
135011
|
+
|| col.columnType.startsWith('varchar')
|
|
135012
|
+
|| col.columnType.startsWith('binary')
|
|
135013
|
+
|| col.columnType.startsWith('varbinary'))
|
|
134849
135014
|
&& table.primaryKeys.includes(col.name)) {
|
|
134850
135015
|
const generator = new generatorsMap.GenerateUniqueString[0]();
|
|
134851
135016
|
return generator;
|
|
134852
135017
|
}
|
|
134853
135018
|
if ((col.columnType === 'text'
|
|
134854
135019
|
|| col.columnType === 'blob'
|
|
134855
|
-
|| col.columnType.
|
|
134856
|
-
|| col.columnType.
|
|
134857
|
-
|| col.columnType.
|
|
134858
|
-
|| col.columnType.
|
|
135020
|
+
|| col.columnType.startsWith('char')
|
|
135021
|
+
|| col.columnType.startsWith('varchar')
|
|
135022
|
+
|| col.columnType.startsWith('binary')
|
|
135023
|
+
|| col.columnType.startsWith('varbinary'))
|
|
134859
135024
|
&& col.name.toLowerCase().includes('name')) {
|
|
134860
135025
|
const generator = new generatorsMap.GenerateFirstName[0]();
|
|
134861
135026
|
return generator;
|
|
134862
135027
|
}
|
|
134863
135028
|
if ((col.columnType === 'text'
|
|
134864
135029
|
|| col.columnType === 'blob'
|
|
134865
|
-
|| col.columnType.
|
|
134866
|
-
|| col.columnType.
|
|
134867
|
-
|| col.columnType.
|
|
134868
|
-
|| col.columnType.
|
|
135030
|
+
|| col.columnType.startsWith('char')
|
|
135031
|
+
|| col.columnType.startsWith('varchar')
|
|
135032
|
+
|| col.columnType.startsWith('binary')
|
|
135033
|
+
|| col.columnType.startsWith('varbinary'))
|
|
134869
135034
|
&& col.name.toLowerCase().includes('email')) {
|
|
134870
135035
|
const generator = new generatorsMap.GenerateEmail[0]();
|
|
134871
135036
|
return generator;
|
|
134872
135037
|
}
|
|
134873
135038
|
if (col.columnType === 'text'
|
|
134874
135039
|
|| col.columnType === 'blob'
|
|
134875
|
-
|| col.columnType.
|
|
134876
|
-
|| col.columnType.
|
|
134877
|
-
|| col.columnType.
|
|
134878
|
-
|| col.columnType.
|
|
135040
|
+
|| col.columnType.startsWith('char')
|
|
135041
|
+
|| col.columnType.startsWith('varchar')
|
|
135042
|
+
|| col.columnType.startsWith('binary')
|
|
135043
|
+
|| col.columnType.startsWith('varbinary')) {
|
|
134879
135044
|
const generator = new generatorsMap.GenerateString[0]();
|
|
134880
135045
|
return generator;
|
|
134881
135046
|
}
|
|
@@ -134885,7 +135050,7 @@ class SeedService {
|
|
|
134885
135050
|
return generator;
|
|
134886
135051
|
}
|
|
134887
135052
|
// DATE, TIME, TIMESTAMP, DATETIME, YEAR
|
|
134888
|
-
if (col.columnType.
|
|
135053
|
+
if (col.columnType.startsWith('datetime')) {
|
|
134889
135054
|
const generator = new generatorsMap.GenerateDatetime[0]();
|
|
134890
135055
|
return generator;
|
|
134891
135056
|
}
|
|
@@ -134897,7 +135062,7 @@ class SeedService {
|
|
|
134897
135062
|
const generator = new generatorsMap.GenerateTime[0]();
|
|
134898
135063
|
return generator;
|
|
134899
135064
|
}
|
|
134900
|
-
if (col.columnType.
|
|
135065
|
+
if (col.columnType.startsWith('timestamp')) {
|
|
134901
135066
|
const generator = new generatorsMap.GenerateTimestamp[0]();
|
|
134902
135067
|
return generator;
|
|
134903
135068
|
}
|
|
@@ -134950,8 +135115,8 @@ class SeedService {
|
|
|
134950
135115
|
return generator;
|
|
134951
135116
|
}
|
|
134952
135117
|
// number section ------------------------------------------------------------------------------------
|
|
134953
|
-
if (col.columnType.
|
|
134954
|
-
|| col.columnType.
|
|
135118
|
+
if (col.columnType.startsWith('real')
|
|
135119
|
+
|| col.columnType.startsWith('numeric')) {
|
|
134955
135120
|
if (col.typeParams.precision !== undefined) {
|
|
134956
135121
|
const precision = col.typeParams.precision;
|
|
134957
135122
|
const scale = col.typeParams.scale === undefined ? 0 : col.typeParams.scale;
|
|
@@ -135068,7 +135233,8 @@ class SeedService {
|
|
|
135068
135233
|
};
|
|
135069
135234
|
}
|
|
135070
135235
|
// get values to generate columns with foreign key
|
|
135071
|
-
// if table posts contains foreign key to table users, then rel.table === 'posts' and rel.refTable === 'users',
|
|
135236
|
+
// if table posts contains foreign key to table users, then rel.table === 'posts' and rel.refTable === 'users',
|
|
135237
|
+
// because table posts has reference to table users.
|
|
135072
135238
|
if (filteredRelations.length !== 0) {
|
|
135073
135239
|
for (const rel of filteredRelations) {
|
|
135074
135240
|
if (table.withFromTable[rel.refTable] !== undefined
|
|
@@ -135185,9 +135351,6 @@ class SeedService {
|
|
|
135185
135351
|
columnsNumber += 1;
|
|
135186
135352
|
columnGenerator = tableGenerators[columnName];
|
|
135187
135353
|
override = tableGenerators[columnName]?.generatedIdentityType === 'always' ? true : override;
|
|
135188
|
-
if (tableName === 'evaluations' && columnName === 'gameId') {
|
|
135189
|
-
console.log();
|
|
135190
|
-
}
|
|
135191
135354
|
columnsGenerators[columnName] = columnGenerator.generator;
|
|
135192
135355
|
columnsGenerators[columnName].init({
|
|
135193
135356
|
count,
|
|
@@ -135202,6 +135365,25 @@ class SeedService {
|
|
|
135202
135365
|
// columnsGenerators[columnName] = uniqueGen;
|
|
135203
135366
|
// }
|
|
135204
135367
|
}
|
|
135368
|
+
// sequence updates will only be performed for PostgreSQL, since MySQL and SQLite already update their sequences correctly on their own.
|
|
135369
|
+
const columnsToUpdateSeq = new Map();
|
|
135370
|
+
if (count > 0 && is(db, PgDatabase) && schema !== undefined && tableName !== undefined
|
|
135371
|
+
&& schema[tableName] !== undefined) {
|
|
135372
|
+
const tableConfig = getTableConfig(schema[tableName]);
|
|
135373
|
+
for (const column of tableConfig.columns) {
|
|
135374
|
+
// TODO should I filter only primary key columns?
|
|
135375
|
+
// should I filter column by dataType or by column drizzle type?
|
|
135376
|
+
// column.dataType === 'number' || column.dataType === 'bigint'
|
|
135377
|
+
if (isPostgresColumnIntLike(column)) {
|
|
135378
|
+
columnsToUpdateSeq.set(column.name, {
|
|
135379
|
+
schemaName: tableConfig.schema,
|
|
135380
|
+
tableName: tableConfig.name,
|
|
135381
|
+
columnName: column.name,
|
|
135382
|
+
valueToUpdate: undefined,
|
|
135383
|
+
});
|
|
135384
|
+
}
|
|
135385
|
+
}
|
|
135386
|
+
}
|
|
135205
135387
|
let maxParametersNumber;
|
|
135206
135388
|
if (is(db, (PgDatabase))) {
|
|
135207
135389
|
// @ts-ignore
|
|
@@ -135233,6 +135415,12 @@ class SeedService {
|
|
|
135233
135415
|
// | boolean;
|
|
135234
135416
|
generatedValue = columnsGenerators[columnName].generate({ i });
|
|
135235
135417
|
row[columnName] = generatedValue;
|
|
135418
|
+
const colToUpdateSeq = columnsToUpdateSeq.get(columnName);
|
|
135419
|
+
if (columnsToUpdateSeq.size !== 0 && colToUpdateSeq !== undefined) {
|
|
135420
|
+
colToUpdateSeq.valueToUpdate = colToUpdateSeq?.valueToUpdate === undefined
|
|
135421
|
+
? generatedValue
|
|
135422
|
+
: intMax([colToUpdateSeq.valueToUpdate, generatedValue]);
|
|
135423
|
+
}
|
|
135236
135424
|
}
|
|
135237
135425
|
if ((insertDataInDb === true || updateDataInDb === true)
|
|
135238
135426
|
&& ((i + 1) % batchSize === 0 || i === count - 1)) {
|
|
@@ -135279,9 +135467,28 @@ class SeedService {
|
|
|
135279
135467
|
}
|
|
135280
135468
|
}
|
|
135281
135469
|
}
|
|
135470
|
+
const columnsToUpdateSeqFiltered = [...columnsToUpdateSeq.values()].filter((col) => col.valueToUpdate !== undefined);
|
|
135471
|
+
if (i === count - 1
|
|
135472
|
+
&& columnsToUpdateSeqFiltered.length !== 0 && db !== undefined) {
|
|
135473
|
+
for (const columnConfig of columnsToUpdateSeq.values()) {
|
|
135474
|
+
if (columnConfig) {
|
|
135475
|
+
await this.updateColumnSequence({ db, columnConfig });
|
|
135476
|
+
}
|
|
135477
|
+
}
|
|
135478
|
+
}
|
|
135282
135479
|
}
|
|
135283
135480
|
return preserveData === true ? generatedValues : [];
|
|
135284
135481
|
};
|
|
135482
|
+
updateColumnSequence = async ({ db, columnConfig: { schemaName, tableName, columnName, valueToUpdate } }) => {
|
|
135483
|
+
if (is(db, PgDatabase)) {
|
|
135484
|
+
const fullTableName = schemaName ? `"${schemaName}"."${tableName}"` : `"${tableName}"`;
|
|
135485
|
+
const rawQuery = `SELECT setval(pg_get_serial_sequence('${fullTableName}', '${columnName}'), ${(valueToUpdate ?? 'null').toString()}, true);`;
|
|
135486
|
+
await db.execute(sql.raw(rawQuery));
|
|
135487
|
+
}
|
|
135488
|
+
// mysql updates auto_increment or serial column by itself
|
|
135489
|
+
// sqlite updates autoincrement column by itself
|
|
135490
|
+
return;
|
|
135491
|
+
};
|
|
135285
135492
|
insertInDb = async ({ generatedValues, db, schema, tableName, override, }) => {
|
|
135286
135493
|
if (is(db, (PgDatabase))) {
|
|
135287
135494
|
const query = db.insert(schema[tableName]);
|
|
@@ -135420,7 +135627,7 @@ async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleSt
|
|
|
135420
135627
|
* // seeding with count and seed specified
|
|
135421
135628
|
* await seed(db, schema, { count: 100000, seed: 1 });
|
|
135422
135629
|
*
|
|
135423
|
-
* //seeding using refine
|
|
135630
|
+
* // seeding using refine
|
|
135424
135631
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
135425
135632
|
* users: {
|
|
135426
135633
|
* columns: {
|
|
@@ -135441,6 +135648,17 @@ async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleSt
|
|
|
135441
135648
|
* },
|
|
135442
135649
|
* }));
|
|
135443
135650
|
*
|
|
135651
|
+
* // seeding while ignoring column
|
|
135652
|
+
* await seed(db, schema).refine((funcs) => ({
|
|
135653
|
+
* users: {
|
|
135654
|
+
* count: 5,
|
|
135655
|
+
* columns: {
|
|
135656
|
+
* name: funcs.fullName(),
|
|
135657
|
+
* photo: false, // the photo column will not be seeded, allowing the database to use its default value.
|
|
135658
|
+
* },
|
|
135659
|
+
* },
|
|
135660
|
+
* }));
|
|
135661
|
+
*
|
|
135444
135662
|
* ```
|
|
135445
135663
|
*/
|
|
135446
135664
|
function seed(db, schema, options) {
|
|
@@ -136146,5 +136364,5 @@ const getSqliteInfo = (sqliteSchema, sqliteTables) => {
|
|
|
136146
136364
|
return { tables, relations: isCyclicRelations, tableRelations };
|
|
136147
136365
|
};
|
|
136148
136366
|
|
|
136149
|
-
export { SeedService, cityNames as cities, countries, firstNames, getGeneratorsFunctions, lastNames, reset, seed, seedForDrizzleStudio };
|
|
136367
|
+
export { AbstractGenerator, SeedService, cityNames as cities, countries, firstNames, getGeneratorsFunctions, lastNames, reset, seed, seedForDrizzleStudio };
|
|
136150
136368
|
//# sourceMappingURL=index.mjs.map
|