Skip to content

Commit d3f82bb

Browse files
committed
Support db schema for users
1 parent 41e6374 commit d3f82bb

File tree

4 files changed

+189
-18
lines changed

4 files changed

+189
-18
lines changed

remix-jokes/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
"@remix-run/node": "^1.5.1",
1717
"@remix-run/react": "^1.5.1",
1818
"@remix-run/serve": "^1.5.1",
19+
"bcrypt": "^5.0.1",
1920
"prisma": "^3.14.0",
2021
"react": "^17.0.2",
2122
"react-dom": "^17.0.2"
2223
},
2324
"devDependencies": {
2425
"@remix-run/dev": "^1.5.1",
2526
"@remix-run/eslint-config": "^1.5.1",
27+
"@types/bcrypt": "^5.0.0",
2628
"@types/node": "^17.0.35",
2729
"@types/react": "^17.0.45",
2830
"@types/react-dom": "^17.0.17",

remix-jokes/prisma/schema.prisma

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,21 @@ datasource db {
1010
url = env("DATABASE_URL")
1111
}
1212

13+
model User {
14+
id String @id @default(uuid())
15+
createdAt DateTime @default(now())
16+
updatedAt DateTime @updatedAt
17+
username String @unique
18+
passwordHash String
19+
jokes Joke[]
20+
}
21+
1322
model Joke {
14-
id String @id @default(uuid())
15-
createdAt DateTime @default(now())
16-
updatedAt DateTime @updatedAt
17-
name String
18-
content String
23+
id String @id @default(uuid())
24+
jokesterId String
25+
jokester User @relation(fields: [jokesterId], references: [id], onDelete: Cascade)
26+
createdAt DateTime @default(now())
27+
updatedAt DateTime @updatedAt
28+
name String
29+
content String
1930
}

remix-jokes/prisma/seed.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PrismaClient } from "@prisma/client";
2+
import bcrypt from "bcrypt";
23

34
const db = new PrismaClient();
45

@@ -42,7 +43,20 @@ const getJokes = () => [
4243
];
4344

4445
const seed = async () => {
45-
await Promise.all(getJokes().map((joke) => db.joke.create({ data: joke })));
46+
// Hash a password
47+
const password = await bcrypt.hash("ankurpaul", 10);
48+
const kody = await db.user.create({
49+
data: {
50+
username: "ankurpaul",
51+
passwordHash: password,
52+
},
53+
});
54+
55+
await Promise.all(
56+
getJokes().map((joke) =>
57+
db.joke.create({ data: { jokesterId: kody.id, ...joke } })
58+
)
59+
);
4660
};
4761

4862
seed();

0 commit comments

Comments
 (0)