Socket
Socket
Sign inDemoInstall

@condor-labs/mongodb

Package Overview
Dependencies
256
Maintainers
5
Versions
30
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @condor-labs/mongodb

This module provide and usefull helper to use mongoose library.


Version published
Weekly downloads
410
decreased by-52.55%
Maintainers
5
Created
Weekly downloads
 

Readme

Source

This module provide and usefull helper to use mongoose library.

See official documentation here.

Compatibility

The minimum supported version of Node.js is v8.

How to use it

To use the library you just need to follow the following steps Install the library with npm

npm install @condor-labs/mongodb

Import the library:

const mongodb = require('@condor-labs/mongodb')(settings);

settings object properties

PropertyDefaultDescription
host (String)127.0.0.1The host name of the database you are connecting to.
port (Number)27017The port number to connect to.
user (String)The username for authentication. This option is mongoose-specific, they are equivalent to the MongoDB driver's auth.username options.
password (String)The password for authentication. This option is mongoose-specific, they are equivalent to the MongoDB driver's auth.password options.
database (String)Specifies which database to connect to and overrides any database specified in the connection string.
replicaSet (String)To connect to a single node replica set, specify the replicaSet option, see Replica Set Host Names
ssl (number)0Flag to indicate the connection will use SSL
authSource (String)See Replica Set Connections
reconnectTries (Number)30000If you're connected to a single server or mongos proxy (as opposed to a replica set), the MongoDB driver will try to reconnect every reconnectInterval milliseconds for reconnectTries times, and give up afterward. When the driver gives up, the mongoose connection emits a reconnectFailed event. This option does nothing for replica set connections.
reconnectInterval (Number)1000See reconnectTries

Examples

constants.js

module.exports = {
    settings: {
        host: 'test_npm_mongodb-shard-00-00-ac0ta.mongodb.net,test_npm_mongodb-shard-00-01-ac0ta.mongodb.net,test_npm_mongodb-shard-00-02-ac0ta.mongodb.net',
        port: 27017,
        database: 'development',
        user: 'my_user',
        password: 'my_password',
        replicaSet: 'test_npm_mongodb-shard-0',
        ssl: true,
        authSource: 'admin',
        reconnectTries: 30000,
        reconnectInterval: 100
    },
    mongoSchemaDefinition: {
        name: String,
        email: String,
        pass: String
    },
    mongoModelName: 'users'
};

index.js

const {
    settings,
    mongoSchemaDefinition,
    mongoModelName
} = require('./constants');

const timeoutError = (ms, errorMessage) => new Promise((resolve, reject) => setTimeout(() => {
    reject(new Error(errorMessage));
}, ms));

try {
    const mongodb = require('@condor-labs/mongodb')(settings);
    (async () => {
        console.log(`isConnected(before): ${mongodb._isConnected()}`);
        // CONNECT TO MONGO
        const client = await mongodb.getClient();
        console.log(`isConnected(after): ${mongodb._isConnected()}`);
        // PREPARE MODEL
        const Schema = client.Schema;
        const testSchema = Schema(mongoSchemaDefinition);
        const testModel = client.model(mongoModelName, testSchema);
        console.log(`user MODEL was set`);
        // QUERY DATA IN COLLECTION
        const res = await Promise.race([
            timeoutError(5000, 'Timeout error getting the Test Model'),
            testModel.findOne({})
        ]);
        // VALIDATE FOUND DATA
        if (res !== null) {
            console.log(`TESTING OK!!!`);
        }
        else {
            console.log(`TESTING WAS BAD :(`);
        }

        process.exit(1);
    })();

} catch (error) {
    console.error(error)
}

How to Publish

Increasing package version

You will need to update the package.json file placed in the root folder.

identify the property version and increase the right number in plus one.

Login in NPM by console.

 npm login
 [Enter username]
 [Enter password]
 [Enter email]

If all is ok the console will show you something like this : Logged in as USERNAME on https://registry.npmjs.org/.

Uploading a new version

 npm publish --access public

Ref: https://docs.npmjs.com/getting-started/publishing-npm-packages

Note: you will need to have a NPM account, if you don't have one create one here: https://www.npmjs.com/signup

Contributors

The original author and current lead maintainer of this module is the Condorlabs development team.

More about Condorlabs Here.

License

MIT

Keywords

FAQs

Last updated on 17 Jul 2019

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