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-redline

v0.2.3

Published

node.js thin mongodb document models library

Downloads

4

Readme

mongo-redline Build Status Dependency Status Coverage Status

mongo-redline is a very lite an simple Node.js library to connect to Mongodb and map documents with plain Javascript objects. It's purely schemaless, fully compatible with official Mongo API and made like a toolkit to fit your needs, rather than a full functional framework like Mongoose. It's only a read-only ODM, writes MUST be done manually. You can use it just to connect to Mongo, it may not very useful, but could be efficient : use only one connection, callback made, so easily integrated with node.js and async. If you look for a thin layer to define models in a non intrusive manner, it will give you the beginning of the answer ...

Usage

Define a Model

var redMongo = require('mongo-redline')
  , Mixin1 = require('mixin1');

var MyModel =  redMongo.defineModel({ 

  collection: 'my_models',

  mixins: [Mixin1],

  instanceMethods: {
    get age(){
      return (new Date()).getFullYear() -  this.birthdayYear;
    }
  },

  staticMethods: {
    function doIt(newModel){}
  }

});

Connect, Query and Manipule Objects


var redMongo = require('mongo-redline')
  , myModel = require('my_model');

redMongo.connect({}, function(err){
  if( err) return console.error( "...");
  // Query my_models collection
  my_model.findOne({name: 'toto'}, function(err, model){
    if(err)console.error(err);
    else console.log(model.age)
    redMongo.close();
  });
}

API

Connection

redMongo.connect(options, cb)

Create a MongoDB connection, and callback it has result.

  • options Object, passed to mongodb.Server and new mongodb.Db

    • host: server address, default to 127.0.0.1
    • port: server's port, default to 27017
    • auto_reconnectdefault to true
    • poolSize default to 10
    • w default to 1
    • strict default to true
    • native_parser defaut to true

Models

Create a new redMongo model.

RedMongo models offer:

  • type definitions via redMongo models
  • a binding between collections and redMongo models
  • a binding at reading time between mongo's documents and javascript objets
  • facilities to query (findOne, findAll) documents based redMongo models and a bridge to mongodb API
redMongo.defineModel(options)

Returns a redModel.

  • options Object:

    • collection: collection's name. May be optionnal for polymorphisme.
    • extends: redMongo super model, default as redMongo.Model (see ex pieces.js)
    • mixins: list of javascript objects to be used as mixins for the new model
    • instanceMethods: javascript object used to define documents methods
    • staticMethods: javascript object used to define redModel methods. Adding an entry within this object is similare to set a function attribute on the resulting redModel. 'init' key is reserved to define constructor (see below)

RedModel.bless, defined within staticMethods or in an other manner, is used to 'type' mongo's document. At reading time each document is blessed depending on its collection. For polymorphism you have to do it manually (see below).


var Printable = {
  toString: function(){
    return this.type + ' => ' + this.surface;
  },
}

var Piece  = redMongo.defineModel({
  collection: 'pieces',

  mixins: [Printable],

  staticMethods: {
   
    init: function(name){
      this.name = name;
      this.type = 'piece';
    },

    bless: function (obj){
      switch( obj.type ){
        case 'square':
          return redMongo.Model.bless.bind(Square)(obj);
        case 'circle':
          return redMongo.Model.bless.bind(Circle)(obj);
      default:
       return redMongo.Model.bless.bind(Piece)(obj);
      }
    }
  }
});

var Square = redMongo.defineModel({
  extends: Piece,

  instanceMethods: {
    get surface(){
      return this.size * this.size;
    }
  },
  staticMethods: {
    init: function(size){
      this.size = size;
      this.type = 'square';
    }
  }
});

redModel.findOne( arguments )

Use same signature as node-mongodb-native driver. will call redModel.bless(document) on resulting document.

Return only one result, the first if many.

  Piece.findOne({_id: ObjectId("52de8aa97a2731486fdcf8ee")}, function(err, piece){});
redModel.findAll( arguments )

Same as node-mongodb-native#find, but will call redModel.bless(document) on each resulting document.

  Piece.findAll({type: 'square'}, function(err, pieces){});

In this example, you will extract all squares documents from the collection, resulting documents will be blessed as Square.

Square.findAll() will give you all Pieces, not only Squares. You have to select type manually.

redModel.collection

Give you direct access to node-mongodb-native driver:

  Piece.collection.insert([new Square(2), new Circle(2)], function(err, res){})