Get total count of models in a Builder.io query

Hi, This has to be a really common feature to get the count of all the fields in some meta tag on a paginated query.

For example, in a blog, I need to show page counts and links but I don’t have the information on the number of models. Can you please add the meta tag in the model query to give the information on the number of models that are available/published?

Hi Peeplee! We have heard this request before, and indeed have an internal ticket in our product backlog to add to our API, but no current timeframe. It looks like someone recently made a request to our external feature request board here Return the total amount of items in a model for | Builder.io Ideas

I recommend upvoting this request, as our ideas board is a leading source of direction for our product team!

In the meantime, the best way to iterate over all entries in a model is to use limit and offset, as outlined in this post: What's the maximum limit allowed in a single content API call?

Try that out and see if it works for your use case!

hi @TimG, thanks for your heads up on this feature. Would you know if there is a workaround where it is not necessary to run multiple request to load all the entries? It doesn’t sound performant if I have a big amount of content and I want to render a simple pagination component.

I agree with @Peeplee that this has to be a really common feature. A must-have.

Thanks!

Hi @mstekl the best workaround using our API would be the one outlined in the linked post.

Another option would be to create a data model that is a count of how many content entries you have, and then use a webhook that updates the data model using our write API every time you publish / unpublish content. It would require some script setup, but once created would be relatively low / no maintenance.

For anyone looking for a solution, some help:

  /**
   * Welcome to the medieval age with Builder.io
   * https://forum.builder.io/t/get-total-count-of-models-in-a-builder-io-query/3994
   * https://forum.builder.io/t/whats-the-maximum-limit-allowed-in-a-single-content-api-call/310
   */
  public static async getModelPaginationMetadata(
    model: BuilderModels,
    { limit, query, page }: Required<Pick<CmsGetterOptions, 'limit' | 'query'>> & { page: number },
  ) {
    const entries = await this.getContentAll({
      model,
      options: {
        options: {
          noTargeting: true,
        },
        prerender: false,
        cache: true,
        cacheSeconds: 360,
        query,
      },
    })

    const totalRecords = entries.length
    const totalPages = Math.ceil(totalRecords / limit)

    return {
      totalRecords,
      totalPages,
      pageCurrent: page,
      pageNext: page < totalPages ? page + 1 : null,
      pagePrevious: page > 1 ? page - 1 : null,
    }
  }

and don’t forget to vote here: Return the total amount of items in a model for | Builder.io Ideas so we can query like a boss :slight_smile:

P.S. If you are implementing a pagination on a list of let’s say blog posts, where also the search input is present, make sure both the list query and the method’s above query are equal.

P.S.2 Looking forward for any feedback how to make it even better.