Click here to Skip to main content
15,895,084 members
Articles / Web Development / Node.js

Node.Js And Stuff

Rate me:
Please Sign up or sign in to vote.
4.97/5 (55 votes)
11 Feb 2013CPOL23 min read 361.2K   2.3K   172  
Small demo app using Node.Js/Socket.IO/MongoDB/D3.Js and jQuery.
var GridStore = require('./gridstore').GridStore,
  ObjectID = require('bson').ObjectID;

/**
 * A class representation of a simple Grid interface.
 *
 * @class Represents the Grid.
 * @param {Db} db A database instance to interact with.
 * @param {String} [fsName] optional different root collection for GridFS.
 * @return {Grid}
 */
function Grid(db, fsName) {

  if(!(this instanceof Grid)) return new Grid(db, fsName);

  this.db = db;
  this.fsName = fsName == null ? GridStore.DEFAULT_ROOT_COLLECTION : fsName;
}

/**
 * Puts binary data to the grid
 *
 * @param {Buffer} data buffer with Binary Data.
 * @param {Object} [options] the options for the files.
 * @param {Function} callback this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object.
 * @return {null}
 * @api public
 */
Grid.prototype.put = function(data, options, callback) {
  var self = this;
  var args = Array.prototype.slice.call(arguments, 1);
  callback = args.pop();
  options = args.length ? args.shift() : {};
  // If root is not defined add our default one
  options['root'] = options['root'] == null ? this.fsName : options['root'];

  // Return if we don't have a buffer object as data
  if(!(Buffer.isBuffer(data))) return callback(new Error("Data object must be a buffer object"), null);
  // Get filename if we are using it
  var filename = options['filename'];
  // Create gridstore
  var gridStore = new GridStore(this.db, filename, "w", options);
  gridStore.open(function(err, gridStore) {
    if(err) return callback(err, null);

    gridStore.write(data, function(err, result) {
      if(err) return callback(err, null);

      gridStore.close(function(err, result) {
        if(err) return callback(err, null);
        callback(null, result);
      })
    })
  })
}

/**
 * Get binary data to the grid
 *
 * @param {ObjectID} id ObjectID for file.
 * @param {Function} callback this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object.
 * @return {null}
 * @api public
 */
Grid.prototype.get = function(id, callback) {
  // Validate that we have a valid ObjectId
  if(!(id instanceof ObjectID)) return callback(new Error("Not a valid ObjectID", null));
  // Create gridstore
  var gridStore = new GridStore(this.db, id, "r", {root:this.fsName});
  gridStore.open(function(err, gridStore) {
    if(err) return callback(err, null);

    // Return the data
    gridStore.read(function(err, data) {
      return callback(err, data)
    });
  })
}

/**
 * Delete file from grid
 *
 * @param {ObjectID} id ObjectID for file.
 * @param {Function} callback this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object.
 * @return {null}
 * @api public
 */
Grid.prototype.delete = function(id, callback) {
  // Validate that we have a valid ObjectId
  if(!(id instanceof ObjectID)) return callback(new Error("Not a valid ObjectID", null));
  // Create gridstore
  GridStore.unlink(this.db, id, {root:this.fsName}, function(err, result) {
    if(err) return callback(err, false);
    return callback(null, true);
  });
}

exports.Grid = Grid;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions