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

mongoose-harmony-gridfs

v0.1.5

Published

Mongoose plugin for GridFS based on ES6 Harmony

Downloads

8

Readme

mongoose-harmony-gridfs

Mongoose plugin for GridFS based on ES6 Harmony

Purpose

Store large documents in a MongoDB database using Mongoose with a painless syntax. This project should only be used with generator heavy projects, such as Koa projects.

This project will only work with Node >0.11.

Installation

You can download this plug-in through NPM.

npm install mongoose-harmony-gridfs 

Usage

You must attach this plug-in to your schema to begin using GridFS. Once the plug-in has been attached, all new schema instances will contain GridFS functionality.

var mongoose = require('mongoose');
var plugin   = require('mongoose-harmony-gridfs');

var profileSchema = mongoose.Schema({
    name: String,
    joinedOn: Date
});

var settings = {
    // These are the keys that will be stored in GridFS
    keys: ['photo', 'thumbnail']
};

profileSchema.plugin(plugin, settings); // Attach GridFS
var Profile = mongoose.model('Profile', profileSchema); // Ready to use GridFS

var user = new Profile() // Has GridFS functionality

You can now access GridFS stores by using the keys you've set up in your schema.

var photo = yield user.photo;

Please be wary of key usage. Your original schema should not contain the same keys as the ones included in the plug-in.

Settings

You can configure the plug-in by providing a settings object. At minimum, a key property will be required in order for the plug-in to function.

  • key: Array of strings that determine what keys will be used by the plug-in.
  • bucket: String that determines what GridFS repository should be used. By default it is 'fs'.
  • linkPropertyName: String that determines what the property that is responsible for linking files will be named. By default it is '_gfs'.

CRUD operations

Saving

Once you've created a new instance, you can start saving buffers right away.

var user = new Profile()
var data = "This is a random string!";
user.photo = { buffer: data }; // This sends document information immediately to the database

Information is sent to the database immediately. This plug-in will convert all data in the buffer property to a buffer that can be written to a GridFS store. Any existing information that was previously stored in the database is removed and replaced with the new buffer.

You can also store metadata regarding your file when saving to the database.

var data = { buffer: "This is a random string!", metadata: { user: "mike", id: 23415 } };
user.photo = data;

Loading

If you can yield values because you are calling from a generator, you can yield for a database value. This will return a buffer with all contents in the file.

var photo = yield user.photo;

If you need to provide error handling, you can wrap this statement in a try/catch.

var photo;
try {
    photo = yield user.photo;
} catch (err){
    // Do something with the error
}

If you are operating within a function, you can use Q's done function to retrieve the information in a callback. Similarly, you can use the fail function to catch errors.

var promise = user.photo;
var photo;
promise.done(function(data){
    photo = data;
}).fail(function(err){
    throw err;
});

If you have stored metadata with your file, you can retrieve metadata information by using the metadata(key) method, which also returns a Q promise.

var metadata = yield user.metadata('photo');