Skip to content

Commit 96b300a

Browse files
authored
example: update lowdb example (#327)
1 parent dc8a383 commit 96b300a

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

examples/lowdb/db.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"posts": [
33
{
4-
"id": 1597264162493,
4+
"id": 1633609838244,
55
"title": "test",
66
"likes": 0
77
}
88
]
9-
}
9+
}

examples/lowdb/index.js

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
1-
import low from 'lowdb'
2-
import FileSync from 'lowdb/adapters/FileSync.js'
31
import { App } from '@tinyhttp/app'
42
import { urlencoded } from 'milliparsec'
3+
import { Low, JSONFile } from 'lowdb'
54

65
const app = new App()
7-
const adapter = new FileSync('db.json')
8-
const db = low(adapter)
9-
10-
// get all posts
6+
const adapter = new JSONFile('db.json')
7+
const db = new Low(adapter)
8+
await db.read()
9+
const { posts } = db.data
1110

1211
app.use(urlencoded())
1312

13+
// get all posts
1414
app.get('/', (_, res) => {
15-
res.send(db.getState().posts)
15+
res.send(posts)
1616
})
1717

1818
// get post by id
1919
app.get('/:id', (req, res) => {
20-
res.send(
21-
db
22-
.get('posts')
23-
// @ts-ignore
24-
.find({ id: parseInt(req.params.id) })
25-
.value()
26-
)
20+
const currentPost = posts.find(post => post.id === parseInt(req.params.id))
21+
if (currentPost) {
22+
res.send(currentPost)
23+
} else {
24+
res.send({ msg: `A post with an id ${req.params.id} is not found` })
25+
}
2726
})
2827

2928
// add a post
3029
app.post('/', (req, res) => {
3130
if (req.body.title) {
32-
// @ts-ignore
33-
db.get('posts').push({ id: Date.now(), title: req.body.title, likes: 0 }).write()
31+
posts.push({ id: Date.now(), title: req.body.title, likes: 0 })
32+
db.write()
3433
res.send({ msg: `Post with title of "${req.body.title}" is successfully added` })
3534
} else {
36-
res.send('Post title missing')
35+
res.send({ msg: 'Post title missing' })
3736
}
3837
})
3938

4039
// like a post
4140
app.put('/:id', (req, res) => {
42-
const currentPost = db
43-
.get('posts')
44-
45-
// @ts-ignore
46-
.find({ id: parseInt(req.params.id) })
47-
.value()
48-
currentPost.likes += 1
49-
db.write()
50-
res.send({ msg: `You liked a post with a title of ${currentPost.title}` })
41+
const currentPost = posts.find(post => post.id === parseInt(req.params.id))
42+
if (currentPost) {
43+
currentPost.likes += 1
44+
db.write()
45+
res.send({ msg: `You liked a post with a title of ${currentPost.title}` })
46+
} else {
47+
res.send({ msg: `A post with an id ${req.params.id} is not found` })
48+
}
5149
})
5250

5351
// delete a post
5452
app.delete('/:id', (req, res) => {
55-
db.get('posts')
56-
57-
//@ts-ignore
58-
.remove({ id: parseInt(req.params.id) })
59-
.write()
60-
res.send({ msg: `A post with an id of ${req.params.id} has been deleted` })
53+
const currentPost = posts.filter(post => post.id !== parseInt(req.params.id))
54+
if (posts.length > currentPost.length) {
55+
db.data.posts = currentPost
56+
db.write()
57+
res.send({ msg: `A post with an id of ${req.params.id} has been deleted` })
58+
} else {
59+
res.send({ msg: `A post with an id ${req.params.id} is not found` })
60+
}
6161
})
6262

6363
app.listen(3000, () => console.log('Server is connected on http://localhost:3000'))

examples/lowdb/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
"type": "module",
44
"dependencies": {
55
"@tinyhttp/app": "workspace:*",
6-
"lowdb": "^2.1.0",
6+
"lowdb": "^3.0.0",
77
"milliparsec": "^2.2.0"
88
},
9-
"devDependencies": {
10-
"@types/lowdb": "^1.0.11"
11-
},
129
"scripts": {
1310
"start": "node index.js"
1411
}

0 commit comments

Comments
 (0)