I'm trying to learn how to use DreamFactory 2 but not much info available. Their tutorials assume you already know the server side stuff. I don't, and can't find decent tutorials. JavaScript coding, but I'm also new to that.
I can do basic CRUD from Angular. However, now I want to get the data from a table but replace some of it from another table and send it to the front-end. I setup a virtual relationship with that child table and it is included with the JSON payload as an object inside the parent data as expected.
{
resource: [
"item_name": 12,
"thing_by_thing_id":
"thing_name": "thing"
]
}
Now I need to do the following:
1) Capture the data with a script after a front-end GET call that is returning the data from a table.
2) Replace the value in item_name with the value from thing_name.
3) Send the revised JSON object to the UI (Angular) in response to the GET from Angular through the DF api.
I could do this in Angular but I want to learn how to do it in DF. Use the Node scripting I assume. I also assume that this myService._table.{table_name}.get.post_process is where it should be but not sure because there is also an event process api.
What do I do?
It took a lot of trial and error and reviewing the DF tutorials over and over but I finally figured it out.
1) thing_by_thing_id is created by DF in the Add Virtual Relation section of the table to be retrieved in the Schema section of DF. This is where the foreign table data is imported into the main data object. DF has a video tutorial for doing this but it is outdated. Create this foreign key relationship in the main table, not the foreign one as per the video. No need to setup foreign keys, just this relationship.
2) myService._table.{table_name}.get.post_process is the correct script API.
3) Below is the code to loop through the json object after it is retrieved from the database. DF tutorials don't tell us the API's to capture the data so you have to figure it out from their samples. For post process scripts you need this: event.response.content.resource.
function replaceWithThingNames( record ) {
for (var i = 0; i < record.length; i++) {
var thing_name = record[i].thing_by_thing_id.thing_name;
record[i].item_name = thing_name;
}
return record;
}
if (event.response.content.resource) { // use 'content' for response
const record = event.response.content.resource; // DF scripting API
replaceWithThingNames( record );
}