Skip to content

FelixFurtmayr/mongoose-gridfs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongoose-gridfs

Build Status

mongoose gridfs on top of gridfs-stream

Note!: Ensure mongoose connection before use

Installation

$ npm install --save mongoose mongoose-gridfs

Usage

var fs = require('fs');
var mongoose = require('mongoose');

//mongoose connect
var connection = mongoose.connect('mongodb://localhost/test');

//instantiate mongoose-gridfs
var gridfs = require('mongoose-gridfs')({
  collection:'attachments',
  model:'Attachment',
  mongooseConnection: 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){
  ...
});

API

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.

Options

  • collection a root collection to use in GridFS. default to fs
  • model a model name to use in mongoose. default to File

Example

//instantiate mongoose-gridfs
var gridfs = require('mongoose-gridfs')({
  collection:'attachments',
  model:'Attachment'
});

Schema & Model

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);

Static Methods

write(fileDetails:Object, stream:Readable, done(error, createdFile))

Write a readable stream into gridfs storage

Example
Attachment.write({
filename:'sample.txt',
contentType:'text/plain'
},
fs.createReadStream('/some/path/sample.txt'),
function(error, savedAttachment){
  ...
});

readById(objectid:ObjectId, [done(error, fileContent)]):Stream

Read a file content from gridfs storage.

Example for smaller file size
Attachment.readById(<objectid>, function(error, content){
  ...
})
Example for larger file size
var stream = Attachment.readById(<objectid>);

stream.on('error', fn);

stream.on('data', fn);

stream.on('close', fn);

unlinkById(objectid:ObjectId, done(error, unlinkedFile))

Remove file details and its content from underlying gridfs collection.

Example
Attachment.unlinkById(<objectid>, function(error, unlinkedAttachment){
  ...
});

or

Attachment.unlink(<objectid>, function(error, unlinkedAttachment){
  ...
});

Instance Methods

write(stream:Readable, done(error, createdFile))

Write a readable stream into gridfs storage

Example
var attachment = new Attachment({
  filename:'sample.txt',
  contentType:'text/plain'
});

attachment.write(
fs.createReadStream('/some/path/sample.txt'),
function(error, savedAttachment){
  ...
});

read([done(error, fileContent)]):Stream

Read a file content from gridfs storage.

Example for smaller file size
attachment.read(function(error, content){
  ...
})
Example for larger file size
var stream = attachment.read();

stream.on('error', fn);

stream.on('data', fn);

stream.on('close', fn);

unlink(done(error, unlinkedFile))

Remove file details and its content from underlying gridfs collection.

Example
attachment.unlink(<objectid>, function(error, unlinkedAttachment){
  ...
});

Literature Reviewed

Licence

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.

About

mongoose gridfs on top of gridfs-stream

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%