Adds Drizzle ORM with Infisical for secure database management

Integrates Drizzle ORM to provide a type-safe and modern solution for PostgreSQL database interactions. Establishes Infisical as the central secret management system, ensuring sensitive database credentials and other environment variables are securely loaded dynamically based on the active environment.

Includes a PowerShell script to standardize environment setup for Drizzle CLI operations, fetching secrets from Infisical and local configuration files. New npm scripts `drizzle:push` and `drizzle:pull` are added to streamline database schema migrations.

Sets up an initial database module with a `DatabaseService` and a sample `users` schema to demonstrate Drizzle's functionality and integration with Infisical secrets.
This commit is contained in:
dahyeon committed 2025-11-09 16:05:18 +00:00
1 parent 6aec3e91b8
commit 469e799b01
13 files changed
+1651 -4

No files matched your search

@@ -1,12 +1,14 @@
import { Controller, Get, UseGuards } from "@nestjs/common";
import { AccountService } from "./account.service";
import { JwtAuthGuard } from "../global/auth/jwt.guard";
import { AllowPublicAccess } from "../global/auth/allow-public-access.decorator";
@Controller('account')
export class AccountController {
constructor(private readonly accountService: AccountService) {}
@Get('signin')
@AllowPublicAccess()
signIn(): string {
return this.accountService.signIn();
}
+2
View File
@@ -3,10 +3,12 @@ import { Injectable } from "@nestjs/common";
@Injectable()
export class AccountService {
signIn(): string {
//TODO: Implement sign in logic
return 'Sign In Successful';
}
getAccountInfo(): string {
//TODO: Implement get account info logic
console.log('Fetching account information');
return 'Account Information';
}
@@ -0,0 +1,8 @@
import { Module } from "@nestjs/common";
import { DatabaseService } from "./database.service";
@Module({
providers: [DatabaseService],
exports: [DatabaseService],
})
export class DatabaseModule {}
@@ -0,0 +1,75 @@
import { Injectable } from "@nestjs/common";
import { SecretService } from "../secret/secret.service";
import { drizzle, NodePgDatabase } from "drizzle-orm/node-postgres";
import * as pg from 'pg'
import { eq } from "drizzle-orm";
import { usersTable } from "./schema";
@Injectable()
export class DatabaseService {
private endpoint: string;
private user: string;
private password: string;
private databaseName: string;
private dbAdapter: NodePgDatabase<Record<string, never>> & { $client: pg.Client }
private connection: pg.Client
constructor(
private readonly secretService: SecretService
) {}
async onModuleInit() {
await this.secretService.ready();
this.endpoint = await this.secretService.get("DATABASE_ENDPOINT");
this.user = await this.secretService.get("DATABASE_USER");
this.password = await this.secretService.get("DATABASE_PASSWORD");
this.databaseName = await this.secretService.get("DATABASE_DATABASE_NAME");
this.connection = new pg.Client({
user: this.user,
password: this.password,
database: this.databaseName,
host: this.endpoint,
port: 5432,
})
await this.connection.connect();
this.dbAdapter = drizzle(this.connection)
await this.test();
}
async test() {
const user: typeof usersTable.$inferInsert = {
name: 'John',
age: 30,
email: 'john@example.com',
};
await this.dbAdapter.insert(usersTable).values(user);
console.log('New user created!')
const users = await this.dbAdapter.select().from(usersTable);
console.log('Getting all users from the database: ', users)
/*
const users: {
id: number;
name: string;
age: number;
email: string;
}[]
*/
await this.dbAdapter
.update(usersTable)
.set({
age: 31,
})
.where(eq(usersTable.email, user.email));
console.log('User info updated!')
await this.dbAdapter.delete(usersTable).where(eq(usersTable.email, user.email));
console.log('User deleted!')
}
}
+10
View File
@@ -0,0 +1,10 @@
import { pgTable } from "drizzle-orm/pg-core";
import { integer } from "drizzle-orm/pg-core";
import { varchar } from "drizzle-orm/pg-core";
export const usersTable = pgTable("users", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
name: varchar({ length: 255 }).notNull(),
age: integer().notNull(),
email: varchar({ length: 255 }).notNull().unique(),
});
+3 -2
View File
@@ -5,11 +5,12 @@ import { NatsModule } from './nats/nats.module';
import { JwtService } from './auth/jwt.service';
import { MinioModule } from './minio/minio.module';
import { AuthModule } from './auth/auth.module';
import { DatabaseModule } from './database/database.module';
@Global()
@Module({
imports: [SecretModule, NatsModule, MinioModule, AuthModule, MinioModule],
imports: [SecretModule, NatsModule, MinioModule, AuthModule, MinioModule, DatabaseModule],
providers: [JwtService],
exports: [SecretModule, NatsModule, MinioModule, AuthModule, MinioModule],
exports: [SecretModule, NatsModule, MinioModule, AuthModule, MinioModule, DatabaseModule],
})
export class GlobalModule {}