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
Property | Default | Description |
---|
host (String) | 127.0.0.1 | The host name of the database you are connecting to. |
port (Number) | 27017 | The 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) | 0 | Flag to indicate the connection will use SSL |
authSource (String) | | See Replica Set Connections |
reconnectTries (Number) | 30000 | If 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) | 1000 | See 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()}`);
const client = await mongodb.getClient();
console.log(`isConnected(after): ${mongodb._isConnected()}`);
const Schema = client.Schema;
const testSchema = Schema(mongoSchemaDefinition);
const testModel = client.model(mongoModelName, testSchema);
console.log(`user MODEL was set`);
const res = await Promise.race([
timeoutError(5000, 'Timeout error getting the Test Model'),
testModel.findOne({})
]);
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