Migration v4 - context & DataSource #7096
-
With v3, I could access any dataSource inside any other dataSource through the context.
It allowed me to do the following: // for any DataSource (e.g. Posts)
async retrieveBearerUser() {
return this.context.dataSources.users // using users DataSource here
.findOneById(this.context.userJwtPayload?.subject)
} But now that the I had a context looking like a loop thanks to the previous initialization process: dataSources = {
posts: {
context: {
dataSources: {
posts: ... // and so on
}
}
}
} It may have been an unintended behavior, so how can I still retrieve the user from another dataSource with the new context implementation? Any help would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
There's an example of how to do this in the migration guide, specifically under "If you want to access your entire context's value within your DataSource". (It takes advantage of the fact that you can access On the other hand, if there's only one thing like this that you want to pull off the context (eg |
Beta Was this translation helpful? Give feedback.
There's an example of how to do this in the migration guide, specifically under "If you want to access your entire context's value within your DataSource". (It takes advantage of the fact that you can access
this
inside a class's constructor even before the class is fully constructed.)On the other hand, if there's only one thing like this that you want to pull off the context (eg
userJwtPayload
), another alternative would be to not pass the entirecontext
into your class and instead just pass theuserJwtPayload
itself in, which removes the circular dependency (and also means that when writing tests for your data source class you don't have to mock out the entire context object!).