/
schema.prisma
322 lines (266 loc) · 9.52 KB
/
schema.prisma
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
// output = "./generated/client"
// previewFeatures = ["views", "extendedWhereUnique"]
previewFeatures = ["views"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
// GENERATOR CONFIGURATION
// -----------------------------------------------
generator zod {
provider = "ts-node-dev ../generator/src/bin.ts"
// provider = "zod-prisma-types"
output = "./generated/zod" // default is ./generated/zod
// useMultipleFiles = true // default is false
// createInputTypes = false // default is true
// createModelTypes = false // default is true
// addInputTypeValidation = false // default is true
// addIncludeType = false // default is true
// addSelectType = false // default is true
// validateWhereUniqueInput = true // default is false
createOptionalDefaultValuesTypes = true // default is false
createRelationValuesTypes = true // default is false
createPartialTypes = true // default is false
// useDefaultValidators = true // default is true
// coerceDate = true // default is true
// writeNullishInModelTypes = false // default is false
// prismaClientPath = "./generated/client" // optional
}
generator zodPrismaGen {
provider = "prisma-zod-generator"
output = "./generated/zodPrisma"
}
// MIXED CASE ENUMS AND MODELS
// -----------------------------------------------
// Check if EnumS and modelS with mixed casing work
enum MYValue {
A
B
C
}
model MODELWithUpperCase {
id Int @id @default(autoincrement())
STRING String
MYValue MYValue
}
// OMITTED FIELDS
// -----------------------------------------------
// Omitted fields should not be included in the generated model or input schemas
// Furthermore, for the required field the generated arg schema should have an updated z.ZodType<...> type
model ModelWithOmitFields {
id String @id @default(cuid())
string String?
omitField String? /// @zod.custom.omit([model, input])
omitRequired String /// @zod.custom.omit([model, input])
}
// JS DOC COMMENTS
// -----------------------------------------------
// JSDoc comments should be included in the generated model and input schemas
// The validator comments should be removed from the generated model and input schemas
/// comment line one
/// comment line two
model ModelWithComments {
id String @id @default(uuid())
/// comment before validator @zod.string.min(4).max(10)
/// comment after validator
string String?
omitField String? /// @zod.custom.omit([model, input])
omitRequired String /// @zod.custom.omit([model, input])
}
// CUSTOM VALIDATORS ON SCALARS
// -----------------------------------------------
// Custom validators should be included in the generated model and input schemas
// It should not matter if the comments are above or next to the field
/// comment line one
/// @zod.import(["import { myFunction } from "../../../../utils/myFunction";", "import validator from 'validator';"])
/// comment line two
model MyPrismaScalarsType {
/// @zod.string({ invalid_type_error: "some error with special chars: some + -*#'substring[]*#!§$%&/{}[]", required_error: "some other", description: "some description" }).cuid()
id String @id @default(cuid())
/// Some comment about string @zod.string.min(3, { message: "min error" }).max(10, { message: "max error" })
string String?
/// @zod.custom.use(z.string().refine((val) => validator.isBIC(val), { message: 'BIC is not valid' }))
bic String?
/// @zod.number.lt(10, { message: "lt error" }).gt(5, { message: "gt error" })
float Float
floatOpt Float?
/// @zod.number.int({ message: "error" }).gt(5, { message: "gt error" })
int Int
intOpt Int?
decimal Decimal
decimalOpt Decimal?
date DateTime @default(now())
dateOpt DateTime? /// @zod.date({ invalid_type_error: "wrong date type" })
bigInt BigInt /// @zod.bigint({ invalid_type_error: "error" }).gt(5n, { message: "gt error" })
bigIntOpt BigInt?
/// @zod.custom.use(z.lazy(() => InputJsonValue).refine((val) => myFunction(val), { message: 'Is not valid' }))
json Json
jsonOpt Json?
bytes Bytes /// @zod.custom.use(z.instanceof(Buffer).refine((val) => val ? true : false, { message: 'Value is not valid' }))
bytesOpt Bytes?
/// @zod.custom.use(z.string().refine((val) => myFunction(val), { message: 'Is not valid' }))
custom String?
exclude String? /// @zod.custom.omit(["model", "input"])
updatedAt DateTime @updatedAt
}
// JSON MODEL
// -----------------------------------------------
// Used for testing if the Json null types are correctly transformed
model JsonModel {
id Int @id @default(autoincrement())
json Json
jsonOpt Json?
}
// STANDARD BLOG SCHEMA
// -----------------------------------------------
// This is the standard blog schema from the Prisma docs extended with some custom enums and models
// https://www.prisma.io/docs/concepts/components/prisma-schema/data-model#defining-a-model
enum Role {
USER
ADMIN
}
enum SecondEnum {
ONE
TWO
}
enum AnotherEnum {
ONE
TWO
}
model User {
id String @id @default(cuid()) /// @zod.string.cuid()
email String @unique /// @zod.string.email({ message: "Invalid email address" })
/// some other comment
/// @zod.string.min(1).max(100) some message after
name String?
/// @zod.object.array(.length(2, { message: "error" }).min(1).max(2).nonempty({ message: "error" }))
posts Post[]
profile Profile?
role Role[] @default([USER, ADMIN])
enum AnotherEnum @default(ONE)
scalarList String[]
lat Float
lng Float
location Location? @relation(fields: [lat, lng], references: [lat, lng])
}
/// @zod.import(["import { myFunction } from "../../../../utils/myFunction";"])
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
jsonOpt Json?
anotherJsonOpt Json?
anotherEnum AnotherEnum[] /// @zod.enum.array(.length(2, { message: "error" }).min(1).max(myFunction.some).nonempty({ message: "error" }))
}
model Profile {
id Int @id @default(autoincrement())
bio String
user User @relation(fields: [userId], references: [id])
userId String @unique
role Role[] @default([USER, ADMIN])
second SecondEnum @default(ONE)
}
model Location {
lat Float
lng Float
User User[]
@@id([lat, lng])
}
model NonDefaultModel {
id Int @id
string String
}
model WithDefaultValidators {
id String @id @default(cuid())
idTwo String @default(uuid()) /// @zod.string.noDefault()
integer Int
}
model DecimalModel {
id Int @id @default(autoincrement())
decimal Decimal
decimalOpt Decimal?
}
model DateModel {
id Int @id @default(autoincrement())
date DateTime
dateOpt DateTime?
}
// STRING REGEX VALIDATOR
// -----------------------------------------------
// Used for testing if the string regex validator is correctly transformed
/// @zod.import(["import * as s from '$lib/schemas'"])
model StringRegexModel {
id Int @id @default(autoincrement())
string String /// @zod.string.regex(/^[a-z0-9]+$/i, { message: "error" })
stringTwo String /// @zod.string.regex(/^\\d+\\s*\\d+$/)
stringThree String /// @zod.string.regex(/^d+s*d+$/)
}
// WRONG MODEL LINKED TO INPUT TYPES
// -----------------------------------------------
// This model covers the following case:
// input type: "UserUncheckedCreateWithoutPractice" should match model "User"
// but matches model "Practice" because practice comes first in the models array.
model Practice {
id Int @id @unique() @default(autoincrement())
profilePhoto Int?
user UserTwo[]
}
model UserTwo {
id Int @id @unique() @default(autoincrement())
name String?
profilePhoto String? @db.Text
practiceId Int?
practice Practice? @relation(fields: [practiceId], references: [id])
}
// model Post {
// id String @id @default(auto()) @map("_id") @db.ObjectId
// title String @default("")
// data PostData
// }
// type PostData {
// something String
// somethingElse Int
// someType SomeType
// }
// type SomeType {
// someField String
// }
// type ModeratedContentFile {
// mimetype String
// url String
// }
// model ModeratedContent {
// id String @id @map("_id")
// v Int @map("__v")
// files ModeratedContentFile[]
// }
// view StatisticActiveUsers {
// date DateTime @unique @db.Date
// count Int
// }
// model User {
// id Int @id @default(autoincrement())
// email String @unique
// name String
// posts Post[]
// }
// model Post {
// id Int @id @default(autoincrement())
// title String
// content String?
// author User @relation(fields: [authorId], references: [id])
// authorId Int
// }
// view AbcUser {
// date DateTime @unique @db.Date
// count Int
// }
//