forked from totemstech/node-dynamodb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathddbCommander.js
executable file
·86 lines (75 loc) · 2.19 KB
/
ddbCommander.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
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
var program = require('commander');
program
.version('0.0.1')
.option('-l, --list', 'List Tables')
.option('-s, --scan [table]', 'Scans a table')
.option('-g, --get [table]', 'GetItem from a table')
.option('-c, --create', 'Creates a table')
.parse(process.argv);
var ddb = require('./lib/ddb').ddb({ accessKeyId: process.env.AWS_KEY,
secretAccessKey: process.env.AWS_SECRET });
function handleResponse(err, data) {
if (err) {
console.log('There was an error with your request:');
console.log(err.message);
exit();
}
};
function list() {
ddb.listTables({}, function(err, res) {
console.log('Tables: %s', JSON.stringify(res, null, '\t'));
});
};
function scan(table) {
console.log('scanning: %s', program.scan);
program.prompt('Enter attributes []: ', function(attributes) {
program.prompt('Enter limit (10): ', Number, function(limit) {
program.confirm('count?: ', function(count) {
program.prompt('Enter filter {}: ', function(filter) {
console.log('attributes: %s filter: %s', attributes, filter);
if (attributes) {
attributes = JSON.parse(attributes);
}
if (filter) {
filter = JSON.parse(filter);
}
limit = limit || 10;
var options = {
attributesToGet: attributes,
limit: limit,
count: count,
filter: filter
};
ddb.scan(program.scan, options, function(err, res, cap) {
console.log('%s', JSON.stringify(res, null, '\t'));
console.log('cap used: %s', JSON.stringify(cap, null, '\t'));
scan(table);
});
});
});
});
});
};
function getItem(table) {
program.prompt('Enter key: ', function(key) {
var start = new Date();
ddb.getItem(table, key, null, false, function(err, item) {
if (err) {
return handleResponse(err, item);
}
console.dir(item);
console.log('--took: %dms', new Date() - start);
getItem(table);
});
});
};
if (program.list) {
list();
}
if (program.scan) {
scan(program.scan);
}
if (program.get) {
getItem(program.get);
}