Skip to content

Commit

Permalink
chore: add datasource url to prisma service
Browse files Browse the repository at this point in the history
  • Loading branch information
marcellmueller committed Jan 2, 2025
1 parent 9edf387 commit 9b1a264
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions backend/src/prisma.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import { Injectable, OnModuleInit } from "@nestjs/common";
import { PrismaClient } from "@prisma/client";

const DB_HOST = process.env.POSTGRES_HOST || "localhost";
const DB_USER = process.env.POSTGRES_USER || "postgres";
const DB_PWD = encodeURIComponent(process.env.POSTGRES_PASSWORD || "default"); // this needs to be encoded, if the password contains special characters it will break connection string.
const DB_PORT = process.env.POSTGRES_PORT || 5432;
const DB_NAME = process.env.POSTGRES_DATABASE || "postgres";
const DB_SCHEMA = process.env.POSTGRES_SCHEMA || "rst";
const dataSourceURL = `postgresql://${DB_USER}:${DB_PWD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?schema=${DB_SCHEMA}&connection_limit=5`;

@Injectable()
class PrismaService extends PrismaClient implements OnModuleInit {
databaseUrl: string;

constructor() {
super({
datasources: {
db: {
url: dataSourceURL,
},
},
});
}

async onModuleInit() {
await this.$connect();
}
Expand Down

0 comments on commit 9b1a264

Please sign in to comment.