1.0.2 • Published 4 years ago
iot-mongo v1.0.2
iot-mongo
base mongo db wrapper for easy using of mongoDb
Table of Contents
Features
- persist database connection (using singleton)
- log to external database
Installing
Using npm:
$ npm install iot-mongo
Connect to Mongodb
You can use callback, or promise. The connect function use the singleton pattern on the background.
callback aproach
const uri = '...'
iotMongo.connect(uri, (err, _db) => {
if (err) console.error(err);
// now you can use the database
iotMongo.collection('').find({}).toArray((_err, data) => {
console.log(data);
});
})
promise aproach
const uri = '...';
try {
await iotMongo.connectPromise();
// now you can use the database
const data = await iotMongo.collection('').find({}).toArray();
console.log(data);
} catch (error) {
console.error(error);
}
connect to external database
iotMongo.connectToExternal('uri to external db', (err, client) => {
if (err) console.error(err);
// now you can use the database
client.db('my-db').collection('my-coll').find({}).toArray((_err, data) => {
console.log(data);
});
});
// or you can use promise
const client = await iotMongo.connectToExternalPromise(uri);
... etc