Skip to content

Commit

Permalink
drizzle-seed bug fixes (drizzle-team#3614)
Browse files Browse the repository at this point in the history
Fixed a few seed bugs

---------

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>
  • Loading branch information
OleksiiKH0240 and AndriiSherman authored Nov 25, 2024
1 parent 03f6239 commit 599da5e
Showing 9 changed files with 121 additions and 25 deletions.
2 changes: 2 additions & 0 deletions changelogs/drizzle-seed/0.1.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fixed: [[BUG]: drizzle-seed reset fails without @electric-sql/pglite installed](https://github.com/drizzle-team/drizzle-orm/issues/3603)
- Fixed: [[BUG]: TypeScript type error in drizzle-seed with schema passed to drizzle in IDE](https://github.com/drizzle-team/drizzle-orm/issues/3599)
3 changes: 2 additions & 1 deletion drizzle-seed/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "drizzle-seed",
"version": "0.1.1",
"version": "0.1.2",
"main": "index.js",
"type": "module",
"scripts": {
"build": "tsx scripts/build.ts",
"pack": "(cd dist && npm pack --pack-destination ..) && rm -f package.tgz && mv *.tgz package.tgz",
"test": "vitest --config ./src/tests/vitest.config.ts",
"test:types": "cd src/type-tests && tsc",
"generate-for-tests:pg": "drizzle-kit generate --config=./src/tests/pg/drizzle.config.ts",
"generate-for-tests:mysql": "drizzle-kit generate --config=./src/tests/mysql/drizzle.config.ts",
"generate-for-tests:sqlite": "drizzle-kit generate --config=./src/tests/sqlite/drizzle.config.ts",
20 changes: 10 additions & 10 deletions drizzle-seed/src/index.ts
Original file line number Diff line number Diff line change
@@ -18,13 +18,13 @@ import type { Relation, Table } from './types/tables.ts';

type InferCallbackType<
DB extends
| PgDatabase<any>
| PgDatabase<any, any>
| MySqlDatabase<any, any>
| BaseSQLiteDatabase<any, any>,
SCHEMA extends {
[key: string]: PgTable | PgSchema | MySqlTable | MySqlSchema | SQLiteTable;
},
> = DB extends PgDatabase<any> ? SCHEMA extends {
> = DB extends PgDatabase<any, any> ? SCHEMA extends {
[key: string]:
| PgTable
| PgSchema
@@ -124,7 +124,7 @@ type InferCallbackType<

class SeedPromise<
DB extends
| PgDatabase<any>
| PgDatabase<any, any>
| MySqlDatabase<any, any>
| BaseSQLiteDatabase<any, any>,
SCHEMA extends {
@@ -311,7 +311,7 @@ export async function seedForDrizzleStudio(
*/
export function seed<
DB extends
| PgDatabase<any>
| PgDatabase<any, any>
| MySqlDatabase<any, any>
| BaseSQLiteDatabase<any, any>,
SCHEMA extends {
@@ -328,7 +328,7 @@ export function seed<
}

const seedFunc = async (
db: PgDatabase<any> | MySqlDatabase<any, any> | BaseSQLiteDatabase<any, any>,
db: PgDatabase<any, any> | MySqlDatabase<any, any> | BaseSQLiteDatabase<any, any>,
schema: {
[key: string]:
| PgTable
@@ -341,7 +341,7 @@ const seedFunc = async (
options: { count?: number; seed?: number } = {},
refinements?: RefinementsType,
) => {
if (is(db, PgDatabase<any>)) {
if (is(db, PgDatabase<any, any>)) {
const { pgSchema } = filterPgTables(schema);

await seedPostgres(db, pgSchema, options, refinements);
@@ -404,7 +404,7 @@ const seedFunc = async (
*/
export async function reset<
DB extends
| PgDatabase<any>
| PgDatabase<any, any>
| MySqlDatabase<any, any>
| BaseSQLiteDatabase<any, any>,
SCHEMA extends {
@@ -417,7 +417,7 @@ export async function reset<
| any;
},
>(db: DB, schema: SCHEMA) {
if (is(db, PgDatabase<any>)) {
if (is(db, PgDatabase<any, any>)) {
const { pgSchema } = filterPgTables(schema);

if (Object.entries(pgSchema).length > 0) {
@@ -444,7 +444,7 @@ export async function reset<

// Postgres-----------------------------------------------------------------------------------------------------------
const resetPostgres = async (
db: PgDatabase<any>,
db: PgDatabase<any, any>,
schema: { [key: string]: PgTable },
) => {
const tablesToTruncate = Object.entries(schema).map(([_, table]) => {
@@ -474,7 +474,7 @@ const filterPgTables = (schema: {
};

const seedPostgres = async (
db: PgDatabase<any>,
db: PgDatabase<any, any>,
schema: { [key: string]: PgTable },
options: { count?: number; seed?: number } = {},
refinements?: RefinementsType,
4 changes: 2 additions & 2 deletions drizzle-seed/src/services/SeedService.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ import type { MySqlTable } from 'drizzle-orm/mysql-core';
import { MySqlDatabase } from 'drizzle-orm/mysql-core';
import type { PgTable } from 'drizzle-orm/pg-core';
import { PgDatabase } from 'drizzle-orm/pg-core';
import { PgliteSession } from 'drizzle-orm/pglite';
import type { SQLiteTable } from 'drizzle-orm/sqlite-core';
import { BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core';
import type {
@@ -1037,7 +1036,8 @@ class SeedService {
}
let maxParametersNumber: number;
if (is(db, PgDatabase<any>)) {
maxParametersNumber = is(db._.session, PgliteSession)
// @ts-ignore
maxParametersNumber = db.constructor[entityKind] === 'PgliteDatabase'
? this.postgresPgLiteMaxParametersNumber
: this.postgresMaxParametersNumber;
} else if (is(db, MySqlDatabase<any, any>)) {
17 changes: 17 additions & 0 deletions drizzle-seed/src/type-tests/mysql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { MySqlColumn } from 'drizzle-orm/mysql-core';
import { int, mysqlTable, text } from 'drizzle-orm/mysql-core';
import { drizzle as mysql2Drizzle } from 'drizzle-orm/mysql2';
import { reset, seed } from '../index.ts';

const mysqlUsers = mysqlTable('users', {
id: int().primaryKey().autoincrement(),
name: text(),
inviteId: int('invite_id').references((): MySqlColumn => mysqlUsers.id),
});

{
const db = mysql2Drizzle('');

await seed(db, { users: mysqlUsers });
await reset(db, { users: mysqlUsers });
}
48 changes: 48 additions & 0 deletions drizzle-seed/src/type-tests/pg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { drizzle as nodePostgresDrizzle } from 'drizzle-orm/node-postgres';
import type { PgColumn } from 'drizzle-orm/pg-core';
import { integer, pgTable, text } from 'drizzle-orm/pg-core';
import { drizzle as pgliteDrizzle } from 'drizzle-orm/pglite';
import { drizzle as postgresJsDrizzle } from 'drizzle-orm/postgres-js';
import { reset, seed } from '../index.ts';

const pgUsers = pgTable('users', {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
name: text(),
inviteId: integer('invite_id').references((): PgColumn => pgUsers.id),
});

{
const db0 = nodePostgresDrizzle('', { schema: { users: pgUsers } });

await seed(db0, { users: pgUsers });
await reset(db0, { users: pgUsers });

const db1 = nodePostgresDrizzle('');

await seed(db1, { users: pgUsers });
await reset(db1, { users: pgUsers });
}

{
const db0 = pgliteDrizzle('', { schema: { users: pgUsers } });

await seed(db0, { users: pgUsers });
await reset(db0, { users: pgUsers });

const db1 = pgliteDrizzle('');

await seed(db1, { users: pgUsers });
await reset(db1, { users: pgUsers });
}

{
const db0 = postgresJsDrizzle('', { schema: { users: pgUsers } });

await seed(db0, { users: pgUsers });
await reset(db0, { users: pgUsers });

const db1 = postgresJsDrizzle('');

await seed(db1, { users: pgUsers });
await reset(db1, { users: pgUsers });
}
17 changes: 17 additions & 0 deletions drizzle-seed/src/type-tests/sqlite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { drizzle as betterSqlite3Drizzle } from 'drizzle-orm/better-sqlite3';
import type { SQLiteColumn } from 'drizzle-orm/sqlite-core';
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { reset, seed } from '../index.ts';

const mysqlUsers = sqliteTable('users', {
id: int().primaryKey(),
name: text(),
inviteId: int('invite_id').references((): SQLiteColumn => mysqlUsers.id),
});

{
const db = betterSqlite3Drizzle('');

await seed(db, { users: mysqlUsers });
await reset(db, { users: mysqlUsers });
}
11 changes: 11 additions & 0 deletions drizzle-seed/src/type-tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"composite": false,
"noEmit": true,
"rootDir": "..",
"outDir": "./.cache"
},
"include": [".", "../src"],
"exclude": ["**/playground"]
}
24 changes: 12 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 599da5e

Please sign in to comment.