-
Notifications
You must be signed in to change notification settings - Fork 8
/
database.ts
43 lines (37 loc) · 1.22 KB
/
database.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Knex from 'knex';
// @ts-ignore
import knexDataApiClient from 'knex-aurora-data-api-client';
import { SSMClient, GetParameterCommand } from '@aws-sdk/client-ssm';
const region = process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'eu-west-1';
const ssm = new SSMClient({
region,
});
const client = (params?: any) => {
const _knex = Knex({
client: knexDataApiClient.postgres,
connection: Object.assign(
{
// @ts-ignore
secretArn: process.env.SECRET_ARN,
resourceArn: process.env.CLUSTER_ARN,
database: 'postgres',
region,
},
params.connection || {}
),
});
return _knex;
};
export const knex = async () => {
const clusterArnParameter = await ssm.send(new GetParameterCommand({ Name: '/examples/infra/database/cluster/arn' }));
const secretParameter = await ssm.send(new GetParameterCommand({ Name: '/examples/infra/database/secret/arn' }));
if (!clusterArnParameter.Parameter?.Value || !secretParameter.Parameter?.Value) {
throw new Error('Failed to fetch SSM parameters');
}
return client({
connection: {
secretArn: secretParameter.Parameter?.Value,
resourceArn: clusterArnParameter.Parameter?.Value,
},
});
};