Interact with your data

You can interact with your data through interfaces like the Javascript SDK both serverside and browserside, or using the REST API. 

Javascript SDK server side

The user can execute the following operations on records:

  • create [INSERT]
  • read [GET]
  • update [PATCH]
  • delete [DELETE]

Installation

include the SDK in your node.js application:

const jexiaSDK = require('jexia-sdk-js/node');


Initialization and Authentication

The jexiaClient() function will return an instance of the Client class. On Node.JS, you will need to provide a fetch standard compliant function as a parameter. You will need to add a compatible dependency to your project. For the development of the SDK, we've used node-fetch.

const fetch = require("node-fetch");

In order to authenticate and operate on our datasets, we need to initialize a Jexia CLient and a DataOperationsModule. The Jexia SDK is built as a set of modules (or plugins) structured around a core entity (the Client class used above). In order to use a module you need to:

  • initialize it
  • pass it to the Client when calling the .init() method


const jexiaSDK = require('jexia-sdk-js/node');
const fetch = require('node-fetch');
const jexiaClient = jexiaSDK.jexiaClient;
const dataOperations = jexiaSDK.dataOperations;
 
//Initialize DataOperationsModule
let dataModule = dataOperations();
 
//Initialize Client and pass DataOperationsModule to it.
let client = jexiaClient(fetch).init({projectID: "<your-project-id>", key: "<your-apikey>", secret: "<your-secret"}, dataModule);

Populate the dataset: [INSERT]

const jexiaSDK = require('jexia-sdk-js/node');
const fetch = require('node-fetch');
const jexiaClient = jexiaSDK.jexiaClient;
const dataOperations = jexiaSDK.dataOperations;
 
//Initialize DataOperationsModule
let dataModule = dataOperations();
 
//Initialize Client and pass DataOperationsModule to it.
let client = jexiaClient(fetch).init({projectID: "<your-project-id>", key: "<your-apikey>", secret: "<your-secret"}, dataModule);
 
function insertRecord(jexiaClient){
    jexiaClient.then( (initializedClient) => {
        let postDataset = dataModule.dataset("posts");
        postDataset.insert([{title: "Super awesome post", content:"super awesome content", author:"the author name"}]).execute().then( (records) => {
            console.log("New Record" + records);
            process.exit();
        }).catch( (error) => {
            // there was a problem retrieving the records
            console.log(error);
            process.exit();
        });
    });
}
 
insertRecord(client);

Fetch the records [GET]:

const jexiaSDK = require('jexia-sdk-js/node');
const fetch = require('node-fetch');
const jexiaClient = jexiaSDK.jexiaClient;
const dataOperations = jexiaSDK.dataOperations;
 
//Initialize DataOperationsModule
let dataModule = dataOperations();
 
//Initialize Client and pass DataOperationsModule to it.
let client = jexiaClient(fetch).init({projectID: "<your-project-id>", key: "<your-apikey>", secret: "<your-secret"}, dataModule);
 
//select Records
function selectRecords(jexiaClient){
    jexiaClient.then( (initializedClient) => {
        let postDataset = dataModule.dataset("posts");
        postDataset.select().execute().then( (records) => {
            console.log(records);
            process.exit();
        }).catch( (error) => {
            // there was a problem retrieving the records
            console.log(error);
            process.exit();
        });
    });
}
 
selectRecords(client);

Update the record [PATCH]

const jexiaSDK = require('jexia-sdk-js/node');
const fetch = require('node-fetch');
const jexiaClient = jexiaSDK.jexiaClient;
const dataOperations = jexiaSDK.dataOperations;
const field = jexiaSDK.field;
 
 
//Initialize DataOperationsModule
let dataModule = dataOperations();
 
//Initialize Client and pass DataOperationsModule to it.
let client = jexiaClient(fetch).init({projectID: "<your-project-id>", key: "<your-apikey>", secret: "<your-secret"}, dataModule);
 
function updateRecord(jexiaClient){
    jexiaClient.then( (initializedClient) => {
        let postDataset = dataModule.dataset("posts");
        postDataset.update({title: "Super awesome post2"}).where(field("title").isEqualTo("Super awesome post")).execute().then( (records) => {
            console.log("Record " + JSON.stringify(records)) ;
            process.exit();
        }).catch( (error) => {
            // there was a problem retrieving the records
            console.log(error);
            process.exit();
        });
    });
}
 
updateRecord(client)

Delete the record [DELETE]

const jexiaSDK = require('jexia-sdk-js/node');
const fetch = require('node-fetch');
const jexiaClient = jexiaSDK.jexiaClient;
const dataOperations = jexiaSDK.dataOperations;
 
//Initialize DataOperationsModule
let dataModule = dataOperations();
 
//Initialize Client and pass DataOperationsModule to it.
let client = jexiaClient(fetch).init({projectID: "<your-project-id>", key: "<your-apikey>", secret: "<your-secret"}, dataModule);
 
//delete records
function deleteRecord(jexiaClient){
    jexiaClient.then( (initializedClient) => {
        let postDataset = dataModule.dataset("posts");
        postDataset.delete().where(field("title").isEqualTo("Super awesome post2")).execute().then( (records) => {
            console.log("Record " + JSON.stringify(records)) ;
            process.exit();
        }).catch( (error) => {
            // there was a problem retrieving the records
            console.log(error);
            process.exit();
        });
    });
}
 
deleteRecord(client);

Javascript SDK browser side

In order to use the SDK in your browser application add the SDK in the <head> section of your HTML page. In order to get access to the methods of the SDK, you need to use the namespace jexia when calling a method.

<script src="path/to/jexia-sdk-js/browser.js></script>

The jexiaClient() function will return an instance of the Client class. The browser SDK will use the native Fetch implementation.

Example:

<html>
    <head>
       <script src="node_modules/jexia-sdk-js/browser.js"></script>   
    </head>
    <body>
            <a href="#" id="authorize">Authorize</a>
            <script type="text/javascript">
                 document.getElementById("authorize").onclick = function() {
                     //Initialize dataOperationsModule
                     let dataModule = jexia.dataOperations();
                     let jexiaClient = jexia.jexiaClient().init({
                     projectID: "<your-project-id>",
                     key: "<your-key>",
                     secret: "<your-secret>"
                 },
                dataModule).then((initializedClient) => {
 
                    // you have been succesfully logged in!
                    // you can start using the initializedClient variable here
                    console.log(initializedClient)
                });
            }
          </script>
    </body>
</html>

Populate the dataset: [INSERT]

In case you didn't provide a schema, insert your data schemaless.

<script type="text/javascript">
    document.getElementById("authorize").onclick = function(){
                     //Initialize dataOperationsModule
                     let dataModule = jexia.dataOperations();
                     let jexiaClient = jexia.jexiaClient().init({projectID: "<your-project-id>", key: "<your-apikey>", secret: "your-secret"},dataModule).then((initializedClient) => {
        dataModule.dataset("posts").insert([{title:'The title', content: 'the content'}]).execute().then((records) => {        
                             let data = records;
                    
                             console.log("Inserted..." + JSON.stringify(data))
                         }).catch((error) => {     
                             console.log(error)
                         })
                     })
                 }
</script>

Fetch the records [GET]:

<a href="#" id="authorize">Authorize</a>
<script type="text/javascript">
    document.getElementById("authorize").onclick = function(){
                     //Initialize dataOperationsModule
                     let dataModule = jexia.dataOperations();
                     let jexiaClient = jexia.jexiaClient().init({projectID: "<your-project-id>", key: "<your-apikey>", secret: "<your-secret>"}, dataModule).then((initializedClient) => {
                         dataModule.dataset("posts").select().execute().then((records) => {
                             let data = records;
                             for(let i=0; i < data.length; i++){
                                 document.write("<ul><li>" + data[i].title + "</li></ul>")
                             }
                             console.log(JSON.stringify(data))
                         }).catch((error) => {     
                             console.log(error)
                         })
                     })
                 }
</script>

Update the record [ PATCH ]

<script type="text/javascript">
    document.getElementById("authorize").onclick = function(){
                     //Initialize dataOperationsModule
                     let dataModule = jexia.dataOperations();
                     let jexiaClient = jexia.jexiaClient().init({projectID: "<your-project-id>", key: "<your-apikey>", secret: "<your-secret>"}, dataModule).then((initializedClient) => {
                         dataModule.dataset("posts").update({title: "aNewKeyword2"}).where(field("title").isEqualTo("aNewKeyword")).execute().then((records) => {
                             let data = records;
                             
                             console.log("Updated record" + JSON.stringify(data))
                         }).catch((error) => {     
                             console.log(error)
                         })
                     })
                 }
</script>


Delete the record [DELETE]

<script type="text/javascript">
    document.getElementById("authorize").onclick = function(){
                     //Initialize dataOperationsModule
                     let dataModule = jexia.dataOperations();
                     let jexiaClient = jexia.jexiaClient().init(
                        {projectID: "<your-project-id>", key: "<your-api-key>",
                         secret: "<your-secret>"
                        }, dataModule).then((initializedClient) => {
                        dataModule.dataset("posts").delete().where(field("title").isEqualTo("My post")).execute().then((records) => {
                             let data = records;
                             
                             console.log("Deleted record" + JSON.stringify(data))
                         }).catch((error) => {     
                             console.log(error)
                         })
                     })
                 }
</script>

REST API

Create resource(s) (Supports related fields).

Populate the dataset: [INSERT]

[POST] http://<your-project-id>/rest/[resource_name]
 
Authorization: <jexia-auth-token>


Body:

[
  {
    "<field>": "<value>",
    "<field>": "<value>",
    "<field>": "<value>"
  }
]

Curl

curl -i -H "Accept: application/json" -H ": application/json" -H "Authorization: <jexia-auth-token>" -X POST -d '{"title":"title through curl"}' https://<your-app-id>.app.jexia.com/rest/<dataset>


Fetch the records [GET]:

Resources can be fetched by identifier using GET request:

[GET] http://<your-project-id>/rest/[resource_name]/[primary_key:OPTIONAL]
 
Authorization: <jexia-auth-token>


Example:

[GET] http://<your-project-id>/rest/posts

will return status code [200] OK and set of requested records as a response:

[
  {
    "id": "9e2eabf5-8a2b-4ffa-bb9f-566ab1e2b514",
    "user_id": "f18669ed-0c3b-4aaa-a3e0-f98930d568cb",
    "category_id": "199a7027-781c-48c4-ac82-5cdf22bb8c8c",
    "title": "My first post",
    "post": "I decided to write very good post, so I begin from this start point",
    "posted": true,
    "created_at": "2016-10-27T12:15:31Z",
    "updated_at": "2016-10-27T12:15:31Z"
  },
  {
    "id": "04bca006-cde8-47bc-a8eb-03c982e400de",
    "user_id": "f18669ed-0c3b-4aaa-a3e0-f98930d568cb",
    "category_id": "e837d481-8e35-411b-bc94-e61d84cb927b",
    "title": "My second post",
    "post": "This post will be draft for now. No one should see this.",
    "posted": false,
    "created_at": "2016-10-27T12:15:31Z",
    "updated_at": "2016-10-27T12:15:31Z"
  },
  {
    "id": "a0d140fb-4256-4018-8a26-97f87ad2173f",
    "user_id": "670b4aeb-e7dc-4a14-862f-89f354797abe",
    "category_id": null,
    "title": "How to get started to write a book",
    "post": "Sometime we decide to do something valuable...",
    "posted": true,
    "created_at": "2016-10-27T12:15:31Z",
    "updated_at": "2016-10-27T12:15:31Z"
  },
]

or by the primary key:

[GET] http://<your-project-id>/rest/posts/9e2eabf5-8a2b-4ffa-bb9f-566ab1e2b514
[
  {
    "id": "9e2eabf5-8a2b-4ffa-bb9f-566ab1e2b514",
    "user_id": "f18669ed-0c3b-4aaa-a3e0-f98930d568cb",
    "category_id": "199a7027-781c-48c4-ac82-5cdf22bb8c8c",
    "title": "My first post",
    "post": "I decided to write very good post, so I begin from this start point",
    "posted": true,
    "created_at": "2016-10-27T12:15:31Z",
    "updated_at": "2016-10-27T12:15:31Z"
  }
]

Curl

curl -i -H "Accept: application/json" -H ": application/json" -H "Authorization: <jexia-auth-token>" -X GET https://<project-id>.jexia.com/rest/<dataset>


Update the record [ PATCH ]

Update full content. All required fields should be provided.


[PUT] http://<your-project-id>/rest/[resource_name]

Authorization: <jexia-auth-token>

Example:

[PUT] http://<your-project-id>/rest/posts
 
[
  {
    "id": "PUT_HERE_POST_ID",
    "title": "Updated with JSON",
    "post": "updated content here ..."
  }
]

Curl:

curl -i -H "Accept: application/json" -H "Authorization:<jexia-auth-token>" -X PUT -d '{"id":"<id>","title":"new title through curl"}' https://<project-id>.app.jexia.com/rest/<dataset>


Delete the record [ DELETE ]

[DELETE] http://<your-project-id>/rest/[resource_name]/[primary_key:OPTIONAL]
 
Authorization: <jexia-auth-token>


Single delete by primary key

[DELETE] http://<your-project-id>/rest/posts/9e2eabf5-8a2b-4ffa-bb9f-566ab1e2b514

Bulk delete

[DELETE] http://<your-project-id>/rest/posts
 
[
  {
    "id": "04bca006-cde8-47bc-a8eb-03c982e400de"
  },
  {
    "id": "a0d140fb-4256-4018-8a26-97f87ad2173f"
  }
]

Curl:

curl -i -H "Accept: application/json" -H ": application/json" -H "Authorization:<jexia-auth-token>" -X DELETE  https://<project-id>.app.jexia.com/rest/<dataset>/<id>