Remote queries

Automatically create an enum map of your bases saved queries for quick references to calling them via the network.

Outerbase is incredibly powerful as a data insights tool and the ability to save queries we run and reference often makes jobs easier. Even more so we can take those saved remote queries from Outerbase and have our application code execute them remotely as well.

A huge benefit of executing saved queries is two-fold:

  1. Saved queries in Outerbase can have cached responses for increased response times, particularly helpful for queries that are operation intensive or used by many people.

  2. When you want run a SQL query from a platform such as your frontend without having to expose the actual SQL statement to the platform itself. It only needs to know the saved queries unique ID.

Let's take a look at how we can trigger saved queries from our application.

Add script to package

To generate an ENUM map of our queries names & ID values in your project we can add a script to our package.json file that handles this automatically.

"scripts" {
    "db:queries": "sync-remote-queries PATH=./queries API_KEY=your_api_key"
}

Run the script

With our script in place all we need to do to generate a new Typescript file containing this map locally is by running

npm run db:queries

Using the generated map in your code base helps ensure that you are referencing saved queries that do in fact exist in the Outerbase account you have linked via the API key. When they no longer exist and you re-run the script above your project should start throwing errors to alert you.

Sample output

After running the script above you should see a new index.ts file in the path provided in your package.json file. Now you can reference your saved queries in a type-safe manner.

export enum RemoteQuery {
    'My First Query' = 'ffffffff-c3p0-4cd9-9325-31bfea659ec1',
    'My Second Query' = 'ffffffff-r2d2-4cd9-9325-31bfea659ec1',
}

Requesting a response

With a type-safe enum now in place, we can safely call an SDK function from the OuterbaseConnection class to runSavedQuery which takes a parameter of a remote saved query ID.

const connection = new OuterbaseConnection(
    'YOUR_API_KEY'
)
connection.runSavedQuery(RemoteQuery['My First Query'])

Last updated