npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongo-gridfs-storage

v3.0.1

Published

Wrapper over the native GridFSBucket from the MongoDB Driver, which provide API by Promise style

Downloads

90

Readme

Mongo gridFS storage.

This is a simple wrapper for the MongoDB GridFSBucket.

Install

npm i mongo-gridfs-storage

Usage

You can use mongoose connection

  const MongoGridFSStore = require('mongo-gridfs-storage');

  const mongoose = await mongoose.connect('mongodb://localhost:27017/db');
  const mongoGridFsStorage = new MongoGridFSStore(mongoose.connection.db, { bucketName: 'uploads' });

  ...

or mongo client

  const MongoGridFSStore = require('mongo-gridfs-storage');

  const mongoose = await MongoClient.connect('mongodb://localhost:27017/db');
  const mongoGridFsStorage = new MongoGridFSStore(mongoose.connection.db, { bucketName: 'uploads' });

  ...

Methods short-list

  const mongoGridFsStorage = new MongoGridFSStore(...);

  // Find
  const file = await mongoGridFsStorage.findOne({ filename: 'filename' }); // find one file into store.
  const files = await mongoGridFsStorage.find({ filename: 'filename' }); // find files into store.
  
  // Read
  const file = await mongoGridFsStorage.read({ filename: 'filename' }); // read file buffer into store.

  // Write
  const result = await mongoGridFsStorage.write(stream, { filename: 'filename' }); // write stream into store.

  // Delete
  const result = await mongoGridFsStorage.delete({ filename: 'filename' }); // delete file from store.

  // FindOneAndRead
  const result = await mongoGridFsStorage.findOneAndRead({ filename: 'filename' }); // find file and read buffer.

MongoGridFSStore

Constructor

  • Description

    Created instance of mongo-gridfs-store.

  • Sample call

      const mongoGridFsStorage = MongoGridFSStore(connection, options);
  • Params

    Requried

    connection - MongoDB connection.

    Optional

    options - options for GridFSBucket instance.

    options.bucketName - The 'files' and 'chunks' collections will be prefixed with the bucket name followed by a dot. Default to 'fs'.

    options.chunkSizeBytes - Number of bytes stored in each chunk. Defaults to 255KB(255* 1024).

Methods

findOne

  • Description

    Method find file into MongoDB file storage by filename or id. Return array of simple MongoDB file meta-object as Promise-Object.

  • Sample call

      const mongoGridFsStorage = MongoGridFSStore(connection);
        
      const filter = {
        filename: 'filename'
      };
      
      const options = {};
      
      const file = await mongoGridFsStorage.findOne(filter, options);
  • Params

    Requried

    filter - search options.

    filter._id - id for file.

    filter.id - alias for id parameter.

    filter.filename - filename.

    id or filename are required.

    Optional

    options - options for GridFSBucket find method.

    options.batchSize - Batch size. Default to null. Optional.

    options.limit - limit. Default to null. Optional.

    options.maxTimeMS - maxTimeMS. Default to null. Optional.

    options.noCursorTimeout - set cursor's noCursorTimeout flag. Default to null. Optional.

    options.skip - skip. Default to null. Optional.

    options.sort - sort. Default to null. Optional.

find

  • Description

    Method find files into MongoDB file storage by filename or id. Return array of simple MongoDB file meta-object as Promise-Object.

  • Sample call

      const mongoGridFsStorage = MongoGridFSStore(connection);
        
      const filter = {
        filename: 'filename'
      };
      
      const options = {};
      
      const file = await mongoGridFsStorage.find(filter, options);
  • Params

    Requried

    filter - search options.

    filter._id - id for file.

    filter.id - alias for id parameter.

    filter.filename - filename.

    id or filename are required.

    Optional

    options - options for GridFSBucket find method.

    options.batchSize - Batch size. Default to null. Optional.

    options.limit - limit. Default to null. Optional.

    options.maxTimeMS - maxTimeMS. Default to null. Optional.

    options.noCursorTimeout - set cursor's noCursorTimeout flag. Default to null. Optional.

    options.skip - skip. Default to null. Optional.

    options.sort - sort. Default to null. Optional.

read

  • Description

    Method read file into MongoDB file chunk storage by filename or id. Return Buffer simple MongoDB file meta-object as Promise-Object. If file not found, then return null.

  • Sample call

      const mongoGridFsStorage = MongoGridFSStore(connection);
    
      const filter = {
        filename: 'filename'
      };
    
      const fileBuffer = await mongoGridFsStorage.read(filter);
  • Params

    Requried

    filter - search options.

    filter._id - id for file.

    filter.id - alias for id parameter.

    filter.filename - filename.

    id or filename are required.

write

  • Description

    Method write readable stream into MongoDB file chunk storage and save it with filename or id, then has been set. Return Promise that must be resolved with a file id value in the end.

  • Sample call

      const mongoGridFsStorage = MongoGridFSStore(connection);
    
      const stream = getReadableStream();
        
      const options = {
        filename: 'filename'
      };
    
      const result = await mongoGridFsStorage.write(stream, options);
  • Params

    Requried

    stream - readable stream.

    filter - search options.

    filter._id - id for file.

    filter.id - alias for id parameter.

    filter.filename - filename.

    id or filename are required.

writeBuffer

  • Description

    Method write buffer into MongoDB file chunk storage and save it with filename or id, then has been set. Return Promise that must be resolved with a file id value in the end.

  • Sample call

      const mongoGridFsStorage = MongoGridFSStore(connection);
    
      const buffer = getBuffer();
        
      const options = {
        filename: 'filename'
      };
    
      const result = await mongoGridFsStorage.writeBuffer(buffer, options);
  • Params

    Requried

    buffer - Node JS buffer.

    filter - search options.

    filter._id - id for file.

    filter.id - alias for id parameter.

    filter.filename - filename.

    id or filename are required.

delete

  • Description

    Method remove file into MongoDB file storage by id. Return Promise that must be resolved with a file id in the end.

  • Sample call

      const mongoGridFsStorage = MongoGridFSStore(connection);
        
      const filter = {
        id: <Object ID>
      };
    
      const result = await mongoGridFsStorage.delete(filter);
  • Params

    Requried

    filter - search options.

    filter._id - id for file.

    filter.id - alias for id parameter.

findOneAndRead

  • Description

    Method is composition two methods: findOne and read. Method find by filename or id into storage, read it and return file Buffer. Return null if file not found.

  • Sample call

      const mongoGridFsStorage = MongoGridFSStore(connection);
        
      const filter = {
        filename: 'filename'
      };
    
      const result = await mongoGridFsStorage.findOneAndRead(filter);
  • Params

    Requried

    stream - readable stream.

    filter - search options.

    filter._id - id for file.

    filter.id - alias for id parameter.

    filter.filename - filename.

    id or filename are required.

Test

npm run test

License

MIT © nlapshin