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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ dp.proc({
});
```

### deleteItem

```
dp.proc({
table: 'users',
action: 'delete',
key: {
id: 1
}
})
.then((item) => {
console.log(item); // null
});
```

### Multiple items

#### getItems as Promise Array
Expand Down
5 changes: 5 additions & 0 deletions lib/doc_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class DocClient {
.then(data => data.Attributes);
}

delete(params) {
return this.client.delete(params).promise()
.then(data => null);
}

batchWrite(params) {
return this.client.batchWrite(params).promise()
.then(data => data.UnprocessedItems);
Expand Down
26 changes: 25 additions & 1 deletion lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ class Processor {
return this._wrapFunc ? f : f();
}

/**
* deleteItem
* @param {string} table - TableName
* @param {object} key - Key
*/
delete(table, key) {
const params = {
TableName: table,
Key: key
};

const f = () => {
return this.docClient.delete(params)
.then(data => {
this.logger.info('Deleted %s from %s table', getKey(key), table);
return null;
});
};

return this._wrapFunc ? f : f();
}

/**
* batchWriteItems
* @param {String} table - table name
Expand Down Expand Up @@ -267,7 +289,9 @@ class Processor {
const useBatch = 'useBatch' in opts_ ? opts_.useBatch : true;
const table = data.table || opts_.table;

if (data.action === 'put' || data.item || data.items) {
if (data.action === 'delete') {
return this.delete(table, data.key);
} else if (data.action === 'put' || data.item || data.items) {
if (data.item) {
return this.put(table, data.item);
} else if (data.items) {
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": "dynamo-processor",
"version": "0.7.0",
"version": "0.8.0",
"description": "DynamoDB operation processor for Node.js",
"main": "main.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports.getDoc = function(id) {
TableName: TABLE,
Key: { id }
}).promise()
.then(data => data.Item)
.then(data => data.Item || null)
}

exports.putDoc = function(item) {
Expand Down
15 changes: 15 additions & 0 deletions test/processor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ describe('DynamoProcessor', () => {
});
});

it('deletes an item', () => {
return dp.proc({
action: 'delete',
table: 'tests',
key: { id: 2 }
})
.then(res => {
expect(res).to.be.null;
return helper.getDoc(2);
})
.then(dbItem => {
expect(dbItem).to.be.null
});
});

context('with initFields', () => {
it('updates an item with initial fields', () => {
return dp.proc({
Expand Down