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 🙏

© 2024 – Pkg Stats / Ryan Hefner

supabase-query

v1.0.1

Published

Supercharge your development speed with Supabase and react-query, combined!

Downloads

121

Readme

supabase-query

Supercharge your development speed with Supabase and react-query, combined!

Features

  • Caching – save database reads
  • No more manually writing loading states
  • Easily invalidate after mutations
  • Leverage all of react-query's powerful query- and mutation API
  • Build queries with the Supabase query builder
  • Type support with Supabase v2 types

Installation

yarn add supabase-query @supabase/supabase-js react-query@^3

or with npm:

npm i supabase-query @supabase/supabase-js react-query@^3

Quick start

import { QueryClient, QueryClientProvider, useQueryClient } from "react-query";
import { createClient } from "@supabase/supabase-js";
import {
  SupabaseQueryProvider,
  useSupabaseMutation,
  useSupabaseQuery,
} from "supabase-query";

const queryClient = new QueryClient();
const supabaseClient = createClient("https://foo.bar", "key");

function App() {
  return (
    // Provide SupabaseQuery provider to your app
    <SupabaseQueryProvider client={supabaseClient}>
      <QueryClientProvider client={queryClient}>
        <Todos />
      </QueryClientProvider>
    </SupabaseQueryProvider>
  );
}

function Todos() {
  // Use the provided supabase instance in the callback to build your query
  // The table name ("todos" here) will by default be used as the queryKey (see mutation)
  const { data, isLoading } = useSupabaseQuery((supabase) =>
    supabase.from("todos").select()
  );

  // Mutations
  // Access the client for invalidations
  const queryClient = useQueryClient();
  const { mutate, isLoading: isPosting } = useSupabaseMutation({
    onSuccess: () => queryClient.invalidateQueries("todos"),
  });

  if (isLoading) return <p>Loading...</p>;

  return (
    <div>
      <ul>
        {data.map((todo) => (
          <li key={todo.id}>{todo.name}</li>
        ))}
      </ul>
      <button
        disabled={isPosting}
        // Use the mutate callback to insert to supabase
        onClick={() =>
          mutate((supabase) =>
            supabase.from("todos").insert([{ name: "new todo" }])
          )
        }
      >
        {isPosting ? "Submitting..." : "Add Todo"}
      </button>
    </div>
  );
}
export default App;

API

useSupabaseQuery

const { data, isLoading } = useSupabaseQuery(
  (supabase) => supabase.from("todos").select(),
  {
    onSuccess: () => {
      alert("success");
    },
    queryKey: ["todos", page],
  }
);

useSupabaseQuery accepts two arguments: The first is a function that provides the supabase client as an argument, and it expects a built query in return.

The table name will by default be used as the queryKey for react-query.

The second argument: react-query options, see their docs for all options. The options object also takes in queryKey if you want to override the default key for a more dynamic one.

useSupabaseMutation

import { useQueryClient } from "react-query";

const client = useQueryClient();
const { mutate, isLoading: isPosting } = useSupabaseMutation({
  onSuccess: () => client.invalidateQueries("todos"),
});

function handlePost() {
  mutate((supabase) =>
    supabase.from("todos").insert([{ name: val, done: false }])
  );
}

<Button loading={isPosting} onClick={handlePost}>
  Add todo
</Button>;

The mutate function is used the same way as the useSupabaseQuery callback argument with the supabase client provided.

TypeScript

To leverage Supabase's schema type export you have to pass the schema to supabase-query's hooks and export those hooks in your app for use:

hooks/supabase.ts

import {
  TypedUseSupabaseMutation,
  TypedUseSupabaseQuery,
  useSupabaseMutation,
  useSupabaseQuery,
} from "supabase-query";
import { DatabaseSchema } from "./db.types.ts";

export const useTypedSupabaseQuery: TypedUseSupabaseQuery<DatabaseSchema> =
  useSupabaseQuery;
export const useTypedSupabaseMutation: TypedUseSupabaseMutation<DatabaseSchema> =
  useSupabaseMutation;

Then use it in your app:

import { useTypedSupabaseQuery } from "hooks/supabase";

function Todos() {
  // Data will be typed correctly
  const { data, isLoading } = useTypedSupabaseQuery(
    (supabase) =>
      // The Supabase client will be typed to give inference on all tables
      supabase.from("todos").select(),
    {
      // Data is typed in options
      onSuccess(data) {
        console.log(data[0].done);
      },
    }
  );
}