Skip to content

Commit

Permalink
models/database: update generate.go with postgres db model
Browse files Browse the repository at this point in the history
We need to swap out datetime with timestamptz in order to properly allow for a postgres DB provider
  • Loading branch information
nickmurray47 committed Oct 10, 2023
1 parent 816d823 commit 83d90cc
Showing 1 changed file with 59 additions and 8 deletions.
67 changes: 59 additions & 8 deletions backend/pkg/models/database/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ package main
import (
"encoding/json"
"fmt"
"github.com/dave/jennifer/jen"
"github.com/iancoleman/strcase"
"golang.org/x/exp/slices"
"io/ioutil"
"log"
"os"
"sort"
"strings"

"github.com/dave/jennifer/jen"
"github.com/fastenhealth/fasten-onprem/backend/pkg/config"
"github.com/fastenhealth/fasten-onprem/backend/pkg/errors"
"github.com/iancoleman/strcase"
"golang.org/x/exp/slices"
)

type SearchParameter struct {
Expand Down Expand Up @@ -47,6 +51,22 @@ PLEASE DO NOT EDIT BY HAND
`, "\n"), "\n")

func main() {
// Read config file for database type
appconfig, err := config.Create()
if err != nil {
fmt.Printf("FATAL: %+v\n", err)
os.Exit(1)
}

// Find and read the config file
err = appconfig.ReadConfig("../../../../config.yaml")
if _, ok := err.(errors.ConfigFileMissingError); ok { // Handle errors reading the config file
//ignore "could not find config file"
} else if err != nil {
os.Exit(1)
}
databaseType := appconfig.GetString("database.type")

// Read the search-parameters.json file
searchParamsData, err := ioutil.ReadFile("search-parameters.json")
if err != nil {
Expand Down Expand Up @@ -188,10 +208,18 @@ func main() {
golangFieldStatement = g.Id(fieldName).Id(golangFieldType)
}
}
golangFieldStatement.Tag(map[string]string{
"json": fmt.Sprintf("%s,omitempty", strcase.ToLowerCamel(fieldName)),
"gorm": fmt.Sprintf("column:%s;%s", strcase.ToLowerCamel(fieldName), mapGormType(fieldInfo.FieldType)),
})

if databaseType == "sqlite" {
golangFieldStatement.Tag(map[string]string{
"json": fmt.Sprintf("%s,omitempty", strcase.ToLowerCamel(fieldName)),
"gorm": fmt.Sprintf("column:%s;%s", strcase.ToLowerCamel(fieldName), mapGormTypeSqlite(fieldInfo.FieldType)),
})
} else {
golangFieldStatement.Tag(map[string]string{
"json": fmt.Sprintf("%s,omitempty", strcase.ToLowerCamel(fieldName)),
"gorm": fmt.Sprintf("column:%s;%s", strcase.ToLowerCamel(fieldName), mapGormTypePostgres(fieldInfo.FieldType)),
})
}
}
})

Expand Down Expand Up @@ -663,7 +691,7 @@ func mapFieldType(fieldType string) string {
}

// https://www.sqlite.org/datatype3.html
func mapGormType(fieldType string) string {
func mapGormTypeSqlite(fieldType string) string {
// gorm:"type:text;serializer:json"

switch fieldType {
Expand All @@ -687,3 +715,26 @@ func mapGormType(fieldType string) string {
return "type:text"
}
}

func mapGormTypePostgres(fieldType string) string {
switch fieldType {
case "number":
return "type:real"
case "token":
return "type:text;serializer:json"
case "reference":
return "type:text;serializer:json"
case "date":
return "type:timestamptz"
case "string":
return "type:text;serializer:json"
case "uri":
return "type:text"
case "special":
return "type:text;serializer:json"
case "quantity":
return "type:text;serializer:json"
default:
return "type:text"
}
}

0 comments on commit 83d90cc

Please # to comment.