Awesome REST Backend for Javascript Apps Using DreamFactory
My first impressions of using REST APIs from the front-end was: awesome simplicity in idea, complete pain in the ass to set up on the. I told myself "I always want to use REST for service CRUD but I never want to deal with maintaining it." I mean, there's CORS setup, dealing with the server and database stuff, etc. I just want to easily do my front-end code and not worry about the rest.
In that respect, DreamFactory is a, well, dream. DreamFactory lets you use a nice interface to host and configure storage, CORS, permissions, and every other part of a REST service!
DreamFactory is a free open source REST API platform for mobile, web, and IoT applications. It automatically generates an API for nearly any data source, including SQL, NoSQL, and web services. A variety of runtime packages are available for free on Bitnami and DreamFactory.com. Sample apps are provided for JavaScript, native mobile, and a variety of frameworks.
Highlights of DreamFactory REST API
A quick overview of DreamFactory features:
- Get live, documented REST APIs for SQL, NoSQL, file storage, email, push notifications and remote web services in seconds. No manual API coding required.
- Customize API behavior as needed with server-side scripting on any REST API endpoint.
- Secure each API endpoint with granular, role-based access permissions.
- Sample API code for JavaScript, iOS, Android, etc.
- Call API endpoints from any client application and get JSON or XML back.
- Open source local installers
- Version 2.0 now is available!
Introduction from DreamFactory's Product Manager
An introduction straight from the source:
Connecting to a MySQL Database with JavaScript
The following code samples assume MySQL storage is set up within DreamFactory's admin panel. Read Connecting to a MySQL Database with JavaScript to view the admin side of setup.
The most common use case for me is using JavaScript to interact with a MySQL backend. DreamFactory provides API helpers for many languages but in this case we'll want the JavaScript SDK, a set of extension for jQuery. Essentially each function wraps a jQuery.ajax
call, so if you don't use jQuery, you can easily transcribe it to your desired framework.
Start by defining your APP_API_KEY
and INSTANCE_HOST
:
var APP_API_KEY = 'YOUR_API_KEY'; var INSTANCE_HOST = 'https://df-davidwalsh.enterprise.dreamfactory.com';
Next you need to pass credentials to generate a session key:
$.ajax({ dataType: 'json', contentType: 'application/json; charset=utf-8', url: INSTANCE_HOST + '/api/v2/user/session', data: JSON.stringify({ 'email': ACCOUNT_EMAIL_ADDRESS, 'password': ACCOUNT_PASSWORD }), method: 'POST', success: function (response) { // Handle success }, error: function (response) { // Handle error } });
With the session key created you can now retrieve and create records using DreamFactory's JavaScript API:
// Get records from a "contacts" table // table, params, token, callback $.api.getRecords('contacts', null, session_token, function(response) { // Work with contact data here }); // Specify just the information we want back $.api.getRecords('contacts', 'fields=contact_id,email,age', session_token, function() { // Work with contact data here }); // Create a new record $.api.setRecord('contacts', JSON.stringify({ email: 'someone@somewhere.com', age: 23 }), session_token, callback);
Since the API is nice and easy to use: login
, logout
, register
, getRecords
, setRecord
, updateRecord
, deleteRecord
, and replaceRecord
. Each record-centric function has the same signature (table, params, token, callback
) so you can code without thinking about juggling signatures. As a long-time JavaScript developer I can tell you that coding with consistent and guessable signatures helps speed in development immensely.
Resources and Support
DreamFactory has provided a wealth of information about their product and there are several places to get inspiration and help getting there:
There's no shortage of help when you need it.
Give DreamFactory a Shot!
Once you get used to the DreamFactory interface and have your API and CORS configured, using the DreamFactory libraries helpers let's you quickly get your REST app running. If you are looking to create a REST powered app, sign up at DreamFactory and explore all of their offerings, both hosted and self hosted. You'll be presently surprised by what you find!