Querying data sources
Hoverfly Cloud supports data queries from a CSV Data Source for Enterprise customers to do key value data lookups and simulate persistence.
Reading from a CSV Data Source
You can read data from a CSV data source in a number of ways.
For illustration we will use this data source called "pets":
1000
birds
Archie
available
1001
dogs
Zipper
available
1002
dogs
Teddy
sold
*
no data
no data
no data
Single value lookup with {{csv}}
The most basic query function is to return the value of one field (selected-column) given a field name to search (column-name) and a value to search for in that field (query-value). Of course the query-value would normally be pulled from the request.
Syntax:
Template Example: Display the Name of the pet with Id 1001
Output:
More advanced examples: Single value lookup with {{csv}}
Template Example: Display the Name of the pet with Id taken from position 2 on the path
Template Example: Use conditional logic to check if a pet with Id taken from position 2 on the path exists, and if not return an error. Note that when the csv function is used within an #equal block, it needs to be enclosed in round brackets.
Retrieve all rows as a map with {{csvAsMap}}
Additional functions are available to query the CSV data source to return all or a filtered subset of it’s rows, as a map.
Retrieving the results as a map makes it simple to render back into the template, as you can use the {{this}} expression with the column names to identify which fields you want to render.
Note that as you will use this wrapped within an #each or #first block, the csvAsMap function must be enclosed inside round brackets.
See examples below.
Syntax:
Template Example:
Output:
Retrieve filtered rows as a map with {{csvMatchingRows}}
To filter the map on a single column value at the time of query you can use csvMatchingRows. This will return all the rows where the (column-name) = (query-value).
Note that as you will use this wrapped within an #each or #first block, the csvMatchingRows function must be enclosed inside round brackets.
Template Example:
Output:
Advanced querying with SQL SELECT {{csvSqlCommand}}
Note that as you will use this wrapped within an #each or #first block, the csvSQLCommand must be enclosed inside round brackets.
Syntax:
Template Example:
Output:
Last updated