Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(prisma): introduce CreateOrUpdate function #1455

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions generator/templates/actions/upsert.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,47 @@
return v
}

{{ range $dataSource := $.Datasources }}
{{ $providerName := $dataSource.Provider }}
{{ if ne $providerName "mongodb" }}
func (r {{ $result }}) CreateOrUpdate(
{{/* TODO re-use */}}
{{ range $field := $model.Fields -}}
{{- if $field.RequiredOnCreate $model.PrimaryKey -}}
_{{ $field.Name.GoLowerCase }} {{ $model.Name.GoCase }}WithPrisma{{ $field.Name.GoCase }}SetParam,
{{ end }}
{{- end }}
optional ...{{ $model.Name.GoCase }}SetParam,
) {{ $result }} {
var v {{ $result }}
v.query = r.query

var fields []builder.Field
{{ range $field := $model.Fields -}}
{{- if $field.RequiredOnCreate $model.PrimaryKey -}}
fields = append(fields, _{{ $field.Name.GoLowerCase }}.field())
{{ end }}
{{- end }}

for _, q := range optional {
fields = append(fields, q.field())
}

v.query.Inputs = append(v.query.Inputs, builder.Input{
Name: "create",
Fields: fields,
})

v.query.Inputs = append(v.query.Inputs, builder.Input{
Name: "update",
Fields: fields,
})

return v
}
{{ end }}
{{ end }}

func (r {{ $result }}) Exec(ctx context.Context) (*{{ $modelName }}, error) {
var v {{ $modelName }}
if err := r.query.Exec(ctx, &v); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions test/features/upsert/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ generator db {
}

model Post {
id String @id @default(cuid()) @map("_id")
title String
views Int
id String @id @default(cuid()) @map("_id")
title String
views Int
description String?
}
107 changes: 106 additions & 1 deletion test/features/upsert/upsert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
type cx = context.Context
type Func func(t *testing.T, client *PrismaClient, ctx cx)

var Databases = []test.Database{
test.MySQL,
test.PostgreSQL,
test.SQLite,
}

func TestUpsert(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -85,6 +91,67 @@ func TestUpsert(t *testing.T) {
massert.Equal(t, expected, actual)
},
}, {
name: "CreateOrUpdate when record does not exist",
run: func(t *testing.T, client *PrismaClient, ctx cx) {
actual, err := client.Post.UpsertOne(
Post.ID.Equals("upsert"),
).CreateOrUpdate(
Post.Title.Set("title"),
Post.Views.Set(0),
).Exec(ctx)
if err != nil {
t.Fatalf("fail %s", err)
}

expected := &PostModel{
InnerPost: InnerPost{
ID: actual.ID,
Title: "title",
Views: 0,
},
}

massert.Equal(t, expected, actual)
},
}, {
name: "CreateOrUpdate when record exists",
// Create the post first
run: func(t *testing.T, client *PrismaClient, ctx cx) {
desc := "random desc"
_, err := client.Post.CreateOne(
Post.Title.Set("title"),
Post.Views.Set(0),
Post.Description.Set(desc),
Post.ID.Set("upsert"),
).Exec(ctx)
if err != nil {
t.Fatalf("fail %s", err)
}

// Upsert the post: should only update it since it exists
actual, err := client.Post.UpsertOne(
Post.ID.Equals("upsert"),
).CreateOrUpdate(
Post.Title.Set("title"),
Post.Views.Set(2),
Post.Description.Set("random desc"),
).Exec(ctx)
if err != nil {
t.Fatalf("fail %s", err)
}

expected := &PostModel{
InnerPost: InnerPost{
ID: "upsert",
Title: "title",
Views: 2,
Description: &desc,
},
}

massert.Equal(t, expected, actual)
},
}, {
name: "transaction",
// language=GraphQL
before: []string{`
Expand Down Expand Up @@ -122,13 +189,51 @@ func TestUpsert(t *testing.T) {
},
}

massert.Equal(t, expected, query.Result())
},
}, {
name: "transaction CreateOrUpdate",
run: func(t *testing.T, client *PrismaClient, ctx cx) {
// Create the post first
desc := "random desc"
_, err := client.Post.CreateOne(
Post.Title.Set("title"),
Post.Views.Set(2),
Post.Description.Set(desc),
Post.ID.Set("upsert"),
).Exec(ctx)
if err != nil {
t.Fatalf("fail %s", err)
}

// Upsert: should only update it
query := client.Post.UpsertOne(
Post.ID.Equals("upsert"),
).CreateOrUpdate(
Post.Title.Set("title"),
Post.Views.Set(3),
).Tx()

if err := client.Prisma.Transaction(query).Exec(ctx); err != nil {
t.Fatalf("fail %s", err)
}

expected := &PostModel{
InnerPost: InnerPost{
ID: "upsert",
Title: "title",
Views: 3,
Description: &desc,
},
}

massert.Equal(t, expected, query.Result())
},
}}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
test.RunSerial(t, test.Databases, func(t *testing.T, db test.Database, ctx context.Context) {
test.RunSerial(t, Databases, func(t *testing.T, db test.Database, ctx context.Context) {
client := NewClient()
mockDBName := test.Start(t, db, client.Engine, tt.before)
defer test.End(t, db, client.Engine, mockDBName)
Expand Down
Loading