mongotoolbox
v0.0.3
Published
Utility for MongoDB native driver which adds unexisting indexes and removes unused
Downloads
1
Maintainers
Readme
Mongotoolbox
MongoDB toolbox for index management and simple connection to authorized database
Using
This utility requires:
- Node.js
>= 6.0.0
- mongodb driver
^2.2.4
Install package with npm:
npm i -S mongotools
Ensure indexes at collection
const mongotools = require('mongotools');
mongotools.ensureIndex(db.collection('myCollection'))
.index({ someUniqueIndex: 1 }, { sparse: true, unique: true })
.writeIndexes();
Connect to database
const mongotools = require('mongotools');
mongotools.connectAndAuth('mongodb://localhost:27017')
.then((db) => {
// use connected db object
});
Connect to database and authenticate
const mongotools = require('mongotools');
const url = 'mongodb://localhost:27017';
const options = { user: 'username', password: 'foo' };
mongotools
.connectAndAuth(url, options)
.then((db) => {
// use connected db object
});
API
connectAndAuth method
- connectAndAuth(url: string, options: object)
url
: mongodb connection stringoptions
options for mongodb connect methodoptions.user
: username for authenticationoptions.password
: password for authentication
EnsureIndex class
new EnsureIndex(mongoDbCollection)
- creates instance of mongodb collection
index(definition: string, options: object)
definition
: object accepted by mongooptions
: object accepted by mongo
writeIndexes()
- removes unwanted indexes and writes non-existing ones