MqttDispatcher/dist/mongosave.js
Wolfgang Hottgenroth a0a922f851 connected to atlas
2017-08-23 17:54:50 +02:00

50 lines
1.7 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const CallChain = require("./callchain");
const log = require("./log");
const MongoDB = require("mongodb");
class MongoSave extends CallChain.ABaseChainItem {
constructor(url) {
super('MongoSave');
this._url = url;
this._mongoClient = new MongoDB.MongoClient();
this._connectPending = false;
}
func(message) {
if (!this._dbh) {
log.info("Not database connection yet");
if (!this._connectPending) {
this._connectPending = true;
this._mongoClient.connect(this._url)
.then((db) => {
log.info("Successfully opened MongoDB connect");
this._dbh = db;
})
.catch((err) => {
log.error(`Failure when opening MongoDB connect: ${err}`);
this._dbh = undefined;
});
}
else {
log.info("Connecting to database is pending");
}
}
if (this._dbh) {
log.info("Database handle is available");
let coll = this._dbh.collection("iot");
coll.insertOne(message)
.then((res) => {
log.info(`Successfully wrote one item in database: ${res.insertedId}`);
})
.catch((err) => {
log.error(`Failure when trying to write one item in database: ${err}`);
});
}
else {
log.error(`No database connection yet, drop message ${message}`);
}
return "<<" + message + ">>";
}
}
exports.MongoSave = MongoSave;
//# sourceMappingURL=mongosave.js.map