nestjsdrizzle

Can't import Drizzle package in NestJS


I'm having trouble importing a package from mysql2/promise into NestJS project. I was following official documentation

import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import * as schema from './schema';

export const DrizzleAsyncProvider = 'drizzleProvider';

export const drizzleProvider = {
  provide: DrizzleAsyncProvider,
  useFactory: async () => {
    console.log(mysql, '<<<');

    const connection = await mysql.createConnection({
      host: 'host',
      user: 'user',
      database: 'database',
      password: 'password'
    });
    const db = drizzle(connection, { schema, mode: 'default' });

    return db;
  },
  exports: [DrizzleAsyncProvider],
}

For some reason, I'm getting next error:

[Nest] 16326  - 04/20/2024, 8:31:46 PM     LOG [NestFactory] Starting Nest application...
undefined <<<
[Nest] 16326  - 04/20/2024, 8:31:46 PM   ERROR [ExceptionHandler] Cannot read properties of undefined (reading 'createConnection')
TypeError: Cannot read properties of undefined (reading 'createConnection')

Solution

  • Drizzle docs seem to be outdated. Please try this:

    import * as mysql from "mysql2/promise";