forked from launchdarkly/node-server-sdk-dynamodb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add key prefix option (launchdarkly#2)
- Loading branch information
1 parent
3e03505
commit e4b65c6
Showing
4 changed files
with
114 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
function paginationHelper(params, executeFn, startKey) { | ||
return new Promise(function(resolve, reject) { | ||
if (startKey) { | ||
params['ExclusiveStartKey'] = startKey; | ||
} | ||
executeFn(params, function(err, data) { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
|
||
if ('LastEvaluatedKey' in data) { | ||
paginationHelper(params, executeFn, data['LastEvaluatedKey']).then(function (nextPageItems) { | ||
resolve(data.Items.concat(nextPageItems)); | ||
}); | ||
} else { | ||
resolve(data.Items); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function queryHelper(client, params, startKey) { | ||
return paginationHelper(params, function(params, cb) { return client.query(params, cb); }, startKey); | ||
} | ||
|
||
function batchWrite(client, tableName, ops) { | ||
var writePromises = []; | ||
// BatchWrite can only accept 25 items at a time, so split up the writes into batches of 25. | ||
for (var i = 0; i < ops.length; i += 25) { | ||
var requestItems = {}; | ||
requestItems[tableName] = ops.slice(i, i+25); | ||
writePromises.push(new Promise(function(resolve, reject) { | ||
client.batchWrite({ | ||
RequestItems: requestItems | ||
}, function(err) { | ||
err ? reject(err) : resolve(); | ||
}); | ||
})); | ||
} | ||
return writePromises; | ||
} | ||
|
||
module.exports = { | ||
batchWrite: batchWrite, | ||
paginationHelper: paginationHelper, | ||
queryHelper: queryHelper | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters