drizzle-seed 0.3.2-905c951 → 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 +69 -2
- package/index.cjs.map +1 -1
- package/index.d.cts +12 -1
- package/index.d.mts +12 -1
- package/index.d.ts +12 -1
- package/index.mjs +71 -4
- package/index.mjs.map +1 -1
- package/package.json +6 -6
- 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 +4 -0
- package/services/utils.d.mts +4 -0
- package/services/utils.d.ts +4 -0
package/index.cjs
CHANGED
|
@@ -131219,6 +131219,17 @@ const equalSets = (set1, set2) => {
|
|
|
131219
131219
|
const isValidDate = (date) => {
|
|
131220
131220
|
return !Number.isNaN(date.getTime());
|
|
131221
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
|
+
};
|
|
131222
131233
|
|
|
131223
131234
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
131224
131235
|
class AbstractGenerator {
|
|
@@ -135224,7 +135235,8 @@ class SeedService {
|
|
|
135224
135235
|
};
|
|
135225
135236
|
}
|
|
135226
135237
|
// get values to generate columns with foreign key
|
|
135227
|
-
// 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.
|
|
135228
135240
|
if (filteredRelations.length !== 0) {
|
|
135229
135241
|
for (const rel of filteredRelations) {
|
|
135230
135242
|
if (table.withFromTable[rel.refTable] !== undefined
|
|
@@ -135355,6 +135367,25 @@ class SeedService {
|
|
|
135355
135367
|
// columnsGenerators[columnName] = uniqueGen;
|
|
135356
135368
|
// }
|
|
135357
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
|
+
}
|
|
135358
135389
|
let maxParametersNumber;
|
|
135359
135390
|
if (drizzleOrm.is(db, (pgCore.PgDatabase))) {
|
|
135360
135391
|
// @ts-ignore
|
|
@@ -135386,6 +135417,12 @@ class SeedService {
|
|
|
135386
135417
|
// | boolean;
|
|
135387
135418
|
generatedValue = columnsGenerators[columnName].generate({ i });
|
|
135388
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
|
+
}
|
|
135389
135426
|
}
|
|
135390
135427
|
if ((insertDataInDb === true || updateDataInDb === true)
|
|
135391
135428
|
&& ((i + 1) % batchSize === 0 || i === count - 1)) {
|
|
@@ -135432,9 +135469,28 @@ class SeedService {
|
|
|
135432
135469
|
}
|
|
135433
135470
|
}
|
|
135434
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
|
+
}
|
|
135435
135481
|
}
|
|
135436
135482
|
return preserveData === true ? generatedValues : [];
|
|
135437
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
|
+
};
|
|
135438
135494
|
insertInDb = async ({ generatedValues, db, schema, tableName, override, }) => {
|
|
135439
135495
|
if (drizzleOrm.is(db, (pgCore.PgDatabase))) {
|
|
135440
135496
|
const query = db.insert(schema[tableName]);
|
|
@@ -135573,7 +135629,7 @@ async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleSt
|
|
|
135573
135629
|
* // seeding with count and seed specified
|
|
135574
135630
|
* await seed(db, schema, { count: 100000, seed: 1 });
|
|
135575
135631
|
*
|
|
135576
|
-
* //seeding using refine
|
|
135632
|
+
* // seeding using refine
|
|
135577
135633
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
135578
135634
|
* users: {
|
|
135579
135635
|
* columns: {
|
|
@@ -135594,6 +135650,17 @@ async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleSt
|
|
|
135594
135650
|
* },
|
|
135595
135651
|
* }));
|
|
135596
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
|
+
*
|
|
135597
135664
|
* ```
|
|
135598
135665
|
*/
|
|
135599
135666
|
function seed(db, schema, options) {
|