mongoose gridfs on top of new gridfs api
Note!: Ensure mongoose connection before use
$ npm install --save mongoose mongoose-gridfs
// dependencies
const fs = require('fs');
const mongoose = require('mongoose');
const gridfs = require('mongoose-gridfs');
// ensure mongoose connect
mongoose.connect('mongodb://localhost/test');
// instantiate mongoose-gridfs
const { model: Attachment } = gridfs({
collection: 'attachments',
model: 'Attachment',
mongooseConnection: mongoose.connection
});
// create or save a file to gridfs
const readStream = fs.createReadStream('/some/path/sample.txt');
const options = ({ filename: 'sample.txt', contentType: 'text/plain' });
Attachment.write(options, readStream, (error, file) => { ... });
// for larger file size, read a file and receive a readable stream
const readStream = Attachment.readById(objectid);
// for smaller file size, read a file and receive a buffer
Attachment.readById(objectid, (error, buffer) => { ... });
// remove file details and its content from gridfs
Attachment.unlinkById(objectid, (error) => { ... });
mongoose-gridfs
wrap new gridfs api 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
const gridfs = require('mongoose-gridfs')({
collection: 'attachments',
model: 'Attachment'
});
To obtain underlying model use
const Attachment = gridfs.model
To obtain underlying schema for self model registration use
const 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
const readStream = fs.createReadStream('/some/path/sample.txt');
const options = ({ filename: 'sample.txt', contentType: 'text/plain' });
Attachment.write(options, readStream, (error, attachment) => { ... });
Read a file content from gridfs storage.
Attachment.readById(objectid, (error, content) => { ... });
const readStream = Attachment.readById(objectid);
readStream.on('error', fn);
readStream.on('data', fn);
readStream.on('close', fn);
Remove file details and its content from underlying gridfs collection.
Attachment.unlinkById(objectid, (error, unlinkedAttachment) => { ... });
Attachment.unlink(objectid, (error, unlinkedAttachment) => { ... });
Write a readable stream into gridfs storage
const readStream = fs.createReadStream('/some/path/sample.txt');
const attachment = new Attachment({
filename: 'sample.txt',
contentType: 'text/plain'
});
attachment.write(readStream, function (error, attachment) => { ... });
Read a file content from gridfs storage.
attachment.read((error, content) => { ... });
const readStream = attachment.read();
readStream.on('error', fn);
readStream.on('data', fn);
readStream.on('close', fn);
Remove file details and its content from underlying gridfs collection.
attachment.unlink((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.