Jexia and JS

Get to know all the basic pieces of code to get the advantage of Jexia through JavaScript.

Install for NodeJs

With NodeJs you will need to install node-fetch and ws dependencies:

npm install jexia-sdk-js node-fetch ws --save

Install for Web browser

npm install jexia-sdk-js --save

Initialize

To use dataset features, just add dataOperations module to the client initialization. Notice, that for Node.js you have to import dataOperations from jexia-sdk-js/node, but if you are running your application in a browser, you should import it from jexia-sdk-js/browser.

import { jexiaClient, dataOperations } from "jexia-sdk-js/node"; // "jexia-sdk-js/browser" for browser applications

const dataModule = dataOperations();

jexiaClient().init({
  projectID: "<your-project-id>",
  key: "<your-project-api-key>",
  secret: "<your-project-api-secret>",
}, dataModule);

There is an option to have access via User management system instead of API. For this you need to create at least 1 user, policy for AllUsers. As well you need to import UMS module. With example below you can inititate Jexia and select all records which is elligable for Elon@tesla.com

import { jexiaClient, dataOperations, UMSModule} from "jexia-sdk-js/node"; // "jexia-sdk-js/browser" for browser applications

const dom = dataOperations();

jexiaClient().init({  
  projectID: "your-project-id"
}, dom, ums);

ums.signIn({  
  email: 'Elon@tesla.com',  
  password: 'user-password',  
  default: true
});  

dom.dataset('rockets').select();  

Currently, SDK is focused on consuming data. You can execute the following operations on records:

  • CREATE
  • READ
  • UPDATE
  • DELETE

Executing an operation on a set of records is done through a Query. Depending on their type, Queries give the user access to some (or all) of the following options:

  • Filtering (only records satisfying a certain condition will be retrieved/affected).
  • Sorting (the records will be sorted by a rule and direction before the action is executed).
  • Limiting/offsetting (only a certain number of records, starting from a certain position in the Dataset will be affected).
  • Selecting the fields to be retrieved (when the user does not want the entire record to be retrieved, only certain columns).

It is important to notice that all these features are handled server-side. More you can get on JS SDK documentation page 

Inserting records

Records can be inserted to a dataset either by sending an array or a single one. The response will always be an array though.

[..]
const posts = dataModule.dataset("posts");

// Multiple records
const insertQuery = posts.insert([
  { title: "New Post", content: "content here" },
  { title: "Another Post", content: "some more content" },
]);

// Single record
const insertQuery = posts.insert({
  title: "New Post",
  content: "content here",
});

// Either way, the response will be an array
insertQuery
  .execute()
  .then(records => {
    // you will always get an array of created records, including their generated IDs (even when inserting a single record)
  }).catch(error => {
    // you can see the error info here, if something goes wrong
  });
[..]

Updating records

The update method is used to modify records, it always has to be used with the .where method and a filter criteria.

[..]
try {
  const filter = field("author_name").isInArray(["Tom", "Harry"]);
  const affectedRecords = await dataModule
    .dataset("posts")
    .update({ title: "Same title for tom and harry posts" })
    .where(filter)
    .execute();

  console.log(affectedRecords);
  // output: 
  // [
  //   { title: "Same title for tom and harry posts", author_name: "Tom" }, 
  //   { title: "Same title for tom and harry posts", author_name: "Tom" }, 
  //   { title: "Same title for tom and harry posts", author_name: "Harry" }, 
  //   { title: "Same title for tom and harry posts", author_name: "Tom" }, 
  //   ...
  // ]
} catch (error) {
  // you can see the error info here, if something goes wrong
}

Deleting records

The delete method is used to delete records, it always has to be used with the .where method and a filter criteria. It is done in this way to avoid accidental deletion of all records from your DataSet:

[..]
try {
  const posts = await dataModule.dataset("posts")
    .delete()
    .where(field("title").isLike("test"))
    .execute();

  // you will be able to access the deleted posts here
  // they won't be stored in the DB anymore, but maybe you
  // want to display a visual confirmation of what was deleted
} catch (error) {
  // you can see the error info here, if something goes wrong
}
[..]

Only retrieving certain fields

You can use .fields method to restrict fields you want to retrieve.

import { jexiaClient, dataOperations, field } from 'jexia-sdk-js/browser'
[..]
const posts = await dataModule.dataset("posts")
  .select()
  .fields("id", "title") // you can also pass an array of field names too
  .execute();

console.log(posts);
// output:
// [ 
//   { id: "78574c6a-b5a0-45da-a589-9671a88f7f53", title: "Some post title" },
//   { id: "1b118939-85fc-47f0-9b4f-a6de00922e15", title: "Some other post title" },
// ]
[..]

Filtering records

You can use the filtering to select what records a certain query will operate on.

In order to define a filter, you can use the exposed method field.

import { field } from "jexia-sdk-js/node";

const isAuthorTom = field("author_name").isEqualTo("Tom");
const isAuthorDick = field("author_name").isEqualTo("Dick");
const isAuthorTomOrDick = isAuthorTom.or(isAuthorDick);

// In order to use these conditions, they need to be added to a query through `.where` method

dataModule.dataset("posts")
  .select()
  .where(isAuthorTomOrDick);

[..]

more datails you can get from JS SDK documentation

our npm module can be found in npmjs