From 599da5ef7a037904989069596c8879690f9c5322 Mon Sep 17 00:00:00 2001 From: Oleksii Khomenko <47694554+OleksiiKH0240@users.noreply.github.com> Date: Mon, 25 Nov 2024 19:03:30 +0200 Subject: [PATCH] drizzle-seed bug fixes (#3614) Fixed a few seed bugs --------- Co-authored-by: AndriiSherman --- changelogs/drizzle-seed/0.1.2.md | 2 + drizzle-seed/package.json | 3 +- drizzle-seed/src/index.ts | 20 +++++----- drizzle-seed/src/services/SeedService.ts | 4 +- drizzle-seed/src/type-tests/mysql.ts | 17 ++++++++ drizzle-seed/src/type-tests/pg.ts | 48 +++++++++++++++++++++++ drizzle-seed/src/type-tests/sqlite.ts | 17 ++++++++ drizzle-seed/src/type-tests/tsconfig.json | 11 ++++++ pnpm-lock.yaml | 24 ++++++------ 9 files changed, 121 insertions(+), 25 deletions(-) create mode 100644 changelogs/drizzle-seed/0.1.2.md create mode 100644 drizzle-seed/src/type-tests/mysql.ts create mode 100644 drizzle-seed/src/type-tests/pg.ts create mode 100644 drizzle-seed/src/type-tests/sqlite.ts create mode 100644 drizzle-seed/src/type-tests/tsconfig.json diff --git a/changelogs/drizzle-seed/0.1.2.md b/changelogs/drizzle-seed/0.1.2.md new file mode 100644 index 000000000..4df3c89f4 --- /dev/null +++ b/changelogs/drizzle-seed/0.1.2.md @@ -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) \ No newline at end of file diff --git a/drizzle-seed/package.json b/drizzle-seed/package.json index 32ada8a46..dbac30737 100644 --- a/drizzle-seed/package.json +++ b/drizzle-seed/package.json @@ -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", diff --git a/drizzle-seed/src/index.ts b/drizzle-seed/src/index.ts index 3f766e219..093ecaf5c 100644 --- a/drizzle-seed/src/index.ts +++ b/drizzle-seed/src/index.ts @@ -18,13 +18,13 @@ import type { Relation, Table } from './types/tables.ts'; type InferCallbackType< DB extends - | PgDatabase + | PgDatabase | MySqlDatabase | BaseSQLiteDatabase, SCHEMA extends { [key: string]: PgTable | PgSchema | MySqlTable | MySqlSchema | SQLiteTable; }, -> = DB extends PgDatabase ? SCHEMA extends { +> = DB extends PgDatabase ? SCHEMA extends { [key: string]: | PgTable | PgSchema @@ -124,7 +124,7 @@ type InferCallbackType< class SeedPromise< DB extends - | PgDatabase + | PgDatabase | MySqlDatabase | BaseSQLiteDatabase, SCHEMA extends { @@ -311,7 +311,7 @@ export async function seedForDrizzleStudio( */ export function seed< DB extends - | PgDatabase + | PgDatabase | MySqlDatabase | BaseSQLiteDatabase, SCHEMA extends { @@ -328,7 +328,7 @@ export function seed< } const seedFunc = async ( - db: PgDatabase | MySqlDatabase | BaseSQLiteDatabase, + db: PgDatabase | MySqlDatabase | BaseSQLiteDatabase, schema: { [key: string]: | PgTable @@ -341,7 +341,7 @@ const seedFunc = async ( options: { count?: number; seed?: number } = {}, refinements?: RefinementsType, ) => { - if (is(db, PgDatabase)) { + if (is(db, PgDatabase)) { const { pgSchema } = filterPgTables(schema); await seedPostgres(db, pgSchema, options, refinements); @@ -404,7 +404,7 @@ const seedFunc = async ( */ export async function reset< DB extends - | PgDatabase + | PgDatabase | MySqlDatabase | BaseSQLiteDatabase, SCHEMA extends { @@ -417,7 +417,7 @@ export async function reset< | any; }, >(db: DB, schema: SCHEMA) { - if (is(db, PgDatabase)) { + if (is(db, PgDatabase)) { const { pgSchema } = filterPgTables(schema); if (Object.entries(pgSchema).length > 0) { @@ -444,7 +444,7 @@ export async function reset< // Postgres----------------------------------------------------------------------------------------------------------- const resetPostgres = async ( - db: PgDatabase, + db: PgDatabase, schema: { [key: string]: PgTable }, ) => { const tablesToTruncate = Object.entries(schema).map(([_, table]) => { @@ -474,7 +474,7 @@ const filterPgTables = (schema: { }; const seedPostgres = async ( - db: PgDatabase, + db: PgDatabase, schema: { [key: string]: PgTable }, options: { count?: number; seed?: number } = {}, refinements?: RefinementsType, diff --git a/drizzle-seed/src/services/SeedService.ts b/drizzle-seed/src/services/SeedService.ts index d5166faae..2588f110e 100644 --- a/drizzle-seed/src/services/SeedService.ts +++ b/drizzle-seed/src/services/SeedService.ts @@ -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)) { - maxParametersNumber = is(db._.session, PgliteSession) + // @ts-ignore + maxParametersNumber = db.constructor[entityKind] === 'PgliteDatabase' ? this.postgresPgLiteMaxParametersNumber : this.postgresMaxParametersNumber; } else if (is(db, MySqlDatabase)) { diff --git a/drizzle-seed/src/type-tests/mysql.ts b/drizzle-seed/src/type-tests/mysql.ts new file mode 100644 index 000000000..d1c0949b3 --- /dev/null +++ b/drizzle-seed/src/type-tests/mysql.ts @@ -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 }); +} diff --git a/drizzle-seed/src/type-tests/pg.ts b/drizzle-seed/src/type-tests/pg.ts new file mode 100644 index 000000000..68faf844b --- /dev/null +++ b/drizzle-seed/src/type-tests/pg.ts @@ -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 }); +} diff --git a/drizzle-seed/src/type-tests/sqlite.ts b/drizzle-seed/src/type-tests/sqlite.ts new file mode 100644 index 000000000..1b060e6a3 --- /dev/null +++ b/drizzle-seed/src/type-tests/sqlite.ts @@ -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 }); +} diff --git a/drizzle-seed/src/type-tests/tsconfig.json b/drizzle-seed/src/type-tests/tsconfig.json new file mode 100644 index 000000000..09df8e1bb --- /dev/null +++ b/drizzle-seed/src/type-tests/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.build.json", + "compilerOptions": { + "composite": false, + "noEmit": true, + "rootDir": "..", + "outDir": "./.cache" + }, + "include": [".", "../src"], + "exclude": ["**/playground"] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fb912db04..24d14e7c5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -10702,8 +10702,8 @@ snapshots: dependencies: '@aws-crypto/sha256-browser': 3.0.0 '@aws-crypto/sha256-js': 3.0.0 - '@aws-sdk/client-sso-oidc': 3.583.0(@aws-sdk/client-sts@3.583.0) - '@aws-sdk/client-sts': 3.583.0 + '@aws-sdk/client-sso-oidc': 3.583.0 + '@aws-sdk/client-sts': 3.583.0(@aws-sdk/client-sso-oidc@3.583.0) '@aws-sdk/core': 3.582.0 '@aws-sdk/credential-provider-node': 3.583.0(@aws-sdk/client-sso-oidc@3.583.0)(@aws-sdk/client-sts@3.583.0) '@aws-sdk/middleware-host-header': 3.577.0 @@ -10789,11 +10789,11 @@ snapshots: transitivePeerDependencies: - aws-crt - '@aws-sdk/client-sso-oidc@3.583.0(@aws-sdk/client-sts@3.583.0)': + '@aws-sdk/client-sso-oidc@3.583.0': dependencies: '@aws-crypto/sha256-browser': 3.0.0 '@aws-crypto/sha256-js': 3.0.0 - '@aws-sdk/client-sts': 3.583.0 + '@aws-sdk/client-sts': 3.583.0(@aws-sdk/client-sso-oidc@3.583.0) '@aws-sdk/core': 3.582.0 '@aws-sdk/credential-provider-node': 3.583.0(@aws-sdk/client-sso-oidc@3.583.0)(@aws-sdk/client-sts@3.583.0) '@aws-sdk/middleware-host-header': 3.577.0 @@ -10832,7 +10832,6 @@ snapshots: '@smithy/util-utf8': 3.0.0 tslib: 2.8.1 transitivePeerDependencies: - - '@aws-sdk/client-sts' - aws-crt '@aws-sdk/client-sso@3.478.0': @@ -11099,11 +11098,11 @@ snapshots: - '@aws-sdk/client-sso-oidc' - aws-crt - '@aws-sdk/client-sts@3.583.0': + '@aws-sdk/client-sts@3.583.0(@aws-sdk/client-sso-oidc@3.583.0)': dependencies: '@aws-crypto/sha256-browser': 3.0.0 '@aws-crypto/sha256-js': 3.0.0 - '@aws-sdk/client-sso-oidc': 3.583.0(@aws-sdk/client-sts@3.583.0) + '@aws-sdk/client-sso-oidc': 3.583.0 '@aws-sdk/core': 3.582.0 '@aws-sdk/credential-provider-node': 3.583.0(@aws-sdk/client-sso-oidc@3.583.0)(@aws-sdk/client-sts@3.583.0) '@aws-sdk/middleware-host-header': 3.577.0 @@ -11142,6 +11141,7 @@ snapshots: '@smithy/util-utf8': 3.0.0 tslib: 2.8.1 transitivePeerDependencies: + - '@aws-sdk/client-sso-oidc' - aws-crt '@aws-sdk/core@3.477.0': @@ -11296,7 +11296,7 @@ snapshots: '@aws-sdk/credential-provider-ini@3.583.0(@aws-sdk/client-sso-oidc@3.583.0)(@aws-sdk/client-sts@3.583.0)': dependencies: - '@aws-sdk/client-sts': 3.583.0 + '@aws-sdk/client-sts': 3.583.0(@aws-sdk/client-sso-oidc@3.583.0) '@aws-sdk/credential-provider-env': 3.577.0 '@aws-sdk/credential-provider-process': 3.577.0 '@aws-sdk/credential-provider-sso': 3.583.0(@aws-sdk/client-sso-oidc@3.583.0) @@ -11503,7 +11503,7 @@ snapshots: '@aws-sdk/credential-provider-web-identity@3.577.0(@aws-sdk/client-sts@3.583.0)': dependencies: - '@aws-sdk/client-sts': 3.583.0 + '@aws-sdk/client-sts': 3.583.0(@aws-sdk/client-sso-oidc@3.583.0) '@aws-sdk/types': 3.577.0 '@smithy/property-provider': 3.0.0 '@smithy/types': 3.0.0 @@ -11704,7 +11704,7 @@ snapshots: '@aws-sdk/token-providers@3.568.0(@aws-sdk/client-sso-oidc@3.583.0)': dependencies: - '@aws-sdk/client-sso-oidc': 3.583.0(@aws-sdk/client-sts@3.583.0) + '@aws-sdk/client-sso-oidc': 3.583.0 '@aws-sdk/types': 3.567.0 '@smithy/property-provider': 2.2.0 '@smithy/shared-ini-file-loader': 2.4.0 @@ -11713,7 +11713,7 @@ snapshots: '@aws-sdk/token-providers@3.577.0(@aws-sdk/client-sso-oidc@3.583.0)': dependencies: - '@aws-sdk/client-sso-oidc': 3.583.0(@aws-sdk/client-sts@3.583.0) + '@aws-sdk/client-sso-oidc': 3.583.0 '@aws-sdk/types': 3.577.0 '@smithy/property-provider': 3.0.0 '@smithy/shared-ini-file-loader': 3.0.0 @@ -16279,7 +16279,7 @@ snapshots: '@npmcli/fs': 3.1.1 fs-minipass: 3.0.3 glob: 10.4.1 - lru-cache: 10.2.2 + lru-cache: 10.4.3 minipass: 7.1.2 minipass-collect: 2.0.1 minipass-flush: 1.0.5