-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (73 loc) · 1.83 KB
/
index.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
const couchbase = require('couchbase');
const express = require('express');
const app = express();
const port = 3000; // TODO: add env vars
const cluster = new couchbase.Cluster("couchbase://localhost", {
username: "Administrator",
password: "password" // FIXME: should not be hard coded
});
const bucket = cluster.bucket("travel-sample");
const collection = bucket.defaultCollection();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.get('/airlines', async (req, res) => {
let qs;
if (true) { // use this to check input formats
qs = `SELECT * from \`travel-sample\` WHERE type="airline" LIMIT 5;`
}
let result, rows;
try {
result = await cluster.query(qs);
rows = result.rows;
} catch (e) {
console.log(e);
res.send(400);
return;
}
res.send({
data: rows,
context: [qs]
});
});
app.get('/airlines/:key', async (req, res) => {
getAirlineByKey(req.params['key']).then((result) => {
res.send(result);
}).catch((err) => {
console.log("Error fetching airline by key");
res.send(400);
return;
});
});
// Helper/Test functions and definitions TODO: move these to a db file
const airline = {
type: "airline",
id: 216,
callsign: "PKT",
iata: null,
iaco: null,
name: "United Airlines"
};
const upsertDocument = async (doc) => {
try {
const key = `${doc.type}_${doc.id}`;
const result = await collection.upsert(key, doc);
console.log("Upsert Result: ");
console.log(result);
} catch (error) {
console.log("Upsert Error: ");
console.log(error);
}
};
const getAirlineByKey = async (key) => {
try {
const result = await collection.get(key);
return result;
} catch (error) {
console.log("Get Error: ");
console.log(error);
}
};
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});