Skip to content

Commit

Permalink
Use aws-sdk-js-v3 which is more compact than v2
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Aug 17, 2021
1 parent 7b3fa6e commit 6f1f45b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
25 changes: 16 additions & 9 deletions index-another-concurrent-test.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
const {DocumentClient} = require('aws-sdk/clients/dynamodb');
const {DynamoDB} = require('@aws-sdk/client-dynamodb');
const {DynamoDBDocument} = require('@aws-sdk/lib-dynamodb');

const ddb = new DocumentClient({
convertEmptyValues: true,
endpoint: 'localhost:8000',
sslEnabled: false,
region: 'local-env'
});
const ddb = DynamoDBDocument.from(
new DynamoDB({
endpoint: 'http://localhost:8000',
tls: false,
region: 'local-env'
}),
{
marshallOptions: {
convertEmptyValues: true
}
}
);

it('should insert item into another table concurrently', async () => {
await ddb.put({TableName: 'users', Item: {id: '1', hello: 'world'}}).promise();
await ddb.put({TableName: 'users', Item: {id: '1', hello: 'world'}});

const {Item} = await ddb.get({TableName: 'users', Key: {id: '1'}}).promise();
const {Item} = await ddb.get({TableName: 'users', Key: {id: '1'}});

expect(Item).toEqual({
id: '1',
Expand Down
25 changes: 16 additions & 9 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
const {DocumentClient} = require('aws-sdk/clients/dynamodb');
const {DynamoDB} = require('@aws-sdk/client-dynamodb');
const {DynamoDBDocument} = require('@aws-sdk/lib-dynamodb');

const ddb = new DocumentClient({
convertEmptyValues: true,
endpoint: 'localhost:8000',
sslEnabled: false,
region: 'local-env'
});
const ddb = DynamoDBDocument.from(
new DynamoDB({
endpoint: 'http://localhost:8000',
tls: false,
region: 'local-env'
}),
{
marshallOptions: {
convertEmptyValues: true
}
}
);

it('should insert item into table', async () => {
await ddb.put({TableName: 'files', Item: {id: '1', hello: 'world'}}).promise();
await ddb.put({TableName: 'files', Item: {id: '1', hello: 'world'}});

const {Item} = await ddb.get({TableName: 'files', Key: {id: '1'}}).promise();
const {Item} = await ddb.get({TableName: 'files', Key: {id: '1'}});

expect(Item).toEqual({
id: '1',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@
"dynamodb-local": "0.0.31"
},
"devDependencies": {
"@aws-sdk/client-dynamodb": "^3.26.0",
"@aws-sdk/lib-dynamodb": "^3.26.0",
"@shelf/eslint-config": "0.19.0",
"@shelf/prettier-config": "0.0.7",
"aws-sdk": "2.631.0",
"eslint": "7.6.0",
"husky": "4.2.5",
"jest": "26.2.2",
Expand Down
10 changes: 5 additions & 5 deletions setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {resolve} = require('path');
const cwd = require('cwd');
const DynamoDB = require('aws-sdk/clients/dynamodb');
const {DynamoDB} = require('@aws-sdk/client-dynamodb');
const DynamoDbLocal = require('dynamodb-local');
const debug = require('debug')('jest-dynamodb');

Expand All @@ -22,8 +22,8 @@ module.exports = async function () {
} = typeof config === 'function' ? await config() : config;

const dynamoDB = new DynamoDB({
endpoint: `localhost:${port}`,
sslEnabled: false,
endpoint: `http://localhost:${port}`,
tls: false,
region: 'local-env',
...clientConfig
});
Expand All @@ -32,7 +32,7 @@ module.exports = async function () {

try {
const {TableNames: tableNames} = await Promise.race([
dynamoDB.listTables().promise(),
dynamoDB.listTables({}),
new Promise(resolve => setTimeout(resolve, 1000))
]);
await deleteTables(dynamoDB, tableNames); // cleanup leftovers
Expand All @@ -53,7 +53,7 @@ module.exports = async function () {
};

async function createTables(dynamoDB, tables) {
return Promise.all(tables.map(table => dynamoDB.createTable(table).promise()));
return Promise.all(tables.map(table => dynamoDB.createTable(table)));
}

async function deleteTables(dynamoDB, tableNames) {
Expand Down
6 changes: 2 additions & 4 deletions teardown.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ module.exports = async function (jestArgs) {
}
} else {
const dynamoDB = global.__DYNAMODB_CLIENT__;
const {TableNames: tableNames} = await dynamoDB.listTables().promise();
await Promise.all(
tableNames.map(tableName => dynamoDB.deleteTable({TableName: tableName}).promise())
);
const {TableNames: tableNames} = await dynamoDB.listTables();
await Promise.all(tableNames.map(tableName => dynamoDB.deleteTable({TableName: tableName})));
}
};

0 comments on commit 6f1f45b

Please sign in to comment.