Code Monkey home page Code Monkey logo

mongo-gridfs's Introduction

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

mongo-gridfs's People

Contributors

dependabot-preview[bot] avatar quen2404 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mongo-gridfs's Issues

Cannot delete file

delete(id: string): Promise<boolean>
ERROR [ExceptionsHandler] This method requires a valid topology instance

find and findOne not working as expected.

I'm trying to use GridFS using this library based on nestjs/nest#1169 (comment) example that you had provided.

The basics work. But when I'm using find or findOne, these always return null or errors out saying that no file was found.

I tried a simple query like findOne({ _id: '<someValidId>' }). This does not work but putting the same id in findById('<someValidId>') works.

Edit:

Works only when I send in parsed ObjectId and not raw strings

Difficulties initializing the library

Hi,

When running :

 const mongooseConnection = mongoose.connect(process.env.MONGODB_URI);
 
 let gridFS = new MongoGridFS.MongoGridFS(mongooseConnection, "fs");
 gridFS.findById("59e085f272882d728e2fa4c2").then((item) => {
     console.log(item);
 }).catch((err) => {
     console.error(err);
 });

I get the following :

TypeError: db.collection is not a function

I cannot do const mongooseConnection = await.... as this is not in an async function, but in the root of a .js file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.