Skip to content

Commit 5a1a18b

Browse files
committed
Merge branch 'lerna' of git://github.com/micro-analytics/adapter-flat-file-db into lerna
2 parents 67d3e0b + 1764133 commit 5a1a18b

File tree

7 files changed

+187
-0
lines changed

7 files changed

+187
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
dist.js
3+
npm-debug.log
4+
views.db
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: node_js
2+
node_js:
3+
- node
4+
- 7
5+
- 6
6+
matrix:
7+
allow_failures:
8+
- node_js: 6
9+
script: npm run test
10+
cache: yarn
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2017 Maximilian Stoiber
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# `micro-analytics-adapter-flat-file-db`
2+
3+
Use [`flat-file-db`](https://github.com/mafintosh/flat-file-db) to store your [`micro-analytics`](https://github.com/mxstbr/micro-analytics) data!
4+
5+
## Usage
6+
7+
**This is the default adapter for `micro-analytics`**, to use it simply start it without any arguments:
8+
9+
```
10+
micro-analytics
11+
```
12+
13+
## Options
14+
15+
Options are set via environment variables. These are the possible options for this adapter:
16+
17+
```sh
18+
DB_NAME # set the location the database should be created in
19+
```
20+
21+
## License
22+
23+
Copyright ©️ 2017 Maximilian Stoiber, licensed under the MIT license. See [LICENSE](LICENSE) for more information.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const path = require('path')
2+
const flatfile = require('flat-file-db')
3+
const promisify = require('then-flat-file-db')
4+
const escapeRegexp = require('escape-regex')
5+
const Observable = require("zen-observable")
6+
7+
let db
8+
9+
function init(options) {
10+
db = promisify(flatfile.sync(path.resolve(process.cwd(), options.dbName || 'views.db')))
11+
}
12+
13+
// This is here for backwards compatability should be removed at some point
14+
init({dbName: process.env.DB_NAME})
15+
16+
const keyRegex = (str) => {
17+
str = str.split('*').map( s => escapeRegexp(s)).join('*')
18+
return new RegExp('^' + str.replace('*','.*'))
19+
}
20+
21+
22+
let handlers = [];
23+
24+
const observable = new Observable((observer) => {
25+
handlers.push((data) => observer.next(data))
26+
let index = handlers.length;
27+
return () => {
28+
handlers = [...handlers.slice(0, index), ...handlers.slice(index)]
29+
}
30+
});
31+
32+
module.exports = {
33+
options: [
34+
{
35+
name: 'db-name',
36+
description: 'The name of the flat-file-db file.',
37+
defaultValue: process.env.DB_NAME || 'views.db'
38+
}
39+
],
40+
init,
41+
put: (key, value) => {
42+
handlers.forEach(handler => {
43+
handler({key, value});
44+
})
45+
return db.put(key, value)
46+
},
47+
has: (key) => Promise.resolve(db.has(key)),
48+
keys: () => Promise.resolve(db.keys()),
49+
// Get a value and filter it
50+
get: async (key, options) => {
51+
let value
52+
try {
53+
value = await db.get(key)
54+
} catch (err) {
55+
value = { views: [] }
56+
}
57+
58+
return {
59+
views: value.views.filter(view => {
60+
if (options && options.before && view.time > options.before) return false
61+
if (options && options.after && view.time < options.after) return false
62+
return true
63+
})
64+
}
65+
},
66+
// Get all values starting with a certain pathname and filter their views
67+
getAll: async function getAll(options) {
68+
const data = {}
69+
const keys = (await module.exports.keys()).filter((key) => {
70+
return options.ignoreWildcard ? key.startsWith(options.pathname) : key.match(keyRegex(options.pathname))
71+
})
72+
73+
for (let key of keys) {
74+
data[key] = await module.exports.get(key, { before: options.before, after: options.after })
75+
}
76+
77+
await Promise.all(keys)
78+
79+
return data
80+
},
81+
subscribe: (cb) => {
82+
return observable.subscribe(cb);
83+
}
84+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "micro-analytics-adapter-flat-file-db",
3+
"version": "1.3.0",
4+
"description": "Store your micro-analytics data in flat-file-db!",
5+
"main": "dist.js",
6+
"scripts": {
7+
"test": "jest",
8+
"build": "async-to-gen index.js --out-file dist.js",
9+
"prepublish": "npm run build"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/mxstbr/micro-analytics-adapter-flat-file-db.git"
14+
},
15+
"keywords": [
16+
"micro-analytics",
17+
"adapter",
18+
"flat-file-db"
19+
],
20+
"author": "Max Stoiber <[email protected]> (http://mxstbr.com/)",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/mxstbr/micro-analytics-adapter-flat-file-db/issues"
24+
},
25+
"homepage": "https://github.com/mxstbr/micro-analytics-adapter-flat-file-db#readme",
26+
"dependencies": {
27+
"escape-regex": "^1.0.7",
28+
"flat-file-db": "^1.0.0",
29+
"then-flat-file-db": "^1.0.0",
30+
"zen-observable": "^0.4.0"
31+
},
32+
"peerDependencies": {
33+
"micro-analytics-cli": "^1.2.0"
34+
},
35+
"devDependencies": {
36+
"async-to-gen": "^1.3.0",
37+
"jest": "^19.0.2",
38+
"micro-analytics-cli": "1.2.0-11"
39+
}
40+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const path = require('path')
2+
const flatfile = require('flat-file-db')
3+
const promisify = require('then-flat-file-db')
4+
const test = require('micro-analytics-cli/adapter-tests/unit-tests')
5+
6+
const db = promisify(flatfile.sync(path.resolve(process.cwd(), process.env.DB_NAME || 'views.db')))
7+
const adapter = require('./index')
8+
9+
10+
test({
11+
name: 'flat-file-db',
12+
modulePath: path.resolve(__dirname, './index.js'),
13+
beforeAll: () => {
14+
adapter.init({ dbName: 'views.db' })
15+
},
16+
beforeEach: () => {
17+
return db.clear();
18+
}
19+
})

0 commit comments

Comments
 (0)