🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

mongo-repositories

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongo-repositories

Tiny layer over mongoDB native driver to simplify queries, validation and indexing

latest
Source
npmnpm
Version
0.2.21
Version published
Weekly downloads
4
-87.5%
Maintainers
1
Weekly downloads
 
Created
Source

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 }]);

Keywords

MongoDB

FAQs

Package last updated on 01 Mar 2022

Did you know?

Socket

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