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