Using Scripts tutorials?

With my 1.5.9 version of dreamfactory installed, I am eager to start playing with the new scripting abilities. I am not sure i understand exactly how it works the

Lets say i have a table called ‘pizzashops’ and each record has a lattitude, and longitude of where they are located.

Sometimes I want a list of all pizza shops and sometimes I just want to retrieve the pizza shops within a certain distance of where i am now.

For the sake of this question lets just assume i have the ‘magicFormula’ for figuring that out and i just need to pass the magicFormula: a ‘desired_distance’ and my ‘myLat’ and ‘myLon’.

how would i go about writing that script? how do i toggle it on and off for a given API call? and how do i pass it the three params.

this is a very rudimentary question i know. but please bare with me, i have JS chops and my only previous experience writing any server side API stuff was pretty much writing my own PHP scripts for talking directly to a mySQL DB.

Hey Erik, we’re slammed with the 1.6 release about to come out which has some significant improvements to server-side scripting. We’ll have a bunch of code examples in the docs coming up here in the next few weeks.

In the meantime, take a look at the links below. There are a few simple examples in the blog post and Todd’s demo from the 1.5 webinar. Also, server-side scripting is all Javascript, we’ll support other languages like PHP in the future.

Hi Erik,

This will be possible with our 1.6 release which is coming out in the next week or so. You will have full access to the request from the scripts so you can do things like this.

GET /rest/db/pizzashops?desiredDistance=10&myLat=34.073318&myLon=−84.281086

This script would be for get.post_process, meaning after the records are fetched from the db. You can then loop through and remove any that are not within the specified distance. Currently in 1.5.9 you can’t edit the post_process script since it is new for 1.6. Depending on the type of db there are also ways to have the db do the distance filter if there are too many records to loop through them all in the script.

if (event && event.request && event.request.query && event.request.body && event.request.body.record) {
    var desiredDistance = event.request.query.desiredDistance;
    if (desiredDistance) {
        var myLat = event.request.query.myLat;
        var myLon = event.request.query.myLon;
        var size = event.request.body.record.length;
        for (var i = size - 1; i >= 0; i--) {
            var record = event.request.body.record[i];
            if (!magicFormula(myLat, myLon,, record.lon, desiredDistance)) {
                delete record;

Hope this helps reply back if you need more info.

thanks… a couple questions… when you say “delete record” its not deleting from the table right? just the response?

also, i am using the DB that comes installed with dreamfactory… so i am curious if the distance filer can be done on the db side as you speak of… would love examples… the method you demonstrate above where it deletes results, seems like it would take double the time to give me the response since it is getting all records and then filtering… if thats the case i might as well get all records and filter on the front end…


Correct, this script would just delete from the results array not the table. The script would absolutely slow things down by having to find and delete unwanted records.

The default db for DreamFactory is MySQL. Check out this post, specifically the paper (slide deck) referenced in the top answer.

I have seen this post and its very helpful. but if i understand correctly i can’t use my own custom php script to access the dreamfactory DB… can i? if so please do tell

i don’t suppose i can upload a php file to the files section and have it connect to the dreamfactory db?.. that would be the bees knees…

Hi Todd,

I know that the server side scripting has changed quite a bit since the time of this post but it doesn’t seem that I can find any way of implementing a similar issue I am facing. I have a similar problem where I want to query and add my special filter before accessing the database. Is there a way to do that using SQL? I feel that looping through all the unfiltered results is very inefficient especially if I am only looking for a few results out of the thousands/millions of records that the unfiltered SQL query may return.
I hope that my question is clear.


Sorry for the late reply. You can write a script on get.pre_process that changes the filter to whatever you want. It could be a single line like this:


The filter could be based on the original filter passed in to the API, or not.