UNPKG

testcontainers-dynamodb

Version:

Testcontainers DynamoDB is a NodeJS library that supports tests, providing lightweight, throwaway instances of DynamoDB local database

99 lines 3.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DynamoDBContainer = exports.StartedDynamoDBContainer = void 0; const testcontainers_1 = require("testcontainers"); const aws_sdk_1 = require("aws-sdk"); class StartedDynamoDBContainer { constructor(startedContainer, initData) { this.startedContainer = startedContainer; this.initData = initData; } exec(command) { return this.startedContainer.exec(command); } getContainerIpAddress() { return this.startedContainer.getContainerIpAddress(); } getId() { return this.startedContainer.getId(); } getMappedPort(port) { return this.startedContainer.getMappedPort(port); } getName() { return this.startedContainer.getName(); } logs() { return this.startedContainer.logs(); } stop(options) { return this.startedContainer.stop(options); } endpointUrl() { return `http://${this.startedContainer.getContainerIpAddress()}:${this.startedContainer.getMappedPort(DynamoDBContainer.MAPPED_PORT)}`; } get clientConfig() { return { endpoint: this.endpointUrl(), region: 'eu-central-1', credentials: { accessKeyId: 'dummy', secretAccessKey: 'dummy', }, }; } createDocumentClient() { return new aws_sdk_1.DynamoDB.DocumentClient(this.clientConfig); } createDynamoClient() { return new aws_sdk_1.DynamoDB(this.clientConfig); } async resetData(overrideData) { const createTableClient = this.createDynamoClient(); const documentClient = this.createDocumentClient(); for (const tableStructure of (overrideData || this.initData)) { await createTableClient.deleteTable({ TableName: tableStructure.table.TableName }).promise().catch(err => { if (err.code !== 'ResourceNotFoundException') { throw err; } }); await createTableClient.createTable(tableStructure.table).promise(); if (tableStructure.items && tableStructure.items.length > 0) { const tableName = tableStructure.table.TableName; const putRequests = tableStructure.items.map(x => ({ PutRequest: { Item: x } })); for (const requests of this.chunkArray(putRequests, 25)) { await documentClient.batchWrite({ RequestItems: { [tableName]: requests, }, }).promise(); } } } } chunkArray(array, size) { let result = []; for (let i = 0; i < array.length; i += size) { let chunk = array.slice(i, i + size); result.push(chunk); } return result; } } exports.StartedDynamoDBContainer = StartedDynamoDBContainer; class DynamoDBContainer extends testcontainers_1.GenericContainer { constructor(initStructure = []) { super(DynamoDBContainer.IMAGE_NAME); this.initStructure = initStructure; this.withExposedPorts(DynamoDBContainer.MAPPED_PORT); } async start() { const startedContainer = new StartedDynamoDBContainer(await super.start(), this.initStructure); await startedContainer.resetData(); return startedContainer; } } exports.DynamoDBContainer = DynamoDBContainer; DynamoDBContainer.IMAGE_NAME = 'amazon/dynamodb-local'; DynamoDBContainer.MAPPED_PORT = 8000; //# sourceMappingURL=DynamoDBContainer.js.map