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.d.cts
CHANGED
|
@@ -263,7 +263,7 @@ export declare function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject,
|
|
|
263
263
|
* // seeding with count and seed specified
|
|
264
264
|
* await seed(db, schema, { count: 100000, seed: 1 });
|
|
265
265
|
*
|
|
266
|
-
* //seeding using refine
|
|
266
|
+
* // seeding using refine
|
|
267
267
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
268
268
|
* users: {
|
|
269
269
|
* columns: {
|
|
@@ -284,6 +284,17 @@ export declare function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject,
|
|
|
284
284
|
* },
|
|
285
285
|
* }));
|
|
286
286
|
*
|
|
287
|
+
* // seeding while ignoring column
|
|
288
|
+
* await seed(db, schema).refine((funcs) => ({
|
|
289
|
+
* users: {
|
|
290
|
+
* count: 5,
|
|
291
|
+
* columns: {
|
|
292
|
+
* name: funcs.fullName(),
|
|
293
|
+
* photo: false, // the photo column will not be seeded, allowing the database to use its default value.
|
|
294
|
+
* },
|
|
295
|
+
* },
|
|
296
|
+
* }));
|
|
297
|
+
*
|
|
287
298
|
* ```
|
|
288
299
|
*/
|
|
289
300
|
export declare function seed<DB extends PgDatabase<any, any> | MySqlDatabase<any, any, any, any> | BaseSQLiteDatabase<any, any> | LibSQLDatabase<any>, SCHEMA extends {
|
package/index.d.mts
CHANGED
|
@@ -263,7 +263,7 @@ export declare function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject,
|
|
|
263
263
|
* // seeding with count and seed specified
|
|
264
264
|
* await seed(db, schema, { count: 100000, seed: 1 });
|
|
265
265
|
*
|
|
266
|
-
* //seeding using refine
|
|
266
|
+
* // seeding using refine
|
|
267
267
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
268
268
|
* users: {
|
|
269
269
|
* columns: {
|
|
@@ -284,6 +284,17 @@ export declare function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject,
|
|
|
284
284
|
* },
|
|
285
285
|
* }));
|
|
286
286
|
*
|
|
287
|
+
* // seeding while ignoring column
|
|
288
|
+
* await seed(db, schema).refine((funcs) => ({
|
|
289
|
+
* users: {
|
|
290
|
+
* count: 5,
|
|
291
|
+
* columns: {
|
|
292
|
+
* name: funcs.fullName(),
|
|
293
|
+
* photo: false, // the photo column will not be seeded, allowing the database to use its default value.
|
|
294
|
+
* },
|
|
295
|
+
* },
|
|
296
|
+
* }));
|
|
297
|
+
*
|
|
287
298
|
* ```
|
|
288
299
|
*/
|
|
289
300
|
export declare function seed<DB extends PgDatabase<any, any> | MySqlDatabase<any, any, any, any> | BaseSQLiteDatabase<any, any> | LibSQLDatabase<any>, SCHEMA extends {
|
package/index.d.ts
CHANGED
|
@@ -263,7 +263,7 @@ export declare function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject,
|
|
|
263
263
|
* // seeding with count and seed specified
|
|
264
264
|
* await seed(db, schema, { count: 100000, seed: 1 });
|
|
265
265
|
*
|
|
266
|
-
* //seeding using refine
|
|
266
|
+
* // seeding using refine
|
|
267
267
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
268
268
|
* users: {
|
|
269
269
|
* columns: {
|
|
@@ -284,6 +284,17 @@ export declare function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject,
|
|
|
284
284
|
* },
|
|
285
285
|
* }));
|
|
286
286
|
*
|
|
287
|
+
* // seeding while ignoring column
|
|
288
|
+
* await seed(db, schema).refine((funcs) => ({
|
|
289
|
+
* users: {
|
|
290
|
+
* count: 5,
|
|
291
|
+
* columns: {
|
|
292
|
+
* name: funcs.fullName(),
|
|
293
|
+
* photo: false, // the photo column will not be seeded, allowing the database to use its default value.
|
|
294
|
+
* },
|
|
295
|
+
* },
|
|
296
|
+
* }));
|
|
297
|
+
*
|
|
287
298
|
* ```
|
|
288
299
|
*/
|
|
289
300
|
export declare function seed<DB extends PgDatabase<any, any> | MySqlDatabase<any, any, any, any> | BaseSQLiteDatabase<any, any> | LibSQLDatabase<any>, SCHEMA extends {
|
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
|
|
|
@@ -131217,6 +131217,17 @@ const equalSets = (set1, set2) => {
|
|
|
131217
131217
|
const isValidDate = (date) => {
|
|
131218
131218
|
return !Number.isNaN(date.getTime());
|
|
131219
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
|
+
};
|
|
131220
131231
|
|
|
131221
131232
|
/* eslint-disable drizzle-internal/require-entity-kind */
|
|
131222
131233
|
class AbstractGenerator {
|
|
@@ -135222,7 +135233,8 @@ class SeedService {
|
|
|
135222
135233
|
};
|
|
135223
135234
|
}
|
|
135224
135235
|
// get values to generate columns with foreign key
|
|
135225
|
-
// 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.
|
|
135226
135238
|
if (filteredRelations.length !== 0) {
|
|
135227
135239
|
for (const rel of filteredRelations) {
|
|
135228
135240
|
if (table.withFromTable[rel.refTable] !== undefined
|
|
@@ -135353,6 +135365,25 @@ class SeedService {
|
|
|
135353
135365
|
// columnsGenerators[columnName] = uniqueGen;
|
|
135354
135366
|
// }
|
|
135355
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
|
+
}
|
|
135356
135387
|
let maxParametersNumber;
|
|
135357
135388
|
if (is(db, (PgDatabase))) {
|
|
135358
135389
|
// @ts-ignore
|
|
@@ -135384,6 +135415,12 @@ class SeedService {
|
|
|
135384
135415
|
// | boolean;
|
|
135385
135416
|
generatedValue = columnsGenerators[columnName].generate({ i });
|
|
135386
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
|
+
}
|
|
135387
135424
|
}
|
|
135388
135425
|
if ((insertDataInDb === true || updateDataInDb === true)
|
|
135389
135426
|
&& ((i + 1) % batchSize === 0 || i === count - 1)) {
|
|
@@ -135430,9 +135467,28 @@ class SeedService {
|
|
|
135430
135467
|
}
|
|
135431
135468
|
}
|
|
135432
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
|
+
}
|
|
135433
135479
|
}
|
|
135434
135480
|
return preserveData === true ? generatedValues : [];
|
|
135435
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
|
+
};
|
|
135436
135492
|
insertInDb = async ({ generatedValues, db, schema, tableName, override, }) => {
|
|
135437
135493
|
if (is(db, (PgDatabase))) {
|
|
135438
135494
|
const query = db.insert(schema[tableName]);
|
|
@@ -135571,7 +135627,7 @@ async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleSt
|
|
|
135571
135627
|
* // seeding with count and seed specified
|
|
135572
135628
|
* await seed(db, schema, { count: 100000, seed: 1 });
|
|
135573
135629
|
*
|
|
135574
|
-
* //seeding using refine
|
|
135630
|
+
* // seeding using refine
|
|
135575
135631
|
* await seed(db, schema, { count: 1000 }).refine((funcs) => ({
|
|
135576
135632
|
* users: {
|
|
135577
135633
|
* columns: {
|
|
@@ -135592,6 +135648,17 @@ async function seedForDrizzleStudio({ sqlDialect, drizzleStudioObject, drizzleSt
|
|
|
135592
135648
|
* },
|
|
135593
135649
|
* }));
|
|
135594
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
|
+
*
|
|
135595
135662
|
* ```
|
|
135596
135663
|
*/
|
|
135597
135664
|
function seed(db, schema, options) {
|