// npm import * as AWS from 'aws-sdk'; import * as _ from 'lodash'; import * as assert from 'assert'; // app import { config, log } from '../../config'; export async function flushDynamoDBTable(): Promise { const dynamoDBClient: AWS.DynamoDB.DocumentClient = new AWS.DynamoDB.DocumentClient({ region: config.cacheDynamo.region, endpoint: config.cacheDynamo.endpoint, }); const input: AWS.DynamoDB.DocumentClient.ScanInput = { TableName: config.cacheDynamo.tableName as string, }; /* eslint-disable no-await-in-loop */ do { const output: AWS.DynamoDB.DocumentClient.ScanOutput = await dynamoDBClient.scan(input).promise(); input.ExclusiveStartKey = output.LastEvaluatedKey; for (const item of output.Items as AWS.DynamoDB.DocumentClient.ItemList) { await dynamoDBClient.delete({ TableName: config.cacheDynamo.tableName as string, Key: _.pick(item, ['pk', 'sk']), ReturnValues: 'NONE', ReturnConsumedCapacity: 'NONE', ReturnItemCollectionMetrics: 'NONE', }).promise(); } } while (input.ExclusiveStartKey != null); /* eslint-enable no-await-in-loop */ } export async function ensureTestDynamoDBTable(): Promise { const dynamoDBInstance = new AWS.DynamoDB({ region: config.cacheDynamo.region, endpoint: config.cacheDynamo.endpoint, }); try { await dynamoDBInstance.createTable({ TableName: config.cacheDynamo.tableName as string, BillingMode: 'PAY_PER_REQUEST', ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5, }, LocalSecondaryIndexes: [ { IndexName: 'pk_score', Projection: { ProjectionType: 'ALL', }, KeySchema: [ { KeyType: 'HASH', AttributeName: 'pk', }, { KeyType: 'RANGE', AttributeName: 'score', }, ], }, ], AttributeDefinitions: [ { AttributeName: 'pk', AttributeType: 'S', }, { AttributeName: 'score', AttributeType: 'N', }, { AttributeName: 'sk', AttributeType: 'S', }, ], KeySchema: [ { KeyType: 'HASH', AttributeName: 'pk', }, { KeyType: 'RANGE', AttributeName: 'sk', }, ], }).promise(); } catch (error) { if ((error as AWS.AWSError).code !== 'ResourceInUseException') { log.debug(error); assert.fail('Cannot create LocalStack DynamoDB Table'); } } }