No i dont think so, it cant help. Lets discuss more difficult case to understand:
I have Users with relational table -> Cars. In this table i have list of owner cars for example.
Then we have another table -> Ads containing list of metadata plus relation to Cars table.
And finally third table -> Offers Containing relation to User (client), relation to Ads table and for example offered price.
And I want inverted relation constructed like this:
Offers -> Ads -> Cars -> Users
It mean I have offer related to add, and there is car owned by user and i want to receive his name.
To achieve this you have to get list car.object id in first step.
And then find users with relation Cars and setup where clause to that objectID.
But its just too complicated, specially when you open backendless console and you have such a nice field: child of Users.Cars! Why we just cant use this property?!
I could easily add to backendless options -> load relations -> “Ads.Cars.Users.Cars” (or different syntax…)
Because what I did, is to automatise this process and created custom API call in business logic. This is how first draft looks:
'use strict';
class InverseRelationService {
/**
* @param {String} query
* @returns {Object}
*/
invert(query) {
let rp = require('request-promise');
let jsonQuery = JSON.parse(query.replace(/\'/g, '"'));
var returnData = [];
function doWork(text, data) {
var promiseChain = null;
for (var i = 0, len = data.length; i < len; i++) {
let objectId = data;
let whereClause = "cars.objectId='" + objectId + "'";
let URL = 'https://api.backendless.com/v1/data/' + jsonQuery.invertedTable + '?loadRelations=' + jsonQuery.invertedRelation + "&where=" + whereClause;
console.log('InvertedTable URL:' + URL);
let invertedOptions = {
uri: URL,
headers: {
'application-id': 'D33CD664-1EE3-401F-FF16-E3B50D22B700',
'secret-key': '8A2F0C39-33F5-386F-FFA9-B362F41F0200',
'application-type': 'REST'
},
json: true // Automatically parses the JSON string in the response
};
if (promiseChain == null) {
promiseChain = rp(invertedOptions)
.then((inverted) => {
//Construct dict
var objDict = {[objectId]:inverted.data};
returnData.push(objDict);
}).catch((invertedErr) => {
console.error(invertedErr);
});
}
else {
promiseChain = promiseChain.then(() => {
return rp(invertedOptions)
.then((inverted) => {
//Construct dict
var objDict = {[objectId]:inverted.data};
returnData.push(objDict);
}).catch((invertedErr) => {
console.error(invertedErr);
})
})
}
}
return promiseChain;
}
//Get inverted table data
var URL = 'https://api.backendless.com/v1/data/' + jsonQuery.tableName + "?loadRelations=" + jsonQuery.relation;
if (jsonQuery.query != undefined) {
if (jsonQuery.query.condition != undefined) {
URL += "&where=" + jsonQuery.query.condition;
}
}
console.log('mainTable URL:' + URL);
let options = {
uri: URL,
headers: {
'application-id': 'D33CD664-1EE3-401F-FF16-E3B50D22B700',
'secret-key': '8A2F0C39-33F5-386F-FFA9-B362F41F0200',
'application-type': 'REST'
},
json: true // Automatically parses the JSON string in the response
};
return rp(options).then((res) => {
//Construct new array
var relationIDs = [];
var relations = jsonQuery.relation.split(".");
console.log('relations:' + relations);
for (var i = 0, len = res.data.length; i < len; i++) {
//Sort out deepness
var root = res.data;
for (var r = 0, rlen = relations.length; r < rlen; r++) {
root = root[relations[r]];
}
relationIDs.push(root.objectId);
}
console.log(relationIDs);
return relationIDs;
}
).then(doWork.bind(null, '')).then(() => {
return returnData;
})
.catch((err) => {
console.log(err);
err
});
}
}
Backendless.enablePromises();
Backendless.ServerCode.addService(InverseRelationService);
API call:
As far as API services does not support anything but string/int/bool in some strange behaviour your call has to look like this:
“{‘tableName’:‘CarsOffers’,‘relation’:‘cars.owner’,‘query’:{‘condition’:‘price>0’},‘invertedTable’:‘Users’, ‘invertedRelation’:‘cars’}”
And then proper " and ’ is replaced inside function.
What it does to to take your initial table -> look for relation and take objectID
And then it will call based on results your Inverted table inside related data and perform FIND