Fi GridFS
Convenience layer for Mongo's GridFS on Node.js applications
This module uses Aaron Heckmann's gridfs-stream module to stream data into GridFS.
Installing
npm install --save fi-gridfs
Usage
var gridfs = ;
Initialization
You must initialize it with your current Mongo instance and db connection before using it:
gridfs;
If you're using mongoose, just pass mongoose.connection.db
and mongoose.mongo
:
var mongoose = ; mongoose; mongooseconnection; mongooseconnection;
Writing a file
You can write from a path String
pointing to a file, a Stream.Readable
object created from the fs
module or a Buffer
.
You can define your source as a String
:
var source = '/path/to/the/file.ext';
As a Stream.Readable
:
var source = fs;
Or as a Buffer
:
var source = 'important buffer data here';
And then save it to GridFS with:
gridfs;
A common fsfile Object
should look like this:
_id: ObjectId filename: String contentType: String length: Number chunkSize: Number uploadDate: Date aliases: Object metadata: Object md5: String
Reading a file
To read a file you must provide a String
than can be either a valid ObjectId
or a file name.
So, you can define your file as an ObjectId
:
var file = '55a52e49a562f0bb2627f38e';
Or as a file name:
var file = 'secret_document.docx';
And then get access to the file with:
gridfs;
The rs
parameter is a Stream.Readable
object that can be piped, written or anything that Stream.Readable
's can do.
Removing a file
To remove a file you must provide a String
than can be either a valid ObjectId
or a file name.
You can remove the file via it's ObjectId
:
var file = '55a52e49a562f0bb2627f38e';
Or it's file name:
var file = 'secret_document.docx';
And then remove the file with:
gridfs;