Skip to content

Commit

Permalink
Add support for async configs
Browse files Browse the repository at this point in the history
defineConfig now accepts a promise for a config or function that returns one - fixes drizzle-team#1982
  • Loading branch information
guillaumervls committed Feb 5, 2025
1 parent 1dd14ac commit 78c7c13
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion drizzle-kit/src/cli/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ export const drizzleConfigFromFile = async (
unregister();

// --- get response and then check by each dialect independently
const res = configCommonSchema.safeParse(content);
const res = configCommonSchema.safeParse(await content);
if (!res.success) {
console.log(res.error);
if (!('dialect' in content)) {
Expand Down
4 changes: 2 additions & 2 deletions drizzle-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,6 @@ export type Config =
*
* See https://orm.drizzle.team/kit-docs/config-reference#strict
*/
export function defineConfig(config: Config) {
return config;
export function defineConfig(config: Config | Promise<Config> | (() => Config | Promise<Config>)) {
return typeof config === 'function' ? config() : config;
}
14 changes: 14 additions & 0 deletions drizzle-kit/tests/cli-async-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { test as brotest } from '@drizzle-team/brocli';
import { assert, expect, test } from 'vitest';
import { exportRaw } from '../src/cli/schema';

test('export with async config', async (t) => {
const res = await brotest(exportRaw, '');

if (res.type !== 'handler') assert.fail(res.type, 'handler');
expect(res.options).toStrictEqual({
dialect: 'postgresql',
schema: './schema.ts',
sql: true,
});
});
9 changes: 9 additions & 0 deletions drizzle-kit/tests/cli/async.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from '../../src';

export default defineConfig(async () => ({
schema: './schema.ts',
dialect: 'postgresql',
dbCredentials: {
url: 'postgresql://postgres:[email protected]:5432/db',
},
}));

0 comments on commit 78c7c13

Please sign in to comment.