Created
February 2, 2018 15:07
-
-
Save radzionc/6f1f8e5912863b6bc7ddb3abfd94ba61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config() | |
const localDynamo = require('local-dynamo') | |
const { setupAWS } = require('../src/utils/aws') | |
const recreateTables = async (silent = true) => { | |
const database = new AWS.DynamoDB() | |
const existingTables = await database | |
.listTables() | |
.promise() | |
.then(data => data.TableNames) | |
await Promise.all( | |
existingTables.map(TableName => | |
database | |
.deleteTable({ TableName }) | |
.promise() | |
.catch( | |
err => | |
!silent && | |
console.error('Delete fail: ', JSON.stringify(err, null, 2)) | |
) | |
.then( | |
data => | |
!silent && | |
data && | |
console.log('Deleted table: ', JSON.stringify(data, null, 2)) | |
) | |
) | |
) | |
const waiter = new AWS.ResourceWaiter(database, 'tableNotExists') | |
await Promise.all( | |
existingTables.map(TableName => | |
waiter.wait({ TableName, $waiter: { delay: 1 } }).promise() | |
) | |
) | |
await Promise.all( | |
TABLES_PARAMS.map(params => | |
database | |
.createTable(params) | |
.promise() | |
.catch( | |
err => | |
!silent && | |
console.error( | |
'Unable to create table: ', | |
JSON.stringify(err, null, 2) | |
) | |
) | |
.then( | |
data => | |
!silent && | |
data && | |
console.log('Database created: ', JSON.stringify(data, null, 2)) | |
) | |
) | |
) | |
const createdWaiter = new AWS.ResourceWaiter(database, 'tableExists') | |
await Promise.all( | |
TABLES_PARAMS.map(({ TableName }) => | |
createdWaiter.wait({ TableName, $waiter: { delay: 1 } }).promise() | |
) | |
) | |
} | |
module.exports = () => { | |
localDynamo.launch(null, process.env.DYNAMO_TESTING_PORT) | |
return recreateTables() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment