|
1 | 1 | import AWS from 'aws-sdk'; |
2 | | -import Promise from 'bluebird'; |
3 | 2 |
|
4 | 3 | export default ({awsClient: aws, region, endpoint, tableName: TableName, |
5 | 4 | consistentRead: ConsistentRead = true, |
6 | 5 | readCapacity: ReadCapacityUnits = 5, |
7 | 6 | writeCapacity: WriteCapacityUnits = 5}) => { |
8 | 7 | const awsClient = aws || new AWS.DynamoDB({region, endpoint}); |
9 | 8 |
|
10 | | - const deleteItem = id => Promise.fromCallback(cb => |
11 | | - awsClient.deleteItem({TableName, Key: {id: {S: id}}}, cb) |
12 | | - ); |
| 9 | + const deleteItem = id => awsClient.deleteItem({TableName, Key: {id: {S: id}}}).promise(); |
13 | 10 |
|
14 | 11 | return { |
15 | 12 | init: (autoCreate = false) => { |
16 | | - const describe = Promise.fromCallback(cb => awsClient.describeTable({TableName}, cb)); |
| 13 | + const describe = awsClient.describeTable({TableName}).promise(); |
17 | 14 | if (autoCreate) { |
18 | | - return describe.catch(() => Promise.fromCallback(cb => |
| 15 | + return describe.catch(() => |
19 | 16 | awsClient.createTable({ |
20 | 17 | TableName, |
21 | 18 | AttributeDefinitions: [{AttributeName: 'id', AttributeType: 'S'}], |
22 | 19 | KeySchema: [{AttributeName: 'id', KeyType: 'HASH'}], |
23 | 20 | ProvisionedThroughput: {ReadCapacityUnits, WriteCapacityUnits} |
24 | | - }, cb)) |
| 21 | + }).promise() |
25 | 22 | ); |
26 | 23 | } |
27 | 24 | return describe; |
28 | 25 | }, |
29 | 26 |
|
30 | | - get: id => Promise.fromCallback(cb => |
31 | | - awsClient.getItem({TableName, ConsistentRead, Key: {id: {S: id}}}, cb) |
32 | | - ).then(data => { |
33 | | - if (data.Item && data.Item.content && data.Item.expires) { |
34 | | - return { |
35 | | - content: JSON.parse(data.Item.content.S.toString()), |
36 | | - expires: Number(data.Item.expires.N) |
37 | | - }; |
38 | | - } |
39 | | - return null; |
40 | | - }), |
| 27 | + get: id => awsClient.getItem({TableName, ConsistentRead, Key: {id: {S: id}}}).promise() |
| 28 | + .then(data => { |
| 29 | + if (data.Item && data.Item.content && data.Item.expires) { |
| 30 | + return { |
| 31 | + content: JSON.parse(data.Item.content.S.toString()), |
| 32 | + expires: Number(data.Item.expires.N) |
| 33 | + }; |
| 34 | + } |
| 35 | + return null; |
| 36 | + }), |
41 | 37 |
|
42 | | - put: (id, expires, content) => Promise.fromCallback(cb => |
| 38 | + put: (id, expires, content) => |
43 | 39 | awsClient.putItem({ |
44 | 40 | TableName, Item: { |
45 | 41 | id: {S: id}, |
46 | 42 | expires: {N: expires.toString()}, |
47 | 43 | content: {S: JSON.stringify(content)} |
48 | 44 | } |
49 | | - }, cb) |
50 | | - ), |
| 45 | + }).promise(), |
51 | 46 |
|
52 | | - setExpires: (id, expires) => Promise.fromCallback(cb => |
| 47 | + setExpires: (id, expires) => |
53 | 48 | awsClient.updateItem({ |
54 | 49 | TableName, |
55 | 50 | Key: {id: {S: id}}, |
56 | 51 | UpdateExpression: 'SET expires = :value', |
57 | 52 | ExpressionAttributeValues: {':value': {N: expires.toString()}} |
58 | | - }, cb) |
59 | | - ), |
| 53 | + }).promise(), |
60 | 54 |
|
61 | 55 | delete: deleteItem, |
62 | 56 |
|
63 | 57 | deleteExpired: when => { |
64 | | - const scan = startKey => Promise.fromCallback(cb => |
| 58 | + const scan = startKey => |
65 | 59 | awsClient.scan({ |
66 | 60 | TableName, |
67 | 61 | FilterExpression: 'expires < :when', |
68 | 62 | ExpressionAttributeValues: {':when': {N: when.toString()}}, |
69 | 63 | ProjectionExpression: 'id', |
70 | 64 | ExclusiveStartKey: startKey |
71 | | - }, cb) |
72 | | - ); |
| 65 | + }).promise(); |
73 | 66 |
|
74 | 67 | const deletePage = ({scanned, deleted}, startKey = null) => |
75 | 68 | // perform the scan to find expired sessions |
76 | 69 | scan(startKey) |
77 | | - // use Promise.each to delete each of them one by one so we don't use all the |
78 | | - // provisioned capacity |
79 | | - .then(data => Promise.each(data.Items.map(i => i.id.S), deleteItem) |
| 70 | + .then(data => Promise.all(data.Items.map(i => i.id.S), deleteItem) |
80 | 71 | // once all the sessions are deleted, work out if there are more results to scan |
81 | 72 | .then(ids => { |
82 | 73 | const lastKey = data.LastEvaluatedKey; |
|
0 commit comments