-3

I am currently trying to figure out how to setup my Backand app and its REST API. This question is related to question: Backand deep querying. However, I was hoping that I could get some best practice code examples on how to perform server side code to perform a loop and create a JSON responds with the following criteria:

I want to be able to make a REST request to Backand and get one data object back that has manipulated/merged two data objects from my database.

I have an object called "media" and another named "users". Obviously, users contain user information and media contains information on a picture that the user has uploaded. The two objects are related by the userId and by collection set in Backand. I want to make a GET request that responds with a JSON object with all pictures and a nested user object on each picture object that contains the related user information. I know that I get back "relatedObjects", and I could then make some manipulation on the client side, but I am hoping that there is another easier way to do this from the Backand administration system either on server side code or as a query.

So, my question is, what's the best way to produce a REST call that responds a database object with nested related data object through Backand?

Here's the object models (shorten for clarity):

User object model as set up in Backand

{
"name": "users",
"fields": {
  "media": {
    "collection": "media",
    "via": "user"
  },
  "email": {
    "type": "string"
  },
  "firstName": {
    "type": "string"
  },
  "lastName": {
    "type": "string"
  }
} }

Media object model as set up in Backand

 {
"name": "media",
"fields": {
  "description": {
    "type": "string"
  },
  "thumbnail": {
    "type": "string"
  },
  "fullImage": {
    "type": "string"
  },
  "user": {
    "object": "users"
  }
}}

Final JSON response that I am looking for:

{
description: 'Blah',
thumbnail: 'someImageUrl.jpg',
fullImage: 'someImageUrl.jpg',
user: { 
  firstName: 'John'
  lastName: 'Smith'
  email: 'john@smith.com'
}
}
Community
  • 1
  • 1
trustkool
  • 90
  • 11
  • what you want to acheive?? – Wasiq Muhammad Apr 26 '16 at 18:06
  • Hi Wasiq, I thought it was pretty clear. I am asking what the best practice is for Backand BAAS App when combing related objects in to one object. My best guess is that it should be done through server-side javascript in Backand and I'd appreciate any server-side javascript code that achieves this in the best way. – trustkool Apr 26 '16 at 18:49
  • This is opinion based question. If you use relational DB then you'll have to do join-tables. If you use NoSQL DB then it'll be easier, since you can pre-populate pointers before sending the data back to the client. Mongodb is a good example of a NoSQL DB. – Muli Yulzary Apr 26 '16 at 19:53

1 Answers1

1

Just in case anybody else comes across this, I chose to do it with server-side javascript code, since my backend, SQL and NoSQL query skills are very weak. I'm guessing a noSQL query would probably be better in terms of performance. And I would still like to see how it could be done in noSQL. Anyway my server-side javascript code in a Backand action does the job. Here it is:

/* globals
  $http - Service for AJAX calls 
  CONSTS - CONSTS.apiUrl for Backands API URL
  Config - Global Configuration
  socket - Send realtime database communication
  files - file handler, performs upload and delete of files
  request - the current http request
*/
'use strict';
function backandCallback(userInput, dbRow, parameters, userProfile) {


    var response = [];
    var request =
    $http({
        method: "GET",
        url: CONSTS.apiUrl + "/1/objects/media",
        headers: {"Authorization": userProfile.token},
        params: {
        exclude: 'metadata',
        deep: true
        }
    });

    var object = request.data;
    var related = request.relatedObjects.users;

    for (media in object) {
        if (object.hasOwnProperty(media)) {
            for (user in related) {
                if (object[media].user == related[user].id) {
                    response.push({
                    id: object[media].id,
                    thumbnailUrl: object[media].thumbnail,
                    description: object[media].description,
                    fullName: related[user].firstName + ' ' + related[user].lastName,
                    email: related[user].email
                    });
                }
            }
        }
    }

    return  response;
}
trustkool
  • 90
  • 11