export async function getDatabaseParamsFromSSM()

in src/lib/ssm.ts [8:47]


export async function getDatabaseParamsFromSSM(ssm: SSM): Promise<DBConfig> {
	const dbConfigPath = `/support-reminders/db-config/${ssmStage}`;

	const ssmResponse = await ssm
		.getParametersByPath({
			Path: dbConfigPath,
			WithDecryption: true,
		})
		.promise();

	if (ssmResponse.Parameters) {
		const p = ssmResponse.Parameters;
		const url = p.find(({ Name }) => Name === `${dbConfigPath}/url`);
		const password = p.find(
			({ Name }) => Name === `${dbConfigPath}/password`,
		);
		const username = p.find(
			({ Name }) => Name === `${dbConfigPath}/username`,
		);

		if (
			url &&
			url.Value &&
			password &&
			password.Value &&
			username &&
			username.Value
		) {
			return {
				url: isRunningLocally()
					? 'jdbc:postgresql://localhost/contributions'
					: url.Value,
				password: password.Value,
				username: username.Value,
			};
		}
	}

	throw new Error(`Could not get config from SSM path ${dbConfigPath}`);
}