New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-harmony-gridfs

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-harmony-gridfs

Mongoose plugin for GridFS based on ES6 Harmony

  • 0.1.5
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 02 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc