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

nuxt-prisma

v0.0.3

Published

Prisma support for Nuxt.

Downloads

8

Readme

Nuxt Prisma

npm version npm downloads License

Features

  • Adds prisma client to event.context in the server using a Nuxt server middleware.
  • (OPTIONAL) Sets default role with SET ROLE.
  • (OPTIONAL) Reads an attribute from JWT and sets local role accordingly using SET LOCAL ROLE.
  • (OPTIONAL) Copies JWT data to the current session settings using set_config().
  • (OPTIONAL) Allows a RLS implementation with options and presets. (PostgreSQL)
  • (OPTIONAL) Provides preset: supabase.

Quick Setup

  1. Add nuxt-prisma dependency to your project
npm install --save-dev nuxt-prisma
  1. Add nuxt-prisma to the modules section of nuxt.config.ts
export default defineNuxtConfig({
  modules: ['nuxt-prisma'],
  prisma: { preset: "supabase" }
});
  1. If options.dbConfigName or options.jwtRoleAttribute is used, you need to enable prisma client extensions. To use Prisma Client extensions, you must enable the clientExtensions preview feature flag in the generator block of your schema.prisma file. Learn more
generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["clientExtensions"]
}

Usage

/server/api/item.get.ts

export default eventHandler(async (event) => {
  const prisma = event.context.prisma;
  const item = await prisma.item.findUniqueOrThrow({ where: { id } });
}

Options

Below is an example for Supabase.

export default defineNuxtConfig({
  modules: ['nuxt-prisma'],
  prisma: { preset: "supabase" } // Options preset. Apply multiple options for a specific framework.
});

Supabase preset sets options as below.

export default defineNuxtConfig({
  modules: ['nuxt-prisma'],
  prisma: {
    defaultRole: "anon",                // Default database role to use for unauthenticated users. Set by `SET ROLE`.
    contextTokenAttribute: "_token",    // H3 event context attribute to get JWT token. WARNING: The token should be validated previously. This module does not validate JWT token.
    jwtRoleAttribute: "role",           // JWT attribute to get database role from.
    dbConfigName: "request.jwt.claims", // Database config name to assign contents of the JWT token. All decoded data is assigned to this config as a stringified JSON.
  }
});

Example above adds a Nuxt server middleware as explained below.

|Option|Value|Description| |---|---|---| | defaultRole | anon | Sets new prisma client's role to anon using SET ROLE anon | | contextTokenAttribute | _token | Reads the JWT token from event.context._token and decodes it. | | jwtRoleAttribute | role | Reads role attribute from JWT token (i.e. { ... "role": "authenticated" }) and sets local role to it's value using SET LOCAL ROLE authenticated. | | dbConfigName | request.jwt.claims | JWT data is written local config variable using set_config('request.jwt.claims', '{ ... }', true) |

IMPORTANT NOTES:

You should add a validated JWT to the context previously. JWT is not validated by this module. Storing a non-validated JWT in the context is unsecure.