mongo-repositories
v0.2.21
Published
Tiny layer over mongoDB native driver to simplify queries, validation and indexing
Downloads
6
Maintainers
Readme
mongo-repositories
Tiny layer over mongoDB native driver to simplify queries, validation and indexing
Install
npm i -S mongo-repositories
Get started
const RepositoryFactory = require('mongo-repositories');
// OR: import RepositoryFactory from 'mongo-repositories';
const Repository = new RepositoryFactory({
prefix: 'g-', // prefixing index names to be distinguishable
/**
* Log logic
* @optional
* @default console.log
* @Set to "undefined" or "null" to disable logs
*/
log: undefined
});
Connect to MongoDB
/** @return Promise */
await Repository.connect('mongo://Connection_string');
Disconnect from MongoDB
/** @return Promise */
await Repository.close();
Parse ObjectId from String
/** @return Promise */
var id = Repository.parseObjectId('HexObjectId');
Collection
Create new Collection
const Collection = Repository.from({
name: 'collectionName',
/** Set of used indexes */
indexes: [
{
name: 'g-indexName' // must be prefixed with same defined Repository.prefix
/** @See additional attributes from Mongo documentation */
}
],
define: function (collection) {
// Collection is the native mongo driver collection
// @See mongo documentation for NodeJS native driver
// at: http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html
return {
myMethod(args) {
/* Your logic */
}
// Your method will be accessible via: data= await MyCollection.myMethod(args);
};
}
});
// Collection will be accessible via:
const sameCollection = Repositories.all.collectionName;
Predefined methods
var MyCollection = Repository.from({
/* logic */
});
/** Insert document */
await MyCollection.insertOne({ doc });
/** Insert list of documents */
await MyCollection.insertMany([{ doc }]);