From 7039e021fa282ccda1e2411b86571d432f5ce279 Mon Sep 17 00:00:00 2001 From: steebchen Date: Wed, 8 Nov 2023 15:44:48 +0700 Subject: [PATCH] feat(mongodb): add support for object types --- generator/ast/dmmf/dmmf.go | 15 +++- generator/templates/models.gotpl | 13 ++- generator/templates/types.gotpl | 18 ++++ .../databases/mongodb/objects/default_test.go | 85 +++++++++++++++++++ test/databases/mongodb/objects/schema.prisma | 33 +++++++ test/databases/mysql/json/.gitignore | 2 - test/databases/mysql/raw/.gitignore | 2 - test/databases/postgresql/array/.gitignore | 2 - test/databases/postgresql/json/.gitignore | 2 - 9 files changed, 155 insertions(+), 17 deletions(-) create mode 100644 generator/templates/types.gotpl create mode 100644 test/databases/mongodb/objects/default_test.go create mode 100644 test/databases/mongodb/objects/schema.prisma delete mode 100644 test/databases/mysql/json/.gitignore delete mode 100644 test/databases/mysql/raw/.gitignore delete mode 100644 test/databases/postgresql/array/.gitignore delete mode 100644 test/databases/postgresql/json/.gitignore diff --git a/generator/ast/dmmf/dmmf.go b/generator/ast/dmmf/dmmf.go index a1b0d9409..2a2421b66 100644 --- a/generator/ast/dmmf/dmmf.go +++ b/generator/ast/dmmf/dmmf.go @@ -211,8 +211,19 @@ type EnumValue struct { // Datamodel contains all types of the Prisma Datamodel. type Datamodel struct { - Models []Model `json:"models"` - Enums []Enum `json:"enums"` + Models []Model `json:"models"` + Types []ObjectType `json:"types"` + Enums []Enum `json:"enums"` +} + +// ObjectType is a MongoDB object type +type ObjectType struct { + Name types.String `json:"name"` + DbName types.String `json:"dbName"` + Fields []Field `json:"fields"` + PrimaryKey string `json:"primaryKey"` + UniqueFields []string `json:"uniqueFields"` + UniqueIndexes []string `json:"uniqueIndexes"` } type UniqueIndex struct { diff --git a/generator/templates/models.gotpl b/generator/templates/models.gotpl index 573dec15c..4c2631b50 100644 --- a/generator/templates/models.gotpl +++ b/generator/templates/models.gotpl @@ -10,13 +10,12 @@ // Inner{{ $model.Name.GoCase }} holds the actual data type Inner{{ $model.Name.GoCase }} struct { {{ range $field := $model.Fields }} - {{- if not $field.Kind.IsRelation -}} - {{- if $field.IsRequired }} - {{ $field.Name.GoCase }} {{ if $field.IsList }}[]{{ end }}{{ $field.Type.Value }} {{ $field.Name.Tag $field.IsRequired }} - {{- else }} - {{ $field.Name.GoCase }} {{ if $field.IsList }}[]{{ else }}*{{ end }}{{ $field.Type.Value }} {{ $field.Name.Tag $field.IsRequired }} - {{- end }} - {{- end -}} + {{- if eq $field.RelationName "" -}} + {{ $field.Name.GoCase -}} + {{- if $field.IsList }}[]{{ else }}{{- if not $field.IsRequired }}*{{ end }} {{ end -}} + {{ $field.Type.Value }}{{ if eq $field.Kind "object" }}Type{{ end -}} + {{ $field.Name.Tag $field.IsRequired }} + {{- end }} {{ end }} } diff --git a/generator/templates/types.gotpl b/generator/templates/types.gotpl new file mode 100644 index 000000000..2976dcdc3 --- /dev/null +++ b/generator/templates/types.gotpl @@ -0,0 +1,18 @@ +{{- /*gotype:github.com/steebchen/prisma-client-go/generator.Root*/ -}} + +{{/* Types are for MongoDB object types */}} + +{{ range $type := $.DMMF.Datamodel.Types }} + // {{ $type.Name.GoCase }}Type + type {{ $type.Name.GoCase }}Type struct { + {{ range $field := $type.Fields }} + {{- if not $field.Kind.IsRelation -}} + {{- if $field.IsRequired }} + {{ $field.Name.GoCase }} {{ if $field.IsList }}[]{{ end }}{{ $field.Type.Value }} {{ $field.Name.Tag $field.IsRequired }} + {{- else }} + {{ $field.Name.GoCase }} {{ if $field.IsList }}[]{{ else }}*{{ end }}{{ $field.Type.Value }} {{ $field.Name.Tag $field.IsRequired }} + {{- end }} + {{- end -}} + {{ end }} + } +{{ end }} diff --git a/test/databases/mongodb/objects/default_test.go b/test/databases/mongodb/objects/default_test.go new file mode 100644 index 000000000..a62ccd2f8 --- /dev/null +++ b/test/databases/mongodb/objects/default_test.go @@ -0,0 +1,85 @@ +package raw + +import ( + "context" + "testing" + + "github.com/steebchen/prisma-client-go/test" + "github.com/steebchen/prisma-client-go/test/helpers/massert" +) + +type cx = context.Context +type Func func(t *testing.T, client *PrismaClient, ctx cx) + +func TestObjects(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + before []string + run Func + }{{ + name: "types", + // language=GraphQL + before: []string{` + mutation { + result: createOneUser(data: { + id: "id1", + email: "email1", + info: { + age: 0, + }, + infoOpt: { + age: 0, + }, + list: { + create: [ + { + age: 0, + }, + ] + } + }) { + id + } + } + `}, + run: func(t *testing.T, client *PrismaClient, ctx cx) { + user, err := client.User.FindUnique( + User.ID.Equals("id1"), + ).Exec(ctx) + if err != nil { + t.Fatal(err) + } + + massert.Equal(t, &UserModel{ + InnerUser: InnerUser{ + ID: "id1", + Email: "email1", + Info: InfoType{ + Age: 0, + }, + InfoOpt: &InfoType{ + Age: 0, + }, + List: []InfoType{ + { + Age: 0, + }, + }, + }, + }, user) + }, + }} + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + client := NewClient() + + mockDB := test.Start(t, test.MongoDB, client.Engine, tt.before) + defer test.End(t, test.MongoDB, client.Engine, mockDB) + + tt.run(t, client, context.Background()) + }) + } +} diff --git a/test/databases/mongodb/objects/schema.prisma b/test/databases/mongodb/objects/schema.prisma new file mode 100644 index 000000000..8c37b599f --- /dev/null +++ b/test/databases/mongodb/objects/schema.prisma @@ -0,0 +1,33 @@ +datasource db { + provider = "mongodb" + url = env("__REPLACE__") +} + +generator db { + provider = "go run github.com/steebchen/prisma-client-go" + output = "." + disableGoBinaries = true + package = "raw" +} + +model User { + id String @id @default(cuid()) @map("_id") + email String @unique + username String + info Info + infoOpt Info? + list Info[] + posts Post[] +} + +type Info { + age Int +} + +// unused relation to check for conflicts etc. +model Post { + id String @id @default(cuid()) @map("_id") + title String + author User @relation(fields: [user_id], references: [id]) + user_id String +} diff --git a/test/databases/mysql/json/.gitignore b/test/databases/mysql/json/.gitignore deleted file mode 100644 index a0c7514ad..000000000 --- a/test/databases/mysql/json/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# gitignore generated by Prisma Client Go. DO NOT EDIT. -*_gen.go diff --git a/test/databases/mysql/raw/.gitignore b/test/databases/mysql/raw/.gitignore deleted file mode 100644 index a0c7514ad..000000000 --- a/test/databases/mysql/raw/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# gitignore generated by Prisma Client Go. DO NOT EDIT. -*_gen.go diff --git a/test/databases/postgresql/array/.gitignore b/test/databases/postgresql/array/.gitignore deleted file mode 100644 index a0c7514ad..000000000 --- a/test/databases/postgresql/array/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# gitignore generated by Prisma Client Go. DO NOT EDIT. -*_gen.go diff --git a/test/databases/postgresql/json/.gitignore b/test/databases/postgresql/json/.gitignore deleted file mode 100644 index a0c7514ad..000000000 --- a/test/databases/postgresql/json/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# gitignore generated by Prisma Client Go. DO NOT EDIT. -*_gen.go