npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@modelence/react-query

v1.0.2

Published

React Query utilities for Modelence

Readme

@modelence/react-query

React Query utilities for Modelence method calls.

Installation

npm i @modelence/react-query @tanstack/react-query

Overview

This package provides modelenceQuery and modelenceMutation factory functions that can be used with TanStack Query's native useQuery and useMutation hooks. This approach, recommended by TanStack, gives you direct access to TanStack Query's full API while providing Modelence-specific query configurations.

Usage

Basic Query

import { useQuery } from '@tanstack/react-query';
import { modelenceQuery } from '@modelence/react-query';

function TodoList() {
  const { data, isPending, error } = useQuery(
    modelenceQuery('todo.getAll', { limit: 10 })
  );
  
  if (isPending) return <div>Loading...</div>;
  if (error) return <div>Error: {error.message}</div>;
  
  return (
    <div>
      {data?.map(todo => (
        <div key={todo.id}>{todo.title}</div>
      ))}
    </div>
  );
}

Basic Mutation

import { useMutation, useQueryClient } from '@tanstack/react-query';
import { modelenceMutation } from '@modelence/react-query';

function CreateTodo() {
  const queryClient = useQueryClient();
  
  const { mutate: createTodo, isPending } = useMutation({
    ...modelenceMutation('todo.create'),
    onSuccess: () => {
      // Invalidate and refetch todos
      queryClient.invalidateQueries({ queryKey: ['todo.getAll'] });
    },
  });
  
  return (
    <button 
      onClick={() => createTodo({ title: 'New Todo', completed: false })}
      disabled={isPending}
    >
      {isPending ? 'Creating...' : 'Create Todo'}
    </button>
  );
}

Advanced Usage

Query with Additional Options

import { useQuery } from '@tanstack/react-query';
import { modelenceQuery } from '@modelence/react-query';

function TodoDetail({ id }: { id: string }) {
  const { data: todo } = useQuery({
    ...modelenceQuery('todo.getById', { id }),
    enabled: !!id, // Only run query if id exists
    staleTime: 5 * 60 * 1000, // 5 minutes
    refetchOnWindowFocus: false,
  });
  
  return <div>{todo?.title}</div>;
}

Mutation with Default Args

import { useMutation } from '@tanstack/react-query';
import { modelenceMutation } from '@modelence/react-query';

function UpdateTodo({ todoId }: { todoId: string }) {
  const { mutate: updateTodo } = useMutation({
    ...modelenceMutation('todo.update', { id: todoId }), // Default args
    onSuccess: (data) => {
      console.log('Todo updated:', data);
    },
  });
  
  return (
    <button onClick={() => updateTodo({ title: 'Updated Title' })}>
      Update Todo
    </button>
  );
}

Manual Cache Operations

import { useQueryClient } from '@tanstack/react-query';
import { createQueryKey, modelenceQuery } from '@modelence/react-query';

function TodoActions() {
  const queryClient = useQueryClient();
  
  const refreshTodos = () => {
    queryClient.invalidateQueries({ 
      queryKey: createQueryKey('todo.getAll', { limit: 10 }) 
    });
  };
  
  const prefetchTodo = (id: string) => {
    queryClient.prefetchQuery({
      ...modelenceQuery('todo.getById', { id }),
      staleTime: 10 * 60 * 1000, // 10 minutes
    });
  };
  
  return (
    <div>
      <button onClick={refreshTodos}>Refresh Todos</button>
      <button onClick={() => prefetchTodo('123')}>Prefetch Todo</button>
    </div>
  );
}