forked from ryanfitz/vogels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.js
93 lines (75 loc) · 2.38 KB
/
query.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
87
88
89
90
91
92
93
'use strict';
var vogels = require('../index'),
util = require('util'),
_ = require('lodash'),
async = require('async'),
Joi = require('joi'),
AWS = vogels.AWS;
AWS.config.loadFromPath(process.env.HOME + '/.ec2/credentials.json');
var Account = vogels.define('example-query', {
hashKey : 'name',
rangeKey : 'email',
timestamps : true,
schema : {
name : Joi.string(),
email : Joi.string().email(),
age : Joi.number(),
},
indexes : [
{hashKey : 'name', rangeKey : 'createdAt', type : 'local', name : 'CreatedAtIndex'}
]
});
var printResults = function (err, resp) {
console.log('----------------------------------------------------------------------');
if(err) {
console.log('Error running query', err);
} else {
console.log('Found', resp.Count, 'items');
console.log(util.inspect(_.pluck(resp.Items, 'attrs')));
if(resp.ConsumedCapacity) {
console.log('----------------------------------------------------------------------');
console.log('Query consumed: ', resp.ConsumedCapacity);
}
}
console.log('----------------------------------------------------------------------');
};
var loadSeedData = function (callback) {
callback = callback || _.noop;
async.times(25, function(n, next) {
var prefix = n %5 === 0 ? 'foo' : 'test';
Account.create({email: prefix + n + '@example.com', name : 'Test ' + n %3, age: n}, next);
}, callback);
};
var runQueries = function () {
// Basic query against hash key
Account.query('Test 0').exec(printResults);
// Run query limiting returned items to 3
Account.query('Test 0').limit(3).exec(printResults);
// Query with rang key condition
Account.query('Test 1')
.where('email').beginsWith('foo')
.exec(printResults);
// Run query returning only email and created attributes
// also returns consumed capacity query took
Account.query('Test 2')
.where('email').gte('[email protected]')
.attributes(['email','createdAt'])
.returnConsumedCapacity()
.exec(printResults);
// Run query against secondary index
Account.query('Test 0')
.usingIndex('CreatedAtIndex')
.where('createdAt').lt(new Date().toISOString())
.descending()
.exec(printResults);
};
async.series([
async.apply(vogels.createTables.bind(vogels)),
loadSeedData
], function (err) {
if(err) {
console.log('error', err);
process.exit(1);
}
runQueries();
});