This repository has been archived by the owner on Nov 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (87 loc) · 2.45 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
94
95
96
97
const express = require("express")
const { v4: uuid } = require("uuid")
const db = require("./lowdb")
const app = express()
const port = 3000
app.use(express.json())
// Get all collection items
app.get('/:collection', (req, res) => {
const collection = req.params.collection
res.send(db.get(collection))
})
// Get collection item count
app.get('/:collection/count', (req, res) => {
const collection = req.params.collection
res.send(db.get(collection).size().value())
})
// Query collection items
app.get('/:collection/search', (req, res) => {
// filter={ field: value, field: value, ... } <-- base64 encoded value
// sortBy='field'
// take='5'
const collection = req.params.collection
let dbRef = db.get(collection)
if (req.query.filter) {
let filter = Buffer.from(req.query.filter, 'base64').toString()
filter = JSON.parse(filter)
dbRef = dbRef.filter(filter)
}
if (req.query.sortBy) {
dbRef = dbRef.sortBy(req.query.sortBy)
}
if (req.query.take) {
dbRef = dbRef.take(Number(req.query.take))
}
res.send(dbRef)
})
// Get collection item
app.get('/:collection/:id', (req, res) => {
const collection = req.params.collection
const id = req.params.id
let item = db.get(collection).find({ id }).value()
if (item) {
res.send(item)
} else {
res.status(404).send('Item not found')
}
})
// Create collection item
app.post('/:collection', (req, res) => {
const collection = req.params.collection
let item = req.body
let id = uuid()
item.id = id
db.get(collection)
.push(item)
.write()
item = db.get(collection).find({ id }).value()
res.send(item)
})
// Update collection item
app.put('/:collection/:id', (req, res) => {
const collection = req.params.collection
const id = req.params.id
let item = req.body
db.get(collection)
.find({ id })
.assign(item)
.write()
item = db.get(collection).find({ id }).value()
res.send(item)
})
// Delete collection item
app.delete('/:collection/:id', (req, res) => {
const collection = req.params.collection
const id = req.params.id
let result = db.get(collection).remove({ id }).write()
if (result) {
if (result.length) {
res.send('Delete successful')
} else {
res.status(404).send('Invalid request')
}
} else {
res.status(400).send('Delete failed')
}
})
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))