0.2.21 • Published 2 years ago

mongo-repositories v0.2.21

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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

2 years ago

0.2.20

2 years ago

0.2.19

2 years ago

0.2.18

2 years ago

0.2.17

3 years ago

0.2.16

3 years ago

0.2.15

3 years ago

0.2.14

3 years ago

0.2.13

3 years ago

0.2.12

3 years ago

0.2.11

3 years ago

0.2.10

3 years ago

0.2.7

3 years ago

0.2.9

3 years ago

0.2.8

3 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.4

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.3

3 years ago

0.0.1

3 years ago