Updated on 2023-11-24 GMT+08:00

Orchestrating Data Sources

A data API can contain multiple data sources. Therefore, an API request can call multiple data sources. For example, the query result of the first data source can be used as the parameter of the second data source.

MySQL is used as an example. Assume that the data API contains data source 1 and data source 2, user01 is the data table of data source 1, and user02 is the data table of data source 2. The structures of the two tables are as follows:

Table 1 Table structures

Data Source

Table Name

Parameter

Data source 1

user01

  • id (int)
  • name (varchar)

Data source 2

user02

  • user_id (int)
  • user_age (int)
  • user_sex (varchar)

The data source SQL statement is designed as follows:

For data source 1, query the ID of the data record whose name is zhang in table user01. Assume that the return object from data source 1 is default1.

select id from user01 where name='zhang';

For data source 2, go to table user02 and find the data record of user_age corresponding to the ID found in table user01. Assume that the return object from data source 2 is default2.

select user_age from user02 where user_id=${default1[0].id};

${default1[0].id} indicates the query result from data source 1. (default1 indicates the return object from data source 1, and id indicates the query field of data source 1.)

Assume that the data tables user01 and user02 contain the following data records:

user01

user02

id

name

user_id

user_age

user_sex

1

zhang

2

17

Female

2

li

3

18

Male

3

wang

1

18

Male

The following response is returned when the data API is called:

{
     "default1":[{
         "id":1
     }],
     "default2":[{
         "user_age":18
     }]
}