From e98758efe2b55f4686dcd2987bd7c1af2b64b338 Mon Sep 17 00:00:00 2001 From: = <=> Date: Mon, 23 Feb 2026 23:23:08 +0400 Subject: [PATCH] setup connexion bdd --- .vscode/settings.json | 2 +- apps/api/package.json | 2 ++ apps/api/src/app.module.ts | 3 ++- apps/api/src/database/database.module.ts | 9 +++++++++ apps/api/src/database/database.provider.ts | 20 ++++++++++++++++++++ apps/api/tsconfig.json | 7 ++++++- pnpm-lock.yaml | 6 ++++++ 7 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 apps/api/src/database/database.module.ts create mode 100644 apps/api/src/database/database.provider.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index dc0ed90..84707c2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "cSpell.words": ["nestjs", "nodenext", "nuxt", "tailwindcss"] + "cSpell.words": ["nestjs", "nodenext", "nuxt", "postgres", "tailwindcss"] } diff --git a/apps/api/package.json b/apps/api/package.json index 8f6baec..034be25 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -23,6 +23,8 @@ "@nestjs/common": "^11.0.1", "@nestjs/core": "^11.0.1", "@nestjs/platform-express": "^11.0.1", + "drizzle-orm": "^0.45.1", + "postgres": "^3.4.8", "reflect-metadata": "^0.2.2", "rxjs": "^7.8.1" }, diff --git a/apps/api/src/app.module.ts b/apps/api/src/app.module.ts index 8662803..1453aaf 100644 --- a/apps/api/src/app.module.ts +++ b/apps/api/src/app.module.ts @@ -1,9 +1,10 @@ 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], }) diff --git a/apps/api/src/database/database.module.ts b/apps/api/src/database/database.module.ts new file mode 100644 index 0000000..2e3515b --- /dev/null +++ b/apps/api/src/database/database.module.ts @@ -0,0 +1,9 @@ +import { Global, Module } from '@nestjs/common'; +import { DatabaseProvider, DRIZZLE_CLIENT } from './database.provider'; + +@Global() +@Module({ + providers: [DatabaseProvider], + exports: [DRIZZLE_CLIENT], +}) +export class DatabaseModule {} diff --git a/apps/api/src/database/database.provider.ts b/apps/api/src/database/database.provider.ts new file mode 100644 index 0000000..c375a13 --- /dev/null +++ b/apps/api/src/database/database.provider.ts @@ -0,0 +1,20 @@ +import { drizzle, PostgresJsDatabase } from 'drizzle-orm/postgres-js'; +import postgres from 'postgres'; +import * as schema from '@proj/db'; +import { Provider } from '@nestjs/common'; + +export const DRIZZLE_CLIENT = 'DRIZZLE_CLIENT'; + +export type DrizzleDB = PostgresJsDatabase; + +export const DatabaseProvider: Provider = { + provide: DRIZZLE_CLIENT, + useFactory: (): DrizzleDB => { + const connectionString = + process.env.DATABASE_URL || 'postgres://user:pass@localhost:5432/db'; + const client = postgres(connectionString); + + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment + return drizzle(client, { schema }) as DrizzleDB; + }, +}; diff --git a/apps/api/tsconfig.json b/apps/api/tsconfig.json index 02a649b..86f8366 100644 --- a/apps/api/tsconfig.json +++ b/apps/api/tsconfig.json @@ -12,6 +12,7 @@ "allowSyntheticDefaultImports": true, "target": "ES2023", "sourceMap": true, + "rootDir": "../..", "outDir": "./dist", "incremental": true, "skipLibCheck": true, @@ -19,6 +20,10 @@ "forceConsistentCasingInFileNames": true, "noImplicitAny": false, "strictBindCallApply": false, - "noFallthroughCasesInSwitch": false + "noFallthroughCasesInSwitch": false, + "paths": { + "@proj/db": ["../../packages/db/index.ts"], + "@proj/db/*": ["../../packages/db/*"] + } } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2f71521..e726175 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,12 @@ importers: '@nestjs/platform-express': specifier: ^11.0.1 version: 11.1.14(@nestjs/common@11.1.14)(@nestjs/core@11.1.14) + drizzle-orm: + specifier: ^0.45.1 + version: 0.45.1(postgres@3.4.8) + postgres: + specifier: ^3.4.8 + version: 3.4.8 reflect-metadata: specifier: ^0.2.2 version: 0.2.2