├── .editorconfig ├── .eslintignore ├── .eslintrc ├── .github ├── sync.yml └── workflows │ ├── publish.yaml │ └── sync.yaml ├── .gitignore ├── .nuxtrc ├── LICENSE ├── README.md ├── docs ├── content │ ├── 1.getting-started │ │ ├── 1.index.md │ │ ├── 2.installation.md │ │ ├── 3.quick-start.md │ │ ├── 4.getting-help.md │ │ └── _dir.yml │ ├── 1.index.md │ └── _dir.yml └── public │ └── nuxt-prisma.txt ├── nuxt-prisma ├── nuxt.config.ts └── server │ └── middleware │ └── 0.prisma.js ├── package-lock.json ├── package.json └── tsconfig.json /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_size = 2 5 | indent_style = space 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | indent_size = 4 14 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules 3 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "@nuxtjs/eslint-config-typescript" 4 | ], 5 | "rules": { 6 | "@typescript-eslint/no-unused-vars": [ 7 | "off" 8 | ] 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /.github/sync.yml: -------------------------------------------------------------------------------- 1 | sidebase/nuxt-prisma: 2 | - docs/content 3 | - docs/public 4 | sidebase/docs: 5 | - source: docs/content/ 6 | dest: content/4.nuxt-prisma/ 7 | - source: docs/public/ 8 | dest: public/ 9 | -------------------------------------------------------------------------------- /.github/workflows/publish.yaml: -------------------------------------------------------------------------------- 1 | name: Publish to NPM 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | publish: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v3 14 | 15 | - name: Use Node.js 16.14.2 16 | uses: actions/setup-node@v3 17 | with: 18 | node-version: 16.14.2 19 | 20 | - run: npm ci 21 | 22 | - uses: JS-DevTools/npm-publish@v1 23 | with: 24 | token: ${{ secrets.NPM_TOKEN }} 25 | access: public 26 | -------------------------------------------------------------------------------- /.github/workflows/sync.yaml: -------------------------------------------------------------------------------- 1 | name: Sync docs 2 | on: 3 | push: 4 | branches: 5 | - main 6 | workflow_dispatch: 7 | jobs: 8 | sync: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Checkout Repository 12 | uses: actions/checkout@master 13 | - name: Run GitHub File Sync 14 | uses: BetaHuhn/repo-file-sync-action@v1 15 | with: 16 | GH_PAT: ${{ secrets.GH_TOKEN }} 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Dependencies 2 | node_modules 3 | 4 | # Logs 5 | *.log* 6 | 7 | # Temp directories 8 | .temp 9 | .tmp 10 | .cache 11 | 12 | # Yarn 13 | **/.yarn/cache 14 | **/.yarn/*state* 15 | 16 | # Generated dirs 17 | dist 18 | 19 | # Nuxt 20 | .nuxt 21 | .output 22 | .vercel_build_output 23 | .build-* 24 | .env 25 | .netlify 26 | 27 | # Env 28 | .env 29 | 30 | # Testing 31 | reports 32 | coverage 33 | *.lcov 34 | .nyc_output 35 | 36 | # VSCode 37 | .vscode/* 38 | !.vscode/settings.json 39 | !.vscode/tasks.json 40 | !.vscode/launch.json 41 | !.vscode/extensions.json 42 | !.vscode/*.code-snippets 43 | 44 | # Intellij idea 45 | *.iml 46 | .idea 47 | 48 | # OSX 49 | .DS_Store 50 | .AppleDouble 51 | .LSOverride 52 | .AppleDB 53 | .AppleDesktop 54 | Network Trash Folder 55 | Temporary Items 56 | .apdisk 57 | 58 | # sqlite 59 | *.sqlite 60 | *.sqlite-journal 61 | -------------------------------------------------------------------------------- /.nuxtrc: -------------------------------------------------------------------------------- 1 | imports.autoImport=false 2 | typescript.includeWorkspace=true 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 SIDESTREAM GmbH 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nuxt-prisma 2 | 3 | [![npm version][npm-version-src]][npm-version-href] 4 | [![npm downloads][npm-downloads-src]][npm-downloads-href] 5 | [![GitHub stars](https://badgen.net/github/stars/sidebase/nuxt-prisma)](https://GitHub.com/sidebase/nuxt-prisma/) 6 | [![License][license-src]][license-href] 7 | [![Follow us on Twitter](https://badgen.net/badge/icon/twitter?icon=twitter&label)](https://twitter.com/sidebase_io) 8 | [![Join our Discord](https://badgen.net/badge/icon/discord?icon=discord&label)](https://discord.gg/NDDgQkcv3s) 9 | 10 | **DEPRECATION NOTICE:** `nuxt-prisma` will be depreacated at the 31.01.2023 - read https://github.com/sidebase/nuxt-prisma/issues/6 for reasoning and process. The new process to add Prisma to Nuxt 3 sidebase apps is to run: 11 | ```sh 12 | # also works with `pnpm` and `yarn` 13 | > npm create sidebase@latest 14 | ``` 15 | select the `Merino` stack and then the `Prisma ORM` option. Need help? Ask away: https://discord.gg/NDDgQkcv3s 16 | 17 | > `nuxt-prisma` is an open source Nuxt 3 layer that provides an Prisma ORM integration for Nuxt 3 applications. 18 | 19 | ## Quick start 20 | 21 | Follow the [Quick Start documentation](https://sidebase.io/nuxt-prisma/getting-started) to get going. 22 | 23 | `nuxt-prisma` is a part of `sidebase`, the productive Nuxt 3 starter. If you want to have a template that has `nuxt-prisma` and the Prisma CLI integrated out of the box, use sidebase: 24 | ```sh 25 | npm create sidebase@latest 26 | ``` 27 | 28 | Read the [sidebase starter docs](https://sidebase.io/sidebase/getting-started) for more. 29 | 30 | 31 | [npm-version-src]: https://img.shields.io/npm/v/@sidebase/nuxt-prisma/latest.svg 32 | [npm-version-href]: https://npmjs.com/package/@sidebase/nuxt-prisma 33 | 34 | [npm-downloads-src]: https://img.shields.io/npm/dt/@sidebase/nuxt-prisma.svg 35 | [npm-downloads-href]: https://npmjs.com/package/@sidebase/nuxt-prisma 36 | 37 | [license-src]: https://img.shields.io/npm/l/@sidebase/nuxt-prisma.svg 38 | [license-href]: https://npmjs.com/package/@sidebase/nuxt-prisma 39 | -------------------------------------------------------------------------------- /docs/content/1.getting-started/1.index.md: -------------------------------------------------------------------------------- 1 | --- 2 | description: "Introduction to `nuxt-prisma` and its features as an prisma ORM layer for your Nuxt 3 application." 3 | --- 4 | 5 | # Introduction 6 | 7 | `nuxt-prisma` was an open source Nuxt layer that provided an Prisma ORM integration for Nuxt 3 applications. It is now in the process of being deprecated in favor of [the sidebase CLI](/sidebase): 8 | ::code-group 9 | ```bash [npm] 10 | npm create sidebase@latest 11 | ``` 12 | ```bash [pnpm] 13 | pnpm create sidebase@latest 14 | ``` 15 | ```bash [yarn] 16 | # Note: Due to a known problem with `yarn`, it is not possible to force yarn to always use `@latest`: https://github.com/yarnpkg/yarn/issues/6587 17 | yarn create sidebase 18 | ``` 19 | :: 20 | 21 | It also allows adding Prisma to Nuxt 3 projects and exposes the same helpers and utilities as this layer. The deprecation will comeplete on the 31.01.2023. Afterwards these docs will forward to [the sidebase CLI](/sidebase). Follow [this issue](https://github.com/sidebase/nuxt-prisma/issues/6) for the deprecation. 22 | 23 | `nuxt-prisma`: 24 | ::list{type="success"} 25 | - Adds `prisma: PrismaClient` to the `event.context.prisma` which allows you to access a `PrismaClient` on the server-side, the client is setup following [prisma best practices](https://www.prisma.io/docs/guides/performance-and-optimization/connection-management) 26 | - Adds `resetDatabase`, a testing utility that resets your database, this can be useful for testing, e.g., in a `beforeEach` hook 27 | :: 28 | 29 | After setting up your Nuxt 3 app with the sidebase CLI your endpoints will be able to do this: 30 | ```ts 31 | // file: ~/server/api/example.get.ts 32 | export default eventHandler((event) => { 33 | return event.context.prisma.example.findMany() 34 | }) 35 | ``` 36 | 37 | See more in the [Quick Start section](/nuxt-prisma/getting-started/quick-start). 38 | -------------------------------------------------------------------------------- /docs/content/1.getting-started/2.installation.md: -------------------------------------------------------------------------------- 1 | --- 2 | description: "How to install nuxt-prisma." 3 | --- 4 | 5 | # Installation 6 | 7 | To setup a Nuxt 3 project with Prisma ORM use the [sidebase CLI](/sidebase/welcome): 8 | ::code-group 9 | ```bash [npm] 10 | npm create sidebase@latest 11 | ``` 12 | ```bash [pnpm] 13 | pnpm create sidebase@latest 14 | ``` 15 | ```bash [yarn] 16 | # Note: Due to a known problem with `yarn`, it is not possible to force yarn to always use `@latest`: https://github.com/yarnpkg/yarn/issues/6587 17 | yarn create sidebase 18 | ``` 19 | :: 20 | 21 | In the CLI you can either the Merino stack or the Cheviot stack - if you choose the Merino stack, select "Prisma ORM" when the CLI asks you for what modules to use. And done! 22 | -------------------------------------------------------------------------------- /docs/content/1.getting-started/3.quick-start.md: -------------------------------------------------------------------------------- 1 | --- 2 | description: "How to use nuxt-prisma." 3 | --- 4 | 5 | # Quick Start 6 | 7 | After [installing the package](/nuxt-prisma/getting-started/installation), checkout the [sidebase "with Prisma" quick start](sidebase/welcome/quick-start#with-prisma) or the [more detailed Prisma description](/sidebase/components/prisma) to get further information. 8 | 9 | This page summarizes covers some additional information related to endpoint-usage and testing. 10 | 11 | ## Use Prisma in Endpoints 12 | 13 | To use Prisma in one of your endpoints, you can now easily: 14 | ```ts 15 | // file: ~/server/api/example.get.ts 16 | export default eventHandler(event => event.context.prisma.example.findMany()) 17 | ``` 18 | 19 | This also works in your app-middleware! 20 | 21 | ## Database Setup for Jest / Vitest tests 22 | 23 | The prisma-integration layer also includes `resetDatabase` which is a function that quickly bootstrap a clean database, e.g., for a testing setup. Here's how to use it: 24 | ```ts 25 | import { describe, beforeEach } from 'vitest' 26 | import { resetDatabase } from '~/prisma/utils' 27 | 28 | beforeEach(() => { 29 | resetDatabase() 30 | }) 31 | 32 | describe('test that involves the database', () => { 33 | // Add code that depends on a setup, clean database here. 34 | 35 | // You can also add multiple tests, they won't corrupt each other as the database is cleaned up after every go 36 | }) 37 | ``` 38 | 39 | ::alert{type="danger"} 40 | `resetDatabase` drops all data of the database that is currently configured via the environment variable `DATABASE_URL`, never run `resetDatabase` in production. 41 | :: 42 | -------------------------------------------------------------------------------- /docs/content/1.getting-started/4.getting-help.md: -------------------------------------------------------------------------------- 1 | --- 2 | description: "How to get help when using `nuxt-prisma` in your Vue / Nuxt 3 application." 3 | --- 4 | 5 | # Getting Help 6 | 7 | At some point, you may find that there's an issue you need some help with. 8 | 9 | But don't worry! We're a friendly community of developers and we'd love to help. Concretely this means to: 10 | - Checkout the docs (page that you are currently viewing), 11 | - Search open issues and discussions: https://github.com/sidebase/nuxt-prisma/issues 12 | - Hop on Discord to ask us directly: https://discord.gg/VzABbVsqAc, 13 | - Open an issue to file a bug, ask for an enhancement or get an answer to a question: https://github.com/sidebase/nuxt-prisma/issues/new/choose 14 | 15 | We aim to follow the getting-help standards of the nuxt-project as described here and ask you to do the same when opening an issue or pinging us for help: https://nuxt.com/docs/community/getting-help#getting-help. 16 | -------------------------------------------------------------------------------- /docs/content/1.getting-started/_dir.yml: -------------------------------------------------------------------------------- 1 | title: Getting Started 2 | icon: heroicons-outline:sparkles 3 | -------------------------------------------------------------------------------- /docs/content/1.index.md: -------------------------------------------------------------------------------- 1 | --- 2 | navigation: false 3 | redirect: /nuxt-prisma/getting-started 4 | --- 5 | -------------------------------------------------------------------------------- /docs/content/_dir.yml: -------------------------------------------------------------------------------- 1 | title: nuxt-prisma 2 | icon: heroicons-outline:circle-stack 3 | -------------------------------------------------------------------------------- /docs/public/nuxt-prisma.txt: -------------------------------------------------------------------------------- 1 | nuxt-prisma placeholder 2 | -------------------------------------------------------------------------------- /nuxt-prisma/nuxt.config.ts: -------------------------------------------------------------------------------- 1 | import { resolve } from 'path' 2 | import { execSync } from 'child_process' 3 | import { defineNuxtConfig } from 'nuxt/config' 4 | import { PrismaClient } from '@prisma/client' 5 | import type { H3Event } from 'h3' 6 | 7 | declare module 'h3' { 8 | interface H3EventContext { 9 | prisma: PrismaClient 10 | } 11 | } 12 | 13 | // Reset database via programmatic prisma invocation, see https://github.com/prisma/prisma/issues/13549#issuecomment-1144883246 14 | export function resetDatabase () { 15 | execSync(`cd ${process.cwd()} && DATABASE_URL=${process.env.DATABASE_URL} prisma db push --force-reset`, { stdio: 'inherit' }) 16 | } 17 | 18 | export function usePrisma (event: any | H3Event): PrismaClient { 19 | return event.context.prisma 20 | } 21 | 22 | /** 23 | * Takes a path to a file, makes it absolute and then sets the `DATABASE_URL` environment variable to a value of the form `file:/path/to/db.sqlite`. 24 | * 25 | * This method can be helpful for development and testing to ensure that all code uses the same, absolute `db.sqlite` file. 26 | * 27 | * @param pathToSqliteFile string The location of the `db.sqlite` file. E.g.: `./db.sqlite` or `db.sqlite` or `/Users/test/nuxtprisma/db.sqlite` 28 | * @param environmentVariableName string Name of the environment variable to export the `file:/...` database url to, this is the name that prisma uses in the `schema.prisma` `env(...)` directive 29 | */ 30 | export function setAbsoluteSqliteDatabaseUrlForPrisma (pathToSqliteFile: string = resolve('./db.sqlite'), environmentVariableName = 'DATABASE_URL') { 31 | if (process.env.DATABASE_URL) { 32 | // User or nuxt set their own `DATABASE_URL`, do not overwrite it 33 | return 34 | } 35 | 36 | // We need to resolve again in case a relative path was passed 37 | const absoluteDbPath = `file:${resolve(pathToSqliteFile)}` 38 | process.env[environmentVariableName] = absoluteDbPath 39 | } 40 | 41 | export default defineNuxtConfig({}) 42 | -------------------------------------------------------------------------------- /nuxt-prisma/server/middleware/0.prisma.js: -------------------------------------------------------------------------------- 1 | import { PrismaClient } from '@prisma/client' 2 | import { eventHandler } from 'h3' 3 | 4 | let prisma 5 | export default eventHandler((event) => { 6 | if (!prisma) { 7 | prisma = new PrismaClient() 8 | } 9 | event.context.prisma = prisma 10 | }) 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "sidestream-tech ", 3 | "bugs": { 4 | "url": "https://github.com/sidebase/nuxt-prisma/issues/new" 5 | }, 6 | "homepage": "https://github.com/sidebase/nuxt-prisma", 7 | "name": "@sidebase/nuxt-prisma", 8 | "version": "0.1.2", 9 | "license": "MIT", 10 | "type": "module", 11 | "main": "./nuxt-prisma/nuxt.config.ts", 12 | "files": [ 13 | "nuxt-prisma" 14 | ], 15 | "scripts": { 16 | "dev": "cd nuxt-prisma && nuxi dev", 17 | "dev:build": "nuxi build nuxt-prisma" 18 | }, 19 | "dependencies": { 20 | "@prisma/client": "^4.8.0", 21 | "h3": "^1.0.2" 22 | }, 23 | "devDependencies": { 24 | "@nuxtjs/eslint-config-typescript": "^12.0.0", 25 | "eslint": "^8.30.0", 26 | "nuxt": "^3.0.0", 27 | "prisma": "^4.8.0" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | {} 2 | --------------------------------------------------------------------------------