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
10 changes: 6 additions & 4 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
env:
POSTGRES_USER: testuser
POSTGRES_PASSWORD: testpassword
POSTGRES_DB: valkyrie_test
POSTGRES_DB: valk_test
ports:
- 5432:5432
options: >-
Expand Down Expand Up @@ -47,11 +47,13 @@ jobs:
run: |
PG_URL="${{ secrets.PG_DATABASE_URL }}"
if [ -z "$PG_URL" ]; then
PG_URL="postgres://testuser:testpassword@localhost:5432/valkyrie_test?sslmode=disable"
PG_URL="postgres://testuser:testpassword@localhost:5432/valk_test?sslmode=disable"
else
PG_URL="${PG_URL/valkyrie_test/valk_test}"
fi
sed -i 's/provider = "sqlite"/provider = "postgres"/' integration/schema.prisma
rm -f integration/valkyrie/migrations/*.sql
rm -f integration/valk/migrations/*.sql
make integration-gen
cd integration
../bin/valkyrie migrate -u "$PG_URL" init_pg
../bin/valk migrate -u "$PG_URL" init_pg
PG_DATABASE_URL="$PG_URL" go test -v ./...
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
bin
*.db
.vscode
docker-compose.yml
docker-compose.yml
api-example.gotpl
6 changes: 3 additions & 3 deletions cli/getConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ type OutputConfig struct {

func GetConfig() *Config {
var config Config
configFile, err := os.ReadFile("valkyrie.json")
configFile, err := os.ReadFile("valk.json")
if err != nil {
log.Fatal("valkyrie.json not found")
log.Fatal("valk.json not found")
return nil
}

Expand All @@ -52,7 +52,7 @@ func GetConfig() *Config {
// hasAll = true
}
if !slices.Contains(LogLevels, l) && l != "all" {
log.Fatalf("invalid log level in valkyrie.json: %q (must be one of: query, info, warn, error, all)", l)
log.Fatalf("invalid log level in valk.json: %q (must be one of: query, info, warn, error, all)", l)
return nil
}
}
Expand Down
6 changes: 3 additions & 3 deletions cli/handleGenerate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os"
"path/filepath"
"strings"
"valkyrie/generator"
"valkyrie/schema"
"valk/generator"
"valk/schema"
)

func handleGenerate() {
Expand Down Expand Up @@ -44,7 +44,7 @@ func handleGenerate() {

pkgName := filepath.Base(config.Output.Client)
if pkgName == "." || pkgName == "" {
pkgName = "valkyrie"
pkgName = "valk"
}

outputs, err := generator.GenerateClient(*schemaDef, pkgName, embedRelDir, config.Output.Migrations, config.Log)
Expand Down
2 changes: 1 addition & 1 deletion cli/handleHelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func PrintHelp() {

fmt.Println("Usage: valkyrie <command/flag> [arguments]")
fmt.Println("Usage: valk <command/flag> [arguments]")
fmt.Println("\nAvailable commands:")

for _, cmd := range Commands {
Expand Down
8 changes: 4 additions & 4 deletions cli/handleInit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

func handleInit() {
os.WriteFile("valkyrie.json", []byte(configFileContent), 0644)
fmt.Println("creating valkyrie.json ....")
os.WriteFile("valk.json", []byte(configFileContent), 0644)
fmt.Println("creating valk.json ....")
}

var configFileContent string = `
Expand All @@ -20,8 +20,8 @@ var configFileContent string = `
"schema": "./schema.prisma",

"output": {
"client": "./valkyrie",
"migrations": "./valkyrie/migrations"
"client": "./valk",
"migrations": "./valk/migrations"
}
}
`
6 changes: 3 additions & 3 deletions cli/handleMigrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"strings"
"time"

"valkyrie/migration"
"valkyrie/schema"
"valk/migration"
"valk/schema"

_ "github.com/lib/pq"
"github.com/pressly/goose/v3"
Expand Down Expand Up @@ -41,7 +41,7 @@ func handleMigrate(args []string) {

cfg := GetConfig()
if cfg == nil {
fmt.Println("Error: valkyrie.json not found or invalid")
fmt.Println("Error: valk.json not found or invalid")
os.Exit(1)
}

Expand Down
2 changes: 1 addition & 1 deletion generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"go/format"
"path/filepath"
"text/template"
"valkyrie/schema"
"valk/schema"
)

//go:embed templates/*.gotpl
Expand Down
4 changes: 2 additions & 2 deletions generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package generator
import (
"strings"
"testing"
"valkyrie/schema"
"valk/schema"
)

func TestGenerateClient_NativeDBConstraints(t *testing.T) {
Expand Down Expand Up @@ -41,7 +41,7 @@ func TestGenerateClient_NativeDBConstraints(t *testing.T) {
},
}

outputs, err := GenerateClient(sch, "valkyrie", "", "", nil)
outputs, err := GenerateClient(sch, "valk", "", "", nil)
if err != nil {
t.Fatalf("failed to generate client: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion generator/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package generator

import (
"strings"
"valkyrie/schema"
"valk/schema"
)

func capitalize(s string) string {
Expand Down
20 changes: 10 additions & 10 deletions generator/templates/model_create.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ func (s *{{ .Model.Name }}Select) hasAnyRelation() bool {
{{- end }}
}

func (d *{{ .Model.Name }}Delegate) Create(input {{ .Model.Name }}CreateInput) *CreateBuilder[{{ .Model.Name }}, {{ .Model.Name }}CreateInput, {{ .Model.Name }}Select, {{ .Model.Name }}Omit] {
return &CreateBuilder[{{ .Model.Name }}, {{ .Model.Name }}CreateInput, {{ .Model.Name }}Select, {{ .Model.Name }}Omit]{
func (d *{{ .Model.Name }}Delegate) Create(input {{ .Model.Name }}Create) *CreateBuilder[{{ .Model.Name }}, {{ .Model.Name }}Create, {{ .Model.Name }}Select, {{ .Model.Name }}Omit] {
return &CreateBuilder[{{ .Model.Name }}, {{ .Model.Name }}Create, {{ .Model.Name }}Select, {{ .Model.Name }}Omit]{
client: d.client,
input: input,
execFunc: d.client.execute{{ .Model.Name }}Create,
}
}

func (q *Queries) execute{{ .Model.Name }}Create(ctx context.Context, input {{ .Model.Name }}CreateInput, selects *{{ .Model.Name }}Select, omits *{{ .Model.Name }}Omit) (*{{ .Model.Name }}, error) {
func (q *Queries) execute{{ .Model.Name }}Create(ctx context.Context, input {{ .Model.Name }}Create, selects *{{ .Model.Name }}Select, omits *{{ .Model.Name }}Omit) (*{{ .Model.Name }}, error) {
if q.{{ .Model.Name }}.beforeCreate != nil {
if err := q.{{ .Model.Name }}.beforeCreate(ctx, &input); err != nil {
return nil, err
Expand Down Expand Up @@ -73,7 +73,7 @@ func (q *Queries) execute{{ .Model.Name }}Create(ctx context.Context, input {{ .
return res, nil
}

func (q *Queries) {{ .Model.Name }}InputToMap(input {{ .Model.Name }}CreateInput) map[string]any {
func (q *Queries) {{ .Model.Name }}InputToMap(input {{ .Model.Name }}Create) map[string]any {
m := make(map[string]any)
{{- range $field := .Model.ScalarFields }}
{{- if $field.EnumRef }}
Expand Down Expand Up @@ -125,23 +125,23 @@ func (q *Queries) {{ .Model.Name }}InputToMap(input {{ .Model.Name }}CreateInput
return m
}

func (d *{{ .Model.Name }}Delegate) CreateMany(inputs []{{ .Model.Name }}CreateInput) *CreateManyBuilder[{{ .Model.Name }}, {{ .Model.Name }}CreateInput] {
return &CreateManyBuilder[{{ .Model.Name }}, {{ .Model.Name }}CreateInput]{
func (d *{{ .Model.Name }}Delegate) CreateMany(inputs []{{ .Model.Name }}Create) *CreateManyBuilder[{{ .Model.Name }}, {{ .Model.Name }}Create] {
return &CreateManyBuilder[{{ .Model.Name }}, {{ .Model.Name }}Create]{
client: d.client,
inputs: inputs,
execFunc: d.client.execute{{ .Model.Name }}CreateMany,
}
}

func (d *{{ .Model.Name }}Delegate) CreateManyAndReturn(inputs []{{ .Model.Name }}CreateInput) *CreateManyAndReturnBuilder[{{ .Model.Name }}, {{ .Model.Name }}CreateInput, {{ .Model.Name }}Select, {{ .Model.Name }}Omit] {
return &CreateManyAndReturnBuilder[{{ .Model.Name }}, {{ .Model.Name }}CreateInput, {{ .Model.Name }}Select, {{ .Model.Name }}Omit]{
func (d *{{ .Model.Name }}Delegate) CreateManyAndReturn(inputs []{{ .Model.Name }}Create) *CreateManyAndReturnBuilder[{{ .Model.Name }}, {{ .Model.Name }}Create, {{ .Model.Name }}Select, {{ .Model.Name }}Omit] {
return &CreateManyAndReturnBuilder[{{ .Model.Name }}, {{ .Model.Name }}Create, {{ .Model.Name }}Select, {{ .Model.Name }}Omit]{
client: d.client,
inputs: inputs,
execFunc: d.client.execute{{ .Model.Name }}CreateManyAndReturn,
}
}

func (q *Queries) execute{{ .Model.Name }}CreateMany(ctx context.Context, inputs []{{ .Model.Name }}CreateInput) (int64, error) {
func (q *Queries) execute{{ .Model.Name }}CreateMany(ctx context.Context, inputs []{{ .Model.Name }}Create) (int64, error) {
if len(inputs) == 0 {
return 0, nil
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func (q *Queries) execute{{ .Model.Name }}CreateMany(ctx context.Context, inputs
return count, err
}

func (q *Queries) execute{{ .Model.Name }}CreateManyAndReturn(ctx context.Context, inputs []{{ .Model.Name }}CreateInput, selects *{{ .Model.Name }}Select, omits *{{ .Model.Name }}Omit) ([]*{{ .Model.Name }}, error) {
func (q *Queries) execute{{ .Model.Name }}CreateManyAndReturn(ctx context.Context, inputs []{{ .Model.Name }}Create, selects *{{ .Model.Name }}Select, omits *{{ .Model.Name }}Omit) ([]*{{ .Model.Name }}, error) {
if len(inputs) == 0 {
return nil, nil
}
Expand Down
10 changes: 5 additions & 5 deletions generator/templates/model_structs.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type {{ .Model.Name }} struct {
{{- end }}
}

// {{ .Model.Name }}CreateInput represents the input structure for creation
type {{ .Model.Name }}CreateInput struct {
// {{ .Model.Name }}Create represents the input structure for creation
type {{ .Model.Name }}Create struct {
{{- range $field := .Model.ScalarFields }}
{{ capitalize $field.Name }} {{ if $field.EnumRef }}{{ if $field.IsArray }}[]{{ $field.EnumRef.Name }}Type{{ else }}*{{ $field.EnumRef.Name }}Type{{ end }}{{ else }}{{ if $field.IsArray }}{{ $field.GoType }}{{ else }}{{ if and (ne $field.Default nil) (not $field.Optional) }}*{{ end }}{{ $field.GoType }}{{ end }}{{ end }} `json:"{{ $field.Name }}"`
{{- end }}
Expand Down Expand Up @@ -37,11 +37,11 @@ type {{ .Model.Name }}Omit struct {

type {{ .Model.Name }}Delegate struct {
client *Queries
beforeCreate func(context.Context, *{{ .Model.Name }}CreateInput) error
beforeCreate func(context.Context, *{{ .Model.Name }}Create) error
afterCreate func(context.Context, *{{ .Model.Name }}) error
}

func (d *{{ .Model.Name }}Delegate) BeforeCreate(hook func(context.Context, *{{ .Model.Name }}CreateInput) error) {
func (d *{{ .Model.Name }}Delegate) BeforeCreate(hook func(context.Context, *{{ .Model.Name }}Create) error) {
d.beforeCreate = hook
}

Expand Down Expand Up @@ -94,7 +94,7 @@ func (q *Queries) select{{ .Model.Name }}Cols(selects *{{ .Model.Name }}Select,
return cols
}

func (input {{ .Model.Name }}CreateInput) Validate() error {
func (input {{ .Model.Name }}Create) Validate() error {
errs := &ValidationError{}

{{- range $field := .Model.ScalarFields }}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module valkyrie
module valk

go 1.26.4

Expand Down
10 changes: 5 additions & 5 deletions integration/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"crypto/rand"
"fmt"
"integration/valkyrie"
"integration/valk"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestCreationBenchmark(t *testing.T) {
t.Logf("Running %d iterations of ORM Create...", iterations)
startORM := time.Now()
for i := range iterations {
_, err := db.User.Create(valkyrie.UserCreateInput{
_, err := db.User.Create(valk.UserCreate{
Email: fmt.Sprintf("orm-%d@example.com", i),
PhoneNum: fmt.Sprintf("+54321%d", i),
}).Exec(ctx)
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestCreationBenchmark(t *testing.T) {
t.Fatalf("Raw SQL write failed: %v", err)
}

var res valkyrie.User
var res valk.User
err = db.Raw().QueryRowContext(ctx,
query(
`SELECT "id", "email", "phoneNum", "role", "referredById" FROM "User" WHERE "id" = ?`,
Expand All @@ -112,7 +112,7 @@ func BenchmarkORMCreate(b *testing.B) {
ctx := context.Background()

for i := 0; b.Loop(); i++ {
_, err := db.User.Create(valkyrie.UserCreateInput{
_, err := db.User.Create(valk.UserCreate{
Email: fmt.Sprintf("bench-orm-%d@example.com", i),
PhoneNum: fmt.Sprintf("+98765%d", i),
}).Exec(ctx)
Expand Down Expand Up @@ -168,7 +168,7 @@ func BenchmarkRawSQLCreateWithScan(b *testing.B) {
b.Fatalf("Raw SQL write failed: %v", err)
}

var res valkyrie.User
var res valk.User
err = db.Raw().QueryRowContext(ctx,
query(
`SELECT "id", "email", "phoneNum", "role", "referredById" FROM "User" WHERE "id" = ?`,
Expand Down
12 changes: 6 additions & 6 deletions integration/create_many_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"integration/valkyrie"
"integration/valk"
"testing"
)

Expand All @@ -14,7 +14,7 @@ func TestCreateMany(t *testing.T) {
defer cleanup()

t.Run("CreateMany returns correct count", func(t *testing.T) {
count, err := client.User.CreateMany([]valkyrie.UserCreateInput{
count, err := client.User.CreateMany([]valk.UserCreate{
{
Email: "bulk1@example.com",
PhoneNum: "+111",
Expand Down Expand Up @@ -48,15 +48,15 @@ func TestCreateMany(t *testing.T) {
})

t.Run("CreateManyAndReturn works and supports Select", func(t *testing.T) {
author, err := client.User.Create(valkyrie.UserCreateInput{
author, err := client.User.Create(valk.UserCreate{
Email: "author@example.com",
PhoneNum: "+444",
}).Exec(ctx)
if err != nil {
t.Fatalf("failed to create author: %v", err)
}

posts, err := client.Post.CreateManyAndReturn([]valkyrie.PostCreateInput{
posts, err := client.Post.CreateManyAndReturn([]valk.PostCreate{
{
Title: "Post One",
AuthorId: author.Id,
Expand All @@ -65,10 +65,10 @@ func TestCreateMany(t *testing.T) {
Title: "Post Two",
AuthorId: author.Id,
},
}).Select(valkyrie.PostSelect{
}).Select(valk.PostSelect{
Id: true,
Title: true,
Author: &valkyrie.UserSelect{
Author: &valk.UserSelect{
Email: true,
},
}).Exec(ctx)
Expand Down
Loading
Loading