How to retrieve database credentials from drizzle.config.ts

I'm attempting to retrieve Neon database credentials dynamically from drizzle.config.ts , instead of saving them in a .env, by running drizzle-kit push. The issue, both methods I tried rely on top-level await and throw Top-level await is currently not supported with the "cjs" output format exception. 1. @aws-sdk/client-ssm example:
const { SSMClient, GetParameterCommand } = require("@aws-sdk/client-ssm");

// Create an SSM client
const ssmClient = new SSMClient();

// Define the parameter name
const parameterName = "your-parameter-name";

// Create a command to get the parameter
const command = new GetParameterCommand({ Name: parameterName, WithDecryption: true });
const { SSMClient, GetParameterCommand } = require("@aws-sdk/client-ssm");

// Create an SSM client
const ssmClient = new SSMClient();

// Define the parameter name
const parameterName = "your-parameter-name";

// Create a command to get the parameter
const command = new GetParameterCommand({ Name: parameterName, WithDecryption: true });
OR 2. SST Secrets https://docs.sst.dev/config#top-level-await Is there a way to accomplish this from within drizzle.config.ts? I'm using "drizzle-kit": "^0.20.1" and "drizzle-orm": "^0.29.0"
Config | SST
Working with environment variables and secrets in SST.
5 Replies
t
t13mo ago
Maybe try using an IIFE:
(async () => {
const env = await getEnv();
})();
(async () => {
const env = await getEnv();
})();
Twiser
TwiserOP13mo ago
Thank you, I tried this but unfortunately it doesn't resolve in-time before defineConfig ends up running
(async () => {
const env = await getEnv();
})();

export default defineConfig({
schema: "./schema.ts",
driver: 'pg',
dbCredentials: {
connectionString: env,
},
verbose: true,
strict: true,
})
(async () => {
const env = await getEnv();
})();

export default defineConfig({
schema: "./schema.ts",
driver: 'pg',
dbCredentials: {
connectionString: env,
},
verbose: true,
strict: true,
})
t
t13mo ago
npm
synchronized-promise
Turn ES6 Promise into synchronize function call, a simple wrapper of deasync package. Latest version: 0.3.1, last published: 4 years ago. Start using synchronized-promise in your project by running npm i synchronized-promise. There are 27 other projects in the npm registry using synchronized-promise.
t
t13mo ago
I've also tried wrapping the whole config in an async function but it'll export a promise that way so seems like there is no other solution other than using this/reimplementing it yourself
Twiser
TwiserOP13mo ago
thanks for looking into it!
Want results from more Discord servers?
Add your server