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 🙏

© 2026 – Pkg Stats / Ryan Hefner

tl-gridfs

v1.2.0

Published

Convenience layer for Mongo's GridFS on Node.js applications

Readme

Tl 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 tl-gridfs

Usage

var gridfs = require('tl-gridfs');

Initialization

You must initialize it with your current Mongo instance and db connection before using it:

gridfs.init(db, mongo);

If you're using mongoose, just pass mongoose.connection.db and mongoose.mongo:

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/your-db-name', options);

mongoose.connection.on('error', function (err) {
  throw err;
});

mongoose.connection.once('open', function () {
  gridfs.init(mongoose.connection.db, mongoose.mongo);
});

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.createReadStream('/path/to/the/file.ext');

Or as a Buffer:

var source = new Buffer('important buffer data here');

And then save it to GridFS with:

gridfs.write(source, function (err, fsfile) {
  if (err) {
    throw err;
  }

  /* Do whatever you want with your fsfile */
  console.log("The file %s named %d has a length of %d", fsfile._id, fsfile.filename, fsfile.length);
});

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.read(file, function (err, fsfile, rs) {
  if (err) {
    throw err;
  }

  /* Now you have your fsfile object and a nice read stream */
});

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.remove(file, function (err) {
  if (err) {
    throw err;
  }

  /* File has been removed */
});