Skip to content

Commit

Permalink
Fixed formatting, bumped all dependencies to newer versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Xithrius committed Sep 14, 2023
1 parent ad586ac commit c18a1f2
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 530 deletions.
9 changes: 3 additions & 6 deletions site/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ npx prisma migrate dev --name init

When `npx prisma migrate dev` is executed against a newly created database, seeding is also triggered. The seed file in [`prisma/seed.ts`](./prisma/seed.ts) will be executed and your database will be populated with the sample data.


### 3. Configuring your authentication provider

In order to get this example to work, you need to configure the [GitHub](https://next-auth.js.org/providers/github) and/or [Email](https://next-auth.js.org/providers/email) authentication providers from NextAuth.js.
Expand Down Expand Up @@ -120,8 +119,8 @@ model Post {
}

model User {
id Int @default(autoincrement()) @id
name String?
id Int @default(autoincrement()) @id
name String?
email String @unique
posts Post[]
+ profile Profile?
Expand Down Expand Up @@ -189,17 +188,15 @@ const userWithUpdatedProfile = await prisma.user.update({
});
```


### 3. Build new UI features in React

Once you have added a new endpoint to the API (e.g. `/api/profile` with `/POST`, `/PUT` and `GET` operations), you can start building a new UI component in React. It could e.g. be called `profile.tsx` and would be located in the `pages` directory.

In the application code, you can access the new endpoint via `fetch` operations and populate the UI with the data you receive from the API calls.


## Switch to another database (e.g. PostgreSQL, MySQL, SQL Server, MongoDB)

If you want to try this example with another database than SQLite, you can adjust the the database connection in [`prisma/schema.prisma`](./prisma/schema.prisma) by reconfiguring the `datasource` block.
If you want to try this example with another database than SQLite, you can adjust the the database connection in [`prisma/schema.prisma`](./prisma/schema.prisma) by reconfiguring the `datasource` block.

Learn more about the different connection configurations in the [docs](https://www.prisma.io/docs/reference/database-reference/connection-urls).

Expand Down
7 changes: 3 additions & 4 deletions site/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { signOut, useSession } from "next-auth/react";

const Header: React.FC = () => {
const router = useRouter();
const isActive: (pathname: string) => boolean = (pathname) =>
router.pathname === pathname;
const isActive: (pathname: string) => boolean = (pathname) => router.pathname === pathname;

const {data: session, status} = useSession();
const { data: session, status } = useSession();

let left = (
<div className="left">
Expand Down Expand Up @@ -41,7 +40,7 @@ const Header: React.FC = () => {

let right = null;

if (status === 'loading') {
if (status === "loading") {
left = (
<div className="left">
<Link href="/" legacyBehavior>
Expand Down
5 changes: 2 additions & 3 deletions site/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ const Layout: React.FC<Props> = (props) => (
margin: 0;
padding: 0;
font-size: 16px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji",
"Segoe UI Symbol";
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
background: rgba(0, 0, 0, 0.05);
}
Expand Down
4 changes: 2 additions & 2 deletions site/next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const { withSuperjson } = require('next-superjson')
const { withSuperjson } = require("next-superjson");

module.exports = withSuperjson()({})
module.exports = withSuperjson()({});
33 changes: 16 additions & 17 deletions site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@
"superjson": "1.13.1"
},
"devDependencies": {
"@types/next-auth": "3.13.0",
"@types/node": "18.17.15",
"@types/react": "18.2.21",
"prisma": "5.3.0",
"ts-node": "10.9.1",
"@types/react-dom": "17.0.15",
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/eslint-plugin-tslint": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"autoprefixer": "^10.4.14",
"@types/node": "^20.6.0",
"@types/react": "^18.2.21",
"prisma": "^5.3.0",
"ts-node": "^10.9.1",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/eslint-plugin-tslint": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"autoprefixer": "^10.4.15",
"cssnano": "^6.0.1",
"eslint": "^8.40.0",
"eslint-config-next": "^13.4.1",
"postcss": "^8.4.23",
"prettier": "^2.8.8",
"prettier-plugin-tailwindcss": "^0.2.8",
"tailwindcss": "^3.3.2",
"typescript": "^5.0.4"
"eslint": "^8.49.0",
"eslint-config-next": "^13.4.19",
"postcss": "^8.4.29",
"prettier": "^3.0.3",
"prettier-plugin-tailwindcss": "^0.5.4",
"tailwindcss": "^3.3.3",
"typescript": "^5.2.2"
},
"prisma": {
"seed": "ts-node prisma/seed.ts"
Expand Down
15 changes: 6 additions & 9 deletions site/pages/api/post/[id].ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import { getSession } from 'next-auth/react';
import prisma from '../../../lib/prisma'

import type { NextApiRequest, NextApiResponse } from "next";
import { getSession } from "next-auth/react";
import prisma from "../../../lib/prisma";

// DELETE /api/post/:id
export default async function handle(req: NextApiRequest, res: NextApiResponse) {
const postId = req.query.id;

const session = await getSession({ req })
const session = await getSession({ req });

if (req.method === "DELETE") {
if (session) {
Expand All @@ -16,11 +15,9 @@ export default async function handle(req: NextApiRequest, res: NextApiResponse)
});
res.json(post);
} else {
res.status(401).send({ message: 'Unauthorized' })
res.status(401).send({ message: "Unauthorized" });
}
} else {
throw new Error(
`The HTTP ${req.method} method is not supported at this route.`
);
throw new Error(`The HTTP ${req.method} method is not supported at this route.`);
}
}
9 changes: 4 additions & 5 deletions site/pages/api/post/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import prisma from '../../../lib/prisma'
import { getSession } from 'next-auth/react'

import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "../../../lib/prisma";
import { getSession } from "next-auth/react";

// POST /api/post
// Required fields in body: title
Expand All @@ -20,6 +19,6 @@ export default async function handle(req: NextApiRequest, res: NextApiResponse)
});
res.json(result);
} else {
res.status(401).send({ message: 'Unauthorized' })
res.status(401).send({ message: "Unauthorized" });
}
}
10 changes: 5 additions & 5 deletions site/pages/api/publish/[id].ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import { getSession } from 'next-auth/react';
import prisma from '../../../lib/prisma'
import type { NextApiRequest, NextApiResponse } from "next";
import { getSession } from "next-auth/react";
import prisma from "../../../lib/prisma";

// PUT /api/publish/:id
export default async function handle(req: NextApiRequest, res: NextApiResponse) {
const postId = req.query.id;
const session = await getSession({ req })
const session = await getSession({ req });

if (session) {
const post = await prisma.post.update({
Expand All @@ -14,6 +14,6 @@ export default async function handle(req: NextApiRequest, res: NextApiResponse)
});
res.json(post);
} else {
res.status(401).send({ message: 'Unauthorized' })
res.status(401).send({ message: "Unauthorized" });
}
}
5 changes: 2 additions & 3 deletions site/pages/drafts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { GetServerSideProps } from "next";
import Layout from "../components/Layout";
import Post, { PostProps } from "../components/Post";
import { useSession, getSession } from "next-auth/react";
import prisma from '../lib/prisma'

import prisma from "../lib/prisma";

export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
const session = await getSession({ req });
Expand Down Expand Up @@ -34,7 +33,7 @@ type Props = {
};

const Drafts: React.FC<Props> = (props) => {
const {data: session}= useSession();
const { data: session } = useSession();

if (!session) {
return (
Expand Down
2 changes: 1 addition & 1 deletion site/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import type { GetServerSideProps } from "next";
import Layout from "../components/Layout";
import Post, { PostProps } from "../components/Post";
import prisma from '../lib/prisma'
import prisma from "../lib/prisma";

export const getServerSideProps: GetServerSideProps = async () => {
const feed = await prisma.post.findMany({
Expand Down
9 changes: 4 additions & 5 deletions site/pages/p/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import ReactMarkdown from "react-markdown";
import Layout from "../../components/Layout";
import Router from "next/router";
import { PostProps } from "../../components/Post";
import prisma from '../../lib/prisma'
import prisma from "../../lib/prisma";
import { useSession } from "next-auth/react";


export const getServerSideProps: GetServerSideProps = async ({ params }) => {
const post = await prisma.post.findUnique({
where: {
Expand All @@ -28,19 +27,19 @@ async function publishPost(id: number): Promise<void> {
await fetch(`/api/publish/${id}`, {
method: "PUT",
});
await Router.push("/")
await Router.push("/");
}

async function deletePost(id: number): Promise<void> {
await fetch(`/api/post/${id}`, {
method: "DELETE",
});
await Router.push("/")
await Router.push("/");
}

const Post: React.FC<PostProps> = (props) => {
const { data: session, status } = useSession();
if (status === 'loading') {
if (status === "loading") {
return <div>Authenticating ...</div>;
}
const userHasValidSession = Boolean(session);
Expand Down
52 changes: 26 additions & 26 deletions site/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
import { PrismaClient, Prisma } from '@prisma/client'
import { PrismaClient, Prisma } from "@prisma/client";

const prisma = new PrismaClient()
const prisma = new PrismaClient();

const userData: Prisma.UserCreateInput[] = [
{
name: 'Alice',
email: 'alice@prisma.io',
name: "Alice",
email: "alice@prisma.io",
posts: {
create: [
{
title: 'Join the Prisma Slack',
content: 'https://slack.prisma.io',
title: "Join the Prisma Slack",
content: "https://slack.prisma.io",
published: true,
},
],
},
},
{
name: 'Nilu',
email: 'nilu@prisma.io',
name: "Nilu",
email: "nilu@prisma.io",
posts: {
create: [
{
title: 'Follow Prisma on Twitter',
content: 'https://www.twitter.com/prisma',
title: "Follow Prisma on Twitter",
content: "https://www.twitter.com/prisma",
published: true,
},
],
},
},
{
name: 'Mahmoud',
email: 'mahmoud@prisma.io',
name: "Mahmoud",
email: "mahmoud@prisma.io",
posts: {
create: [
{
title: 'Ask a question about Prisma on GitHub',
content: 'https://www.github.com/prisma/prisma/discussions',
title: "Ask a question about Prisma on GitHub",
content: "https://www.github.com/prisma/prisma/discussions",
published: true,
},
{
title: 'Prisma on YouTube',
content: 'https://pris.ly/youtube',
title: "Prisma on YouTube",
content: "https://pris.ly/youtube",
},
],
},
},
]
];

async function main() {
console.log(`Start seeding ...`)
console.log(`Start seeding ...`);
for (const u of userData) {
const user = await prisma.user.create({
data: u,
})
console.log(`Created user with id: ${user.id}`)
});
console.log(`Created user with id: ${user.id}`);
}
console.log(`Seeding finished.`)
console.log(`Seeding finished.`);
}

main()
.then(async () => {
await prisma.$disconnect()
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})
console.error(e);
await prisma.$disconnect();
process.exit(1);
});
16 changes: 3 additions & 13 deletions site/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
Expand All @@ -19,14 +15,8 @@
"jsx": "preserve",
"incremental": true
},
"exclude": [
"node_modules"
],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": ["node_modules"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"ts-node": {
"compilerOptions": {
"module": "commonjs"
Expand Down
Loading

0 comments on commit c18a1f2

Please sign in to comment.