mongoose gridfs on top of gridfs-stream
Note!: Ensure mongoose connection before use
$ npm install --save mongoose mongoose-gridfs
var fs = require('fs');
var mongoose = require('mongoose');
//mongoose connect
mongoose.connect('mongodb://localhost/test');
//instantiate mongoose-gridfs
var gridfs = require('mongoose-gridfs')({
collection:'attachments',
model:'Attachment',
mongooseConnection: mongoose.connection
});
//obtain a model
Attachment = gridfs.model;
//create or save a file
Attachment.write({
filename:'sample.txt',
contentType:'text/plain'
},
fs.createReadStream('/some/path/sample.txt'),
function(error, createdFile){
...
});
//for larger file size
//read a file and receive a stream
var stream = Attachment.readById(<objectid>);
//for smaller file size
//read a file and receive a buffer
Attachment.readById(<objectid>, function(error, buffer){
...
});
//remove file details and its content from gridfs
Attachment.unlinkById(<objectid>, function(error, unlinkedAttachment){
...
});
mongoose-gridfs
wrap gridfs-stream to provide valid mongoose schema
and model
to use with MongoDB GridFS.
Each instance of mongoose-gridfs
is binded to a specific GridFS collection
and mongoose model
or schema
by using options.
collection
a root collection to use in GridFS. default tofs
model
a model name to use in mongoose. default toFile
Example
//instantiate mongoose-gridfs
var gridfs = require('mongoose-gridfs')({
collection:'attachments',
model:'Attachment'
});
To obtain underlying model use
var Attachment = gridfs.model
To obtain underlying schema for self model registration use
var AttachmentSchema = gridfs.schema;
//attach plugins
//ensure indexes
//register and export a model
module.export = mongoose.model('Attachment', AttachmentSchema);
Write a readable stream into gridfs storage
Attachment.write({
filename:'sample.txt',
contentType:'text/plain'
},
fs.createReadStream('/some/path/sample.txt'),
function(error, savedAttachment){
...
});
Read a file content from gridfs storage.
Attachment.readById(<objectid>, function(error, content){
...
})
var stream = Attachment.readById(<objectid>);
stream.on('error', fn);
stream.on('data', fn);
stream.on('close', fn);
Remove file details and its content from underlying gridfs collection.
Attachment.unlinkById(<objectid>, function(error, unlinkedAttachment){
...
});
or
Attachment.unlink(<objectid>, function(error, unlinkedAttachment){
...
});
Write a readable stream into gridfs storage
var attachment = new Attachment({
filename:'sample.txt',
contentType:'text/plain'
});
attachment.write(
fs.createReadStream('/some/path/sample.txt'),
function(error, savedAttachment){
...
});
Read a file content from gridfs storage.
attachment.read(function(error, content){
...
})
var stream = attachment.read();
stream.on('error', fn);
stream.on('data', fn);
stream.on('close', fn);
Remove file details and its content from underlying gridfs collection.
attachment.unlink(<objectid>, function(error, unlinkedAttachment){
...
});
The MIT License (MIT)
Copyright (c) 2015 lykmapipo & Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.