A MongoDB helper allows using MongoDB easier.
Due to this library is a wrapper for MongoDB the mongodb package is required. Please check your package.json file.
- mongodb
npm install --save @kevinwang0316/mongodb-helper mongodb
import { initialConnects, getDB, promiseFindResult } from '@kevinwang0316/mongodb-helper';
// For NodeJS
// const { initialConnects, getDB, promiseFindResult } = require(''@kevinwang0316/mongodb-helper'');
// Initialize your connection before use it. The connection pool will be saved in a global
await initialConnects('database url', 'database name', 1);
// use promiseFindresult
const result = await promiseFindResult(db => db
.collection('collection name')
.find({});
// use db directly
const result = getDB().collection('collection name')
.insert({}, (err, response) => {
if (err) reject(err);
resolve(response.ops[0]);
});
The @kevinwang0316/log library is using
If you want to change the defual log level (debug):
Add a log_level variable to your .env file. Or if you are using Fass solution such as AWS Lambda, set up the log_level to your environment.
Log all level of information (debug, info, warn, error) log_level=DEBUG
Log info and above levels of information (info, warn, error) log_level=INFO
Log warn and above levels of information (warn, error) log_level=WARN
Log just error level of information (error) log_level=ERROR
MongoDB Helper is licensed under MIT License - see the License file.