Use Drizzle in NestJS app

Hi everyone ! I am new to both drizzle and NestJS and was wondering if there was any recipe out there to use Drizzle in NestJS (I couldn't find anything) should I create a service or something ? (as Prisma ?)
9 Replies
Louistiti
LouistitiOP17mo ago
Okay so basically what I did is this : drizzle.module.ts
import { Module } from '@nestjs/common';
import { DrizzleService } from './drizzle.service';

@Module({
providers: [DrizzleService],
exports: [DrizzleService],
})
export class DrizzleModule {}
import { Module } from '@nestjs/common';
import { DrizzleService } from './drizzle.service';

@Module({
providers: [DrizzleService],
exports: [DrizzleService],
})
export class DrizzleModule {}
drizzle.service.ts
import { drizzle, PostgresJsDatabase } from 'drizzle-orm/postgres-js';
import { Injectable } from '@nestjs/common';
import postgres, { Sql } from 'postgres';
import * as process from 'process';
import { sql } from 'drizzle-orm';

@Injectable()
export class DrizzleService {
private db: PostgresJsDatabase;
private queryClient: Sql<{}>;

constructor() {
this.connect();
}

private async connect(): Promise<void> {
const connectionString = process.env.DATABASE_URL;

if (!connectionString) {
throw new Error('No environment variable DATABASE_URL');
}
this.queryClient = postgres(connectionString);
this.db = drizzle(this.queryClient);

await this.clearDb();
}

public getDb(): PostgresJsDatabase {
return this.db;
}

public async clearDb(): Promise<void> {
const query = sql<string>`SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_type = 'BASE TABLE';
`;

const tables = await this.db.execute(query);

for (let table of tables) {
const query = sql.raw(`TRUNCATE TABLE ${table.table_name} CASCADE;`);
await this.db.execute(query);
}
}
}
import { drizzle, PostgresJsDatabase } from 'drizzle-orm/postgres-js';
import { Injectable } from '@nestjs/common';
import postgres, { Sql } from 'postgres';
import * as process from 'process';
import { sql } from 'drizzle-orm';

@Injectable()
export class DrizzleService {
private db: PostgresJsDatabase;
private queryClient: Sql<{}>;

constructor() {
this.connect();
}

private async connect(): Promise<void> {
const connectionString = process.env.DATABASE_URL;

if (!connectionString) {
throw new Error('No environment variable DATABASE_URL');
}
this.queryClient = postgres(connectionString);
this.db = drizzle(this.queryClient);

await this.clearDb();
}

public getDb(): PostgresJsDatabase {
return this.db;
}

public async clearDb(): Promise<void> {
const query = sql<string>`SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_type = 'BASE TABLE';
`;

const tables = await this.db.execute(query);

for (let table of tables) {
const query = sql.raw(`TRUNCATE TABLE ${table.table_name} CASCADE;`);
await this.db.execute(query);
}
}
}
app.module.ts
import { Module } from '@nestjs/common';

import { AppController } from './app.controller';
import {DrizzleModule} from "./config/drizzle/drizzle.module";

@Module({
imports: [DrizzleModule, ...your modules],
controllers: [AppController],
providers: [...your providers],
})
export class AppModule {}
import { Module } from '@nestjs/common';

import { AppController } from './app.controller';
import {DrizzleModule} from "./config/drizzle/drizzle.module";

@Module({
imports: [DrizzleModule, ...your modules],
controllers: [AppController],
providers: [...your providers],
})
export class AppModule {}
parmetra
parmetra13mo ago
Hello! Is this a working solution?
Louistiti
LouistitiOP13mo ago
Yes it works
olup
olup13mo ago
we also make the module global for easyer use
Louistiti
LouistitiOP13mo ago
What does that change exactly ? (I am new to NestJS)
olup
olup13mo ago
You just don't have to import it in module files
Louistiti
LouistitiOP13mo ago
Ohhhh awesome !
zzao
zzao10mo ago
Bro, I try this implementations and I can't run the app, i got this error :
this.queryClient = postgres(connectionString);
TypeError: (0 , postgres_1.default) is not a function
this.queryClient = postgres(connectionString);
TypeError: (0 , postgres_1.default) is not a function
Obs: My connectionString is rigth
Mithlesh
Mithlesh10mo ago
GitHub
GitHub - knaadh/nestjs-drizzle: A NestJS module for integrating Dri...
A NestJS module for integrating DrizzleORM with Postgres, MySQL, SQLite, Turso and Planetscale drivers - GitHub - knaadh/nestjs-drizzle: A NestJS module for integrating DrizzleORM with Postgres, My...
Want results from more Discord servers?
Add your server