-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add with-prisma
example
#979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
50b31e8
Added initial example
NuroDev 841e227
Added basic docker-compose MySQL database
NuroDev 1cda125
Updated README.md
NuroDev 7fa2317
Removed .gitignore
NuroDev 0e67f8b
Added symlink to database package
NuroDev 203ab9b
Updated README.md
NuroDev a8ac01c
Added Turborepo JSON schema
NuroDev f43b7a1
Added multiple Prisma database scripts to Turborepo pipeline
NuroDev c17d469
Updated db pipelines to add `outputs`
NuroDev dbc985a
Merge branch 'main' into examples/prisma
NuroDev be5963a
Merge branch 'main' into examples/prisma
tknickman 737fed9
Merge branch 'main' into examples/prisma
tknickman 0b40308
Replace esno with tsx
NuroDev abb476b
Added property to Prisma example package.json
NuroDev 81bff71
Added initial seed user
NuroDev ca05439
Updated README.md
NuroDev 0a9a4f5
Updated pnpm-lock.yaml
NuroDev 6dff0a5
Fixed docker-compise.yml value
NuroDev f21d9d8
Merge branch 'main' into examples/prisma
tknickman 5b57c35
Merge branch 'main' into examples/prisma
NuroDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Environment variables declared in this file are automatically made available to Prisma. | ||
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema | ||
|
||
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB (Preview) and CockroachDB (Preview). | ||
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings | ||
|
||
# Defaults to the local database deployed from the `docker-compose.yml` stack | ||
|
||
DATABASE_URL="mysql://root@127.0.0.1:3306/turborepo" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("./packages/config/eslint-preset.js"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
node_modules | ||
.pnp | ||
.pnp.js | ||
|
||
# testing | ||
coverage | ||
|
||
# next.js | ||
.next/ | ||
out/ | ||
build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
!packages/database/.env | ||
|
||
# turbo | ||
.turbo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# Turborepo starter | ||
|
||
This is an official starter turborepo. | ||
|
||
## What's inside? | ||
|
||
This turborepo includes the following packages/apps: | ||
|
||
### Apps and Packages | ||
|
||
- `web`: another [Next.js](https://nextjs.org) app | ||
- `config`: `eslint` configurations (includes `eslint-config-next` and `eslint-config-prettier`) | ||
- `database`: [Prisma](https://prisma.io/) ORM wrapper to manage & access your database | ||
- `tsconfig`: `tsconfig.json`s used throughout the monorepo | ||
|
||
Each package/app is 100% [TypeScript](https://www.typescriptlang.org/). | ||
|
||
### Utilities | ||
|
||
This turborepo has some additional tools already setup for you: | ||
|
||
- [TypeScript](https://www.typescriptlang.org/) for static type checking | ||
- [ESLint](https://eslint.org/) for code linting | ||
- [Prettier](https://prettier.io) for code formatting | ||
- [Prisma](https://prisma.io/) for database ORM | ||
- [Docker Compose](https://docs.docker.com/compose/) for local database | ||
|
||
### Database | ||
|
||
We use [Prisma](https://prisma.io/) to manage & access our database. As such you will need a database for this project, either locally or hosted in the cloud. | ||
|
||
To make this process easier, we offer a [`docker-compose.yml`](https://docs.docker.com/compose/) file to deploy a MySQL server locally with a new database named `turborepo` (To change this update the `MYSQL_DATABASE` environment variable in the `docker-compose.yml` file): | ||
|
||
```bash | ||
cd my-turborepo | ||
docker-compose up -d | ||
``` | ||
|
||
Once deployed you will need to copy the `.env.example` file to `.env` in order for Prisma to have a `DATABASE_URL` environment variable to access. | ||
|
||
```bash | ||
cp .env.example .env | ||
``` | ||
|
||
If you added a custom database name, or use a cloud based database, you will need to update the `DATABASE_URL` in your `.env` accordingly. | ||
|
||
Once deployed & up & running, you will need to create & deploy migrations to your database to add the necessary tables. This can be done using [Prisma Migrate](https://www.prisma.io/migrate): | ||
NuroDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```bash | ||
npx prisma migrate dev | ||
``` | ||
|
||
If you need to push any existing migrations to the database, you can use either the Prisma db push or the Prisma migrate deploy command(s): | ||
|
||
```bash | ||
yarn run db:push | ||
|
||
# OR | ||
|
||
yarn run db:migrate:deploy | ||
``` | ||
|
||
There is slight difference between the two commands & [Prisma offers a breakdown on which command is best to use](https://www.prisma.io/docs/concepts/components/prisma-migrate/db-push#choosing-db-push-or-prisma-migrate). | ||
|
||
An optional additional step is to seed some initial or fake data to your database using [Prisma's seeding functionality](https://www.prisma.io/docs/guides/database/seed-database). | ||
|
||
To do this update check the seed script located in `packages/database/src/seed.ts` & add or update any users you wish to seed to the database. | ||
|
||
Once edited run the following command to run tell Prisma to run the seed script defined in the Prisma configuration: | ||
|
||
```bash | ||
yarn run db:seed | ||
``` | ||
|
||
For further more information on migrations, seeding & more, we recommend reading through the [Prisma Documentation](https://www.prisma.io/docs/). | ||
|
||
### Build | ||
|
||
To build all apps and packages, run the following command: | ||
|
||
```bash | ||
cd my-turborepo | ||
yarn run build | ||
``` | ||
|
||
### Develop | ||
|
||
To develop all apps and packages, run the following command: | ||
|
||
```bash | ||
cd my-turborepo | ||
yarn run dev | ||
``` | ||
|
||
## Useful Links | ||
|
||
Learn more about the power of Turborepo: | ||
|
||
- [Pipelines](https://turborepo.org/docs/features/pipelines) | ||
- [Caching](https://turborepo.org/docs/features/caching) | ||
- [Remote Caching (Beta)](https://turborepo.org/docs/features/remote-caching) | ||
- [Scoped Tasks](https://turborepo.org/docs/features/scopes) | ||
- [Configuration Options](https://turborepo.org/docs/reference/configuration) | ||
- [CLI Usage](https://turborepo.org/docs/reference/command-line-reference) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
# vercel | ||
.vercel |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
## Getting Started | ||
|
||
First, run the development server: | ||
|
||
```bash | ||
yarn dev | ||
``` | ||
|
||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. | ||
|
||
You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file. | ||
|
||
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`. | ||
|
||
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages. | ||
|
||
## Learn More | ||
|
||
To learn more about Next.js, take a look at the following resources: | ||
|
||
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. | ||
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. | ||
|
||
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! | ||
|
||
## Deploy on Vercel | ||
|
||
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_source=github.com&utm_medium=referral&utm_campaign=turborepo-readme) from the creators of Next.js. | ||
|
||
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
module.exports = { | ||
reactStrictMode: true, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"private": true, | ||
"name": "web", | ||
"version": "1.0.0", | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start", | ||
"lint": "next lint" | ||
}, | ||
"dependencies": { | ||
"next": "^12.1.3", | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^17.0.12", | ||
"@types/react": "^17.0.37", | ||
"@types/react-dom": "^17.0.11", | ||
"config": "*", | ||
"database": "*", | ||
"eslint": "^7.32.0", | ||
"tsconfig": "*", | ||
"typescript": "^4.5.3" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { prisma } from "database"; | ||
|
||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
/** | ||
* Users | ||
* | ||
* @description A basic API endpoint to retrieve all the users in the database | ||
*/ | ||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
if (req.method !== "GET") { | ||
res.setHeader("Allow", ["GET"]); | ||
return res.status(405).end(`Method ${req.method} Not Allowed`); | ||
} | ||
|
||
try { | ||
const users = await prisma.user.findMany(); | ||
if (!users) | ||
throw { | ||
message: "Failed to retrieve users", | ||
status: 500, | ||
}; | ||
|
||
return res.status(200).json({ | ||
users, | ||
}); | ||
} catch ({ message = "An unknown error occured", status = 500 }) { | ||
console.error({ message, status }); | ||
return res.status(status).end(message); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default function IndexPage() { | ||
return ( | ||
<div> | ||
<h1>Hello World</h1> | ||
<a href="/api/users">View Users</a> | ||
</div> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "tsconfig/nextjs.json", | ||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], | ||
"exclude": ["node_modules"] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
version: "3" | ||
|
||
volumes: | ||
database: | ||
driver: local | ||
|
||
services: | ||
mysql: | ||
platform: linux/amd64 | ||
image: mysql:8.0.28 | ||
container_name: turborepo_mysql | ||
restart: always | ||
ports: | ||
- 3306:3306 | ||
environment: | ||
MYSQL_DATABASE: turborepo | ||
MYSQL_ALLOW_EMPTY_PASSWORD: 1 | ||
volumes: | ||
- database:/var/lib/mysql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"private": true, | ||
"packageManager": "pnpm@7", | ||
"workspaces": [ | ||
"apps/*", | ||
"packages/*" | ||
], | ||
"prisma": { | ||
"schema": "packages/database/prisma/schema.prisma", | ||
"seed": "tsx packages/database/src/seed.ts" | ||
}, | ||
"scripts": { | ||
"build": "turbo run build", | ||
"db:migrate:deploy": "turbo run db:migrate:deploy", | ||
"db:push": "turbo run db:push", | ||
"db:seed": "turbo run db:seed", | ||
"dev": "turbo run dev --parallel", | ||
"format": "prettier --write \"**/*.{ts,tsx,md}\"", | ||
"generate": "turbo run generate", | ||
"lint": "turbo run lint" | ||
}, | ||
"devDependencies": { | ||
"prettier": "^2.5.1", | ||
"tsx": "^3.7.1", | ||
"turbo": "latest" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = { | ||
extends: ["next", "prettier"], | ||
settings: { | ||
next: { | ||
rootDir: ["apps/*/", "packages/*/"], | ||
}, | ||
}, | ||
rules: { | ||
"@next/next/no-html-link-for-pages": "off", | ||
"react/jsx-key": "off", | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "config", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"files": [ | ||
"eslint-preset.js" | ||
], | ||
"dependencies": { | ||
"eslint-config-next": "^12.0.8", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-react": "7.28.0" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^4.5.3" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../.env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("config/eslint-preset"); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.