UNPKG

@uecsio/query-cache

Version:

Cache management utilities for TanStack Query (Vue Query)

160 lines (145 loc) 5.06 kB
import { useQueryClient } from '@tanstack/vue-query'; export { useQueryClient } from '@tanstack/vue-query'; /** * @uecsio/query-cache * Cache management utilities for TanStack Query (Vue Query) */ /** * Get the query client instance * @returns {import('@tanstack/vue-query').QueryClient} The TanStack Query client */ function getQueryClient() { return useQueryClient() } /** * Clear cache by query key pattern * Removes all queries that match the provided key pattern * @param {Array|string} queryKey - Query key or key pattern to clear * @example * // Clear specific query * clearCache(['users', 1]) * * // Clear all queries starting with 'users' * clearCache(['users']) * * // Clear single key * clearCache('todos') */ function clearCache(queryKey) { const queryClient = useQueryClient(); const key = Array.isArray(queryKey) ? queryKey : [queryKey]; queryClient.removeQueries({ queryKey: key }); } /** * Invalidate cache by query key pattern * Marks queries as stale and triggers refetch if they're active * @param {Array|string} queryKey - Query key or key pattern to invalidate * @example * // Invalidate specific query * invalidateCache(['users', 1]) * * // Invalidate all queries starting with 'users' * invalidateCache(['users']) */ function invalidateCache(queryKey) { const queryClient = useQueryClient(); const key = Array.isArray(queryKey) ? queryKey : [queryKey]; queryClient.invalidateQueries({ queryKey: key }); } /** * Clear all cache in the application * Use with caution - removes all TanStack Query cache */ function clearAllCache() { const queryClient = useQueryClient(); queryClient.clear(); } /** * Refetch queries by key pattern * Forces an immediate refetch of matching queries * @param {Array|string} queryKey - Query key or key pattern to refetch * @param {Object} options - Refetch options * @returns {Promise<void>} */ async function refetchQueries(queryKey, options = {}) { const queryClient = useQueryClient(); const key = Array.isArray(queryKey) ? queryKey : [queryKey]; return await queryClient.refetchQueries({ queryKey: key, ...options }) } /** * Reset queries to their initial state * @param {Array|string} queryKey - Query key or key pattern to reset * @param {Object} options - Reset options */ function resetQueries(queryKey, options = {}) { const queryClient = useQueryClient(); const key = Array.isArray(queryKey) ? queryKey : [queryKey]; queryClient.resetQueries({ queryKey: key, ...options }); } /** * Get cached data by query key * @param {Array|string} queryKey - Query key to retrieve * @returns {any} The cached data or undefined if not found */ function getCachedData(queryKey) { const queryClient = useQueryClient(); const key = Array.isArray(queryKey) ? queryKey : [queryKey]; return queryClient.getQueryData(key) } /** * Set cached data by query key * @param {Array|string} queryKey - Query key to set * @param {any} data - Data to cache * @param {Object} options - Set options */ function setCachedData(queryKey, data, options = {}) { const queryClient = useQueryClient(); const key = Array.isArray(queryKey) ? queryKey : [queryKey]; queryClient.setQueryData(key, data, options); } /** * Check if a query exists in cache * @param {Array|string} queryKey - Query key to check * @returns {boolean} True if the query exists in cache */ function hasCache(queryKey) { const queryClient = useQueryClient(); const key = Array.isArray(queryKey) ? queryKey : [queryKey]; return queryClient.getQueryData(key) !== undefined } /** * Cancel ongoing queries by key pattern * Useful for preventing race conditions * @param {Array|string} queryKey - Query key or key pattern to cancel * @returns {Promise<void>} */ async function cancelQueries(queryKey) { const queryClient = useQueryClient(); const key = Array.isArray(queryKey) ? queryKey : [queryKey]; return await queryClient.cancelQueries({ queryKey: key }) } /** * Get query state by key * @param {Array|string} queryKey - Query key to get state for * @returns {Object|undefined} Query state object or undefined */ function getQueryState(queryKey) { const queryClient = useQueryClient(); const key = Array.isArray(queryKey) ? queryKey : [queryKey]; return queryClient.getQueryState(key) } /** * Prefetch a query * Useful for preloading data before it's needed * @param {Array|string} queryKey - Query key to prefetch * @param {Function} queryFn - Query function to fetch data * @param {Object} options - Prefetch options * @returns {Promise<void>} */ async function prefetchQuery(queryKey, queryFn, options = {}) { const queryClient = useQueryClient(); const key = Array.isArray(queryKey) ? queryKey : [queryKey]; return await queryClient.prefetchQuery({ queryKey: key, queryFn, ...options }) } export { cancelQueries, clearAllCache, clearCache, getCachedData, getQueryClient, getQueryState, hasCache, invalidateCache, prefetchQuery, refetchQueries, resetQueries, setCachedData }; //# sourceMappingURL=index.esm.js.map