Mosca lib/persistence/mongo.js

    Description

    Copyright (c) 2013-2014 Matteo Collina, http://matteocollina.com

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use,
    copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following
    conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.

    Source

    "use strict";
    
    var AbstractPersistence = require("./abstract");
    var mongo = require('mongodb');
    var MongoClient = mongo.MongoClient;
    var util = require("util");
    var async = require("async");
    var Matcher = require("./matcher");
    var topicPatterns = require("./utils").topicPatterns;
    var extend = require("extend");
    var defaults = {
      ttl: {
        // TTL for subscriptions is 1 hour
        subscriptions: 60 * 60 * 1000,
    
        // TTL for packets is 1 hour
        packets: 60 * 60 * 1000,
      },
      mongo: {}
    };

    MongoPersistence

    function
    MongoPersistence()
    • @param: {Object}optionsThe options, as describe above.
    • @param: {Function}doneThe callback that will be called
    • @: when the persistance is ready

    Description

    A persistance based on MongoDB.
    It currently performs in save mode.

    The current options include:
    - url, the connection URL of the database
    - ttl, an object containing three values:
    * subscriptions, the time (ms) after which subscriptions
    will expire. It defaults to 1 hour.
    * packets, the time (ms) after which packets will expire.
    It defaults to 1 hour.
    - mongo, all the options for the MongoDB driver.
    - connection, a MongoDB client to be reused

    Source

    function MongoPersistence(options, done) {
      if (!(this instanceof MongoPersistence)) {
        return new MongoPersistence(options, done);
      }
    
    
      this.options = extend(true, {}, defaults, options);
      this.options.mongo.safe = true;
    
      var that = this;
    
      var connected = function(err, db) {
        if (err) {
          if (done) {
            return done(err);
          }
          // we have no way of providing an error handler
          throw err;
        }
    
        that.db = db;
        async.parallel([
          function(cb) {
            db.collection("subscriptions", function(err, coll) {
              that._subscriptions = coll;
              async.parallel([
                that._subscriptions.ensureIndex.bind(that._subscriptions, "client"),
                that._subscriptions.ensureIndex.bind(that._subscriptions, { "added": 1 }, { expireAfterSeconds: Math.round(that.options.ttl.subscriptions / 1000 )} )
              ], cb);
            });
          },
          function(cb) {
            db.collection("packets", function(err, coll) {
              that._packets = coll;
              that._packets.ensureIndex("client", cb);
            });
          },
          function(cb) {
            db.collection("retained", function(err, coll) {
              that._retained = coll;
              that._retained.ensureIndex("topic", { unique: true }, cb);
            });
          }
        ], function(err) {
          if (done) {
            done(err, that);
          }
        });
      };
    
      // Connect to the db
      if (options.connection) {
        connected(null, this.options.connection);
      } else {
        MongoClient.connect(this.options.url, this.options.mongo, connected);
      }
    }
    
    util.inherits(MongoPersistence, AbstractPersistence);

    exports

    property
    module.exports

      Description

      Export it as a module

      Source

      module.exports = MongoPersistence;