Skip to content

Commit

Permalink
Merge pull request #30 from mxstbr/flat-file-adapter
Browse files Browse the repository at this point in the history
Extract flat-file-db adapter into separate package
  • Loading branch information
mxstbr authored Jan 15, 2017
2 parents c3d0186 + 9f66f5d commit 0f330ea
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 49 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Public analytics as a Node.js microservice, no sysadmin experience required.

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

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.
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.

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

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

### Database adapters

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`)_
By default, `micro-analytics` uses `flat-file-db`, a fast in-process flat file database, which makes for easy setup and backups.

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`

Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dependencies": {
"flat-file-db": "^1.0.0",
"micro": "6.1.0",
"micro-analytics-adapter-flat-file-db": "^1.0.3",
"promise": "^7.1.1",
"shelljs": "^0.7.6"
},
Expand All @@ -41,5 +42,10 @@
},
"execMap": {
"js": "micro"
},
"jest": {
"collectCoverageFrom": [
"src/**/*.js"
]
}
}
22 changes: 10 additions & 12 deletions src/db.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
const promise = require('promise')

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

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

if (process.env.DB_ADAPTER) {
const adapterName = `micro-analytics-adapter-${process.env.DB_ADAPTER}`
try {
adapter = require(adapterName)
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
// Console.error a warning message, but normally exit the process to avoid printing ugly npm ERR lines and stack trace.
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`)
process.exit(0)
}
try {
adapter = require(adapterName)
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
// Console.error a warning message, but normally exit the process to avoid printing ugly npm ERR lines and stack trace.
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`)
process.exit(0)
} else {
throw err
}
} else {
adapter = require('./flat-file-adapter')
}

module.exports = {
Expand Down
35 changes: 0 additions & 35 deletions src/flat-file-adapter.js

This file was deleted.

5 changes: 5 additions & 0 deletions tests/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const micro = require('micro')
const noop = () => {}

// Mock the database
const DB = () => {
Expand All @@ -16,6 +17,10 @@ const DB = () => {
},
has: (key) => ({}.hasOwnProperty.call(data, key)),
keys: () => Object.keys(data),
del: noop,
clear: noop,
close: noop,
on: noop,
}),

// Custom methods used in tests
Expand Down
2 changes: 2 additions & 0 deletions writing-adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

`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`.

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)!

## Overview

The methods every adapter has to have are:
Expand Down

0 comments on commit 0f330ea

Please sign in to comment.