mongoose-fs-fork

0.3.2 • Public • Published

mongoose-fs-fork

Fork of Mongoose plugin for large attributes storage in gridstore

Status

This is the last update on this fork. Please use mongoose-gridstore module for latest updates.

Installation

npm install mongoose-fs-fork

or add it to your package.json.

Usage

This module is a mongoose plugin that decorates your schema with large size attachments.

Schema decoration

var mongoose  = require('mongoose');
var gridStore = require('mongoose-fs-fork');
 
var emailSchema = new mongoose.Schema({
    from   : {type:String},
    to     : {type:String},
    subject: {type:String}
});
 
emailSchema.plugin(gridStore);
var Email = mongoose.model('Email', emailSchema);

Plugin options

 
emailSchema.plugin(gridStore, {    
    keys     : ['property1', 'property2'],  //optional, property names that you want to add to the attachment object.
    mongoose : mongoose  //optional, the mongoose instance your app is using. Defaults to latest mongoose version.
});

Adding an attachment

Once you have decorated your schema as shown above you can start adding attachments.

var email = new Email();
 
email.addAttachment("file.txt", new Buffer('test'))
.then(function(doc) {
    //email contains the attachment. promise returns the doc for further promise chaining.
})
.catch(function(err) {
    throw err;
});

Accessing attachments

email.attachments.forEach(function(attachment) {
    console.log(attachment.name);
    console.log(attachment.mime-type);
});

Attachment object

var attachment = {
    filename : '', //the filename of the attachment
    buffer   : new Buffer(), //buffer object with the content of your attachment
    mimetype : '' //mime-type of your attachment
};

If you have specified the keys option, these keys are added automatically as properties to the attachment object. The keys will be stored as meta-data in the gridstore. Keys are explicitly updated as follows:

email.attachments.forEach(function(attachment) {
    attachment.property1 = 'test property 1'  //any javascript object you like
    attachment.property2 = 'test property 2'  //any javascript object you like
});
 
email.save();

Retrieving attachments

email.loadAttachments()
.then(function(doc) {
    //your email object now contains the attachments
    console.log(doc.attachments.length); 
})
.catch(function(err) {
    throw err;
});

Saving attachments

When you save the document its attachements are stored in the gridstore. The pre-middleware detaches the buffer, keys etc. from the attachments because mongodb cannot store large files. Since mongoose does not contain post middleware to manipulate the document after a save, you have to reload attachments yourself right after a save (or find for that matter):

var email = new Email();
 
email.addAttachment("file.txt", new Buffer('test'))
.then(function() {
    return email.save();
})
.then(email.loadAttachments)
.then(function(doc) {
    //doc now contains all attachments again after a save.
})
.catch(function(err) {
    throw(err);
});
 
//Query and loadAttachments
Email.find({}, function(err,docs) {
    if(err) throw err;
    docs.forEach(function(doc) {
        doc.loadAttachments.done();
    });
})

Updating attachments

 
email.updateAttachment('file.txt', new Buffer('updated test'))
.then(function(doc) {
    //modified document including attachments is given back by the promise for further chaining.
})
.catch(function(err) {
    console.log('error updating attachment');
    throw err;
});

Removing attachments

email.removeAttachment('file.json')
.then(function(doc) {
    //modified document including updated attachments is given back by the promise
})
.catch(function(err) {
    console.log('error removing attachment');
    throw err;
});

Test

Above scenarios have been tested and can be found in the test directory of the node module. You can verify the package by executing mocha test in the root of the module.

Package Sidebar

Install

npm i mongoose-fs-fork

Weekly Downloads

7

Version

0.3.2

License

MIT

Last publish

Collaborators

  • chief.intelligence