AdonisJS API
    Preparing search index...

    Class Collection<Transformer, MaxDepth, Variant>

    Represents a collection of transformed items from an array of source data. This class is typically created using the BaseTransformer.transform() method when passing an array.

    Type Parameters

    • Transformer extends Record<string, any>

      The transformer class used to transform individual items

    • MaxDepth extends Next[number]

      Maximum depth for nested transformations

    • Variant extends string

      The transformer method variant to use for serialization

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

      const users = [user1, user2, user3]
      const collection = UserTransformer.transform(users)
      const serialized = await serialize(collection)
    Index
    • Creates a new Collection instance. This constructor is typically not called directly. Use BaseTransformer.transform() instead.

      Type Parameters

      • Transformer extends Record<string, any>

        The transformer class used to transform individual items

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

        Maximum depth for nested transformations

      • Variant extends string

        The transformer method variant to use for serialization

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

        const users = [user1, user2, user3]
        const collection = UserTransformer.transform(users)
        const serialized = await serialize(collection)

      Parameters

      • transformerData: [resource: any[], ...rest: any[]]

        Array of data to be transformed

      • transformer: new (...args: any[]) => Transformer

        Constructor for the transformer class

      • maxDepth: MaxDepth

        Maximum depth for nested transformations

      • variant: Variant

        Variant method name to use for transformation

      • OptionaldebuggingError: RuntimeException

        Runtime exception for debugging purposes

      Returns Collection<Transformer, MaxDepth, Variant>

    $type: "collection" = 'collection'

    Type identifier for the collection resource

    transformer: new (...args: any[]) => Transformer

    Constructor for the transformer class

    maxDepth: MaxDepth

    Maximum depth for nested transformations

    variant: Variant

    Variant method name to use for transformation

    • Specify the maximum depth of nested relationships to be resolved when serializing the 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 Collection<Transformer, T, Variant>

      // Limit depth to 2 levels
      const posts = PostTransformer.transform(userData.posts)
      .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 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 Collection<Transformer, MaxDepth, V>

      // Use default toObject variant
      const users = UserTransformer.transform(userData)

      // Use custom toSummary variant for lighter payload
      const userSummaries = UserTransformer.transform(userData)
      .useVariant('toSummary')

      // Use detailed variant for admin view
      const detailedUsers = UserTransformer.transform(userData)
      .useVariant('toDetailed')
    • Resolve the collection by transforming each item and resolving nested relationships. This method is typically called internally by the serializer.

      The resolution process:

      1. Creates a transformer instance for each item in the collection
      2. Calls the variant method on each transformer
      3. Recursively resolves nested relationships up to the specified depth
      4. Returns an array of resolved objects

      Parameters

      • container: ContainerResolver<any>

        AdonisJS container resolver for dependency injection

      • depth: number

        Current depth level in the transformation tree

      • OptionalmaxDepth: number

        Optional maximum depth override. When set to -1, uses unlimited depth

      Returns Promise<InferData<Transformer, Variant, -1, 0>[]>