Skip to content
This repository was archived by the owner on Jan 21, 2019. It is now read-only.
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
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
"url": "https://github.com/packetloop/connect-dynamodb-session/issues"
},
"peerDependencies": {
"aws-sdk": "2",
"bluebird": "3"
"aws-sdk": "2"
},
"devDependencies": {
"aws-sdk": "2.3.6",
Expand All @@ -39,7 +38,6 @@
"babel-plugin-add-module-exports": "0.2.1",
"babel-preset-es2015": "6.6.0",
"blue-tape": "0.2.0",
"bluebird": "3.3.5",
"codacy-coverage": "1.1.3",
"eslint": "2.8.0",
"eslint-config-airbnb": "8.0.0",
Expand Down
51 changes: 21 additions & 30 deletions src/dynamo.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,73 @@
import AWS from 'aws-sdk';
import Promise from 'bluebird';

export default ({awsClient: aws, region, endpoint, tableName: TableName,
consistentRead: ConsistentRead = true,
readCapacity: ReadCapacityUnits = 5,
writeCapacity: WriteCapacityUnits = 5}) => {
const awsClient = aws || new AWS.DynamoDB({region, endpoint});

const deleteItem = id => Promise.fromCallback(cb =>
awsClient.deleteItem({TableName, Key: {id: {S: id}}}, cb)
);
const deleteItem = id => awsClient.deleteItem({TableName, Key: {id: {S: id}}}).promise();

return {
init: (autoCreate = false) => {
const describe = Promise.fromCallback(cb => awsClient.describeTable({TableName}, cb));
const describe = awsClient.describeTable({TableName}).promise();
if (autoCreate) {
return describe.catch(() => Promise.fromCallback(cb =>
return describe.catch(() =>
awsClient.createTable({
TableName,
AttributeDefinitions: [{AttributeName: 'id', AttributeType: 'S'}],
KeySchema: [{AttributeName: 'id', KeyType: 'HASH'}],
ProvisionedThroughput: {ReadCapacityUnits, WriteCapacityUnits}
}, cb))
}).promise()
);
}
return describe;
},

get: id => Promise.fromCallback(cb =>
awsClient.getItem({TableName, ConsistentRead, Key: {id: {S: id}}}, cb)
).then(data => {
if (data.Item && data.Item.content && data.Item.expires) {
return {
content: JSON.parse(data.Item.content.S.toString()),
expires: Number(data.Item.expires.N)
};
}
return null;
}),
get: id => awsClient.getItem({TableName, ConsistentRead, Key: {id: {S: id}}}).promise()
.then(data => {
if (data.Item && data.Item.content && data.Item.expires) {
return {
content: JSON.parse(data.Item.content.S.toString()),
expires: Number(data.Item.expires.N)
};
}
return null;
}),

put: (id, expires, content) => Promise.fromCallback(cb =>
put: (id, expires, content) =>
awsClient.putItem({
TableName, Item: {
id: {S: id},
expires: {N: expires.toString()},
content: {S: JSON.stringify(content)}
}
}, cb)
),
}).promise(),

setExpires: (id, expires) => Promise.fromCallback(cb =>
setExpires: (id, expires) =>
awsClient.updateItem({
TableName,
Key: {id: {S: id}},
UpdateExpression: 'SET expires = :value',
ExpressionAttributeValues: {':value': {N: expires.toString()}}
}, cb)
),
}).promise(),

delete: deleteItem,

deleteExpired: when => {
const scan = startKey => Promise.fromCallback(cb =>
const scan = startKey =>
awsClient.scan({
TableName,
FilterExpression: 'expires < :when',
ExpressionAttributeValues: {':when': {N: when.toString()}},
ProjectionExpression: 'id',
ExclusiveStartKey: startKey
}, cb)
);
}).promise();

const deletePage = ({scanned, deleted}, startKey = null) =>
// perform the scan to find expired sessions
scan(startKey)
// use Promise.each to delete each of them one by one so we don't use all the
// provisioned capacity
.then(data => Promise.each(data.Items.map(i => i.id.S), deleteItem)
.then(data => Promise.all(data.Items.map(i => i.id.S), deleteItem)
// once all the sessions are deleted, work out if there are more results to scan
.then(ids => {
const lastKey = data.LastEvaluatedKey;
Expand Down
Loading