Skip to content
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
62 changes: 60 additions & 2 deletions generator/templates/model_create.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,66 @@ func (q *Queries) execute{{ .Model.Name }}Create(ctx context.Context, input {{ .
if err := input.Validate(); err != nil {
return nil, err
}
m := q.{{ .Model.Name }}InputToMap(input)
cols, vals := mapToColsVals(m, {{ .Model.Name }}ColOrder)
var cols []string
var vals []any

{{- range $field := .Model.ScalarFields }}
{{- $col := $field.EffectiveColName }}
{{- $fieldName := capitalize $field.Name }}
{{- if $field.EnumRef }}
{{- if $field.IsArray }}
if input.{{ $fieldName }} != nil {
cols = append(cols, "{{ $col }}")
vals = append(vals, input.{{ $fieldName }})
}
{{- else }}
if input.{{ $fieldName }} != nil {
cols = append(cols, "{{ $col }}")
vals = append(vals, *input.{{ $fieldName }})
}
{{- end }}
{{- else }}
{{- if $field.IsArray }}
if input.{{ $fieldName }} != nil {
cols = append(cols, "{{ $col }}")
vals = append(vals, input.{{ $fieldName }})
}
{{- else }}
{{- if and $field.Default (eq $field.Default.Kind.String "Func") }}
if input.{{ $fieldName }} != nil {
cols = append(cols, "{{ $col }}")
vals = append(vals, *input.{{ $fieldName }})
} else {
cols = append(cols, "{{ $col }}")
{{- if eq $field.Default.FuncName "cuid" }}
vals = append(vals, generateCUID())
{{- else if eq $field.Default.FuncName "uuid" }}
vals = append(vals, generateUUID())
{{- else if eq $field.Default.FuncName "now" }}
vals = append(vals, time.Now())
{{- end }}
}
{{- else if or $field.Optional (ne $field.Default nil) }}
if input.{{ $fieldName }} != nil {
cols = append(cols, "{{ $col }}")
vals = append(vals, *input.{{ $fieldName }})
}
{{- else }}
{{- if and $field.IsID (eq $field.GoType "string") }}
cols = append(cols, "{{ $col }}")
if input.{{ $fieldName }} != "" {
vals = append(vals, input.{{ $fieldName }})
} else {
vals = append(vals, generateCUID())
}
{{- else }}
cols = append(cols, "{{ $col }}")
vals = append(vals, input.{{ $fieldName }})
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}

returningCols := q.select{{ .Model.Name }}Cols(selects, omits)

Expand Down
16 changes: 16 additions & 0 deletions integration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"integration/valk"
"integration/valk/category"
Expand All @@ -26,6 +27,10 @@ type SeedData struct {
}

func seed(db *valk.DB, ctx context.Context) *SeedData {
db.User.BeforeCreate(func(ctx context.Context, uc *user.Create) error {
return errors.New("AAAAAAAAH")
})

referrer, err := db.User.Create(user.Create{
Email: "referrer@example.com",
PhoneNum: "555-0001",
Expand Down Expand Up @@ -140,6 +145,17 @@ func main() {

printJSON(all)

bleh, err := db.User.FindMany(user.And(
user.Email.Contains("@example"),
user.Or(
user.Email.EQ("referred@example.com"),
user.PhoneNum.EQ("+1111"),
),
)).Select(user.Select{
Id: true,
}).Exec(ctx)
printJSON(bleh)

fmt.Println("=== QUERY 1: Deep Nested Select ===")
resUser, err := db.User.FindFirst(
user.Email.EQ("referred@example.com"),
Expand Down
3 changes: 2 additions & 1 deletion integration/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"integration/valk"
"integration/valk/comment"
"integration/valk/post"
"integration/valk/user"
"strings"
"sync"
Expand Down Expand Up @@ -570,7 +571,7 @@ func TestJsonField(t *testing.T) {
t.Fatalf("failed to create user: %v", err)
}

p, err := db.Post.Create(valk.PostCreate{
p, err := db.Post.Create(post.Create{
Title: "JSON Post",
AuthorId: u.Id,
}).Exec(ctx)
Expand Down
4 changes: 1 addition & 3 deletions integration/valk.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"database": {
"url_env": "DATABASE_URL",
Expand All @@ -11,5 +10,4 @@
"client": "./valk",
"migrations": "./valk/migrations"
}
}

}
12 changes: 10 additions & 2 deletions integration/valk/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,16 @@ func (q *Queries) executeCategoryCreate(ctx context.Context, input CategoryCreat
if err := input.Validate(); err != nil {
return nil, err
}
m := q.CategoryInputToMap(input)
cols, vals := mapToColsVals(m, CategoryColOrder)
var cols []string
var vals []any
if input.Id != nil {
cols = append(cols, "id")
vals = append(vals, *input.Id)
} else {
cols = append(cols, "id")
}
cols = append(cols, "name")
vals = append(vals, input.Name)

returningCols := q.selectCategoryCols(selects, omits)

Expand Down
8 changes: 6 additions & 2 deletions integration/valk/categoryToPost.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,12 @@ func (q *Queries) executeCategoryToPostCreate(ctx context.Context, input Categor
if err := input.Validate(); err != nil {
return nil, err
}
m := q.CategoryToPostInputToMap(input)
cols, vals := mapToColsVals(m, CategoryToPostColOrder)
var cols []string
var vals []any
cols = append(cols, "postId")
vals = append(vals, input.PostId)
cols = append(cols, "categoryId")
vals = append(vals, input.CategoryId)

returningCols := q.selectCategoryToPostCols(selects, omits)

Expand Down
27 changes: 25 additions & 2 deletions integration/valk/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,31 @@ func (q *Queries) executeCommentCreate(ctx context.Context, input CommentCreate,
if err := input.Validate(); err != nil {
return nil, err
}
m := q.CommentInputToMap(input)
cols, vals := mapToColsVals(m, CommentColOrder)
var cols []string
var vals []any
if input.Id != nil {
cols = append(cols, "id")
vals = append(vals, *input.Id)
} else {
cols = append(cols, "id")
vals = append(vals, generateCUID())
}
cols = append(cols, "textify")
vals = append(vals, input.Textify)
cols = append(cols, "dummy3")
vals = append(vals, input.Dummy3)
cols = append(cols, "dummy1")
vals = append(vals, input.Dummy1)
cols = append(cols, "dummy2")
vals = append(vals, input.Dummy2)
cols = append(cols, "postId")
vals = append(vals, input.PostId)
cols = append(cols, "authorId")
vals = append(vals, input.AuthorId)
if input.Meta != nil {
cols = append(cols, "meta")
vals = append(vals, *input.Meta)
}

returningCols := q.selectCommentCols(selects, omits)

Expand Down
23 changes: 21 additions & 2 deletions integration/valk/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,27 @@ func (q *Queries) executePostCreate(ctx context.Context, input PostCreate, selec
if err := input.Validate(); err != nil {
return nil, err
}
m := q.PostInputToMap(input)
cols, vals := mapToColsVals(m, PostColOrder)
var cols []string
var vals []any
if input.Id != nil {
cols = append(cols, "id")
vals = append(vals, *input.Id)
} else {
cols = append(cols, "id")
vals = append(vals, generateCUID())
}
cols = append(cols, "title")
vals = append(vals, input.Title)
if input.Content != nil {
cols = append(cols, "content")
vals = append(vals, *input.Content)
}
if input.Published != nil {
cols = append(cols, "published")
vals = append(vals, *input.Published)
}
cols = append(cols, "authorId")
vals = append(vals, input.AuthorId)

returningCols := q.selectPostCols(selects, omits)

Expand Down
17 changes: 15 additions & 2 deletions integration/valk/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,21 @@ func (q *Queries) executeProfileCreate(ctx context.Context, input ProfileCreate,
if err := input.Validate(); err != nil {
return nil, err
}
m := q.ProfileInputToMap(input)
cols, vals := mapToColsVals(m, ProfileColOrder)
var cols []string
var vals []any
if input.Id != nil {
cols = append(cols, "id")
vals = append(vals, *input.Id)
} else {
cols = append(cols, "id")
vals = append(vals, generateCUID())
}
if input.Bio != nil {
cols = append(cols, "bio")
vals = append(vals, *input.Bio)
}
cols = append(cols, "userId")
vals = append(vals, input.UserId)

returningCols := q.selectProfileCols(selects, omits)

Expand Down
31 changes: 29 additions & 2 deletions integration/valk/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,35 @@ func (q *Queries) executeUserCreate(ctx context.Context, input UserCreate, selec
if err := input.Validate(); err != nil {
return nil, err
}
m := q.UserInputToMap(input)
cols, vals := mapToColsVals(m, UserColOrder)
var cols []string
var vals []any
if input.Id != nil {
cols = append(cols, "id")
vals = append(vals, *input.Id)
} else {
cols = append(cols, "id")
vals = append(vals, generateCUID())
}
cols = append(cols, "email")
vals = append(vals, input.Email)
cols = append(cols, "phoneNum")
vals = append(vals, input.PhoneNum)
if input.Password != nil {
cols = append(cols, "password")
vals = append(vals, *input.Password)
}
if input.Role != nil {
cols = append(cols, "role")
vals = append(vals, *input.Role)
}
if input.RoleOptional != nil {
cols = append(cols, "roleOptional")
vals = append(vals, *input.RoleOptional)
}
if input.ReferredById != nil {
cols = append(cols, "referredById")
vals = append(vals, *input.ReferredById)
}

returningCols := q.selectUserCols(selects, omits)

Expand Down
Loading