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

prisma-hooks

v0.1.8

Published

100% type-safe, generated react-query hooks for quering any model in your prisma-managed database.

Downloads

59

Readme

🪝 Prisma Hooks

Build

100% type-safe, generated react-query hooks for quering any model in your prisma-managed database.

Built originally on a Next.js project, these hooks can be used in any project that uses a React frontend and a Node.js backend. However, if not using Next.js (particularly, if you have a separate frontend and backend), you will need to maintain a copy of the schema.prisma file in both your client and in your server (since we rely on it for generating the custom hooks). You will only ever run npx prisma-hooks generate in the client, but be sure whenever changes are made to you schema file on the server that it is also updated in the client copy.

Install

yarn add prisma-hooks

Or via npm

npm i prisma-hooks

Quickstart

  1. Add your api handler (any node environment works)
// server.ts
import { prisma } from "services/prisma";
import { hamdlePrismaQuery } from "prisma-hooks";
...

// Handles requests like the following `/:model/:action?count=<boolean>`
export const handler(req) => {
  const res = await handlePrismaQuery({
    model: req.query.model,
    action: req.query.action,
    count: req.query.count ?? false,
    query: req.body,
    db: prisma,
  });

  if (res.error) {
    return res.status(422).json(res);
  }

  return res.status(200).json(res);
}
  1. Generate your 100% type-safe custom hooks on the client

Note: a prisma/schema.prisma file must exist on the client, even if your client and server are separate. A schema file must be preset on the client in order for the generator to work.

npx prisma-hooks generate
  1. Use your newly generated hooks!
// With `count` included
const { data: { data: posts = [], _count = 0 } = {}, isLoading } =
  useFindManyPosts({
    query: {
      where: { id: 1 },
      include: { comments: true },
      orderBy: { createdAt: "desc" },
    },
    options: { keepPreviousData: true, enabled: true },
    count: true,
  });
// Without `count` included
const { data: posts = [], isLoading } = useFindManyPosts({
  query: // ...
  options: // ...
});
const { mutateAsync: upsertPost, isLoading } = useUpsertPost();
...
upsertPost({
  where: { id: 1 },
  create: { name: "New Name" },
  update: { name: "Udated Name" },
});

A mutation action (like createMany) is a useMutation from react-query while a query action (like findMany) is a useQuery.

Known issues

For some reason, development bundlers like Webpack will "forget" that the hooks were generated. If running a Next.js application, for example, you may run into the error TypeError: (0 , prisma_hooks__WEBPACK_IMPORTED_MODULE_2__.<hook>) is not a function. VSCode also forgets sometimes. To resolve, simply run npx prisma-hooks generate again or delete the .next folder and restart your server. In other words, clear your development caches and the generated hooks will be working once again.

API Reference

  • handlePrismaQuery
  • useFindUniqueUser
  • useFindFirstUser
  • useFindManyUsers
  • useCountUser
  • useAggregateUser
  • useGroupByUser
  • useUpdateUser
  • useUpdateManyUsers
  • useUpsertUser
  • useDeleteUser
  • useDeleteManyUsers
  • useCreateUser
  • useCreateManyUsers

Contribution

We love contributions! Please feel free to create a fork for making any changes you see fit, this is still a work in progress.