---
description: A potentially more robust way to generate the database url.
---

# Generating database url in codebase

We have successfully adopted the usage of **environment variables** in this section. However, we are creating the **database url** from the credentials directly in the <mark style="color:purple;">.env</mark> file. We could also do so in the codebase, avoiding the need to have it inside <mark style="color:purple;">.env</mark>. This would also allow for a more concise setup when deploying the application.

First, we should remove this variable from <mark style="color:purple;">.env</mark> and <mark style="color:purple;">.env.example</mark>. After that, we can also remove it from the **validation schema** in the <mark style="color:purple;">env.constants</mark> file, and even remove the <mark style="color:blue;">`expandVariables`</mark> option from the <mark style="color:blue;">`ConfigModule`</mark> configuration in the <mark style="color:blue;">`EnvModule`</mark>, as we're no longer using it.

Then, inside the <mark style="color:purple;">database.config</mark> file, we should extract all the credentials from the <mark style="color:purple;">.env</mark> file and then create the <mark style="color:blue;">`url`</mark> from it. Finally, use it in the <mark style="color:blue;">`config`</mark>.

```typescript
const user = process.env.DATABASE_USER;
const password = process.env.DATABASE_PASSWORD;
const host = process.env.DATABASE_HOST;
const port = process.env.DATABASE_PORT;
const name = process.env.DATABASE_NAME;

const url = `postgresql://${user}:${password}@${host}:${port}/${name}`;

const config = {
  // ...
  url,
  // ...
} as const satisfies TypeOrmModuleOptions;
```

{% hint style="info" %}
Remember to do the same in the <mark style="color:purple;">data-source</mark> file.
{% endhint %}

<mark style="color:green;">**Commit**</mark> - Generating database url in codebase
