forked from jed/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScan.js
74 lines (57 loc) · 1.48 KB
/
Scan.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var Predicates = require("./Predicates")
, Attributes = require("./Attributes")
function Scan(table, database) {
this.TableName = table
Object.defineProperty(
this, "database",
{value: database, enumerable: false}
)
}
Scan.prototype = {
filter: function(predicates) {
this.ScanFilter = new Predicates(predicates)
return this
},
get: function() {
this.AttributesToGet = Array.prototype.concat.apply([], arguments)
return this
},
count: function() {
this.Count = true
return this
},
limit: function(limit) {
if(isNaN(limit) || ((limit|0) != limit) || (limit|0) < 1) {
throw new Error("Limit should be an natural number");
}
this.Limit = limit;
return this;
},
fetch: function(cb) {
var self = this
, response = []
!function loop(ExclusiveStartKey) {
self.database.request(
"Scan",
self,
function fetch(err, data) {
if (err) return cb(err)
if (self.Count) return cb(null, data.Count)
data.Items.forEach(function(item) {
response.push(Attributes.prototype.parse(item))
})
if (data.LastEvaluatedKey) {
if (self.Limit != null && self.Limit < response.length) {
loop(data.LastEvaluatedKey)
} else {
cb(null, response)
}
} else {
cb(null, response)
}
}
)
}(this.ExclusiveStartKey)
}
}
module.exports = Scan