-
|
Hello, considering this non-Colada setup using a Pinia store: export const myStore = defineStore("myStore", () => {
const users = ref<Record<number, { name: string }>>();
const fetchUsers = async (ids: string[]) => {
const newUserIds = ids.filter((id) => !(Object.keys(users.value)));
const newUsers = await fetch('/users/'+newUserIds.join(','));
Object.assign(users.value, newUsers);
}
});What would be an equivalent solution that's idiomatic in Colada? My naive attempt is export const myStore = defineStore("myStore", () => {
const users = ref<Record<number, { name: string }>>({});
const fetchUsers = (userIds: MaybeRefOrGetter<number[]>) => {
const newUserIds = computed(() =>
toValue(userIds).filter(
(id) => !Object.keys(users.value).includes(id.toString()),
),
);
return useQuery({
key: () => ["users", newUserIds.value.sort().join(",")] as const,
query: () => fetch("/users/" + newUserIds.value.join(",")),
enabled: () => !!newUserIds.value.length,
});
};
return {
fetchUsers,
};
});but the users are not stored in the store then, because The alternative would be to use Thanks in advance for your suggestions :-) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
I would set the data in the query cache. It sounds like you want cache normalization like this: #531 Also, if you call |
Beta Was this translation helpful? Give feedback.
I would set the data in the query cache. It sounds like you want cache normalization like this: #531
Also, if you call
useQuery()in a store, it will never get gced. You can use defineQuery or defineQueryOptions. I recommend to check https://pinia-colada.esm.dev/guide/queries.html#Organizing-Queries