Create relations between datasets

Jexia allows you to relate two or more datasets and create one-to-many or many-to-many relationships between datasets.

Create a relation

In order to create a relation, head over to the "Relations" tab in the dashboard:

Drag and drop a line from one dataset to the other. Now a screen will pop up asking you to choose the cardinalities. In this example, one author has many books and one book belongs to one author, making this a one to many relationship. After you chose the cardinalities your relations panel will look like this:

Fetch related datasets through SDK

Relations can be added to a query in order to have the query apply not only to the main dataset the query is created from, but also to related records from other datasets.

[..]
let books = dataModule.dataset("books");
let authors = dataModule.dataset("authors");
posts.select().relation(authors).execute().then( (records) => {
  // objects in the records array will now contain a property called "authors"
  // which will be an array holding the comments related to a particular post.
});
[..]


Fetch related datasets through REST API 

When you create a one-to-many relationship, a related field in the dataset with the "many" cardinality is created automatically. You can fetch your related datasets like this:

[GET]https://<your-project-id>.app.jexia.com/rest/<dataset1>/<dataset2>
 
Authorization: <jexia-token>

or like this:

[GET] https://<your-project-id>.app.jexia.com/rest/<dataset2>/<dataset1>
 
Authorization: <jexia-token>

Example:

Request:

[GET]https://<your-project-id>.app.jexia.com/rest/authors/books
 
Authorization: <jexia-token>


Response:

[
    {
        "id": "127b54a4-f880-47ae-8760-89f42234a4af",
        "created_at": "2018-02-13T13:40:49.61886Z",
        "updated_at": null,
        "name": "The Author",
        "email": "theauthor@book.com",
        "books": [
            {
                "id": "363cdcfa-9bf9-42b2-b69d-dd00fb811ca3",
                "created_at": "2018-02-13T14:26:25.07194Z",
                "updated_at": null,
                "title": "My third book",
                "publishing_date": null,
                "authors_id": "127b54a4-f880-47ae-8760-89f42234a4af"
            },
            {
                "id": "7b45cb5e-bab6-49d2-8c6d-0ecdbc771fa5",
                "created_at": "2018-02-13T13:37:24.99517Z",
                "updated_at": null,
                "title": "My first book",
                "publishing_date": null,
                "authors_id": "127b54a4-f880-47ae-8760-89f42234a4af"
            }
        ]
    }
]


Request:

[GET]https://<your-project-id>.app.jexia.com/rest/books/authors
 
Authorization: <jexia-token>


Response:

[
  {
        "id": "7b45cb5e-bab6-49d2-8c6d-0ecdbc771fa5",
        "created_at": "2018-02-13T13:37:24.99517Z",
        "updated_at": null,
        "title": "My first book",
        "publishing_date": null,
        "authors_id": "127b54a4-f880-47ae-8760-89f42234a4af",
        "authors": [
            {
                "id": "127b54a4-f880-47ae-8760-89f42234a4af",
                "created_at": "2018-02-13T13:40:49.61886Z",
                "updated_at": null,
                "name": "The Author",
                "email": "theauthor@book.com"
            }
        ]
    }
 
]


Curl:

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