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

mongo-crud-layer

v2.0.2

Published

A simple CRUD layer for node-mongodb-native v2

Downloads

24

Readme

Mongo-CRUD-Layer

Build Status npm version Coverage Status

A simple CRUD-interface for the node-mongodb-native driver. Abstracts the creation, reading, updating and deleting of documents into more comfortable methods.

Latest added feature:

  • support for GridFS / GridStore

Installation

npm install mongo-crud-layer

or add it to your package.json.

Usage

1. Instantiation

First, create a new MongoCrud instance to store the databases URI and (optional) some options.

var mongocrud = new MongoCrud(uri, options, false);
  • uri - the URI to MongoDB, e.g. mongodb://localhost:27017/mongocrud-test
  • options - optional settings
  • gridFS - set to true, if objects exceed MongoDB document limit of 16mb, to store objects with GridFS mechanism
var MongoCrud = require('mongo-crud-layer');
var mongocrud = new MongoCrud(); 
// or 
var mongocrud = new MongoCrud('mongodb://localhost:27017/mongocrudtest');
// or
var mongocrud = new MongoCrud('mongodb://localhost:27017/mongocrudtest', {}, true);
...

2. API

The API offers the following methods:

create(obj, collection, callback)

Creates a MongoDB document from the given object and stores it into the given collection. Returns the _id of the stored document.

  • obj - the object to store
  • collection - the collection to store the object in
  • callback - a callback function with two parameters: error and the ObjectID of the stored object
var DB_URI = 'mongodb://localhost:27017/mongocrud-test';
var COLLECTION = 'objectStore';
var OBJ = {
    name: 'Athyrion'
};
var ID;
...
mongocrud.create(OBJ, COLLECTION, function(err, id) {
    console.log(id);
    ID = id;
});
...

read(criteria, collection, callback)

Searches the given collection for documents matching the given criteria and returns the found documents. The criterium should be in most cases using the _id from the creation process, e.g. criteria = {_id: _id}.

  • criteria - the criteria to search for in the database, usually the object's _id
  • collection - the collection to search in
  • callback - a callback function with two parameters: error and the found documents
...
mongocrud.read({ _id: ID }, COLLECTION, function(err, doc) {
    console.log(doc); // { _id: 55617c9226d7023b19edcdd1, name: "Athyrion" }
});
...

readAll(collection, callback)

Returns all documents stored in the given collection as an array. !!! Not available in GridFS mode !!!

  • collection - the collection to search in
  • callback - a callback function with two parameters: error and an array of objects from the collection
...
mongocrud.readAll(COLLECTION, function(err, docs) {
    console.log(docs); // [ { _id: 55617c9226d7023b19edcdd1, name: "Athyrion" }, ...]
});
...

update(criteria, obj, collection, callback)

Replaces the object in the given collection with given object. !!! Not available in GridFS mode !!!

  • criteria - the criteria to search for in the database, usually the object's _id
  • obj - the replacing object
  • collection - the collection to search in
  • callback - a callback function with two parameters: error and a mongodb result object
...

OBJ.name = 'Athyrion-Westeros';

mongocrud.update({ _id: ID }, OBJ, COLLECTION, function(err, res) {
    console.log(res); // { ok: 1, n: 1 }
});
...

delete(critera, collection, callback)

Deletes the document that matches the given criteria.

  • criteria - the criteria to search for in the database, usually the object's _id
  • collection - the collection to search in
  • callback - a callback function with two parameters: error and a mongodb [result object](http://docs.mongodb.org/manual/reference/command/insert/#insert-command-output
...
mongocrud.delete({ _id: ID }, COLLECTION, function(err, res) {
    console.log(res); // { ok: 1, n: 1 }
});
...

close(callback)

Closes the connection to the database.

  • callback - the callback function receiving null, if the closing was successful, otherwise a MongoError
...
mongocrud.close(function(result) {
    // handle error
});