Skip to content

Commit

Permalink
fix(json): adapt internal json update handling (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen authored Mar 30, 2021
1 parent 964976c commit a7eef73
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion generator/templates/actions/find.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@
{{/* TODO consider upcoming non-set methods */}}
field := q.field()
{{/* if scalar, wrap in 'set' */}}
if field.Value != nil {
_, isJson := field.Value.(types.JSON)
if field.Value != nil && !isJson {
v := field.Value
field.Fields = []builder.Field{
{
Expand Down
3 changes: 2 additions & 1 deletion generator/templates/actions/upsert.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
{{/* TODO re-use */}}
field := q.field()
{{/* if scalar, wrap in 'set' */}}
if field.Value != nil {
_, isJson := field.Value.(types.JSON)
if field.Value != nil && !isJson {
v := field.Value
field.Fields = []builder.Field{
{
Expand Down
44 changes: 44 additions & 0 deletions test/types/json/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,50 @@ func TestJSON(t *testing.T) {
t.Fatalf("fail %s", err)
}

assert.Equal(t, expected, actual)
},
}, {
name: "json update field",
run: func(t *testing.T, client *PrismaClient, ctx cx) {
var b JSON = []byte(`"hi"`)
created, err := client.User.CreateOne(
User.JSON.Set(b),
User.JSONOpt.Set(b),
User.ID.Set("123"),
).Exec(ctx)
if err != nil {
t.Fatalf("fail %s", err)
}

expected := &UserModel{
InnerUser: InnerUser{
ID: "123",
JSON: b,
JSONOpt: &b,
},
}

assert.Equal(t, expected, created)

updated, err := client.User.FindUnique(
User.ID.Equals("123"),
).Update(
User.JSON.Set(b),
User.JSONOpt.Set(b),
).Exec(ctx)
if err != nil {
t.Fatalf("fail %s", err)
}

assert.Equal(t, expected, updated)

actual, err := client.User.FindUnique(
User.ID.Equals("123"),
).Exec(ctx)
if err != nil {
t.Fatalf("fail %s", err)
}

assert.Equal(t, expected, actual)
},
}}
Expand Down

0 comments on commit a7eef73

Please sign in to comment.