Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.env
.idea
.vscode
228 changes: 207 additions & 21 deletions backend/bun.lock

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions backend/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from 'drizzle-kit';

if (!process.env.DATABASE_URL) {
throw new Error('DATABASE_URL environment variable is required');
}

export default defineConfig({
dialect: 'postgresql',
schema: './src/database/schema/*.ts',
out: './drizzle',
dbCredentials: {
url: process.env.DATABASE_URL,
},
strict: true,
verbose: true,
});
10 changes: 9 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:e2e": "jest --config ./test/jest-e2e.json",
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate",
"db:push": "drizzle-kit push",
"db:studio": "drizzle-kit studio"
},
"dependencies": {
"@nestjs/common": "^11.0.1",
"@nestjs/core": "^11.0.1",
"@nestjs/platform-express": "^11.0.1",
"drizzle-orm": "^0.45.2",
"pg": "^8.20.0",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1"
},
Expand All @@ -37,7 +43,9 @@
"@types/express": "^5.0.0",
"@types/jest": "^30.0.0",
"@types/node": "^22.10.7",
"@types/pg": "^8.20.0",
"@types/supertest": "^6.0.2",
"drizzle-kit": "^0.31.10",
"eslint": "^9.18.0",
"eslint-config-prettier": "^10.0.1",
"eslint-plugin-prettier": "^5.2.2",
Expand Down
4 changes: 3 additions & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Module } from '@nestjs/common';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { DatabaseModule } from './database/database.module';

@Module({
imports: [],
imports: [DatabaseModule],
controllers: [AppController],
providers: [AppService],
})
Expand Down
38 changes: 38 additions & 0 deletions backend/src/database/database.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Global, Inject, Module, OnModuleDestroy } from '@nestjs/common';
import { drizzle, NodePgDatabase } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';

import * as schema from './schema';

export const DATABASE_POOL = Symbol('DATABASE_POOL');
export const DRIZZLE = Symbol('DRIZZLE');

export type Database = NodePgDatabase<typeof schema>;

@Global()
@Module({
providers: [
{
provide: DATABASE_POOL,
useFactory: (): Pool => {
if (!process.env.DATABASE_URL) {
throw new Error('DATABASE_URL environment variable is required');
}
return new Pool({ connectionString: process.env.DATABASE_URL });
},
},
{
provide: DRIZZLE,
inject: [DATABASE_POOL],
useFactory: (pool: Pool): Database => drizzle(pool, { schema }),
},
],
exports: [DRIZZLE],
})
export class DatabaseModule implements OnModuleDestroy {
constructor(@Inject(DATABASE_POOL) private readonly pool: Pool) {}

async onModuleDestroy(): Promise<void> {
await this.pool.end();
}
}
1 change: 1 addition & 0 deletions backend/src/database/schema/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
Loading