Skip to content

Commit fa0ae5c

Browse files
committed
Extract flat-file-db adapter into separate package
1 parent c3d0186 commit fa0ae5c

File tree

4 files changed

+16
-47
lines changed

4 files changed

+16
-47
lines changed

package.json

Lines changed: 1 addition & 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
},

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

0 commit comments

Comments
 (0)