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-media-plugin

v0.6.7

Published

Mongoose plugin for handling media files in the cloud.

Downloads

16

Readme

mongoose-media-plugin

Build Status codecov

Description

Mongoose plugin for managing S3 files as if they were stored in mongo.

Adds read and write stream capabilities to a mongo collection. The stream is offloaded to S3 during save.

Prerequisites

You will need a aws account with full access to S3:

///arn:aws:iam::aws:policy/AmazonS3FullAccess
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": "*"
        }
    ]
}

It is highly recommended you pass your AWS credentials via envoiroment vars

$ export AWS_ACCESS_KEY_ID=<your access key>
$ export AWS_SECRET_ACCESS_KEY=<your secret>

Dependancies

node v10+
mongoose@5.*

Installation

$ npm install mongoose-media-plugin

Example usage

import mongoose from 'mongoose';
import MMD, { Configuration } from 'mongoose-media-plugin';
import fs from 'fs';

const
    Schema = mongoose.Schema( {
        name: { type: String },
        //...restofschema
    } ),
    Setup = new Configuration({
        bucket: "<Bucket name>",
        createBucket: true // Set this to create the bucket if it  doesnt exist.
    } );

( async () => {

    await mongoose.connect( process.env.MONGOCONN || "mongodb://localhost:27017/mnp-example", { useNewUrlParser: true, useUnifiedTopology: true } );

    let
        config = await Setup.Configure(),
        local = fs.createReadStream( "<path to local file>" ),
        Media, Insert;

    Schema.plugin( MMP, config );
    Media = new mongoose.model( "media", Schema );

    Insert = new Media( {
        name: "first document",
        body: local
    } );
    
    Insert.save( ( err, saved ) => {
        console.log( err, saved );
        /**
        * Returns....
        * {
        *     _id: <ObjectId>
        *     name: "first document",
        *     ContentType: "video/mp4", //etc
        *     ETag: <S3 Etag>, //useful for setting cache see express example.
        *     Size: <size in bytes for file>,
        *     body: <ReadableStream> // from S3
        *     created: Date,
        *     updated: Date,
        *     uploaded: Date
        * }
        */
        
        // download the file...
        let download = fs.createWriteStream( "<path to new file>" );
        saved.body.pipe( download );
    } );
} )()

Running the express example

Ensure you have mongo running locally and install dependancies:

$ npm install
$ npm run build && npm run example

Running the tests

Ensure you have mongo running locally and install dependancies:

$ npm install
$ npm test

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details