How to properly define/configure Drizzle ORM in Nestjs Application

591 views Asked by At

I am new to Nest and am trying to properly define the modules, services and provider. I dont seem many examples and of the one i have seen, it does not appear to be defined properly.

I am looking to define the app.module, drizzle ORM provider, drizzle service and an employees module & service that use the drizzle provider or service to use a pool connection and perform database queries returning result objects.

Here is what I have for the app.module.ts

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { DrizzleMySqlModule } from '@knaadh/nestjs-drizzle-mysql2';
import * as schema from './drizzle/models/employees.entity';
import { EmployeesModule } from './employees/employees.module';
import { TeamsModule } from './teams/teams.module';
import { ReportsModule } from './reports/reports.module';
import { EmployeesService } from './employees/employees.service';

@Module({
  imports: [
    DrizzleMySqlModule.register({
      mysql: {
        connection: 'pool',
        config: {
          host: '127.0.0.1',
          user: 'xxxx',
          database: 'partner2learn',
        },
      },
      config: { schema: { ...schema }, mode: 'default' },
    }),
    EmployeesModule,
    TeamsModule,
    ReportsModule,
  ],
  controllers: [AppController],
  providers: [AppService, EmployeesService],
})
export class AppModule {} 

Not sure what should be in the:

drizzle.module.ts

drizzle.provider.ts

drizzle.service.ts

and

employees.service.ts

This is what I have tried to put in the employee service.

import { Inject, Injectable } from '@nestjs/common';
import * as schema from '../drizzle/schema';
import { MySql2Database } from 'drizzle-orm/mysql2';

@Injectable()
export class EmployeesService {
    constructor(
        @Inject('') private db: MySql2Database<typeof schema>
    ) { }
    async getData() {
        const books = await this.db.query.employees.findMany();
        return {
            employees: employees
    }
}

Thanks in advance!

0

There are 0 answers