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

mongoose-gridstore

v0.1.25

Published

Mongoose plugin for storing large sized attachments to your document schema

Downloads

164

Readme

alt tag NPM version MIT License NPM downloads

mongoose-gridstore

Promise based mongoose plugin for storing large size attachments to your document schema.

Installation

npm install mongoose-gridstore

or add it to your package.json.

Usage

This module is a mongoose plugin that decorates your schema with large size attachments. Attachments are stored as base64 strings.

Granularity

You have the ability to partially/fully load all attachments or do the same for a single attachment.

Schema decoration

var mongoose  = require('mongoose');
var gridStore = require('mongoose-gridstore');

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.
});

API

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(''),    //base64 string 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;
});

Loading attachments

Load all attachments

email.loadAttachments()
.then(function(doc) {
	//All attachments including buffers are in the attachments array.
})
.catch(function(err) {
	console.log('error loading all attachments');
	throw err;
});

Partially load all attachments

email.partialLoadAttachments()
.then(function(doc) {
	//All attachments are in the attachments array. Buffers are empty for each attachment.
})
.catch(function(err) {
	console.log('error partial loading all attachments');
	throw err;
});

Partially load a single attachment

email.partialLoadSingleAttachment('file.json')
.then(function(doc) {
	//only filename, keys and mimetype filled in the attachment. Buffer is empty.
})
.catch(function(err) {
	console.log('error partial loading attachment');
	throw err;
});

Full load of a single attachment

email.loadSingleAttachment('file.json')
.then(function(doc) {
	//attachment is fully loaded and stored in the attachments array.
})
.catch(function(err) {
	console.log('error full loading 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.