forked from xarvh/dynamo-table-extensions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
59 lines (44 loc) · 1.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var async = require('async')
var dynamoTable = require('./slim')
module.exports = dynamoTable
var proto = dynamoTable.DynamoTable.prototype
// Ensures that no more than capacityRatio * writeCapacity items are written per second
proto.throttledBatchWrite = function(capacityRatio, items, cb) {
if (!(capacityRatio > 0)) return cb(new Error('non-positive capacityRatio'))
var self = this
self.describeTable(function(err, info) {
if (err) return cb(err)
var itemsPerSecond = Math.ceil(info.ProvisionedThroughput.WriteCapacityUnits * capacityRatio);
var written = 0;
var ready = true;
var waitAndWrite = function(cb) {
async.until(function() {return ready}, function(cb) {setTimeout(cb, 10)}, function(err) {
if (err) return cb(err)
ready = false
setTimeout(function() {ready = true}, 1000)
var write = items.slice(written, written + itemsPerSecond)
self.batchWrite(write, function(err) {
if (err) return cb(err)
written += write.length;
cb();
})
})
}
async.whilst(function() {return written < items.length}, waitAndWrite, cb)
})
}
proto.truncate = function(cb) {
async.series([this.deleteTable.bind(this), this.createTable.bind(this)], cb)
}
proto.addNew = function(record, cb) {
var key = Array.isArray(this.key) ? this.key[0] : this.key;
if (record[key])
return this.put(record, cb);
var self = this;
// this.nextId is added by `dynamo-table-id`, which must be loaded
this.nextId(function(err, id) {
if (err) return cb(err);
record[key] = id;
self.put(record, cb);
});
}