0.0.1 • Published 5 years ago

mongolab-data-api-promise v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

mongolab-data-api-promise

mongolab-data-api is a node.js module designed to allow you to access mLab's Data API with minimal overhead.

I designed mongolab-data-api-promise so the only documentation you need, in addition to the Data API specification, is how to install node, how to install the module, and how to make a call. Also, this module and this documentation is based off of mongolab-data-api by gamontal. Their version is no longer being maintained and sync oriented. If you need a synced version, be sure to check it out their version.

Installation

Using npm:

$ npm install mongolab-data-api-promise

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

$ cd ~/.node_modules
$ git clone git://github.com/Electrox-Qui-Mortem/mongolab-data-api-promise.git

Usage

To require the library and initialize it with your account API key:

var mLab = require('mongolab-data-api-promise')('<Your Api Key Here>');

Examples

List databases

mLab.listDatabases()
    .then(databases => {
        console.log(data); // => [db1, db2, db3, ...]
    }, err => {
        console.log(err)
    })
;

List collections

mLab.listCollections('exampledb')
    .then(collections => {
        console.log(collections); // => [coll1, coll2, ...]
    }, err => {
        console.log(err)
    });

List documents

var opt = {
    database: 'exampledb',
    collectionName: 'examples',
    query: '{ "key": "value" }'
};

mLab.listDocuments(options, function (err, data) {
  console.log(data); //=> [ { _id: 1234, ...  } ]
});

Methods

listDatabases

Get the databases linked to the authenticated account. Returns an array

.lastDatabases(callback)

listCollections

Get the collections in the specified database. Returns an array.

.listCollections(database)

Parameters:

NameDescriptionTypeRequired
databaseMongoDB database nameStringYes

listDocuments

Get the documents in the specified collection. Returns an array.

.listDocuments(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringYes
collectionNameMongoDB collection nameStringYes
queryrestrict results by the specified JSON queryStringNo
resultCountreturn the result count for this queryBooleanNo
setOfFieldsspecify 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
sortOrderspecify the order in which to sort each specified field (1- ascending; -1 - descending)StringNo
skipResultsnumber of documents to skipNumberNo
limitnumber of documents to returnNumberNo

insertDocuments

Create a new document in the specified collection. Returns nothing.

.insertDocuments(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringYes
collectionNameMongoDB collection nameStringYes
documentsa document or array of documents to be insertedObject/ArrayYes

updateDocuments

Update one or more documents in the specified collection. Returns nothing

.updateDocuments(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringYes
collectionNameMongoDB collection nameStringYes
datareplacement document or update modifiersObjectYes
queryonly update document(s) matching the specified JSON queryStringNo
allDocumentsupdate 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. Returns nothing

.deleteDocuments(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringYes
collectionNameMongoDB collection nameStringYes
queryonly replace the document(s) matching the specified JSON queryStringNo

viewDocument

View a single document. Returns nothing

.viewDocument(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringYes
collectionNameMongoDB collection nameStringYes
idthe document's id-Yes

updateDocument

Update a single document. Returns nothing

.updateDocument(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringYes
collectionNameMongoDB collection nameStringYes
idthe document's id-Yes
updateObjectobject sent as replacementObjectYes

deleteDocument

Delete a single document. Returns nothing

.deleteDocument(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringYes
collectionNameMongoDB collection nameStringYes
idthe document's id-Yes

runCommand

Run a MongoDB database command. Returns nothing

.runCommand(options)

Options:

NameDescriptionTypeRequired
databaseMongoDB database nameStringYes
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

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 want anything added, you can send me an email here. Also, if you want to add anything yourself or see a problem with the module, you can create a pull request or open an issue. I'll try to fix it within a week. Maybe less!

License

MIT © Jermahl White

0.0.1

5 years ago

0.0.1-alpha

5 years ago

1.1.6

5 years ago