Somewhat old question but the answer is to either use the AWS.RDSDataService()
from the AWS SDK.
(See samples/setup here: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/RDSDataService.html)
Or to use a "wrapper" package which we opted for with the data-api-client
.
const dataApiClient = require('data-api-client');
const db = dataApiClient({
secretArn: process.env.RDS_AURORA_DATA_API_SECRET_ARN || '',
resourceArn: process.env.RDS_AURORA_DATA_API_ARN || '',
database: process.env.RDS_AURORA_DATA_API_DB || '',
region: 'eu-west-1',
options: {
endpoint:
process.env.IS_OFFLINE
? process.env.AURORA_LOCAL_ENDPOINT || 'http://0.0.0.0:8080'
: null
}
});
If you run a local DB and want to develop locally the options.endpoint
to run locally works great with the Docker container koxudaxi/local-data-api
.
Make sure PG Server is running and then run the Docker cmd:
docker run -d --rm -it --name my-data-api -p 8080:80 -e ENGINE=PostgreSQLJDBC -e POSTGRES_HOST=host.docker.internal -e POSTGRES_PORT=
5432 -e POSTGRES_USER=qipdb_user -e POSTGRES_PASSWORD=password -e RESOURCE_ARN=arn:aws:rds:eu-west-1:00000000000:cluster:dummy -e SECRET_ARN=arn:aws:
secretsmanager:eu-west-1:00000000000:secret:dummy koxudaxi/local-data-api
As the Data API only takes SQL we opted to use a "disconnected" knex
instance to produce the SQL (this is completely optional though as normal SQL works directly if preferred):
// knex is only use to produce SQL, it does not have any DB connection!
const connKnex = require('knex');
const knex = connKnex({ client: 'pg' });
Then you can use knex
to get the SQL, like:
async function getSomethingFromDB() {
const sql = await knex
.select('aColumn')
.from('aTable')
.where({
someColumn: 'isSomething'
})
.toString();
const obj = await db.query(sql);
return !obj.records[0] ? null : obj.records[0];
}