AdonisJS API
    Preparing search index...

    Class Paginator<Transformer, MaxDepth, Variant>

    Represents paginated data that combines a collection of transformed items with pagination metadata. This class is typically created using the BaseTransformer.paginate() method.

    Type Parameters

    • Transformer extends Record<string, any>

      The transformer class used to transform each item

    • MaxDepth extends Next[number]

      The maximum depth for nested resource resolution

    • Variant extends string

      The variant method name to use for transformation

      class UserTransformer extends BaseTransformer<User> {
      toObject() {
      return {
      id: this.resource.id,
      name: this.resource.name
      }
      }
      }

      const paginator = UserTransformer.paginate(
      users,
      { page: 1, perPage: 10, total: 100 }
      )

      const result = await serializer.serialize(paginator)
      // Result: { data: [...], metadata: { page: 1, perPage: 10, total: 100 } }
    Index
    • Creates a new Paginator instance. This constructor is typically not called directly. Use BaseTransformer.paginate() instead.

      Type Parameters

      • Transformer extends Record<string, any>

        The transformer class used to transform each item

      • MaxDepth extends 1 | 2 | 3 | 4 | 5 | 6

        The maximum depth for nested resource resolution

      • Variant extends string

        The variant method name to use for transformation

        class UserTransformer extends BaseTransformer<User> {
        toObject() {
        return {
        id: this.resource.id,
        name: this.resource.name
        }
        }
        }

        const paginator = UserTransformer.paginate(
        users,
        { page: 1, perPage: 10, total: 100 }
        )

        const result = await serializer.serialize(paginator)
        // Result: { data: [...], metadata: { page: 1, perPage: 10, total: 100 } }

      Parameters

      Returns Paginator<Transformer, MaxDepth, Variant>

    $type: "paginator" = 'paginator'

    Type identifier for the paginator instance, used for runtime type discrimination.

    The collection of transformed data to paginate

    metaData: any

    Pagination metadata (page, perPage, total, etc.)

    • Specify the maximum depth of nested relationships to be resolved when serializing the paginated collection.

      This is useful to prevent infinite recursion or to limit the amount of data fetched for deeply nested relationships. Relationships beyond this depth will be excluded from the serialized output.

      Type Parameters

      • T extends 1 | 2 | 3 | 4 | 5 | 6

      Parameters

      • value: T

        Maximum depth level for nested transformations (valid values: 1, 2, 3, 4, 5, or 6)

      Returns Paginator<Transformer, T, Variant>

      const paginatedPosts = PostTransformer.paginate(posts, { page: 1, total: 100 })
      .depth(2)

      // This allows: posts -> comments -> author (2 levels)
      // But excludes: posts -> comments -> author -> profile (3 levels)
    • Specify which transformer variant method to use for serialization.

      Transformers can define multiple methods (variants) for different representations of the same data (e.g., toObject, toSummary, toDetailed). This method allows you to choose which variant to use for this paginated collection.

      Type Parameters

      • V extends string

      Parameters

      • value: V

        Name of the transformer variant method to use (must be a method name that returns ResourceData or Promise)

      Returns Paginator<Transformer, MaxDepth, V>

      // Use default toObject variant
      const users = UserTransformer.paginate(userData, { page: 1, total: 100 })

      // Use custom toSummary variant for lighter payload
      const userSummaries = UserTransformer.paginate(userData, { page: 1, total: 100 })
      .useVariant('toSummary')

      // Use detailed variant for admin view
      const detailedUsers = UserTransformer.paginate(userData, { page: 1, total: 100 })
      .useVariant('toDetailed')
    • Resolves the paginated data by combining the serialized collection with pagination metadata. This method is typically called internally by the serializer function.

      Parameters

      • container: ContainerResolver<any>

        Container resolver for dependency injection

      • depth: number

        Current depth level in the transformation tree

      • OptionalmaxDepth: number

        Optional maximum depth override

      Returns Promise<{ data: InferData<Transformer, Variant, -1, 0>[]; metadata: any }>