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 🙏

© 2025 – Pkg Stats / Ryan Hefner

infrastructure-mongodb

v0.5.0

Published

Installation ============

Readme

Installation

npm install infrastructure-mongodb

Configuration

In project_root/config/structures create data.json file (or give other structure name) with the following content:

{
  "path":   "data", 
  "engine": "infrastructure-mongodb/engine",

  "config": {

    "mongodb": {
      "host":            "localhost",
      "port":            27017,
      "db":              "orbits",
      "auto_reconnect":  true,
      "options":         { "nativeParser": true }
    }
    
  }
}
  • "path" is folder where your structure modules are located (based on rootDir)
  • "engine" - add path to module that will load mongodb to engines array
  • "libs" - adding base mongolayer, base mongolayer class will be accessible via env.lib.MongoLayer
  • "config" - the engine will search for config.mongodb object when trying to connect to database. congig.mongodb.options will be passed directly to mongodb.MongoClient.connect. Read more about options here http://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html

Usage

In structure folder path create file of type (named for example MyMongoResource.js): var MongoLayer = require("infrastructure-mongodb/MongoLayer"); module.exports = MongoLayer.extend("MyMongoResource", {

  init: function(cb){ /* async initialization (optional) - run cb([err]) when done*/ },
  
  collectionName: "MongodbCollectionName", // mongodb collection name for this resource

  someSpecificQuery: function(param_1, options, cb){
    
    // this is mongodb collection instance that represents current resource
    var collection = this.collection;
    // read more about it here - http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html

    // The DataLayer instance provides:
    // this.create(doc, cb);
    // this.create([doc1, doc2], cb);
    // this.find(query, options, cb);
    // this.findOne(query, options, cb);
    // this.count(query, options, cb);
    // this.update(query, update, options, cb);
    // this.delete(query, options, cb);

  }

});

Once structure is initialized, datalayer can be called from any other local or remote structure using dot notation

env.i.do("data.MyMongoResource.find", some_query, some_options, cb);
// or
env.i.do("data.MyMongoResource.someSpecificQuery", some_query, some_options, cb);

"objectify" option

In most cases, mongodb ObjectId-s in queries and documents will come as strings. In options object in CRUD methods we can put {$objectify: "path_to_patch"} or {$objectify: ["path_to_patch_1", "path_to_patch_2"]}

env.i.do("data.MyMongoResource.find", { _id: '562ab133c7b795974496fd16' }, {$objectify: "_id" }, function(err, result){
  // ...
});

env.i.do("data.BlogComments.find", 
  { author: {$in: ['562ab133c7b795974496fd16', '562ab133c7b795974496fd17', '562ab133c7b795974496fd18']} },
  { $objectify: "author.$in" }, 
  function(err, result){
  // ...
});

Only when calling "update" we have 2 possible arguments to patch, so "$objectify" option should be mounted on the query or on the update object, not in options.

env.i.do("data.BlogComments.update", 
  { post_id: {$in: ['562ab133c7b795974496fd26', '562ab133c7b795974496fd27', '562ab133c7b795974496fd28']},  $objectify: "post_id.$in" }, 
  { $set: {approved_by: '562ab133c7b795974496fd06' }, $objectify: "$set.approved_by" },
  {},   // Other options
  function(err, result){
  // ...
});

TODO

  • patterns to objectify object properties in nested array

Added (0.2.2)

Note - the cli options will only work with infrastructure version ^1.1.0

--drop or --drop.ModelName command line options

This will cause layer instance to use it's "seed" property. It can be string (url, fs path, related to project root or dot notated config resolve path). It can be array or single object, and will be seeded directly. It can be function that returns array or object.

--seed or --seed.ModelName

This will set seed option to string (url, fs path, related to project root or dot notated config resolve path);

--seed.ModelName=http:eample.com/resource
--seed.ModelName=./seeds/ModelsData.json
--seed.ModelName=seeds.ModelsData

The last will be resolved from config tree. It can point to object, array or string that will be proceeded too.

It also can point to function (DataLayer instance seed property to be function).

function seed(cb){
  // do something async
  cb(err, [ /* models here */ ]);
}

Added (0.4)

  • $dateify option (same as $objectify, but makes dates)
  • MongoModel - require("infrastructure-mongodb/MongoModel")
  • MongoCollection - require("infrastructure-mongodb/MongoCollection")