-
Notifications
You must be signed in to change notification settings - Fork 27
/
local_dynamo.js
49 lines (42 loc) · 1.77 KB
/
local_dynamo.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
40
41
42
43
44
45
46
47
48
49
const { DocumentClient } = require('aws-sdk/clients/dynamodb');
const Promise = require('bluebird');
const dynamo = require('dynamodb');
const dynamoLocal = require('dynamodb-local');
const Account = require('./src/db/schemas/account');
const EmailDomainBlacklist = require('./src/db/schemas/email_domain_blacklist');
const IdentityVerificationMethod = require('./src/db/schemas/identity_verification_method');
const RecoveryMethod = require('./src/db/schemas/recovery_method');
const LOCAL_DYNAMODB_HOST = process.env.LOCAL_DYNAMODB_HOST || 'localhost';
const LOCAL_DYNAMODB_PORT = process.env.LOCAL_DYNAMODB_PORT || 7877;
const TEST_DYNAMODB_PORT = process.env.TEST_DYNAMODB_PORT || 7879;
function createTables() {
const models = [Account, EmailDomainBlacklist, IdentityVerificationMethod, RecoveryMethod];
return Promise.all(models.map((model) => model.createTableAsync()))
.catch(() => {});
}
function overrideLocalDynamo({ port } = { port: LOCAL_DYNAMODB_PORT }) {
dynamo.documentClient(new DocumentClient({
convertEmptyValues: true,
endpoint: `${LOCAL_DYNAMODB_HOST}:${port}`,
sslEnabled: false,
region: 'local-env'
}));
}
async function initLocalDynamo({ dbPath, port } = {}) {
overrideLocalDynamo({ port });
await dynamoLocal.launch(port, dbPath, dbPath && ['-sharedDb']);
await createTables();
return {
port,
terminateLocalDynamo: () => {
console.warn(`Stopping local DynamoDB instance on port ${port}`);
dynamoLocal.stop(port);
},
};
}
module.exports = {
createTables,
initDevelopmentDynamo: () => initLocalDynamo({ dbPath: __dirname, port: LOCAL_DYNAMODB_PORT }),
initTestDynamo: () => initLocalDynamo({ port: TEST_DYNAMODB_PORT }),
overrideLocalDynamo,
};