Best practice Backand regarding JSON transformation and related objects

302 Views Asked by At

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: '[email protected]'
}
}
1

There are 1 best solutions below

0
On

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;
}