Skip to content

Commit b098645

Browse files
committed
Initial commit
0 parents  commit b098645

5 files changed

Lines changed: 102 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

LICENSE

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.

README.md

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

index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const flatfile = require('flat-file-db')
2+
const promisify = require('then-flat-file-db')
3+
4+
const db = promisify(flatfile.sync(process.env.DB_NAME || 'views.db'))
5+
6+
module.exports = {
7+
put: db.put.bind(db),
8+
has: (key) => Promise.resolve(db.has(key)),
9+
keys: () => Promise.resolve(db.keys()),
10+
// Get a value and filter it
11+
get: async (key, options) => {
12+
let value
13+
try {
14+
value = await db.get(key)
15+
} catch (err) {
16+
value = { views: [] }
17+
}
18+
19+
return {
20+
views: value.views.filter(view => {
21+
if (options && options.before && view.time > options.before) return false
22+
if (options && options.after && view.time < options.after) return false
23+
return true
24+
})
25+
}
26+
},
27+
// Get all values starting with a certain pathname and filter their views
28+
getAll: async function getAll(options) {
29+
const data = {}
30+
const keys = await module.exports.keys()
31+
32+
keys
33+
.filter(key => key.startsWith(options.pathname))
34+
.forEach((key) => {
35+
data[key] = module.exports.get(key, { before: options.before, after: options.after })
36+
})
37+
38+
return data
39+
}
40+
}

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "micro-analytics-adapter-flat-file-db",
3+
"version": "1.0.0",
4+
"description": "Store your micro-analytics data in flat-file-db!",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/mxstbr/micro-analytics-adapter-flat-file-db.git"
12+
},
13+
"keywords": [
14+
"micro-analytics",
15+
"adapter",
16+
"flat-file-db"
17+
],
18+
"author": "Max Stoiber <contact@mxstbr.com> (http://mxstbr.com/)",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/mxstbr/micro-analytics-adapter-flat-file-db/issues"
22+
},
23+
"homepage": "https://github.com/mxstbr/micro-analytics-adapter-flat-file-db#readme",
24+
"dependencies": {
25+
"flat-file-db": "^1.0.0",
26+
"then-flat-file-db": "^1.0.0"
27+
},
28+
"peerDependencies": {
29+
"micro-analytics": "^1.2.0"
30+
}
31+
}

0 commit comments

Comments
 (0)