0.2.2 • Published 7 years ago

mlab-data-api v0.2.2

Weekly downloads
4
License
ISC
Repository
github
Last release
7 years ago

mlab-data-api

mlab-data-api is a node.js module designed to allow you to access mLab's Data API with minimal overhead from browser-side. Inspired by mongolab Data API

Installation

Using npm:

$ npm install --save mlab-data-api

If you don't have or don't want to use npm:

$ cd ~/.node_modules
$ git clone git://github.com/bgrusnak/mlab-data-api.git

Usage

To require the library and initialize it

import MLab from 'mlab-data-api';
var mLab=MLab({
  key: '<YOUR MLAB API DATA KEY>',
  host:'https://api.mlab.com', //optional
  uri : '/api',//optional
  version :'1',//optional
  database:'your working database', //optional
  timeout : 10000 //optional
})

Examples

List databases

  mLab.listDatabases()
  .then(function (response) {
    console.log('got',response.data)
  })
  .catch(function (error) {
    console.log('error', error)
  })

List collections

mLab.listCollections('exampledb')
  .then(function (response) {
    console.log('got',response.data)
  })
  .catch(function (error) {
    console.log('error', error)
  })

List documents

var options = {
  database: 'exampledb', //optional
  collection: 'examples',
  query: { 'key': 'value' }
};

mLab.listDocuments(options)
  .then(function (response) {
    console.log('got',response.data)
  })
  .catch(function (error) {
    console.log('error', error)
  });

Methods

All methods returns the Promises for further processing

listDatabases

Get the databases linked to the authenticated account

.listDatabases()

listCollections

Get the collections in the specified database

.listCollections(database)

Parameters:

NameDescriptionTypeRequired
databaseMongoDB database nameStringNo

listDocuments

Get the documents in the specified collection

.listDocuments(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringNo
collectionMongoDB collection nameStringYes
queryrestrict results by the specified JSON queryObjectNo
countreturn the result count for this queryBooleanNo
fieldsspecify the set of fields to include or exclude in each document (1 - include; 0 - exclude)ObjectNo
findOnereturn a single document from the result set (same as findOne() using the mongo shell)BooleanNo
orderspecify the order in which to sort each specified field (1- ascending; -1 - descending)StringNo
skipnumber of documents to skipNumberNo
limitnumber of documents to returnNumberNo

insertDocuments

Create a new document in the specified collection

.insertDocuments(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringNo
collectionMongoDB collection nameStringYes
dataa document or array of documents to be insertedObject/ArrayYes

updateDocuments

Update one or more documents in the specified collection

.updateDocuments(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringNo
collectionMongoDB collection nameStringYes
datareplacement document or update modifiersObjectYes
queryonly update document(s) matching the specified JSON queryObjectNo
allupdate all documents collection or query (if specified). By default only one document is modifiedBooleanNo
upsertinsert the document defined in the request body if none match the specified queryBooleanNo

deleteDocuments

Replace the contents of some or all documents of a collection

.deleteDocuments(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringNo
collectionMongoDB collection nameStringYes
queryonly replace the document(s) matching the specified JSON queryObjectNo

viewDocument

View a single document

.viewDocument(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringNo
collectionMongoDB collection nameStringYes
idthe document's idStringYes

updateDocument

Update a single document

.updateDocument(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringNo
collectionMongoDB collection nameStringYes
idthe document's idStringYes
updateObjectobject sent as replacementObjectYes

deleteDocument

Delete a single document

.deleteDocument(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringNo
collectionMongoDB collection nameStringYes
idthe document's idStringYes

runCommand

Run a MongoDB database command

.runCommand(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringNo
commandsMongoDB database commandObjectYes

Notes

  • Creating a new collection
    • As soon as you POST your first document you should see the collection appear
  • runCommands
    • Only certain MongoDB commands are exposed through the Data API
    • The available commands are:

      • getLastError
      • getPrevError
      • ping
      • profile
      • repairDatabase
      • resetError
      • whatsmyuri
      • convertToCapped
      • distinct
      • findAndModify
      • geoNear
      • reIndex
      • collStats
      • dbStats

Requirements

  • mLab account w/API key.
  • node.js v7.10.0+ (7.10.0 is the version I used to develop this module. I'm unsure if it will work with previous ones. If you run a previous version, and it works, let me know and I'll update this)
  • axios 0.16.2+

Disclaimer

From the official mLab Data API documentation:

mLab databases can be accessed by your application code in two ways.

The first method - the one we strongly recommend - is to connect using one of the MongoDB drivers (as described above). You do not need to use our API if you use the driver. In fact, using a driver provides better performance, better security, and more functionality.

The second method is to connect via mLab’s RESTful Data API. Use this method only if you cannot connect using a MongoDB driver.

Visit mLab's official documentation if you have any security concerns about using the Data API

Contributions

If you run into problems, have questions, or whatever else you can open an issue on this repository. If you'd like to submit a patch, shoot me a pull request. I'd like to keep this module simple, so if you want to add all kinds of crazy functionality - you might want to fork. When in doubt, send a pull request - the worst that can happen is that I won't merge it.

Related

mongolab-data-api: A node.js module for mLab (PKA MongoLab)

License

MIT © Ilya Shlyakhovoy

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago