0.0.6 • Published 4 years ago

mlab-promise v0.0.6

Weekly downloads
9
License
MIT
Repository
github
Last release
4 years ago

mlab-promise

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

I don't intend to remove the old functions, but I added new cache functions which can be disabled. It effectively polls the database and loads all your information so that there's no wait between each function. mlab-promise has all the documentation that you'd need to access your mlab database. Also, this module and this documentation is based off of mongolab-data-api by gamontal. Their version is no longer being maintained and purely synchronous. If you need a something like that, be sure to check it out their version.

Major change

  • All old functions except for runCommand and updateDocuments can now be run normally but will return with cached values for improved performance
  • Cacheing values has yet to be properly documented, however it is much faster and uses much less requests, despite it's startup time of a few seconds
  • Cacheing requires discord.js to also be installed, because the Maps that are written to are Collections

Installation

Using npm:

$ npm install mlab-promise

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

$ cd ~/.node_modules
$ git clone git://github.com/Logos-King/mlab-promise.git

Usage

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

let mlabInteractor = require('mlab-promise')
let mLab = new mlabInteractor('<Your Api Key Here>', "<whether-to-cache>[boolean: true or false]",[ignore='databasenametonotcache']);

Examples

List databases

mlab.on('ready', () => {
    console.log(mlab.databases.map(database => database.name)) // => [db1, db2, db3, ...]
})
mLab.listDatabases()
    .then(databases => {
        console.log(data); // => [db1, db2, db3, ...]
    });

List collections

mlab.on('ready', () => {
    console.log(mlab.databases.get('exampledb').collections.map(collection => collection.name))  // => [coll1, coll2, ...]
})
mLab.listCollections('exampledb')
    .then(collections => {
        console.log(collections); // => [coll1, coll2, ...]
    });

List documents

mlab.on('ready', () => {
    console.log(mlab.databases.get('exampledb').collections.get('examples').documents.map(document => document.data))  //=> [ { _id: 1234, ...  } ]
})
var opt = {
    database: 'exampledb',
    collectionName: 'examples',
    query: '{ "key": "value" }'
};

mLab.listDocuments(options)
    .then(console.log)

Methods

listDatabases

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

.lastDatabases()

listCollections

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

.listCollections(databaseName)

Parameters:

NameDescriptionTypeRequired
options.databaseNameMongoDB database nameStringYes

listDocuments

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

.listDocuments(options)

Options:

NameDescriptionTypeRequired
options.databaseNameMongoDB database nameStringYes
options.collectionNameMongoDB collection nameStringYes
options.queryList of properties that all results will haveQueryNo
options.resultCountNumber of resultsNumberNo
options.setOfFieldsSpecifies either the only fields to include(1) or the fields to exclude(0)Map<string,number>No
options.findOneWhether to stop at the first or notBooleanNo
options.sortOrderMap containing a property name 1 or -1. 1 means to sort the results in ascending order by that property and vice versa. Default is to order them by when way they were addedMap<string,number>No
options.skipResultsNumber of results to skip. Useful for PagingNumberNo
options.limitMaximum amount of resultsNumberNo

insertDocuments

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

.insertDocuments(options)

Options:

NameDescriptionTypeRequired
databaseNameMongoDB database nameoptions.databaseNameMongoDB database nameStringYes
options.collectionNameMongoDB collection nameStringYes
documentsDocuments to insertArrayYes

updateDocuments

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

.updateDocuments(options)

Options:

NameDescriptionTypeRequired
databaseNameMongoDB database nameStringYes
collectionNameMongoDB collection nameStringYes
documentsDocuments to insertArray.<Object>Yes
queryList of properties that all updated documents will haveQueryNo
allDocumentsWhether or not to update all documents found.BooleanNo
upsertWhether to insert document if none is foundBooleanNo

deleteDocuments

Replace the contents of some or all documents of a collection. Returns nothing

.deleteDocuments(options)

Options:

NameDescriptionTypeRequired
databaseNameMongoDB database nameStringYes
collectionNameMongoDB collection nameStringYes
queryList of properties that all deleted documents will have will haveStringNo

viewDocument

View a single document. Returns nothing

.viewDocument(options)

Options:

NameDescriptionTypeRequired
databaseNameMongoDB database nameStringYes
collectionNameMongoDB collection nameStringYes
idID of the document to manipulateStringYes

updateDocument

Update a single document. Returns nothing

.updateDocument(options)

Options:

NameDescriptionTypeRequired
databaseNameMongoDB database nameStringYes
collectionNameMongoDB collection nameStringYes
idID of the document to manipulateStringYes
updateObjectThe document to be used to updateObjectYes

deleteDocument

Delete a single document. Returns nothing

.deleteDocument(options)

Options:

NameDescriptionTypeRequired
databaseNameMongoDB database nameStringYes
collectionNameMongoDB collection nameStringYes
idID of the document to manipulateStringYes

runCommand

Run a MongoDB database command. Returns nothing

.runCommand(options)

Options:

NameDescriptionTypeRequired
databaseNameMongoDB 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 © Logos King

0.0.6

4 years ago

0.0.5

4 years ago

0.0.2

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.1

4 years ago