Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding stream support to Dynalite #70

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions actions/deleteItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = function deleteItem(store, data, cb) {

itemDb.del(key, function(err) {
if (err) return cb(err)
store.emit(table, key, existingItem)
cb(null, returnObj)
})
})
Expand Down
1 change: 1 addition & 0 deletions actions/putItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = function putItem(store, data, cb) {

itemDb.put(key, data.Item, function(err) {
if (err) return cb(err)
store.emit(table, key, existingItem, data.Item)
cb(null, returnObj)
})
})
Expand Down
1 change: 1 addition & 0 deletions actions/updateItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = function updateItem(store, data, cb) {

itemDb.put(key, item, function(err) {
if (err) return cb(err)
store.emit(table, key, oldItem, item)
cb(null, returnObj)
})
})
Expand Down
52 changes: 52 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ function dynalite(options) {

// Ensure we close DB when we're closing the server too
var httpServerClose = server.close, httpServerListen = server.listen
var streamHandler = {};
server.close = function(cb) {
streamHandler = {};
store.db.close(function(err) {
if (err) return cb(err)
// Recreate the store if the user wants to listen again
Expand All @@ -45,6 +47,56 @@ function dynalite(options) {
})
}

store.emit = function(tableDef, meh, before, after) {
var cause = (Math.random()*100000).toString(16);
var table = tableDef.TableName;
var key = tableDef.KeySchema.map(function(ks) { return ks.AttributeName }).reduce(function(key, attr) {
key[attr] = (before || after)[attr];
return key;
}, {});
var payload = {
Records: [{
dynamodb: {
Keys: key,
OldImage: before,
NewImage: after
},
eventName: before === undefined ? 'INSERT' : (after === undefined ? 'REMOVE' : 'MODIFY')
}]
}

var handlers = streamHandler[table] || [];
handlers.forEach(function(handler, idx) {
var id = (Math.random()*10000*(idx+1)).toString(16);
handler.pending++;
setTimeout(send, 0);
function send() {
if (handler.running) return setTimeout(send, 0);
handler.running = true;
handler.fn(payload, {}, function(err) {
if (err) throw err;
handler.pending--;
handler.running = false;
});
}
});
}

server.pendingEvents = function() {
return Object.keys(streamHandler).reduce(function(memo, table) {
return memo + streamHandler[table].reduce(function(h) { return h.pending; });
}, 0);
}

server.stream = function(table, handler) {
streamHandler[table] = streamHandler[table] || []
streamHandler[table].push({
running: false,
pending: 0,
fn: handler
});
}

return server
}

Expand Down