-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastore.js
144 lines (125 loc) · 4.14 KB
/
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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const { Datastore } = require("@google-cloud/datastore");
const TRUCK = "Truck";
require("dotenv").config();
const datastore = new Datastore();
const fromDatastore = function (item) {
item.id = item[Datastore.KEY].id;
return item;
};
// returns an entity in a kind corresponding to the passed id
function getEntityByID(kind, id) {
const key = datastore.key([kind, parseInt(id, 10)]);
return datastore.get(key).then((entity) => {
if (entity[0] === undefined || entity[0] === null) {
return entity;
} else {
return entity.map(fromDatastore);
}
});
}
// returns list of entities in kind
function getUnprotectedEntitiesInKind(kind) {
const q = datastore.createQuery(kind);
return datastore.runQuery(q).then((entities) => {
return entities[0].map(fromDatastore);
});
}
// returns list of entities in kind that belong to specified owner
function getProtectedEntitiesInKind(kind, owner) {
const q = datastore.createQuery(kind);
return datastore.runQuery(q).then((entities) => {
return entities[0]
.map(fromDatastore)
.filter((item) => item.owner === owner);
});
}
// returns true is any of the values in the array are falsy;
// returns false otherwise
function hasFalsyValue(arr) {
for (const el of arr) {
if (!el) {
return true;
}
}
return false;
}
// returns true if any of the values in the array are truthy; false otherwise
function hasTruthyValue(arr) {
for (const el of arr) {
if (el) {
return true;
}
}
return false;
}
// return an object that contains the passed id and the corresponding self link
function convertIdToObjectWithSelfLink(id, endpoint, req) {
return {
id: id,
self: `${req.protocol}://${req.get("host")}/${endpoint}/${id}`,
};
}
// return a modified version of the object that has self links for the load and for its carrier
function addSelfLinksToLoad(load, req) {
const loadWithSelfLinks = {
...load,
self: `${req.protocol}://${req.get("host")}/loads/${load.id}`,
};
if (load.carrier) {
loadWithSelfLinks.carrier = convertIdToObjectWithSelfLink(
load.carrier,
"trucks",
req
);
}
return loadWithSelfLinks;
}
// return a modified version of the object that has self links for the truck and for each of its loads
function addSelfLinksToTruck(truck, req) {
const truckWithSelfLinks = {
...truck,
self: `${req.protocol}://${req.get("host")}/trucks/${truck.id}`,
loads: [],
};
for (let j = 0; j < truck.loads.length; j++) {
const loadID = truck.loads[j];
truckWithSelfLinks.loads.push(
convertIdToObjectWithSelfLink(loadID, "loads", req)
);
}
return truckWithSelfLinks;
}
// remove a load id from a truck's list of loads
function removeLoadFromTruck(truckID, loadID) {
const l_key = datastore.key([TRUCK, parseInt(truckID, 10)]);
return datastore.get(l_key).then((truck) => {
truck[0].loads = truck[0].loads.filter((load) => load != loadID);
return datastore.save({ key: l_key, data: truck[0] });
});
}
// return true if the client accepts JSON responses; false otherwise
function hasJsonInAcceptHeader(req) {
return req.accepts(["application/json"]);
}
// return true if the response is JSON; false otherwise
function hasValidContentType(req) {
return req.get("content-type") === "application/json";
}
// return true if the client is authorized to view the entity
function ownerIsValid(req, entity) {
return entity.owner === req.auth.sub;
}
module.exports.Datastore = Datastore;
module.exports.datastore = datastore;
module.exports.fromDatastore = fromDatastore;
module.exports.getEntityByID = getEntityByID;
module.exports.getUnprotectedEntitiesInKind = getUnprotectedEntitiesInKind;
module.exports.getProtectedEntitiesInKind = getProtectedEntitiesInKind;
module.exports.hasFalsyValue = hasFalsyValue;
module.exports.hasTruthyValue = hasTruthyValue;
module.exports.addSelfLinksToLoad = addSelfLinksToLoad;
module.exports.addSelfLinksToTruck = addSelfLinksToTruck;
module.exports.removeLoadFromTruck = removeLoadFromTruck;
module.exports.hasJsonInAcceptHeader = hasJsonInAcceptHeader;
module.exports.hasValidContentType = hasValidContentType;
module.exports.ownerIsValid = ownerIsValid;