drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Async Configuration

Open
#1,187 5 comments 7 reactions 0 assignees View on GitHub
enhancement priority
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

At present drizzle has a traditional constructor/initializer with literal configuration properties. e.g.

```ts
const pool = new Pool({ connectionString: process.env.DB_URL! });
drizzle(pool, { schema })
```

Now consider that our DB Url is located in a secret manager/store, which requires an `async` request to fetch.

```ts
const dbUrl = await getRemoteSecret('DB_URL');
const pool = new Pool({ connectionString: dbUrl });
drizzle(pool, { schema })
```

In ES module land, this works without issue. However, in Node-CJS land this does not work. Our only alternative is to export an async `init` method and use that wherever we'd like to access our database. That is quite common and normal for folks who are still working in CJS. There is a pattern that could be used to alleviate the need for an async `init` method - allowing for asyn configuration. Bundlers like Vite, Webpack, and Rollup all support async configs for this kind of purpose, so there is some prior art there. One such implementation might look like:

```ts

drizzle(async () => {
const dbUrl = await getRemoteSecret('DB_URL');
const pool = new Pool({ connectionString: dbUrl });

return { client: pool, schema };
})
```

Given how the result of `drizzle` is typically used, that might require some fancy use of `Proxy`.

I noticed there were a few other issues open about top-level await and an older reverted commit which might be solved by this approach.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.