Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-gridfs

Package Overview
Dependencies
Maintainers
1
Versions
70
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-gridfs

mongoose gridfs on top of gridfs-stream

  • 0.6.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
554
decreased by-75.56%
Maintainers
1
Weekly downloads
 
Created
Source

mongoose-gridfs

Build Status

mongoose gridfs on top of new gridfs api

Note!: Ensure mongoose connection before use

Installation

$ npm install --save mongoose mongoose-gridfs

Usage

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){
  ...
});

API

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.

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.

Keywords

FAQs

Package last updated on 27 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc