Skip to content

Commit a467500

Browse files
committed
switch from levelup to flat-file-db
/cc @mafintosh are there any concerns with doing this?
1 parent a51f062 commit a467500

6 files changed

Lines changed: 52 additions & 1109 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
blog-views
1+
views
22
node_modules
33
.DS_STORE

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ With less than 50 lines of code this service is the smallest analytics you'll ev
99
## Built with
1010

1111
- [`micro`](https://github.com/zeit/micro) to create the service.
12-
- [`level`](https://github.com/level/level) to store the data. (and [`then-levelup`](https://github.com/then/then-levelup) to promisify `level`)
12+
- [`flat-file-db`](https://github.com/mafintosh/flat-file-db) to store the data. (and [`promise`](https://github.com/then/promise) to promisify `flat-file-db`)
1313

1414
## Usage
1515

db.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const flatfile = require('flat-file-db')
2+
const promise = require('promise')
3+
4+
const db = flatfile.sync('views')
5+
6+
module.exports = {
7+
// Promisify async operations
8+
put: promise.denodeify(db.put.bind(db)),
9+
del: promise.denodeify(db.del.bind(db)),
10+
clear: promise.denodeify(db.clear.bind(db)),
11+
12+
get: db.get.bind(db),
13+
has: db.has.bind(db),
14+
keys: db.keys.bind(db),
15+
close: db.close.bind(db),
16+
}

index.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
const url = require('url')
2-
const level = require('level')
3-
const then = require('then-levelup')
42
const { send, createError, sendError } = require('micro')
53

6-
const db = then(level('blog-views', {
7-
keyEncoding: 'json',
8-
valueEncoding: 'json'
9-
}))
4+
const db = require('./db')
105

116
module.exports = async function (req, res) {
127
// Check that a page is provided
@@ -19,19 +14,19 @@ module.exports = async function (req, res) {
1914
}
2015
const shouldIncrement = query.inc !== 'false' && query.inc !== false
2116
try {
22-
const { views } = await db.get(pathname)
23-
// Add a view and send the total views back to the client
24-
if (shouldIncrement) {
25-
views.push({ time: Date.now() })
26-
await db.put(pathname, { views })
27-
}
28-
if (req.method === 'GET') {
29-
send(res, 200, { views: views.length })
17+
if (db.has(pathname)) {
18+
const { views } = await db.get(pathname)
19+
// Add a view and send the total views back to the client
20+
if (shouldIncrement) {
21+
views.push({ time: Date.now() })
22+
await db.put(pathname, { views })
23+
}
24+
if (req.method === 'GET') {
25+
send(res, 200, { views: views.length })
26+
} else {
27+
send(res, 200)
28+
}
3029
} else {
31-
send(res, 200)
32-
}
33-
} catch (err) {
34-
if (err.notFound) {
3530
// Initialise the page with one view
3631
if (shouldIncrement) {
3732
await db.put(pathname, { views: [{ time: Date.now() }] })
@@ -41,8 +36,9 @@ module.exports = async function (req, res) {
4136
} else {
4237
send(res, 200)
4338
}
44-
} else {
45-
throw createError(500, 'Internal server error.')
4639
}
40+
} catch (err) {
41+
console.log(err)
42+
throw createError(500, 'Internal server error.')
4743
}
4844
}

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
"author": "Max Stoiber <contact@mxstbr.com> (http://mxstbr.com/)",
1111
"license": "MIT",
1212
"dependencies": {
13-
"level": "1.5.0",
14-
"levelplus": "0.0.5",
13+
"flat-file-db": "^1.0.0",
1514
"micro": "6.1.0",
16-
"then-levelup": "1.0.2"
15+
"promise": "^7.1.1"
1716
},
1817
"devDependencies": {},
1918
"execMap": {

0 commit comments

Comments
 (0)