Skip to content

Commit 0f330ea

Browse files
authored
Merge pull request #30 from mxstbr/flat-file-adapter
Extract flat-file-db adapter into separate package
2 parents c3d0186 + 9f66f5d commit 0f330ea

File tree

6 files changed

+25
-49
lines changed

6 files changed

+25
-49
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Public analytics as a Node.js microservice, no sysadmin experience required.
44

55
[![Build Status](https://travis-ci.org/mxstbr/micro-analytics.svg?branch=master)](https://travis-ci.org/mxstbr/micro-analytics)
66

7-
A tiny analytics server with ~150 lines of code, easy to run and hack around on. It does one thing, and it does it well: count the views of something and making the views publicly accessible via an API.
7+
A tiny analytics server with ~100 lines of code, easy to run and hack around on. It does one thing, and it does it well: count the views of something and making the views publicly accessible via an API.
88

99
(there is currently no frontend to display pretty graphs, feel free to build one yourself!)
1010

@@ -52,7 +52,7 @@ If you want to get all views for all ids, set the `all` query parameter to `true
5252

5353
### Database adapters
5454

55-
By default, `micro-analytics` uses `flat-file-db`, a fast in-process flat file database, which makes for easy setup and backups. _(change the path to the database with the `DB_PATH` env variable, e.g. `$ DB_PATH=storage/analytics.db micro-analytics`)_
55+
By default, `micro-analytics` uses `flat-file-db`, a fast in-process flat file database, which makes for easy setup and backups.
5656

5757
This works fine for side-project usage, but for a production application with bajillions of visitors you might want to use a real database with a _database adapter_. Install the necessary npm package (e.g. `micro-analytics-adapter-xyz`) and then specify the `DB_ADAPTER` environment variable: `$ DB_ADAPTER=xyz micro-analytics`
5858

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"dependencies": {
2727
"flat-file-db": "^1.0.0",
2828
"micro": "6.1.0",
29+
"micro-analytics-adapter-flat-file-db": "^1.0.3",
2930
"promise": "^7.1.1",
3031
"shelljs": "^0.7.6"
3132
},
@@ -41,5 +42,10 @@
4142
},
4243
"execMap": {
4344
"js": "micro"
45+
},
46+
"jest": {
47+
"collectCoverageFrom": [
48+
"src/**/*.js"
49+
]
4450
}
4551
}

src/db.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
const promise = require('promise')
22

33
let adapter
4+
const adapterName = `micro-analytics-adapter-${process.env.DB_ADAPTER || 'flat-file-db'}`
45

56
const repeatCharacter = (char, n) => `${Array(n + 1).join(char)}`
67

7-
if (process.env.DB_ADAPTER) {
8-
const adapterName = `micro-analytics-adapter-${process.env.DB_ADAPTER}`
9-
try {
10-
adapter = require(adapterName)
11-
} catch (err) {
12-
if (err.code === 'MODULE_NOT_FOUND') {
13-
// Console.error a warning message, but normally exit the process to avoid printing ugly npm ERR lines and stack trace.
14-
console.error(`\n${repeatCharacter(' ', 22)}⚠️ ERROR ⚠️\n${repeatCharacter('-', 55)}\nYou specified "${process.env.DB_ADAPTER}" as the DB_ADAPTER, but no package\ncalled "${adapterName}" was found.\n\nPlease make sure you spelled the name correctly and\nhave "npm install"ed the necessary adapter package!\n${repeatCharacter('-', 55)}\n`)
15-
process.exit(0)
16-
}
8+
try {
9+
adapter = require(adapterName)
10+
} catch (err) {
11+
if (err.code === 'MODULE_NOT_FOUND') {
12+
// Console.error a warning message, but normally exit the process to avoid printing ugly npm ERR lines and stack trace.
13+
console.error(`\n${repeatCharacter(' ', 22)}⚠️ ERROR ⚠️\n${repeatCharacter('-', 55)}\nYou specified "${process.env.DB_ADAPTER}" as the DB_ADAPTER, but no package\ncalled "${adapterName}" was found.\n\nPlease make sure you spelled the name correctly and\nhave "npm install"ed the necessary adapter package!\n${repeatCharacter('-', 55)}\n`)
14+
process.exit(0)
15+
} else {
16+
throw err
1717
}
18-
} else {
19-
adapter = require('./flat-file-adapter')
2018
}
2119

2220
module.exports = {

src/flat-file-adapter.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

tests/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const micro = require('micro')
2+
const noop = () => {}
23

34
// Mock the database
45
const DB = () => {
@@ -16,6 +17,10 @@ const DB = () => {
1617
},
1718
has: (key) => ({}.hasOwnProperty.call(data, key)),
1819
keys: () => Object.keys(data),
20+
del: noop,
21+
clear: noop,
22+
close: noop,
23+
on: noop,
1924
}),
2025

2126
// Custom methods used in tests

writing-adapters.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
`micro-analytics` database adapters are simple JavaScript modules which export an object with some methods. They _have_ to be called `micro-analytics-adapter-xyz`, where `xyz` is the name users will pass to the `DB_ADAPTER` environment variable when starting `micro-analytics`.
44

5+
If you want to see an example adapter, check out the default [`flat-file-db` adapter](https://github.com/mxstbr/micro-analytics-adapter-flat-file-db)!
6+
57
## Overview
68

79
The methods every adapter has to have are:

0 commit comments

Comments
 (0)