Skip to content

Search Records

One of the operation of DATA-API is to list the records of a specific entity according to a specific condition.

In order to get a list of a specific entity, a POST request to /api/:entity_name/export.

The search accepts all the Stack9 Query Object to be passed through the request body

Example

For instance, let's say that we need to query all the customers who fit as Baby Boomers (generation X) that call John.

// NodeJS example
fetch('https://april9.stack9.co/api/customer/search', {
  method: 'POST',
  headers: {
    'Api-Key': '<my_secret_api_key>',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    $select: ['id', 'dob', 'name', 'account_type.name'],
    $withRelated: ['account_type'],
    $where: {
      name: {
        $like: '%John%',
      },
      dob: {
        $lt: '1964-12-31',
        $gt: '1946-01-01',
      },
    },
    $sort: {
      dob: -1,
    },
  }),
})
  .then(response => response.json())
  .then(data => {
    const johnBoomers = data;
    console.log(johnBoomers);
  });

// OUTPUT
// [
//   {
//     id: 12,
//     dob: '1963-06-02',
//     name: 'John Doe',
//     account_type: {
//       name: 'Basic'
//     },
//   },
//   {
//     id: 13,
//     dob: '1946-03-10',
//     name: 'Silvester John',
//     account_type: {
//       name: 'Premium'
//     },
//   },
//   ...
// ];

Also you can use GET parameters to create pagination or add a limit to the query results

ParamsTypeDescription
limitnumberThe limit size for the query
pagenumberPage index that represents the offset of your pagination
// NodeJS example
fetch('https://april9.stack9.co/api/customer/search?limit=2&page=0', {
  method: 'POST',
  headers: {
    'Api-Key': '<my_secret_api_key>',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    $where: {
      gender: 'Male',
    },
  }),
})
  .then(response => response.json())
  .then(data => console.log(data));

// OUTPUT
// {
//   results: [
//     {
//       id: 1,
//       dob: '1990-06-01',
//       name: 'John Doe'
//     },
//     {
//       id: 2,
//       dob: '1986-03-10',
//       name: 'James Doe'
//     },
//   ],
//   total: 10
// }

Using the total property, you can calculate how many pages are needed to paginate the records

const limit = 2;
const pagesNeeded = Math.round(data.total / limit);
console.log(pagesNeeded); // 5

Responses

Status CodeOperationResponse
200Resource listed successfullyEntity[] or {results: Entity[]}
400Bad Request Error{ error: string }
404Resource not found