Theme

Advanced Guides

Datasets

Datasets allows a more advanced interaction with the mock server. Combined with the processing configuration in mappings, it can simulate sofisticated interaction found on REST APIs like GET (a list, filtered or not, or a specific item), PUT (for instance updating an item at a key with new data), PATCH (also for instance, can to use to partly update some data) DELETE (to delete an item corresponding to a key) and POST (to insert a new item of data into a dataset).

All data manipulation are based on the fantastic JSON querying and manipulation library JSONata.

Datasets files are supposed to contain representation of data in either the JSON format or the YAML format. Then can be any form of data but support of processing (see below) is only possible with array of data.

Return a list of items

The JSONata expression $ ~> | $ | {}, ['context', 'resolution', 'resolutionTimestamp'] | takes the current state of the dataset and transforms it to exclude the specified properties.

Return a list of items with specific properties

The JSONata expression $ ~> | $ | {}, ['context', 'resolution', 'resolutionTimestamp'] | takes the current state of the dataset and transforms it to include only the specified properties.

Return a specific item

The following mapping returns a specific item given it's key specified as a part of the URL.

Use processing to update datasets

A processing configuration can be added to modify a dataset while processing the request. The processing feature can take some input and transform it to perform a mutation into the store. At the end, data can be returned as the response to the request.

Most of the operation described below will only work when the dataset represents an array of items.

Steps in processing

input

input define what to use as the processing pipeline. This populates a temporary storage area (processingData) on which further steps will be able to operate.

  • dataset, selects which dataset to use as input
  • expression: specify a JSONata expression to filter the dataset used as input. Use $ to use the dataset as is.

match

Use match to identify which part of the dataset will part of later mutations in the dataset.

  • expression: specify a JSONata expression to filter and find a match in the dataset.
  • output: the name of the variable to use to populate the matching data. This variable will be usable in a later store step.

store

Use store to modify the dataset during the processing pipeline.

  • operation:
    • replaceWithRequestBody: this operation will replace the matched part of a dataset with the body of the request.
    • mergeWithRequestBody: this operaton will merge the matched part of a dataset with the body of the request
    • insertRequestBody: this operation will insert the request body in the dataset
    • deleteMatching: the operation will delete the matching part from the dataset.
  • input: The input variable to be used in the store operation. Use requestBody to use the request body.
  • match: The name of the variable containing the matching information for this store operation.

transform

The transform step allows to transform some data used as input using a JSONata expression.

  • input: indicate requestBody to use the request body as input of the transform operation
  • expression: the JSONata expression to use to transform the input
  • output: the name of the variable to use to store the result of the operation.

Examples

Update data in a dataset

This example shows how to implement a PUT operation which will replace the corresponding part of the tickets dataset by data in the body of the request.

Merge data in a dataset

This example shows how to implement a PATCH operation which will merge the corresponding part of the tickets dataset by data in the body of the request.

Delete data in a dataset

This example shows how to implement a DELETE operation which will delete the corresponding part of the tickets dataset.

Insert data in a dataset

This example shows how to implement a POST operation which will insert the body of the request into the tickets dataset. The request body is also transformed by a JSONata expression to populate a property by a new calculated uuid.

Previous
YAML mapping