npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoat

v2.0.7

Published

Mongoat is a MongoDb ODM

Downloads

70

Readme

Mongoat

Codacy Badge Codacy Badge dev dependencies Inline docs

mongoat gif

Description

Mongoat is a MongoDB lightweight wrapper adding hooks (pre/post), automatic createdAt/updatedAt, in a native MongoDB experience. It is written on top of the mongodb npm package.

It does not provides any ODM, model specifications, validation, or things that would force you to use it in a specific way. Mongoat is designed to be used in a MongoDB way: your way.

Features

  • Hooks (pre/post actions: afterSave, beforeSave, etc.)
  • Datetime documents easily (createdAt & updatedAt)
  • Native MongoDB experience and usage (you actually use the native MongoDB driver when you use Mongoat)
  • Versions documents easily (Vermongo specifications)

ToDo:

  • Event triggering on oplog actions

Installation

npm install mongoat

Basic usage

var mongoat = require('mongoat'); //instead of require('mongodb');

And then your mongoat object is to be used like the MongoDB native node.js driver. We just add some features on top of it, see below:

Hooks

You can add multiple before and after hooks for insertions, updates and removals:

before hooks:

db.collection('collectionName').before('insert', function (docToInsert) {
  // triggered when calling to insert()
});

db.collection('collectionName').before('update', function (docToUpdate) {
  // triggered when calling to update() or findAndModify()
});

db.collection('collectionName').before('remove', function (docToRemove) {
  // triggered when calling to remove()
}); 

after hooks:

db.collection('collectionName').after('insert', function (docToInsert) {
  // triggered when calling to insert()
});

db.collection('collectionName').after('update', function (docToUpdate) {
  // triggered when calling to update() or findAndModify()
});

db.collection('collectionName').after('remove', function (docToRemove) {
  // triggered when calling to remove()
}); 

Datetime

Enable datetime feature:

db.collection('collectionName').datetime(true); // Default is false

createdAt:

it will add a createdAt field to all new inserted documents using:

db.collection('collectionName').insert(document, options);

or using one of the following method within the option upsert: ture

db.collection('collectionName').update(query, update, options);
db.collection('collectionName').findAndModify(query, sort, update, options);

updatedAt:

it will add a updatedAt field to all updated documents using:

db.collection('collectionName').update(query, update, options);
// or
db.collection('collectionName').findAndModify(query, sort, update, options);

Versioning

Enable versioning feature:

db.collection('collectionName').version(true); // Default is false

Enabling this feature for a collection, so each time you perform an insert/update/remove it will create a document in the collection collectionName.vermongo and increment the version of the updated document. The _id in this collection is a composite ID, { _id: _id, _version: _version }. The document in the MyCollection collection will also receive a _version field.

If we want to restore a version

db.collection('collectionName').restore(id, version);
  • id of the document to restore is required
  • if version is greater than 0, it will be considered as the version to restore
  • if version is equal or lower than 0, it will be considered as the starting
  • if version is not provided, it will takes default value 0 point of the version to restore (starting form last) ex:
db.collection('collectionName').restore(id, 0); // restore the last version
db.collection('collectionName').restore(id, -2); // restore the last version -2

more about versioning feature here

Tests

  1. Run a MongoDb server if not yet
  2. Run tests npm test

Or to show up code coverage npm run cover it will generate ./coverage folder

Contribution

Please read our Contributing Guidlines before submitting a pull request or an issue !

License

The MIT License MIT

Copyright (c) 2015 Dial Once