JSPM

  • Created
  • Published
  • Downloads 702
  • Score
    100M100P100Q102726F
  • License MIT

MongoDB wrapper for Node.JS 12

Package Exports

  • @paralect/node-mongo

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@paralect/node-mongo) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Node Mongo

npm version

Node Mongo is reactive extension to MongoDB API. It provides few usability improvements to the monk API.

Features

  • ️️Reactive. Fires events as document stored, updated or deleted from database
  • Paging. Implements high level paging API
  • Schema validation. Validates your data before save

Installation

npm i @paralect/node-mongo

Quick example

Connect to the database:

const connectionString = `mongodb://localhost:27017/home-db`;
const db = require('node-mongo').connect(connectionString);

Short API overview, for more details see Full API reference

// create a service to work with specific database collection
const userService = db.createService('users');

// find one document
const user = await userService.findOne({ name: 'Bob' });

// find many documents with pagination
const {results, pagesCount, count } = await userService.find(
  { name: 'Bob' },
  { page: 1, perPage: 30 },
);

// update document
const updatedUser = await userService.updateOne(
  { _id: '1' },
  (doc) => ({ ...doc, name: 'Alex' }),
);

// subscribe to document updates
userService.on('updated', ({ doc, prevDoc }) => {
});

Schema declaration (user.schema.js):

const Joi = require('Joi');

const companySchema = Joi.object({
  _id: Joi.string(),
  createdOn: Joi.date(),
  name: Joi.string(),
  status: Joi.string().valid('active', 'inactive'),
});

exports.schema = companySchema;

exports.validate = (obj) => companySchema.validate(obj);

Schema validation:

const { validate } = require('./user.schema');

const userService = db.createService('users', { validate });

Full API Reference

API Reference.

Change Log

This project adheres to Semantic Versioning.

Every release is documented on the Github Releases page.