-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamodbUtil.js
39 lines (36 loc) · 1.12 KB
/
dynamodbUtil.js
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
const path = require('path');
const util = require('util');
const { DynamoDB } = require('aws-sdk');
async function deriveDynamoDBClient(
{ DynamoDB: config },
emulatorPath = process.cwd(),
) {
if (config === false) {
// if false, we assume dynamo is not needed
return null;
} else if (!config.emulator) {
// if emulator is false, we create a client based on the config object
/* eslint-disable no-console */
console.log('dynamodb config: ', util.inspect(config, false, 2, true));
return new DynamoDB(config);
}
// start the dynamodb emulator
const dynamoEmulator = require('@conduitvc/dynamodb-emulator');
const dbPath = path.join(path.dirname(emulatorPath), '.dynamodb');
const { port } = config;
const emulator = await dynamoEmulator.launch({
dbPath,
port,
});
console.log(`dynamodb emulator port: ${port}, dbPath: ${dbPath}`);
process.on('SIGINT', () => {
// _ensure_ we do not leave java processes lying around.
emulator.terminate().then(() => {
process.exit(0);
});
});
return dynamoEmulator.getClient(emulator);
}
module.exports = {
deriveDynamoDBClient,
};