-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
dmmf.go
394 lines (345 loc) · 9.82 KB
/
dmmf.go
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package dmmf
import (
"github.com/prisma/prisma-client-go/generator/types"
)
// FieldKind describes a scalar, object or enum.
type FieldKind string
// FieldKind values
const (
FieldKindScalar FieldKind = "scalar"
FieldKindObject FieldKind = "object"
FieldKindEnum FieldKind = "enum"
)
// IncludeInStruct shows whether to include a field in a model struct.
func (v FieldKind) IncludeInStruct() bool {
return v == FieldKindScalar || v == FieldKindEnum
}
// IsRelation returns whether field is a relation
func (v FieldKind) IsRelation() bool {
return v == FieldKindObject
}
// DatamodelFieldKind describes a scalar, object or enum.
type DatamodelFieldKind string
// DatamodelFieldKind values
const (
DatamodelFieldKindScalar DatamodelFieldKind = "scalar"
DatamodelFieldKindRelation DatamodelFieldKind = "relation"
DatamodelFieldKindEnum DatamodelFieldKind = "enum"
)
// IncludeInStruct shows whether to include a field in a model struct.
func (v DatamodelFieldKind) IncludeInStruct() bool {
return v == DatamodelFieldKindScalar || v == DatamodelFieldKindEnum
}
// IsRelation returns whether field is a relation
func (v DatamodelFieldKind) IsRelation() bool {
return v == DatamodelFieldKindRelation
}
// Document describes the root of the AST.
type Document struct {
Datamodel Datamodel `json:"datamodel"`
Schema Schema `json:"schema"`
Mappings Mappings `json:"mappings"`
}
type Mappings struct {
ModelOperations []ModelOperation `json:"modelOperations"`
OtherOperations struct {
Read []string `json:"read"`
Write []string `json:"write"`
} `json:"otherOperations"`
}
type ModelOperation struct {
Model types.String `json:"model"`
Aggregate types.String `json:"aggregate"`
CreateOne types.String `json:"createOne"`
DeleteMany types.String `json:"deleteMany"`
DeleteOne types.String `json:"deleteOne"`
FindFirst types.String `json:"findFirst"`
FindMany types.String `json:"findMany"`
FindUnique types.String `json:"findUnique"`
GroupBy types.String `json:"groupBy"`
UpdateMany types.String `json:"updateMany"`
UpdateOne types.String `json:"updateOne"`
UpsertOne types.String `json:"upsertOne"`
}
func (m *ModelOperation) Namespace() string {
return m.Model.GoCase() + "Namespace"
}
// Operator describes a query operator such as NOT, OR, etc.
type Operator struct {
Name string
Action string
}
// Operators returns a list of all query operators such as NOT, OR, etc.
func (Document) Operators() []Operator {
return []Operator{{
Name: "Not",
Action: "NOT",
}, {
Name: "Or",
Action: "OR",
}, {
Name: "And",
Action: "AND",
}}
}
// Action describes a CRUD operation.
type Action struct {
// Type describes a query or a mutation
Type string
Name types.String
}
// ActionType describes a CRUD operation type.
type ActionType struct {
Name types.String
InnerName types.String
List bool
ReturnList bool
}
func (Document) Types() []string {
return []string{"Unique", "Many"}
}
// Variations contains different query capabilities such as Unique, First and Many
func (Document) Variations() []ActionType {
return []ActionType{{
Name: "Unique",
InnerName: "One",
}, {
Name: "First",
List: true,
InnerName: "One",
}, {
Name: "Many",
List: true,
ReturnList: true,
InnerName: "Many",
}}
}
// Actions returns all possible CRUD operations.
func (Document) Actions() []Action {
return []Action{{
"query",
"Find",
}, {
"mutation",
"Create",
}, {
"mutation",
"Update",
}, {
"mutation",
"Delete",
}}
}
// Method defines the method for the virtual types method
type Method struct {
Name string
Action string
}
// Type defines the data struct for the virtual types method
type Type struct {
Name string
Methods []Method
}
func (Document) WriteTypes() []Type {
number := []Method{{
Name: "Increment",
Action: "increment",
}, {
Name: "Decrement",
Action: "decrement",
}, {
Name: "Multiply",
Action: "multiply",
}, {
Name: "Divide",
Action: "divide",
}}
return []Type{{
Name: "Int",
Methods: number,
}, {
Name: "Float",
Methods: number,
}}
}
// SchemaEnum describes an enumerated internal prisma type.
type SchemaEnum struct {
Name types.String `json:"name"`
Values []types.String `json:"values"`
// DBName (optional)
DBName types.String `json:"dBName"`
}
// Enum describes an enumerated type.
type Enum struct {
Name types.String `json:"name"`
Values []EnumValue `json:"values"`
// DBName (optional)
DBName types.String `json:"dBName"`
}
// EnumValue contains detailed information about an enum type.
type EnumValue struct {
Name types.String `json:"name"`
// DBName (optional)
DBName types.String `json:"dBName"`
}
// Datamodel contains all types of the Prisma Datamodel.
type Datamodel struct {
Models []Model `json:"models"`
Enums []Enum `json:"enums"`
}
type UniqueIndex struct {
InternalName string `json:"name"`
Fields []types.String `json:"fields"`
}
// Model describes a Prisma type model, which usually maps to a database table or collection.
type Model struct {
// Name describes the singular name of the model.
Name types.String `json:"name"`
IsEmbedded bool `json:"isEmbedded"`
// DBName (optional)
DBName types.String `json:"dbName"`
Fields []Field `json:"fields"`
UniqueIndexes []UniqueIndex `json:"uniqueIndexes"`
PrimaryKey PrimaryKey `json:"primaryKey"`
}
type PrimaryKey struct {
Name types.String `json:"name"`
Fields []types.String `json:"fields"`
}
func (m Model) Actions() []string {
return []string{"Set", "Equals"}
}
// RelationFieldsPlusOne returns all fields plus an empty one, so it's easier to iterate through it in some gotpl files
func (m Model) RelationFieldsPlusOne() []Field {
var fields []Field
for _, field := range m.Fields {
if field.Kind.IsRelation() {
fields = append(fields, field)
}
}
fields = append(fields, Field{})
return fields
}
// Field describes properties of a single model field.
type Field struct {
Kind FieldKind `json:"kind"`
Name types.String `json:"name"`
IsRequired bool `json:"isRequired"`
IsList bool `json:"isList"`
IsUnique bool `json:"isUnique"`
IsReadOnly bool `json:"isReadOnly"`
IsID bool `json:"isId"`
Type types.Type `json:"type"`
// DBName (optional)
DBName types.String `json:"dBName"`
IsGenerated bool `json:"isGenerated"`
IsUpdatedAt bool `json:"isUpdatedAt"`
// RelationToFields (optional)
RelationToFields []interface{} `json:"relationToFields"`
// RelationOnDelete (optional)
RelationOnDelete types.String `json:"relationOnDelete"`
// RelationName (optional)
RelationName types.String `json:"relationName"`
// HasDefaultValue
HasDefaultValue bool `json:"hasDefaultValue"`
}
func (f Field) RequiredOnCreate() bool {
if !f.IsRequired || f.IsUpdatedAt || f.HasDefaultValue || f.IsReadOnly || f.IsList {
return false
}
if f.RelationName != "" && f.IsList {
return false
}
return true
}
// RelationMethod describes a method for relations
type RelationMethod struct {
Name string
Action string
}
// RelationMethods returns a mapping for the PQL methods provided for relations
func (f Field) RelationMethods() []RelationMethod {
if f.IsList {
return []RelationMethod{{
Name: "Some",
Action: "some",
}, {
Name: "Every",
Action: "every",
}}
}
return []RelationMethod{{
Name: "Where",
Action: "is",
}}
}
// Schema provides the GraphQL/PQL AST.
type Schema struct {
// RootQueryType (optional)
RootQueryType types.String `json:"rootQueryType"`
// RootMutationType (optional)
RootMutationType types.String `json:"rootMutationType"`
InputObjectTypes InputObjectType `json:"inputObjectTypes"`
OutputObjectTypes OutputObject `json:"outputObjectTypes"`
EnumTypes EnumTypes `json:"enumTypes"`
}
type EnumTypes struct {
Prisma []SchemaEnum `json:"prisma"`
Model []SchemaEnum `json:"model"`
}
type InputObjectType struct {
Prisma []CoreType `json:"prisma"`
}
type OutputObject struct {
Prisma []OutputType `json:"prisma"`
}
// OuterInputType provides the arguments of a given field.
type OuterInputType struct {
Name types.String `json:"name"`
InputTypes []SchemaInputType `json:"inputTypes"`
// IsRelationFilter (optional)
IsRelationFilter bool `json:"isRelationFilter"`
}
// SchemaInputType describes an input type of a given field.
type SchemaInputType struct {
IsRequired bool `json:"isRequired"`
IsList bool `json:"isList"`
Type types.Type `json:"type"` // this was declared as ArgType
Kind FieldKind `json:"kind"`
Namespace types.String `json:"namespace"`
// can be "scalar", "inputObjectTypes" or "outputObjectTypes"
Location string `json:"location"`
}
// OutputType describes a GraphQL/PQL return type.
type OutputType struct {
Name types.String `json:"name"`
Fields []SchemaField `json:"fields"`
// IsEmbedded (optional)
IsEmbedded bool `json:"isEmbedded"`
}
// SchemaField describes the information of an output type field.
type SchemaField struct {
Name types.String `json:"name"`
OutputType SchemaOutputType `json:"outputType"`
Args []OuterInputType `json:"args"`
}
// SchemaOutputType describes an output type of a given field.
type SchemaOutputType struct {
Type types.String `json:"type"`
IsList bool `json:"isList"`
IsRequired bool `json:"isRequired"`
Kind FieldKind `json:"kind"`
}
// CoreType describes a GraphQL/PQL input type.
type CoreType struct {
Name types.String `json:"name"`
// IsWhereType (optional)
IsWhereType bool `json:"isWhereType"`
// IsOrderType (optional)
IsOrderType bool `json:"isOrderType"`
// AtLeastOne (optional)
AtLeastOne bool `json:"atLeastOne"`
// AtMostOne (optional)
AtMostOne bool `json:"atMostOne"`
Fields []OuterInputType `json:"fields"`
}