forked from pinojs/pino-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pino-mongodb.js
executable file
·66 lines (57 loc) · 2.42 KB
/
pino-mongodb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env node
'use strict'
const carrier = require('carrier')
const program = require('commander')
const MongoClient = require('mongodb').MongoClient
const parseMongoUrl = require('muri')
const log = require('./lib/log')
const pkg = require('./package.json')
////const makeInsert = require('./lib/makeInsert')
const makeUpsert = require('./lib/makeUpsert')
program
.version(pkg.version)
.description(pkg.description)
.arguments('[mongo-url]')
.option('-c, --collection <name>', 'database collection', 'logs')
.option('-o, --stdout', 'output inserted documents into stdout', false)
.option('-e, --errors', 'output insertion errors into stderr', false)
.parse(process.argv)
const mongoUrl = (program.args[0] || 'mongodb://localhost:27017/logs')
function handleConnection (e, mClient) {
if (e) {
throw e
}
const dbName = parseMongoUrl(mongoUrl).db
const db = mClient.db(dbName)
const emitter = carrier.carry(process.stdin)
const collection = db.collection(program.collection)
////const insert = makeInsert(program.errors, program.stdout)
const upsert = makeUpsert(program.errors, program.stdout)
emitter.on('line', (line) => {
////insert(collection, log(line))
var l = log(line)
//if (program.collection == "mqttuser") {
upsert(collection, {"clientid": l.deviceid}, {"$set":l})
if ((l.type != 'gateway') && (l.type != 'tcpgateway')) {
var dtype = l.type
if (l.type == 'lockgateway') {
dtype = 'lock'
}
var ts = Math.round(new Date().getTime()/1000)
upsert(db.collection("device"), {"_deviceid": l.deviceid}, {"$set":{"_appkey": l.appkey || "iSurpassApp", "_deviceid": l.deviceid, "_isvirtual": true, "_create_ts": ts}})
upsert(db.collection("node"), {"_deviceid": l.deviceid, "_nodeid": 1}, {"$set":{"_appkey": l.appkey || "iSurpassApp", "_deviceid": l.deviceid, "_nodeid": 1, "_logicid": l.deviceid+"-1-0-", "_isentity": true, "_islogic": true, "_nodetype": dtype, "_logicnodetype": dtype, "_create_ts": ts}})
} else {
var ts = Math.round(new Date().getTime()/1000)
upsert(db.collection("device"), {"_deviceid": l.deviceid}, {"$set":{"_appkey": l.appkey || "iSurpassApp", "_deviceid": l.deviceid, "_isvirtual": false, "_create_ts": ts}})
}
//}
})
process.on('SIGINT', () => {
mClient.close(process.exit)
})
}
MongoClient.connect(
mongoUrl,
{ useNewUrlParser: true },
handleConnection
)