Skip to content

Commit 888b147

Browse files
fix: Create you first CRUD Step 1
1 parent 40a7979 commit 888b147

File tree

8 files changed

+80
-69
lines changed

8 files changed

+80
-69
lines changed

pnpm-workspace.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
onlyBuiltDependencies:
2+
- esbuild
3+
- sharp
873 KB
Loading
831 KB
Loading
860 KB
Loading
950 KB
Loading

src/content/docs/tutorials/create-your-first-crud.mdx

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Create your first CRUD 🚧
33
---
44

5-
import WorkInProgress from "../../../components/WorkInProgress.astro";
5+
import WorkInProgress from "@/components/WorkInProgress.astro";
66
import { Aside, Steps, FileTree } from "@astrojs/starlight/components";
77

88
<WorkInProgress />
@@ -12,104 +12,110 @@ Make sure you followed the [Getting Started](/getting-started) before starting t
1212
Let's dive into creating a full new entity with database, backend and ui.<br />
1313
We will create a **"Project"** entity with the full CRUD (Create Read Update Delete) screens.
1414

15-
## Step 1: Create the Project database schema
15+
## Step 1: Create the _Project_ model in the schema
1616

1717
<Steps>
1818

1919
1. Update the Prisma Database Schema
2020

21-
We will use [Prisma](https://www.prisma.io/docs/concepts/components/prisma-schema/data-model) to add the project entity to our database schema.<br />
22-
23-
Because we are creating a new entity, we need to create a new file to store all details related to the new `Project` model.
21+
We will use [Prisma](https://www.prisma.io/docs/concepts/components/prisma-schema/data-model) to add the _Project_ model to our database schema.
2422

2523
Update the `prisma/schema.prisma` file and add a new model called `Project` with the following fields.
2624

2725
```prisma filename="prisma/schema.prisma"
2826
model Project {
29-
id String @id @default(cuid())
30-
createdAt DateTime @default(now())
31-
updatedAt DateTime @updatedAt
32-
name String @unique
33-
description String?
27+
id String @id @default(cuid())
28+
createdAt DateTime @default(now())
29+
updatedAt DateTime @updatedAt
30+
name String @unique
31+
description String?
3432
}
3533
```
3634

37-
Cut the running `pnpm dev{:bash}` if needed, and then run the `pnpm db:push{:bash}` command to update your database.
38-
39-
You can run `pnpm dev{:bash}` again.
35+
Run `pnpm db:push` command to update your database.
4036

4137
2. Create you first project
4238

43-
We can see what is inside of our database with the `pnpm db:ui{:bash}` command.<br />
39+
We can see what is inside of our database with the `pnpm db:ui` command (accessible on <a href="http://localhost:5555" target="_blank">localhost:5555</a>)
4440
You should see your `Project` model and be able to create a new project like the following.
4541

46-
![Step 01](/images/tutorials/new/db-ui-01.png)<br />
47-
![Step 02](/images/tutorials/new/db-ui-02.png)<br />
48-
![Step 03](/images/tutorials/new/db-ui-03.png)<br />
42+
![Prisma UI, select Project modal](@/assets/tutorials/create-your-first-crud/db-ui-1.png)
43+
![Prisma UI, select Add Record](@/assets/tutorials/create-your-first-crud/db-ui-2.png)
44+
![Prisma UI, create Record with name Start UI [web]](@/assets/tutorials/create-your-first-crud/db-ui-3.png)
4945

50-
3. Create database seeds
46+
3. Create fake data using database seeds
5147

52-
For easy development and better Developer eXperience (DX), we will create a new seed for our new `Project` model.<br />
48+
For a better Developer eXperience (DX), we will create a new seed for our new `Project` model.
5349
This will allow every new developer to start with some projects instead of an empty database.
5450

5551
Create an new file `project.ts` in the `prisma/seed` folder with a `createProjects` function.
5652

53+
<FileTree>
54+
55+
- prisma
56+
- seed
57+
- _utils.ts
58+
- index.ts
59+
- **project.ts**
60+
- ...
61+
- src/
62+
- package.json
63+
64+
</FileTree>
65+
5766
```ts filename="prisma/seed/project.ts" showLineNumbers
5867
export async function createProjects() {
59-
// ...
68+
6069
}
6170
```
6271

6372
Add a console.log for better DX.
6473

6574
```ts {2} filename="prisma/seed/project.ts" showLineNumbers
6675
export async function createProjects() {
67-
console.log(`⏳ Seeding projects`);
68-
// ...
76+
console.log(`⏳ Seeding projects`);
6977
}
7078
```
7179

7280
Get existing projects. It will help us to make the seed idempotent.
7381

7482
```ts {1, 5-6} filename="prisma/seed/project.ts" showLineNumbers
75-
import { prisma } from "prisma/seed/utils";
83+
import { db } from '@/server/db';
7684

7785
export async function createProjects() {
78-
console.log(`⏳ Seeding projects`);
86+
console.log(`⏳ Seeding projects`);
7987

80-
const existingProjects = await db.project.findMany();
88+
const existingProjects = await db.project.findMany();
8189
}
8290
```
8391

8492
Create the projects with prisma.
8593

86-
```ts {7-26} filename="prisma/seed/project.ts" showLineNumbers
87-
import { db } from "@/server/db";
88-
94+
```ts {6-25} filename="prisma/seed/project.ts" showLineNumbers
8995
export async function createProjects() {
90-
console.log(`⏳ Seeding projects`);
91-
92-
const existingProjects = await db.project.findMany();
93-
94-
const projects = [
95-
{ name: "Project 1" },
96-
{ name: "Project 2" },
97-
{ name: "Project 3" },
98-
] as const;
99-
100-
const result = await db.project.createMany({
101-
data: projects
102-
.filter(
103-
(project) =>
104-
!existingProjects
105-
.map((existingProject) => existingProject.name)
106-
.includes(project.name)
107-
)
108-
.map(({ name }) => ({ name })),
109-
});
110-
console.log(
111-
`✅ ${existingProjects.length} existing projects 👉 ${result.count} projects created`
112-
);
96+
console.log(`⏳ Seeding projects`);
97+
98+
const existingProjects = await db.project.findMany();
99+
100+
const projects = [
101+
{ name: 'Project 1' },
102+
{ name: 'Project 2' },
103+
{ name: 'Project 3' },
104+
] as const;
105+
106+
const result = await db.project.createMany({
107+
data: projects
108+
.filter(
109+
(project) =>
110+
!existingProjects
111+
.map((existingProject) => existingProject.name)
112+
.includes(project.name)
113+
)
114+
.map(({ name }) => ({ name })),
115+
});
116+
console.log(
117+
`✅ ${existingProjects.length} existing projects 👉 ${result.count} projects created`
118+
);
113119
}
114120
```
115121

@@ -123,31 +129,25 @@ We will create a **"Project"** entity with the full CRUD (Create Read Update Del
123129
import { createUsers } from "./user";
124130

125131
async function main() {
126-
await createBooks();
127-
await createProjects();
128-
await createUsers();
132+
await createBooks();
133+
await createProjects();
134+
await createUsers();
129135
}
130136

131137
main()
132-
.catch((e) => {
138+
.catch((e) => {
133139
console.error(e);
134140
process.exit(1);
135-
})
136-
.finally(() => {
141+
})
142+
.finally(() => {
137143
db.$disconnect();
138-
});
144+
});
139145
```
140146

141-
Finally, run the seed command.
142-
143-
```bash
144-
pnpm db:seed
145-
```
146-
147-
You can check that the project is created by running the `pnpm db:ui{:bash}` command again.
148-
149-
![Seed result](/images/tutorials/new/db-seed-result.png)<br />
147+
Finally, run `pnpm db:seed` to populate the database.
148+
Remember that you can check your database through the Prisma UI (`pnpm db:ui`).
150149

150+
![Seed result](@/assets/tutorials/create-your-first-crud/db-seed-result.png)<br />
151151
</Steps>
152152

153153
---

src/content/links.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { StarlightUserConfig } from "@astrojs/starlight/types";
2+
13
export const socials = [
24
{
35
icon: "github",
@@ -9,7 +11,7 @@ export const socials = [
911
label: "Discord",
1012
href: "https://go.bearstudio.fr/discord",
1113
},
12-
] as const;
14+
] satisfies StarlightUserConfig["social"];
1315

1416
export const extraLinks = [
1517
{

tsconfig.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
{
22
"extends": "astro/tsconfigs/strict",
33
"include": [".astro/types.d.ts", "**/*"],
4-
"compilerOptions": {},
4+
"compilerOptions": {
5+
"baseUrl": ".",
6+
"paths": {
7+
"@/components/*": ["src/components/*"],
8+
"@/assets/*": ["src/assets/*"]
9+
}
10+
},
511
"exclude": ["dist"]
612
}

0 commit comments

Comments
 (0)