Skip to content

Instantly share code, notes, and snippets.

@radzionc
Created February 2, 2018 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radzionc/6f1f8e5912863b6bc7ddb3abfd94ba61 to your computer and use it in GitHub Desktop.
Save radzionc/6f1f8e5912863b6bc7ddb3abfd94ba61 to your computer and use it in GitHub Desktop.
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