The transformer class used to transform the item
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 item = UserTransformer.transform(userData)
const serialized = await serialize(item)
Creates a new Item instance. This constructor is typically not called directly.
Use BaseTransformer.transform() instead.
The transformer class used to transform the item
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 item = UserTransformer.transform(userData)
const serialized = await serialize(item)
The data item to be transformed
Constructor for the transformer class
Maximum depth for nested transformations
Variant method name to use for transformation
OptionaldebuggingError: RuntimeException
Error thrown when the resource is undefined. Create it at the transformer call site to get a useful stack trace
Type identifier for the item 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 item.
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)
// Limit depth to 3 levels
const user = UserTransformer.transform(userData)
.depth(3)
// This allows: user -> posts -> comments -> author (3 levels)
// But excludes: user -> posts -> comments -> author -> profile (4 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 item.
Name of the transformer variant method to use (must be a method name that
returns ResourceData or Promise
// Use default toObject variant
const user = UserTransformer.transform(userData)
// Use custom toSummary variant for lighter payload
const userSummary = UserTransformer.transform(userData)
.useVariant('toSummary')
// Use detailed variant for admin view
const detailedUser = UserTransformer.transform(userData)
.useVariant('toDetailed')
Resolve the item by transforming it and resolving nested relationships. This method is typically called internally by the serializer function.
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 single transformed item from source data. This class is typically created using the
BaseTransformer.transform()method when passing a single object.