> For the complete documentation index, see [llms.txt](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/core-module-backend-development-with-nestjs/configuration/configuration-namespace-and-partial-registration.md).

# Configuration namespace and partial registration

It would be nice to have some type safety for the environment variables.

There is still another interesting improvement we can make in the database connection. Notice that we are accessing the URL directly from <mark style="color:blue;">`process.env`</mark>. Because of this, we cannot benefit from **code completion** and **type safety**. Fortunately, it is possible to store these variables in a type-safe manner through a **configuration namespace**.

Let's create one now, in the file <mark style="color:purple;">database</mark>/<mark style="color:purple;">config</mark>/<mark style="color:purple;">database.config</mark>.

```typescript
export default registerAs('database', () => ({
  url: process.env.DATABASE_URL,
}));
```

With this **configuration namespace**, we only need to access this variable directly from <mark style="color:blue;">`process.env`</mark> once, in order to properly type it. Now, wherever it should be actually used, it can be accessed in a completely type-safe manner.

Now, we should perform the following steps inside the configuration of the <mark style="color:blue;">`TypeOrmModule`</mark> in the <mark style="color:blue;">`DatabaseModule`</mark> in order to use this namespace.

{% hint style="info" %}
This is a very specific process, therefore it may be better to consider many of these steps as boilerplate instead of trying to understand them too deeply.
{% endhint %}

* First, insert it in the <mark style="color:blue;">`imports`</mark> array. After that, this set of variables will become available, but only here. This is called **partial registration**.

```typescript
imports: [ConfigModule.forFeature(databaseConfig)],
```

* Next, inject the <mark style="color:blue;">`KEY`</mark> property of this namespace.

```typescript
inject: [databaseConfig.KEY],
```

* After that, insert, as parameter of the factory, the object that actually has the typed field.

```typescript
databaseConfiguration: ConfigType<typeof databaseConfig>,
```

* After all the previous steps, we can now finally use a type-safe variable in the <mark style="color:blue;">`url`</mark> field.

```typescript
url: databaseConfiguration.url,
```

We should have the following result:

```typescript
TypeOrmModule.forRootAsync({
  imports: [ConfigModule.forFeature(databaseConfig)],
  inject: [databaseConfig.KEY],
  useFactory: (
    databaseConfiguration: ConfigType<typeof databaseConfig>,
  ) => ({
    type: 'postgres',
    url: databaseConfiguration.url,
    autoLoadEntities: true,
  }),
}),
```

<mark style="color:green;">**Commit**</mark> - Using configuration namespace and partial registration
