Replies: 3 comments 5 replies
-
Maybe something like this, but the downside of this is having multiple permission arrays somewhere in the store. If permissions would be a few thousand entities this could be very bad. // duplicate all permissions and current user in store
// make use of UserService in PermissionQueryService
permissionsOfUser2(userId: string) {
return this.useQuery({
queryKey: ['permissions-of-user', userId],
queryFn: () => {
return this.permissionService
.loadAllPermissions()
.pipe(
switchMap((permissions) =>
this.userService
.getCurrentUser()
.pipe(
map((user) =>
this.resolvePermissions(permissions, user.permissionIds)
)
)
)
);
},
});
} |
Beta Was this translation helpful? Give feedback.
-
What you could do is combine the Both of them need to have emitted atleast once, since userPermissions$ = this.user$.pipe(
withLatestFrom(this.permissions$),
map(([user, permissions]) => {
user?.data?.permissionIds.reduce((acc, curr) => {
const permission = permissions.data?.find((perm) => perm.id === curr);
if (permission) return [...acc, permission];
return acc;
}, [] as Permissions[]);
return [user, permissions];
})
); Also this way you can keep the results and transform your data. https://stackblitz.com/edit/angular-ivy-xhag4g?file=src/app/user-page.component.ts |
Beta Was this translation helpful? Give feedback.
-
@kiesman99 use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey @NetanelBasal first of all thank you very much for your work on @ngneat/elf and @ngneat/query and all the other awesome packages!
I am currently working with @ngneat/query and i've come to the point where query composition is needed. Unfortunately i am not quite sure on how to implement this with @ngneat/query. I've come up with a very small sample which should replicate the need i have in a bigger application. If something is not clear feel free to hit me up!
There are some discussions like this at tanstack/query but as tanstack/query relies on promises and ngneat/query on observables (duo to HttpClient) i have issues comparing these both.
DISCLAIMER: Please do not be concerned if the models i am gathering make sense. This example should only re-create an issue in the smallest possible version.
Stackblitz
Git Repo
Models
Services
As you can see i have two services. The
UserService
is able to fetch the current user and thePermissionService
is able to fetch all available permissions. TheUser
-Model however returns a list ofpermissionIds
.QueryServices
I have provided myself a query service for each service class which handles fetching the data and saving it.
Pages
Now i want to have a page which displays all available permissions. This one is easy:
However i also want to have a user-page which displays some user data all permission names which the user has access to.
As i already fetched all permissions i want to reuse this query. Thus i only need to fetch the currentUser, go through the list of permissionIds and match them against the already fetched permissions:
(The
permissionById
could be either memoized or a pipe).This works, however it would be awesome to have something like:
But now i don't have information about
query.isLoading
,query.isError
etc.Do you have a suggestion on how to do something similar to the provided code?
If we come up with some recipes i would be happy to contribute them to this repository!
Beta Was this translation helpful? Give feedback.
All reactions