How would you implement a journaling for every write request?


#1

Hi,

my application needs to log every successful write event to any table to a journal. E.g. “User 1 added a new person with ID 2345” oder “User 2 added a link from person 2345 to person 1 with role Boss” oder “User 1 deleted a share with ID 1234 (company 1, shareholder 2345)”. The complete JSON-request for those writing events should also be included in an extra database field to be able to reconstruct the business events in case of errors and for archive-reasons (backups of the journal-table are archived for legal reasons).

Question: How would you implement this in DreamFactory?

Greets,
spackmat


#2

This could be done with server side scripting. You could write JavaScript code for each table to log the requests. For example when creating a record on table X, the post.post_process script for that table would run after the record was successfully created. The script would have code to write the necessary information to the database. The database could be the local MySQL database or also a remote one. The down side of doing it this way is that each REST call will take longer because of the extra database call done by the script. You can have multiple records in each request so your script would need to handle that.

We have some scripting information and examples on the wiki.

https://github.com/dreamfactorysoftware/dsp-core/wiki


#3

Thanks,

so there is no event for all POST/PUT/PATCH-Requests so I have to maintain a script for each table?

The costs for the doubled write events are OK, logging the all changes is important for the application.


#4

Yes each table has its own scripts. You can put common code in a separate file and include as described here on the wiki.

https://github.com/dreamfactorysoftware/dsp-core/wiki/Server-Side-Scripting


#5

Thanks again.

Is there a way to use PHP based server side scripts? Maybe called from within the JS based scripts? There is plenty of existing validation and business logic implemented in PHP and it would be nasty to port all that to JS. In the documentation I see something with PHP scripts, but I couldn’t get a full context overview of this.

Another question: What is the advantage of this PHPJS approach at all? The disadvantage is, that we have to compile and install a certain (old) version of a yet unstable library. With all its implications. So there must be a reasonable advantage in going that way.


#6

You could put PHP files under /web and call them from your server side scripts (JavaScript). For example you could have a directory /web/validation that contained all your scripts. Then your JavaScript could do something like

var result = platform.api.post("localhost/validation/foo.php", {"name":"test"});

and process the result.

Many of the developers using our platform are front end developers who know JavaScript so it made sense to support that. Unlike PHP would be, the JavaScript environment is sandboxed to prevent mayhem.

You can find some background info on why the older V8 here:

https://github.com/dreamfactorysoftware/dsp-core/wiki/Installing-V8js


#7

Thanks. I read the article about installing V8JS, so I know why and how to install an older version of phpjs, but this is not what server admins like to hear, when I want them to install dependencies for a new software. Anyhow, I got it working on my DEV-machine and my hosters on the stating and production hosts. Works for now.