Skip to content

Latest commit

 

History

History
127 lines (81 loc) · 4.13 KB

DEL-README.md

File metadata and controls

127 lines (81 loc) · 4.13 KB

Delete Method

A microservice REST DELETE method for deleting a record from MongoDB

Installation

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

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

Usage

Provides a way to delete a record from MongoDB via REST DELETE.

Non-Secure Only

Please note that this module is for NON-SECURE delete requests 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

Example

"use strict";

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

var service = {

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

dbCore.Delete(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-delete --save
     npm install @mitchallen/demand --save
    
  3. Run the app and leave it running:

     node index.js
    
  4. Execute the following in the second terminal window (all on one line) (assumes a Mac or Linux and that the port is not busy). Substitute {id} with an id from a music collection in your database. If you want to see a way to create data for testing, see the examples/music-delete folder in the git repo:

     curl -i -X DELETE http://localhost:8006/v1/music/{id}   
    

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:fooword@ds12345.mlab.com: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 module along with a callback:

dbCore.Delete(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.Delete(options, function(err,obj) {
    if(err) {
    	// ...
    }
    var server = obj.server;
    server.close()
}); 

Testing

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

$ npm run test-delete