Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mongo-gridfs

quen24047.1kGPL-3.01.1.0TypeScript support: included

Uses the native GridFSBucket-Api from the new MongoDB NodeJS Driver and wrap it as Promise-Objects

mongodb, gridfs, native-promise, gridFSBucket, gridfs-bucket, nodejs, es6, typescript, mongoose, mongo, async

readme

mongo-gridfs

This is a simple wrapper for the new MongoDB GridFSBucket API.

The old GridStore-API is now deprecated.

How to install

That is simple

npm install mongo-gridfs

OR

yarn add mongo-gridfs

Usage

With MongoClient

const mongoDBConnection = await MongoClient.connect('mongodb://localhost:27017/files');
let gridFS = new MongoGridFS(mongoDBConnection, "attachments");
gridFS.findById("59e085f272882d728e2fa4c2").then((item) => {
    console.log(item);
}).catch((err) => {
    console.error(err);
});

With Mongoose

const mongooseConnection = await Mongoose.connect('mongodb://localhost:27017/files');
let gridFS = new MongoGridFS(mongooseConnection.db, "attachments");
gridFS.findById("59e085f272882d728e2fa4c2").then((item) => {
    console.log(item);
}).catch((err) => {
    console.error(err);
});

Methods

findById

By this method you will simple get the meta-object from the MongoDB as a Promise-Object. If nothing found at the Database, then it will reject and the catch-block will be executed.

gridFS.findById("59e085f272882d728e2fa4c2").then((item) => {
    console.log(item);
}).catch((err) => {
    console.error(err);
});

downloadFile

You will get the file simple written to the filesystem directly from the Database. If nothing found at the Database, then it will reject and the catch-block will be executed.

gridFS.downloadFile("59e085f272882d728e2fa4c2", {
    filename: "test.gif",
    targetDir: "/tmp"
}).then((downloadedFilePath) => {
    console.log(downloadedFilePath);
}).catch((err) => {
    console.error(err);
});

readFileStream

You will get a GridFSBucketReadStream as Promise. If nothing found at the Database, then it will reject and the catch-block will be executed.

This method is very useful, to stream the content directly to the user.

For example with express:

return gridFS.readFileStream(req.params.id).then((item) => {
    item
    .once("error", () => {
        return res.status(400).end();
    }).pipe(res);
}).catch(() => res.status(500));

changelog

1.1.0

  • Expose bucketName, bucket & connection properties in public

1.0.0

  • Change api for downloading files
  • Change constructor parameters

0.1.0

0.0.0

  • Initial version