AdonisJS API
    Preparing search index...

    Class BaseTransformer<T>Abstract

    Base class for creating custom data transformers. Provides utilities for transforming data into consistent API responses with support for relationships and conditional values.

    class UserTransformer extends BaseTransformer<User> {
    toObject() {
    return {
    id: this.resource.id,
    name: this.resource.name,
    // Transform related data
    profile: UserTransformer.transform(this.whenLoaded(this.resource.profile)),
    posts: PostTransformer.transform(this.whenLoaded(this.resource.posts)),
    // Conditional values
    isAdmin: this.when(this.resource.role === 'admin', () => true, false)
    }
    }
    }

    Type Parameters

    • T
    Index
    • Create a new transformer instance with the provided resource data.

      This constructor is typically not called directly. Instead, use the static transform method to create Item or Collection instances.

      Type Parameters

      • T

      Parameters

      • resource: T

        The raw resource data to be transformed

        class UserTransformer extends BaseTransformer<User> {
        constructor(user: User) {
        super(user)
        }
        }

      Returns BaseTransformer<T>

    • Create a paginated collection from an array of data with pagination metadata.

      Type Parameters

      • Self extends new (resource: any, ...rest: any[]) => any

      Parameters

      • this: Self
      • data: ConstructorParameters<Self>[0][]

        Array of data objects to transform into a paginated collection

      • metaData: Record<string, any>

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

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

        // Create paginated collection
        const paginatedUsers = UserTransformer.paginate(
        [user1, user2, user3],
        { page: 1, perPage: 10, total: 50 }
        )
      • ...rest: ExtractTransformerRestTypes<ConstructorParameters<Self>>

      Returns Paginator<InstanceType<Self>, 1, "toObject">

    • Wrap a value in a Maybe container to handle potentially undefined values. This is useful for relationships that may or may not be loaded.

      Type Parameters

      • Value

      Parameters

      • value: Value

        The value to wrap in a Maybe container

        class UserTransformer extends BaseTransformer<User> {
        toObject() {
        return {
        id: this.resource.id,
        profile: ProfileTransformer.transform(this.whenLoaded(this.resource.profile))
        }
        }
        }

      Returns Maybe<Value>

    • Omits the specified keys from a data object and returns a shallow copy without those keys.

      Type Parameters

      • Data extends Record<string, any>
      • Keys extends string | number | symbol

      Parameters

      • data: Data

        The source object to omit keys from

      • keys: Keys[] | readonly Keys[]

        Array of keys to omit from the data object

        class UserTransformer extends BaseTransformer<User> {
        toObject() {
        return this.omit(this.resource.toAttributes(), ['password', 'internalId'])
        }
        }

      Returns Omit<Data, Keys>

    • Picks only the specified keys from a data object and returns a shallow copy containing only those keys.

      Type Parameters

      • Data extends Record<string, any>
      • Keys extends string | number | symbol

      Parameters

      • data: Data

        The source object to pick keys from

      • keys: Keys[] | readonly Keys[]

        Array of keys to pick from the data object

        class UserTransformer extends BaseTransformer<User> {
        toSummary() {
        return this.pick(this.resource, ['id', 'name', 'email'])
        }
        }

      Returns Pick<Data, Keys>

    • Conditionally compute a value based on a boolean condition. This is a cleaner alternative to using ternary operators in transformers.

      Type Parameters

      • Value
      • Fallback = undefined

      Parameters

      • conditional: boolean

        Boolean condition to evaluate

      • resolver: () => Value

        Function that returns the value when condition is true

      • Optionalfallback: Fallback

        Optional fallback value when condition is false (defaults to undefined)

        class UserTransformer extends BaseTransformer<User> {
        toObject() {
        return {
        id: this.resource.id,
        name: this.resource.name,
        // Include role only for admins
        role: this.when(this.resource.role === 'admin', () => 'admin'),
        // Include with fallback
        status: this.when(this.resource.isActive, () => 'active', 'inactive')
        }
        }
        }

      Returns Value | Fallback