这是indexloc提供的服务,不要输入任何密码
Skip to content

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 20 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/with-prisma/.env.example
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"
1 change: 1 addition & 0 deletions examples/with-prisma/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./packages/config/eslint-preset.js");
33 changes: 33 additions & 0 deletions examples/with-prisma/.gitignore
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
104 changes: 104 additions & 0 deletions examples/with-prisma/README.md
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):

```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)
34 changes: 34 additions & 0 deletions examples/with-prisma/apps/web/.gitignore
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
30 changes: 30 additions & 0 deletions examples/with-prisma/apps/web/README.md
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.
5 changes: 5 additions & 0 deletions examples/with-prisma/apps/web/next-env.d.ts
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.
6 changes: 6 additions & 0 deletions examples/with-prisma/apps/web/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
module.exports = {
reactStrictMode: true,
};
26 changes: 26 additions & 0 deletions examples/with-prisma/apps/web/package.json
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"
}
}
34 changes: 34 additions & 0 deletions examples/with-prisma/apps/web/pages/api/users.ts
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);
}
}
8 changes: 8 additions & 0 deletions examples/with-prisma/apps/web/pages/index.tsx
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>
);
}
5 changes: 5 additions & 0 deletions examples/with-prisma/apps/web/tsconfig.json
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"]
}
19 changes: 19 additions & 0 deletions examples/with-prisma/docker-compose.yml
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
27 changes: 27 additions & 0 deletions examples/with-prisma/package.json
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"
}
}
12 changes: 12 additions & 0 deletions examples/with-prisma/packages/config/eslint-preset.js
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",
},
};
17 changes: 17 additions & 0 deletions examples/with-prisma/packages/config/package.json
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"
}
}
1 change: 1 addition & 0 deletions examples/with-prisma/packages/database/.env
1 change: 1 addition & 0 deletions examples/with-prisma/packages/database/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("config/eslint-preset");
Loading