mongoose-gm
Promise based mongoose plugin for storing/manipulating base64 images in gridstore.
Installation
npm install mongoose-gm
Usage
This module is an extension to mongoose-gridstore. This release offers automatic resizing of images, and adds the resized buffers to the attachment.
mongoose-gridstore
All functionality of mongoose-gridstore is inherited. Full API of mongoose-gridstore is added to your schema. See the mongoose-gridstore README.
gm
This module depends on gm, which in turn depends on the imagemagick CLI installed. Without it, it does not work. See the gm README.
Granularity
You have the ability to partially/fully load all images or do the same for a single image.
Schema Decoration
var mongoose = ;var mongooseGM = ; var kittenSchema = name: type:String default:''; kittenSchema;var Kitten = mongoose;
plugin options
Automatic resizing and storing of resized images is supported by the option resize:
var mongoose = ;var mongooseGM = ; var kittenSchema = name: type:String default:''; var options = resize: small: //adds 'small' property to the attachment containing the buffer with resized 256x256 image width: 256 height: 256 medium: //adds 'medium' property to the attachment containing resized 1600 width image. width: 1600 //resize with aspect ratio of original image thumbnail: //adds 'thumbnail' property to the attachment containing 256x256 image + width: 256 + height: 256 + thumbnail: true //resize with gravity center keys : 'property1' 'property2' //optional, additonal keys that you want to add to the attachment object mongoose: mongoose //optional, the mongoose instance your app is using. Defaults to latest version.}; kittenSchema;
resized images
Resized images are automatically stored in your attachment with the specified keys in resize options:
fs;
Image meta data
Image meta data is automatically stored as property in the attachment:
fs;
example
A simple use case example is added at the end of the API description.
API
The module decorates your schema with the following functions:
addImage(name,buffer)
Add an attachment with name and buffer. The image and resized images as specified in the options of the plugin are stored in gridstore.
var kitten = ; kitten;
Accessing attachments
kittenattachments;
Attachment object
var attachment = filename: '' //as specified in your addAttachment call buffer: '' //base64 encoded buffer containing the image mimetype:'' //mimetype of the image metadata:'' //meta data of the image; //based on options of the plugin, the attachment will contain additional keys you've supplied in the options.
updateImage(name,buffer)
Update an attachment with name with the new buffer. The image and resized images as specified in the options of the plugin are stored in gridstore.
kitten;
removeImage(name)
Remove the image from the attachments and gridstore.
kitten;
load()
Load all attachments including images from the gridstore
kitten;
partialLoad()
partially load all attachments from the gridstore
kitten;
loadSingleImage(name)
fully loads a single image into the attachments array
kitten;
partialLoadSingleImage(name)
partially loads a single Image into the attachments array
kitten;
Example
This is a full example mixing in the mongoose-gridstore API.
var mongoose = ;var mongooseGM = ;var fs = ; var kittenSchema = name: type:String default:''; var options = resize: small: width: 256 height: 256 medium: width: 1600 //maintain aspect ratio keys: 'isKittenLicense'; kittenSchema; var Kitten = mongoose;var kitten = ; //add a pdf to the kitten object. //Use the mongoose-gridstore API fs; //add a picture of the kitten to the kitten object. //Use the mongoose-gm API fs; Kitten;
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.