drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.54k stars 643 forks source link

[BUG]: TypeError: Cannot read properties of undefined (reading 'parsers') #2028

Closed berenddeboer closed 8 months ago

berenddeboer commented 8 months ago

What version of drizzle-orm are you using?

0.30.2

What version of drizzle-kit are you using?

0.24.14

Describe the Bug

Switching to 0.30.2 gives a very odd error when instantiating drizzle. Code working fine with previous version. Code looks like this:

    const client = postgres(
      `postgres://${this.credentials.username}:${this.credentials.password}@${host}:${port}/${this.credentials.dbname}`,
      { max: 1, connect_timeout: 3 },
    )
    const db = drizzle(client, {
      logger: this.logger,
    })

Error happens on creating drizzle. I tried to add options to both postgres and drizzle but didn't seem to help.

Expected behavior

Code working without changes.

Environment & setup

This happens with a mocked postgres driver. This is probably the issue.

berenddeboer commented 8 months ago

OK, just leaving this here for someone else stumbling on this: if you mock postgres, you now need to make sure you return an options object, with an empty parsers and serializers array.

iursevla commented 7 months ago

This still happens to me.

/node_modules/src/postgres-js/driver.ts:27 client.options.parsers[type as any] = transparentParser; ^ TypeError: Cannot read properties of undefined (reading 'parsers') at drizzle(r/node_modules/src/postgres-js/driver.ts:27:18) at testRunMigrations (/migrations/run_migrations.ts:22:14) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) Node.js v20.11.0 drizzle-kit: v0.20.14 drizzle-orm: v0.30.6

This is my code

  const client = new Client({
    connectionString: DatabaseConfig.postgresqlConnection,
  });
  await client.connect(); // It connects
  const db = drizzle(client, { schema: { ...jobs } });

https://github.com/drizzle-team/drizzle-orm/issues/2028#issuecomment-2002635197

@berenddeboer what did you change?

crrobinson14 commented 5 months ago

@iursevla Hopefully this will help you or others like me that get here for the same reason. If you change database drivers e.g. to switch from postgres-js to node-postgres make sure you:

  1. Change all your Drizzle-related imports
  2. Change the TYPE that you import
import {drizzle} from 'drizzle-orm/postgres-js';
import {PostgresJsDatabase} from 'drizzle-orm/postgres-js/driver';

becomes:

import {drizzle} from 'drizzle-orm/node-postgres';
import {NodePgDatabase} from 'drizzle-orm/node-postgres/driver';
iursevla commented 5 months ago

Thanks @crrobinson14 for taking the time to reply. I've since figured it out and also created a little repo to showcase multiple schemas: https://github.com/intruder-detection/nestjs-drizzle-multiple-schemas

crrobinson14 commented 5 months ago

@iursevla Very nice contribution! Love the reusable DAO too. It's my one dislike of Drizzle - the API surfaces between .query and .select|insert|update|delete aren't quite the same, and many common operations like findByPK take a fair bit of code. I miss ActiveRecord approaches to this. I love what you did in type safety there.