passthrough
Associations in Dream can be defined with a passthrough constraint. This means that the only way these associations can be loaded is if data is passed through using a passthrough method. This is useful for localization patterns where the locale changes per request and is driven by headers:
export default class Post extends ApplicationModel {
@deco.HasOne('LocalizedText', {
polymorphic: true,
on: 'localizableId',
and: { locale: DreamConst.passthrough },
})
public currentLocalizedText: LocalizedText
}
const reloadedUser = await user
.passthrough({ locale: 'es-ES' })
.load('posts', 'currentLocalizedText')
.execute()
For serializer-driven responses, pass the same value through both channels when needed:
const locale = this.castParam('Accept-Language', 'string', { enum: LocalesEnumValues })
const place = await Place
.passthrough({ locale }) // association constraints
.preloadFor('forGuests')
.findOrFail(this.castParam('id', 'uuid'))
this.serializerPassthrough({ locale }) // serializer callbacks
this.ok(place)