Sdk: how to perform a single request to get data and aggregate count?

Hello folks.

For a index news page, with a rest call I would use meta=filter_count in the same request like the following:

  const data = await $fetch(`url.com/items/news
?sort=-date_created
&filter[status][_eq]=published
&fields=slug,title,date_created
&sort=-date_created
&page=${pageNum}
&limit=${limit}
&meta=filter_count`)

// {
//   data: [ ... ],
//   meta { filter_count },
// }

But right now I can’t figure out how to perform a single request with the sdk to get a similar result.

Right now I’m doing with two ‘readItems’ requests similarly to this:

  const pageNum = 1
  const limit = 10
  let data = null
  let count = 0

  try {
    // Request count
    count = await directus.request<number>(
      readItems('news', {
       aggregate: { count: '*' },
        filter: {
          status: 'published',
        },
        page: pageNum,
        limit,
      }),
    )
    // Request data
    data = await directus.request(
      readItems('news', {
        filter: {
          status: 'published',
        },
        page: pageNum,
        limit,
        sort: ['-date_created'],
        fields: ['slug', 'title', 'date_created'],
      }),
    )
  }
  catch (e) {
    console.error(e)
  }

  const result = {
    count: count?.[0]?.count,
    data,
  }

  return result

Using "@directus/sdk": "^19.1.0" and "nuxt": "^3.17.3".

My apologies if the answer is trivial. Thanks for your help. :folded_hands:

1 Like

This isn’t possible in a single request. You’ll need to make two separate requests: one to fetch the data and another to get the count.

I understand. Thank you for the fast response.

Is there a chance it will be implemented in the future? Should I create a feature request or maybe one was already created/rejected?