Skip to content

Commit

Permalink
feat(mongodb): add support for object types
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Nov 8, 2023
1 parent 4ef9081 commit 7039e02
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 17 deletions.
15 changes: 13 additions & 2 deletions generator/ast/dmmf/dmmf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 6 additions & 7 deletions generator/templates/models.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
}

Expand Down
18 changes: 18 additions & 0 deletions generator/templates/types.gotpl
Original file line number Diff line number Diff line change
@@ -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 }}
85 changes: 85 additions & 0 deletions test/databases/mongodb/objects/default_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
}
}
33 changes: 33 additions & 0 deletions test/databases/mongodb/objects/schema.prisma
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 0 additions & 2 deletions test/databases/mysql/json/.gitignore

This file was deleted.

2 changes: 0 additions & 2 deletions test/databases/mysql/raw/.gitignore

This file was deleted.

2 changes: 0 additions & 2 deletions test/databases/postgresql/array/.gitignore

This file was deleted.

2 changes: 0 additions & 2 deletions test/databases/postgresql/json/.gitignore

This file was deleted.

0 comments on commit 7039e02

Please sign in to comment.