-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mongodb): add support for object types
- Loading branch information
Showing
9 changed files
with
155 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.