How to implement an INSERT OR REPLACE/UPDATE using Event scripts?


#1

Hi,

I’m trying to implement a POST which does a PUT if the record already exist. Basically what I’m trying to do is do an INSERT OR REPLACE/UPDATE logic in the POST event script.

How would one go about doing this? So far what I know is that I need to put some code in the post.pre_process event script.

P.S. I’m new at javascript. Please bear with me.

Thanks,
Allen


#2

Allen, you would perform a get on the database within your script using platform.api.get (see here: https://github.com/dreamfactorysoftware/dsp-core/wiki/API-Access-via-Scripts )
using the request object to create your filter, in order to check if the record already exists.
if it does exist, then use platform.api.put or platform.api.patch (depending on how your data update is structured) and then stop processing the script.
Else, return the request object so the post will process as normal.


#3

Thanks. I haven’t had the chance to try this yet but just wanted to ask. Performance wise, is this better as compared to doing the check in the client itself (Android)?


#4

I think this would depend on a lot of factors such as the size of the DB, the processing power of the server, how many concurrent users you have, the power of the device, etc.