Socket
Socket
Sign inDemoInstall

@brickyang/easy-mongodb

Package Overview
Dependencies
21
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @brickyang/easy-mongodb

Based on MongoDB Native Node.js Driver.


Version published
Weekly downloads
57
decreased by-6.56%
Maintainers
1
Install size
7.31 MB
Created
Weekly downloads
 

Readme

Source

NPM version NPM quality build status Test coverage David deps Known Vulnerabilities npm download

This lib base on node-mongodb-native, provides the official MongoDB native driver and APIs.

It wraps some frequently-used API to make it easy to use but keep all properties as it is. For example, to find a document you need this with official API

db.collection('name')
  .find(query, options)
  .skip(skip)
  .limit(limit)
  .project(project)
  .sort(sort)
  .toArray();

and with this lib

mongo.find('name', { query, skip, limit, project, sort, options });

Installation

npm install --save @brickyang/easy-mongodb

Configuration

Single

const config = {
  host: 'host',
  port: 'port',
  name: 'test',
  user: 'user',
  password: 'password',
  options: {},
};

Replica Set

// mongodb://host1:port1,host2:port2/name?replicaSet=test
const config = {
  host: 'host1,host2',
  port: 'port1,port2',
  name: 'name',
  options: { replicaSet: 'test' },
};

// mongodb://host:port1,host:port2/name?replicaSet=test
const config = {
  host: 'host',
  port: 'port1,port2',
  name: 'name',
  options: { replicaSet: 'test' },
};

Usage

const MongoDB = require('@brickyang/easy-mongodb');

const mongo = new MongoDB(config);

mongo
  .connect()
  .then(client => {
    // `client` is instance of connected MongoClient
  })
  .catch(error => {
    // handle error
  });

// or

mongo.on('connect', () => {
  // do something
});

mongo.on('error', error => {
  // handle error
});

Example

APIs provided by this lib usually need two arguments. The first is commonly the collection name, and the second is an object keeps the arguments of official API. For example, to insert one document using official API

db.collection('name').insertOne(doc, options);

and using lib API

const args = { doc, options };
mongo.insertOne('name', args);

Methods

Until now, this plugin provides these functions:

  • connect
  • insertOne
  • insertMany
  • findOneAndUpdate
  • findOneAndReplace
  • findOneAndDelete
  • updateMany
  • deleteMany
  • find
  • count
  • distinct
  • createIndex
  • listCollection
  • createCollection
  • aggregate

You can always use mongo.db to use all official APIs. Check the APIs here: Node.js MongoDB Driver API.

Promise

function create(doc) {
  mongo
    .insertOne('name', { doc })
    .then(result => console.log(result))
    .catch(error => console.error(error));
}

Async/Await

async function create(doc) {
  try {
    const result = await mongo.insertOne('name', { doc });
    console.log(result);
  } catch (error) {
    console.error(error);
  }
}

If you use mongo.db you could use callback(usually the last argument), but this lib doesn't support callback because Promise and async/await are better.

License

MIT

Keywords

FAQs

Last updated on 26 Aug 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc