Best Practice Approach For Connecting Nest.js With Drizzle ORM?
Hi, I am relatively new to the world of nest.js and drizzle orm. I have decided to learn these two softwares because they provide many benefits when creating fullstack apps. I want to know from an expert/someone who has already done this about how to best setup nest.js with drizzle orm. Any advice with clear explanations is very much appreciated.
I have created this file
src/database/database.service.ts
:
import { Injectable, OnModuleInit } from '@nestjs/common';
import { neon } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-http';
import * as schema from '../../drizzle/schema';
import { db, DatabaseType } from 'drizzle/db';
@Injectable()
export class DatabaseService implements OnModuleInit {
private db: DatabaseType;
constructor() {
this.db = db;
}
async onModuleInit() {
try {
const sql = neon(process.env.DB_URL!);
this.db = drizzle(sql, { schema });
console.log('Database connected successfully');
} catch (error) {
console.error('Failed to connect to the database', error);
throw error;
}
}
getDb() {
return this.db;
}
}
Thanks in advance
Joel0 Replies