-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel-datastore.js
68 lines (56 loc) · 1.79 KB
/
model-datastore.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
'use strict';
const Datastore = require('@google-cloud/datastore');
const projectId = 'planet-real-estate';
const datastore = new Datastore({projectId: projectId});
const kind = 'planet';
function getPlanets(livable, farmable, investment, callback) {
var query = datastore.createQuery('planet');
if (livable)
query = query.filter('isLivable', '=', livable)
if (farmable)
query = query.filter('isFarmable', '=', farmable)
if (investment)
query = query.filter('isInvestment', '=', investment);
datastore
.runQuery(query)
.then(results => {
const planets = results[0].map(x => ({
key: x[datastore.KEY].id,
name: x.name,
price: x.price,
distance: x.distance,
isLivable: x.isLivable,
isFarmable: x.isFarmable,
isInvestment: x.isInvestment
}));
callback(null, planets);
})
.catch(err => {
callback(err);
});
}
function getPlanet(key, callback) {
var query = datastore
.createQuery('planet')
.filter('__key__', '=', datastore.key(['planet', key]));
datastore
.runQuery(query)
.then(results => {
const planet = ({key: results[0][0][datastore.KEY].id,
name: results[0][0].name,
price: results[0][0].price,
distance: results[0][0].distance,
isLivable: results[0][0].isLivable,
isFarmable: results[0][0].isFarmable,
isInvestment: results[0][0].isInvestment
});
callback(null, planet);
})
.catch(err => {
callback(err);
});
}
module.exports = {
getPlanets,
getPlanet
};