nuxt-prisma
v0.0.3
Published
Prisma support for Nuxt.
Downloads
8
Readme
Nuxt Prisma
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
- Add
nuxt-prisma
dependency to your project
npm install --save-dev nuxt-prisma
- Add
nuxt-prisma
to themodules
section ofnuxt.config.ts
export default defineNuxtConfig({
modules: ['nuxt-prisma'],
prisma: { preset: "supabase" }
});
- If
options.dbConfigName
oroptions.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.