Skip to content

Commit 900d0e9

Browse files
committed
Initialize db with correct adapter
1 parent 2ae8905 commit 900d0e9

File tree

3 files changed

+28
-37
lines changed

3 files changed

+28
-37
lines changed

src/db.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
const promise = require('promise')
2-
const Observable = require("zen-observable")
32

4-
let adapter
5-
const adapterName = `micro-analytics-adapter-${process.env.DB_ADAPTER || 'flat-file-db'}`
3+
function initDbAdapter(adapterName) {
4+
let adapter
5+
const repeatCharacter = (char, n) => `${Array(n + 1).join(char)}`
66

7-
const repeatCharacter = (char, n) => `${Array(n + 1).join(char)}`
8-
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-
} else {
17-
throw err
7+
try {
8+
adapter = require(`micro-analytics-adapter-${adapterName}`)
9+
} catch (err) {
10+
if (err.code === 'MODULE_NOT_FOUND') {
11+
// Console.error a warning message, but normally exit the process to avoid printing ugly npm ERR lines and stack trace.
12+
console.error(`\n${repeatCharacter(' ', 22)}⚠️ ERROR ⚠️\n${repeatCharacter('-', 55)}\nYou specified "${adapterName}" as the DB_ADAPTER, but no package\ncalled "micro-analytics-adapter-${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`)
13+
process.exit(0)
14+
} else {
15+
throw err
16+
}
1817
}
19-
}
2018

21-
const observable = new Observable((observer) => {
22-
let timer = setInterval(_ => {
23-
observer.next("hello")
24-
}, 1000)
2519

26-
return _ => clearTimeout(timer)
27-
});
20+
module.exports.get = adapter.get;
21+
module.exports.getAll = adapter.getAll;
22+
module.exports.put = adapter.put;
23+
module.exports.has = adapter.has;
24+
module.exports.keys = adapter.keys;
25+
module.exports.subscribe = adapter.subscribe;
26+
module.exports.hasFeature = (feature) => typeof adapter[feature] === "function";
2827

28+
}
2929

3030
module.exports = {
31-
get: adapter.get,
32-
getAll: adapter.getAll,
33-
put: adapter.put,
34-
has: adapter.has,
35-
keys: adapter.keys,
36-
subscribe: adapter.subscribe,
31+
initDbAdapter: initDbAdapter,
32+
hasFeature: (feature) => false,
3733
}

src/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ const micro = require('micro')
22
const SSE = require('sse')
33
const args = require('args');
44

5-
const handler = require('./handler')
6-
const sseHandler = require('./sse')
7-
85
const flags = args
96
.option(['p', 'port'], 'Port to listen on', process.env.PORT || 3000, Number)
107
.option(['H', 'host'], 'Host to listen on', '0.0.0.0')
118
.option(['a', 'adapter'], 'Database adapter used', process.env.DB_ADAPTER || 'flat-file-db')
129
.parse(process.argv, { name: 'micro-analytics' })
1310

11+
const db = require('./db');
12+
db.initDbAdapter(flags.adapter)
13+
14+
const handler = require('./handler')
1415
const server = micro(handler)
1516
const sse = new SSE(server)
1617
sse.on('connection', sseHandler)

src/sse.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
const db = require('./db')
22

33
module.exports = function handleSseConnection(connection) {
4-
console.log('new connection')
54
const subscription = db.subscribe((event) => {
6-
console.log('new event', event)
7-
connection.send({
8-
event: 'new-event',
9-
data: JSON.stringify(event),
10-
})
5+
connection.send({ event: 'new-event', data: JSON.stringify(event) })
116
})
127

138
connection.on('close', function () {
14-
console.log('closing connection')
159
subscription.unsubscribe()
1610
})
1711
}

0 commit comments

Comments
 (0)