DreamFactory Blog

DreamFactory Now Supports MongoHQ

Posted by Ben Busse on Tue, Oct 8, 2013

    

BenBusseWe’re excited to announce that we’ve partnered with MongoHQ. Now you can connect directly to any MongoHQ database instance from the DreamFactory Services Platform (DSP). In a nutshell, this new feature makes accessing your MongoHQ data really easy.

  1. Securely store your MongoHQ credentials on a server. You never want to do this on the client for security reasons!
  2. Make database calls to your MongoHQ database with a ready-to-go REST API. Now you don’t have to write your own API to access MongoHQ. DreamFactory creates the API for you. Just make an API call and get a JSON document back from the server.
  3. Apply CRUD permissions at the collection level based on user role. This is a great feature if you need to apply different data permissions for different users in your application.

In this blog post, we’ll show you how to connect to MongoHQ in 60 seconds, quickly browse the REST API to access your data stored in MongoHQ, and apply CRUD permissions for different user roles.

Before getting started, you’ll need to sign up for a MongoHQ account if you don’t already have one. The link above provides developers a $50 credit for opening a new account with MongoHQ. 

Once you’ve logged into your DreamFactory Services Platform (DSP), you’ll need to configure your connection to MongoHQ. Make sure to sign up for a DreamFactory account if you don’t already have one. 

Set Up a MongoHQ Service

Once you’re logged into your DSP as an administrator, start by clicking on the ‘Services’ tab on the left-hand side of the screen.

services nav

Then click to Create New Service and fill out the required information about your MongoHQ database. See the screenshot below for an example.

Make sure to select ‘NoSQL DB’ in the Type list box and ‘MongoHQ’ in the NoSQL Type list box. You’ll then need to enter your MongoHQ connection string, database name, database username, and database password. Don’t forget to click the Save button at the bottom of the screen.

describe the image

Browse the REST API for MongoHQ

Once you’ve saved your MongoHQ connection settings, DreamFactory automatically creates a REST API for your MongoHQ database. To browse the API, just click on the ‘API Documentation’ tab on the left-hand side of the screen.

api nav

Then find the API name that you used for MongoHQ (in our example we called the API ‘mongohq’ in the setup screen). You’ll see a total of 14 API calls that allow you to manipulate your MongoHQ data with standard GET, POST, PATCH, PUT, and DELETE verbs.

mongohq api resized 600

We use a great tool called Swagger for our API docs, which makes it really easy to interact with the API and inspect your MongoHQ data right from your web browser. We suggest taking a look at the live Swagger API before writing client-side code to access MongoHQ data from your app.

For example, we have a collection called 'zips' that contains demographic data by zipcode. Let’s say we want to run a simple filter on this data to look at cities in California with a population greater than 50,000. In this example, we’ll also limit the number of records returned to 5.

Doing so is easy. In the API docs, just navigate to the GET request for /mongohq/{table_name}. Then simply enter the name of the ‘zips’ collection, a simple filter string, and set the limit to 5.  

mongohq params resized 600

Now click the ‘Try it out!’ button and you’ll see the API request and response.

mongohq json resized 600

You can also make API calls from the command line. For example, you can use cURL to make a GET request on a particular MongoHQ table and append a BSON filter string in the request. You’ll see the corresponding JSON returned in the response.

And you can see the available MongoHQ API operations right in your browser using the DreamFactory Javascript SDK (for example with FireBug). 

mongohq firebug resized 600

Set Up CRUD Permissions to MongoHQ by User Role

Lastly, if your application requires different data permissions for different user roles, you can easily configure CRUD access for your various MongoHQ collections.

For example, let’s say we have a ‘sales rep’ role and we only want to grant READ and WRITE access to a collection called ‘opportunities.’ We don’t want to allow a ‘sales rep’ user to be able to access other collections and we don’t want a ‘sales rep’ user to create or delete data in the ‘opportunities’ collection.

Setting up data access permissions only takes a few seconds. In this example, you’d simply create a Role called ‘sales rep.’ Then you’d click on the Roles tab on the left-hand side of the screen and assign ‘Read and Write’ service access to opportunities for the MongoHQ service that we just set up. That’s it!

mongohq roles

describe the image

Conclusion

We hope this quick tutorial shows how easy it is to set up MongoHQ in your DSP and access your data with REST and highly configurable CRUD permissions.

Please let us know what you think in the comments section!

Get Our Blog In Your Inbox

Follow Us