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 = ;var gridStore = ; var emailSchema = from : type:String to : type:String subject: type:String; emailSchema;var Email = mongoose;
Plugin options
emailSchema;
API
Adding an attachment
Once you have decorated your schema as shown above you can start adding attachments.
var email = ; email;
Accessing attachments
emailattachments;
Attachment object
var attachment = filename : '' //the filename of the attachment 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:
emailattachments; email;
Retrieving attachments
email;
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 = ; email; //Query and loadAttachmentsEmail
Updating attachments
email;
Removing attachments
email;
Loading attachments
Load all attachments
email;
Partially load all attachments
email;
Partially load a single attachment
email;
Full load of a single attachment
email;
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.