-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathData Modeling Basics.txt
More file actions
133 lines (101 loc) · 2.62 KB
/
Data Modeling Basics.txt
File metadata and controls
133 lines (101 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Data Modeling Basics
Tooling
---------
- Prisma is the ORM of choice when dealing with Javascript
// Example schema.prisma file
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
// example Prisma schema
model User {
id String @id @default(cuid())
email String @unique
username String @unique
name String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Prisma Commands
--------------------------
npx prisma init --url file:./data.db
npx prisma db push
npx prisma db seed // invokes seed.ts in your folder
npx prisma migrate dev // initiates a migration an generates the necessary sql needed to get from point A -> point B
npx primsa migrate deploy
npx prisma studio // launches brower UI to inspect your database
Understanding Relationships
--------------------------------------
[ One-to-One ]
[ One-to-Many ]
[ Many-to-Many ]
Seeding Data
------------------
// seed.ts
import fs from 'node:fs'
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
// delete all users and notes in the database
await prisma.user.deleteMany()
// create a new user
const kody = await prisma.user.create({
data: {
email: 'kody@kcd.dev',
username: 'kody',
name: 'Kody',
},
})
await prisma.note.create({
data: {
id: 'd27a197e',
title: 'Basic Koala Facts',
content:
'Koalas are found in the eucalyptus forests of eastern Australia. They have grey fur with a cream-coloured chest, and strong, clawed feet, perfect for living in the branches of trees!',
ownerId: kody.id,
images: {
create: [
{
altText: 'an adorable koala cartoon illustration',
contentType: 'image/png',
blob: await fs.promises.readFile(
'./tests/fixtures/images/kody-notes/cute-koala.png',
),
},
{
altText: 'a cartoon illustration of a koala in a tree eating',
contentType: 'image/png',
blob: await fs.promises.readFile(
'./tests/fixtures/images/kody-notes/koala-eating.png',
),
},
],
},
},
})
Generating Fake Data for Seeding
----------------------------------------------
- Faker is a great library for generating reputable fake seed data
// Faker example in seed.ts file
import { faker } from '@faker-js/faker'
await prisma.user.create({
data: {
email: faker.internet.email(),
username: faker.internet.userName(),
name: faker.person.fullName(),
notes: {
create: [
{
title: faker.lorem.sentence(),
content: faker.lorem.paragraph(),
},
{
title: faker.lorem.sentence(),
content: faker.lorem.paragraph(),
},
],
},
},
})