The transformer class used to transform individual items
Maximum depth for nested transformations
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)
Creates a new Collection instance. This constructor is typically not called directly.
Use BaseTransformer.transform() instead.
The transformer class used to transform individual items
Maximum depth for nested transformations
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)
Array of data to be transformed
Constructor for the transformer class
Maximum depth for nested transformations
Variant method name to use for transformation
OptionaldebuggingError: RuntimeException
Runtime exception for debugging purposes
Type identifier for the collection resource
Constructor for the transformer class
Maximum depth for nested transformations
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.
Maximum depth level for nested transformations (valid values: 1, 2, 3, 4, 5, or 6)
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.
Name of the transformer variant method to use (must be a method name that
returns ResourceData or Promise
// 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:
AdonisJS container resolver for dependency injection
Current depth level in the transformation tree
OptionalmaxDepth: number
Optional maximum depth override. When set to -1, uses unlimited depth
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.