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

@mitchallen/microservice-mongodb-post

v0.2.2

Published

microservice for posting to MongoDB

Downloads

4

Readme

@mitchallen/microservice-mongodb-post

A microservice REST POST module for inserting records into MongoDB

This module extends the @mitchallen/microservice-core module to create microservice endpoints to connect with MongoDB.

For a background on the core and microservices, visit the core npm page.


Installation

You must use npm 2.7.0 or higher because of the scoped package name.

$ npm init
$ npm install @mitchallen/microservice-mongodb-post --save

Usage

Provides a way to insert records into MongoDB via REST POST.

Non-Secure Only

Please note that this module is for NON-SECURE posts only. For ideas on how to create an SSL version see: @mitchallen/microservice-ssl. You may also want to checkout RichmondJS.

Define a Service Object

Define a service object with the following fields:

  • name: the name of your service (see example to use package name)
  • version: the version of your service (see example to use package version)
  • verbose: true or false, whether or not to echo info to the console
  • prefix: example: if prefix = "/v1", urls will begin with that (/v1/music)
  • port: HTTP port to listen on
  • mongodb: MongoDB connection info
    • mongodb.uri: MongoDB uri (example: mongodb://localhost/test)
  • collectionName: name of the MongoDB collection for inserting records into

Music Post Example

Here is the code from the example folder: examples/music-post/index.js

"use strict";

let demand = require('@mitchallen/demand'),
	dbCore = require('@mitchallen/microservice-mongodb-post',
    prefix = process.env.MUSIC_POST_API_VERSION || '/v1';

var service = {

    name: require("./package").name,
    version: require("./package").version,
    verbose: true,
    prefix: prefix,
    port: process.env.MUSIC_POST_PORT || 8004,
    mongodb: {
        uri: process.env.TEST_MONGO_URI || 'mongodb://localhost/test',
    },
    collectionName: "music",
};

dbCore.Service(service, function(err,obj) {
    if( err ) {
        console.log(err);
        throw new Error( err.message );
    }
});
  1. Create a file with the contents above called index.js

  2. Execute the following at the command line:

     npm install
     npm install @mitchallen/microservice-mongodb-post --save
     npm install @mitchallen/demand --save
  3. Run the app and leave it running:

     node index.js
        
  4. Open up a second terminal window and create a file called data.json with the following contents:

     { 
         "CatalogID": "b123",
         "Artist":"The Beatles",
         "SongTitle":"Abbey Road",
         "Album": "Octopus's Garden"
     }        
  5. Execute the following in the second terminal window (all on one line) (assumes a Mac or Linux and that port 8004 is not busy):

     curl -i -X POST -H "Content-Type: application/json" -d @data.json http://localhost:8004/v1/music
        
  6. Go to your MongoDB console at look at the contents of the music collection. In the MongoDB shell, verify with:

     db.music.find()

MongoDB with Username and Password

To login with a username and password, you would need a uri like this example for mlab.com:

export TEST_MONGO_URI='mongodb://foo:[email protected]:12345/dbnode01'  

You would replace foo with your mlab username and fooword with your password.

You don't have to use your mlab login info. They let you add other users, such as a test user.

The 12345 portion of the string would change to match the uri that mlabs gives you.


Callback

You pass the service object to the Service method along with a callback:

dbCore.Service(service, function(err,obj) {});

The object returned by the callback contains a server field:

{ server: server }

It's a pointer to the express modules server. If you are familiar with express, it's the value returned by app.listen. You don't need to actually return anything.

It was handy for me to use the close method in the unit tests so I wouldn't get port-in-use errors.

Here is an example of how to create it, then use the server return value to close it (checking for null omitted for brevity):

dbCore.Service(options, function(err,obj) {
    if(err) {
    	// ...
    }
    var server = obj.server;
    server.close()
}); 

Example

You can find working examples in the examples folder of the git repo.

  • examples / music-post - demos how to post a record to MongoDB. See that folders README for more info.

Testing

To test, go to the root folder and type (sans $):

$ npm test

Repo(s)


Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.


Version History

Version 0.2.3

  • Updated examples

Version 0.2.2

  • Now uses updated code

Version 0.2.1

  • Bumped version because breaks backward compatability

Version 0.1.2

  • Fixed overwrite of index.js by Gruntfile.js

Version 0.1.1

  • updated example to use published package

Version 0.1.0

  • initial release