Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions docs/aws/dynamo.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ const { aws: { dynamo } } = require( 'lambda-toolkit' );
import { aws } from 'lambda-toolkit';
const { dynamo } = aws;

const item = await dynamo.get( table, { pk: '123' } );
// syntax sugar
const item = await dynamo.get( 'my-table', { pk: '123' } );

// native args
const item = await dynamo.get( 'my-table', {
TableName: 'my-table',
Key: { pk: '123' },
ConsistentRead: true
} );
```

#### Arguments
Expand Down Expand Up @@ -98,7 +106,7 @@ const items = [
{ id: '2', name: 'Roger Corman' },
{ id: '3', name: 'Menahem Golan' }
]
await dynamo.putBatch( table, items );
await dynamo.putBatch( 'my-table', items );
```

It is possible to pass any number of items to upsert, the function will manage the limits and do many operations if necessary, the function will also retry failed items if possible until all items are upserted.
Expand Down Expand Up @@ -146,7 +154,7 @@ import { aws } from 'lambda-toolkit';
const { dynamo } = aws;

// syntax sugar
await dynamo.put( table, { id: '123', name: 'Roger' } );
await dynamo.put( 'my-table', { id: '123', name: 'Roger' } );

// native args
await dynamo.put( {
Expand Down Expand Up @@ -211,7 +219,7 @@ const keys = [
{ id: '2' },
{ id: '3' }
]
await dynamo.removeBatch( table, keys );
await dynamo.removeBatch( 'my-table', keys );
```

It is possible to pass any number of items to remove, the function will manage the limits and do many operations if necessary, the function will also retry failed items if possible until all items are removed.
Expand Down Expand Up @@ -248,7 +256,7 @@ The AWS policy statement to use this function is:

### `fn` remove

This function is an abstraction for DynamoDB's `remove` action, using `RemoveCommand`. It will remove a single record.
This function is an abstraction for DynamoDB's `remove` action, using `DeleteCommand`. It will remove a single record by its key, either with a convenient syntax sugar, or with the native arguments.

```js
// CJS
Expand All @@ -258,19 +266,31 @@ const { aws: { dynamo } } = require( 'lambda-toolkit' );
import { aws } from 'lambda-toolkit';
const { dynamo } = aws;

const removedItem = await dynamo.remove( table, { id: '123' } );
// syntax sugar
const removedItem = await dynamo.remove( 'my-table', { id: '123' } );

// native args
const removedItem = await dynamo.remove( {
TableName: 'my-table',
ConditionExpression: 'attribute_exists(id)',
Key: { id: '123' }
} );
```

#### Arguments

|Name|Type|Description|Default|
|---|---|---|---|
|_If first argument is object_|
|nativeArgs|Object|The `lib-dynamodb` SDK [DeleteCommand arguments](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-lib-dynamodb/Class/DeleteCommand/)||
|_If first argument is string_|
|tableName|String|The name of the table||
|key|Object|An object containing the key (or keys) properties of the record to retrieve||
|key|Object|An object containing the key (or keys) properties of the record to remove||

#### Return

It returns the removed item.
- When using the syntax sugar, returns the removed item, if any, or undefined
- When using the native args, returns what is specified in the `ReturnValues` field

#### Permission

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lambda-toolkit",
"version": "2.0.2",
"version": "2.1.0",
"description": "A set of tools to help and simplify Node Js code development for AWS Lambdas",
"keywords": [
"AWS",
Expand Down
21 changes: 15 additions & 6 deletions src/aws/dynamo/remove.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { DeleteCommand } from '@aws-sdk/lib-dynamodb';

export const remove = async ( client, tableName, key ) => {
const { Attributes: item } = await client.send( new DeleteCommand( {
const parseArgs = args => {
// native args mode
if ( args[0] instanceof Object ) {
return args[0];
}
// sugar mode
return {
ReturnValues: 'ALL_OLD',
TableName: tableName,
Key: key
} ) );
return item;
TableName: args[0],
Key: args[1]
};
};

export const remove = async ( client, ...args ) => {
const response = await client.send( new DeleteCommand( parseArgs( args ) ) );
return response.Attributes;
};
68 changes: 68 additions & 0 deletions src/aws/dynamo/remove.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { describe, it, mock, afterEach } from 'node:test';
import { strictEqual, deepStrictEqual } from 'node:assert';

const commandInstance = {};
const constructorMock = mock.fn( () => commandInstance );

mock.module( '@aws-sdk/lib-dynamodb', {
namedExports: {
DeleteCommand: new Proxy( class DeleteCommand {}, {
construct( _, args ) {
return constructorMock( ...args );
}
} )
}
} );

const { remove } = await import( './remove.js' );

const client = {
send: mock.fn()
};

const tableName = 'table';
const key = { id: '123' };
const item = { id: '123', value: 'foo' };

describe( 'Dynamo Remove Spec', () => {
afterEach( () => {
mock.restoreAll();
client.send.mock.resetCalls();
constructorMock.mock.resetCalls();
} );

describe( 'Sugar mode', () => {
it( 'Should remove an item', async () => {
client.send.mock.mockImplementation( () => ( { Attributes: item } ) );

const result = await remove( client, tableName, key );

deepStrictEqual( result, item );
strictEqual( client.send.mock.calls.length, 1 );
deepStrictEqual( client.send.mock.calls[0].arguments[0], commandInstance );
strictEqual( constructorMock.mock.calls.length, 1 );
deepStrictEqual( constructorMock.mock.calls[0].arguments[0], {
ReturnValues: 'ALL_OLD',
TableName: tableName,
Key: key
} );
} );
} );

describe( 'Native mode', () => {
it( 'Should remove an item', async () => {
client.send.mock.mockImplementation( () => ( { Attributes: item } ) );

const result = await remove( client, { TableName: tableName, Key: key } );

deepStrictEqual( result, item );
strictEqual( client.send.mock.calls.length, 1 );
deepStrictEqual( client.send.mock.calls[0].arguments[0], commandInstance );
strictEqual( constructorMock.mock.calls.length, 1 );
deepStrictEqual( constructorMock.mock.calls[0].arguments[0], {
TableName: tableName,
Key: key
} );
} );
} );
} );