2017 © Pedro Peláez
 

library javascript-sdk

DreamFactory Services Platform(tm) Javascript SDK

image

dreamfactory/javascript-sdk

DreamFactory Services Platform(tm) Javascript SDK

  • Tuesday, April 14, 2015
  • by dreamfactory
  • Repository
  • 19 Watchers
  • 48 Stars
  • 5,615 Installations
  • JavaScript
  • 1 Dependents
  • 0 Suggesters
  • 49 Forks
  • 2 Open issues
  • 19 Versions
  • 0 % Grown

The README.md

Address Book for JavaScript

This repo contains a sample address book application for JavaScript that demonstrates how to use the DreamFactory REST API. It includes new user registration, user login, and CRUD for related tables., (*1)

Installing DreamFactory on your local machine

To download and install DreamFactory, choose the desired installation package here., (*2)

Configuring your DreamFactory instance to run the app

  • Enable CORS for development purposes., (*3)

    • In the admin console, navigate to the Config tab and click on CORS in the left sidebar.
    • Click Add.
    • Set Origin, Paths, and Headers to *.
    • Set Max Age to 0.
    • Allow all HTTP verbs and check the Enabled box.
    • Click update when you are done.
    • More info on setting up CORS is available here.
  • Create a default role for new users and enable open registration, (*4)

    • In the admin console, click the Roles tab then click Create in the left sidebar.
    • Enter a name for the role and check the Active box.
    • Go to the Access tab.
    • Add a new entry under Service Access (you can make it more restrictive later).
      • set Service = All
      • set Component = *
      • check all HTTP verbs under Access
      • set Requester = API
    • Click Create Role.
    • Click the Services tab, then edit the user service. Go to Config and enable Allow Open Registration.
    • Set the Open Reg Role Id to the name of the role you just created.
    • Make sure Open Reg Email Service Id is blank, so that new users can register without email confirmation.
    • Save changes.
  • Import the package file for the app., (*5)

    • From the Apps tab in the admin console, click Import and click 'Address Book for JavaScript' in the list of sample apps. The Address Book package contains the application description, source code, schemas, and sample data.
    • Leave storage service and folder blank. It will use the default local file service named 'files'.
    • Click the Import button. If successful, your app will appear on the Apps tab. You may have to refresh the page to see your new app in the list.
  • Decide if you're going to run the app locally or load it from the instance., (*6)

    • For running locally you clone the repo to your machine and open index.html in the browser. If running locally you need to set the URL for your instance so the app can make the API calls. Go to your local repo and edit script.js. Set the constant INSTANCE_URL to point to your DreamFactory instance such as http://localhost:8080.
    • For running from the instance you launch the app directly from the Apps tab in the admin console. Leave INSTANCE_URL set to empty string.
  • If running from instance make your app files public., (*7)

    • Figure out where your app files are stored. If you used the default storage settings to import the app, it'll be the default local file service named 'files'.
    • Go to the Files tab in the admin console. Find your file service. Double click and find the folder for your app, e.g., AddressBookForJavaScript.
    • Go to the Services tab in the admin console and click the files service. Click the Config tab and add the folder name AddressBookForJavaScript as a public path. Now select the relevant container from the Container drop down. If you used the default storage settings to import the app then select local from the drop down list. Save your changes.
  • Edit your app API key, (*8)

    • If you are running from instance use the file manager to edit script.js and set APP_API_KEY to the key for your new app. The API key is shown on the app details in the Apps tab of the admin console.
    • If you are running locally edit script.js in your local repo.
  • Make sure you have a SQL database service named db. Most DreamFactory instances have a default db service for SQLite. You can add one by going to the Services tab in the admin console and creating a new SQL service. Make sure you set the name to db., (*9)

Running the Address Book app

You can launch the app from the Apps tab in the admin console, or by opening your local index.html in your browser., (*10)

When the app starts up you can register a new user, or log in as an existing user. Currently the app does not support registering and logging in admin users., (*11)

Example API calls

The DreamFactory Address Book for JavaScript uses AJAX to make API calls and the file functions.js contains the most common functions for CRUD operations., (*12)

The general form of a DreamFactory REST API call is:, (*13)

<rest-verb> http[s]://<server-name>/api/v2/[<service-api-name>]/[<resource-path>][?<param-name>=<param-value>], (*14)

The JavaScript app AJAX call format is:, (*15)

     dataType: String
  contentType: String
          url: String
         data: String,Object
       method: String
      headers: Object

Breaking down each parameter: - dataType Format of the returned data. This depends on the application, but will typically be JSON. - contentType The format of data sent to the API. This is commonly JSON and char set UTF-8. - url Holds the value of http[s]://<server-name>/api/v2/[<service-api-name>]/[<resource-path>] from the generic call. You can include the query parameters here. However, it is easier and cleaner to pass in the query parameters by using the data parameter than it is to format them into the url. - data The (optional) query parameters. - method The REST verb. - headers The header object must include the app specific API key and a session token., (*16)

Login and Registration Examples

Login

``` javascript // if the app is imported to the DreamFactory instance leave INSTANCE_URL blank. Email and password are typically input fields in the app UI. var INSTANCE_URL = 'http[s]://'; var email = 'my@email.com'; var password = 'mypassword';, (*17)

$.ajax({ dataType: 'json', contentType: 'application/json; charset=utf-8', url: INSTANCE_URL + '/api/v2/user/session', data: JSON.stringify({ 'email': email, 'password': password }), method:'POST', success:function (response) { // Handle success }, error:function (response) { // Handle error } });, (*18)


The `user/session` API request will return a session token. #### Registration ``` javascript // Email and password are typically input fields in the app UI. var firstname = 'firstname'; var lastname = 'lastname'; var email = 'email'; var new_password = 'new_password'; $.ajax({ dataType: 'json', contentType: 'application/json; charset=utf-8', url: INSTANCE_URL + '/api/v2/user/register?login=true', data: JSON.stringify({ 'first_name': firstname, 'last_name': lastname, 'email': email, 'new_password': password }), method:'POST', success:function (response) { // Handle success }, error:function (response) { // Handle error } });

The API request will return a session token when the (optional) login=true parameter is appended to the url. So with this parameter appended, the new registered user doesn't have to login to get a session token., (*19)

The login and registration examples illustrates how to make API requests to DreamFactory. This Address Book app has functions for common API request types, so in the following examples these functions are used., (*20)

Examples of Fetching Records

Fetch All Records in Table

``` javascript // Use the function getRecords() function to get all records from the contact_group table. // Usage: getRecords(table, params, token, callback) - params can be fields, filters etc., (*21)

$.api.getRecords('contact_group', null, session_token, callback);, (*22)

// create a callback function and handle the returned data there var callback = function(response) { // The response contains an array of group objects }, (*23)


#### Fetching Specific Fields ``` javascript // only need to get the contact_id and full contact name // set the fields param to give us just the fields we need var params = 'fields=contact_id,first_name,last_name'; $.api.getRecords('contacts', params, session_token, callback);

Fetching with a Filter Applied

``` javascript // create filter to get only the contact ids in the group id 10 // note the equal sign is url encoded (%3D) in the filter value; var params = 'filter=contact_group_id%3D10'; $.api.getRecords('contact_group_relationship', params, session_token, callback);, (*24)


### Creating a Record ``` javascript // Store a group by posting a stringified object. The API request will return the new group id if successful. var group = { resource: [ { name: 'My Group' } ] }; var params = JSON.stringify(group); $.api.setRecord('contact_group', params, session_token, callback);

Deleting Records

Deleting a Single Record by ID

``` javascript // a single record can be deleted by id. $.api.deleteRecord('contact', null, session_token, callback);, (*25)


#### Deleting Multiple Records by ID ``` javascript // one or more records from e.g. contact can be deleted by adding one or more ids to the parameter. var params = ids=1,2,3; $.api.deleteRecord('contact', params, session_token, callback);

Deleting Records Matching Field Conditions

javascript // delete all records matching field conditions, in this case all contacts with the last name 'Smith'. var params = 'filter=last_name%3DSmith'; $.api.deleteRecord('contact', params, session_token, callback);, (*26)

Additional Resources

More detailed information on the DreamFactory REST API is available here., (*27)

The live API documentation included in the admin console is a great way to learn how the DreamFactory REST API works., (*28)

The Versions

14/04 2015

1.0.18

1.0.18.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Sykes, Jason

api rest javascript swagger sdk dreamfactory dsp

14/04 2015

1.0.17

1.0.17.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Sykes, Jason

api rest javascript swagger sdk dreamfactory dsp

14/04 2015

1.0.16

1.0.16.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Sykes, Jason

api rest javascript swagger sdk dreamfactory dsp

06/11 2013

1.0.15

1.0.15.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

29/10 2013

1.0.14

1.0.14.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

29/10 2013

1.0.13

1.0.13.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

28/10 2013

1.0.12

1.0.12.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

24/10 2013

1.0.11

1.0.11.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

24/10 2013

1.0.10

1.0.10.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

23/10 2013

1.0.9

1.0.9.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

22/10 2013

1.0.8

1.0.8.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

22/10 2013

1.0.7

1.0.7.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

22/10 2013

1.0.6

1.0.6.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

04/10 2013

1.0.5

1.0.5.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

04/10 2013

1.0.4

1.0.4.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

03/10 2013

1.0.3

1.0.3.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

01/10 2013

1.0.2

1.0.2.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

30/09 2013

1.0.1

1.0.1.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp

30/09 2013

1.0.0

1.0.0.0 http://dreamfactorysoftware.github.io

DreamFactory Services Platform(tm) Javascript SDK

  Sources   Download

Apache2.0

by Jason Sykes

api rest javascript swagger sdk dreamfactory dsp