diff --git a/query.go b/query.go index fe24f11..770b91a 100644 --- a/query.go +++ b/query.go @@ -172,6 +172,10 @@ func type2readerFunc(t string) string { t = "float" } + if t == "jsonb" { + t = "string" + } + return sdk.ToLowerCamelCase(strings.Replace(t, " option", "OrNone", 1)) } diff --git a/result.go b/result.go index 6abf2a7..d11ec80 100644 --- a/result.go +++ b/result.go @@ -33,7 +33,7 @@ func buildStructs(req *plugin.CodeGenRequest) []Struct { for _, column := range table.Columns { s.Fields = append(s.Fields, Field{ Name: FieldName(column.Name, req.Settings), - Type: jsonb2Str(fsType(req, column)), + Type: fsType(req, column), Comment: column.Comment, }) } @@ -83,7 +83,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error) p := query.Params[0] gq.Arg = QueryValue{ Name: paramName(p), - Typ: jsonb2Str(fsType(req, p.Column)), + Typ: fsType(req, p.Column), } } else if len(query.Params) > 1 { var cols []column @@ -128,7 +128,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error) } gq.Ret = QueryValue{ Name: name, - Typ: jsonb2Str(fsType(req, c)), + Typ: fsType(req, c), } } else if len(query.Columns) > 1 { var gs *Struct @@ -142,7 +142,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error) for i, f := range s.Fields { c := query.Columns[i] sameName := f.Name == columnName(c, i) - sameType := f.Type == jsonb2Str(fsType(req, c)) + sameType := f.Type == fsType(req, c) sameTable := sdk.SameTableName(c.Table, &s.Table, req.Catalog.DefaultSchema) if !sameName || !sameType || !sameTable { same = false diff --git a/templates/model.tmpl b/templates/model.tmpl index abbff03..f69df7e 100644 --- a/templates/model.tmpl +++ b/templates/model.tmpl @@ -13,7 +13,7 @@ open System {{- end -}} type {{.Name}} = {{ "{" }} {{range .Fields -}} -{{" "}} {{.Name | pascalCase }}: {{.Type}} {{- if .Comment -}} // {{.Comment}} {{- end }} +{{" "}} {{.Name | pascalCase }}: {{.Type | json2str }} {{- if .Comment -}} // {{.Comment}} {{- end }} {{end -}} {{ "}\n" }} {{end}} diff --git a/templates/query.tmpl b/templates/query.tmpl index 633e06f..d5bc7ff 100644 --- a/templates/query.tmpl +++ b/templates/query.tmpl @@ -30,7 +30,7 @@ type {{.Arg.Type}} = { {{- range .Arg.UniqueFields}} {{if .Ret.EmitStruct}} type {{.Ret.Type}} = { {{- range .Ret.Struct.Fields}} - {{.Name | pascalCase }}: {{.Type}}; + {{.Name | pascalCase }}: {{.Type | json2str }}; {{- end}} } {{- end -}} diff --git a/testdata/gen/chat_snapshot.sql.fs b/testdata/gen/chat_snapshot.sql.fs index 2cf1bfc..4749ed7 100644 --- a/testdata/gen/chat_snapshot.sql.fs +++ b/testdata/gen/chat_snapshot.sql.fs @@ -105,7 +105,7 @@ type ChatSnapshotMetaByUserIDRow = { Uuid: string; Title: string; Summary: string; - Tags: jsonb; + Tags: string; CreatedAt: DateTime; } @@ -115,7 +115,7 @@ let ChatSnapshotMetaByUserID (db: NpgsqlConnection) (userId: int32) = Uuid = read.string "uuid" Title = read.string "title" Summary = read.string "summary" - Tags = read.jsonb "tags" + Tags = read.string "tags" CreatedAt = read.dateTime "created_at"} db |> Sql.existingConnection diff --git a/testdata/gen/model.json b/testdata/gen/model.json index b2d42b7..5084195 100644 --- a/testdata/gen/model.json +++ b/testdata/gen/model.json @@ -1 +1 @@ -[{"Table":{"schema":"public","name":"auth_user"},"Name":"AuthUser","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"password","DBName":"","Type":"string","Comment":""},{"Name":"last_login","DBName":"","Type":"DateTime","Comment":""},{"Name":"is_superuser","DBName":"","Type":"bool","Comment":""},{"Name":"username","DBName":"","Type":"string","Comment":""},{"Name":"first_name","DBName":"","Type":"string","Comment":""},{"Name":"last_name","DBName":"","Type":"string","Comment":""},{"Name":"email","DBName":"","Type":"string","Comment":""},{"Name":"is_staff","DBName":"","Type":"bool","Comment":""},{"Name":"is_active","DBName":"","Type":"bool","Comment":""},{"Name":"date_joined","DBName":"","Type":"DateTime","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"auth_user_management"},"Name":"AuthUserManagement","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"rate_limit","DBName":"","Type":"int32","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"authors"},"Name":"Author","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"name","DBName":"","Type":"string","Comment":""},{"Name":"bio","DBName":"","Type":"string option","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_logs"},"Name":"ChatLog","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"session","DBName":"","Type":"string","Comment":""},{"Name":"question","DBName":"","Type":"string","Comment":""},{"Name":"answer","DBName":"","Type":"string","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_message"},"Name":"ChatMessage","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"uuid","DBName":"","Type":"string","Comment":""},{"Name":"chat_session_uuid","DBName":"","Type":"string","Comment":""},{"Name":"role","DBName":"","Type":"string","Comment":""},{"Name":"content","DBName":"","Type":"string","Comment":""},{"Name":"score","DBName":"","Type":"float","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"created_by","DBName":"","Type":"int32","Comment":""},{"Name":"updated_by","DBName":"","Type":"int32","Comment":""},{"Name":"is_deleted","DBName":"","Type":"bool","Comment":""},{"Name":"is_pin","DBName":"","Type":"bool","Comment":""},{"Name":"token_count","DBName":"","Type":"int32","Comment":""},{"Name":"raw","DBName":"","Type":"string","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_model"},"Name":"ChatModel","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"name","DBName":"","Type":"string","Comment":""},{"Name":"label","DBName":"","Type":"string","Comment":""},{"Name":"is_default","DBName":"","Type":"bool","Comment":""},{"Name":"url","DBName":"","Type":"string","Comment":""},{"Name":"api_auth_header","DBName":"","Type":"string","Comment":""},{"Name":"api_auth_key","DBName":"","Type":"string","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"enable_per_mode_ratelimit","DBName":"","Type":"bool","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_prompt"},"Name":"ChatPrompt","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"uuid","DBName":"","Type":"string","Comment":""},{"Name":"chat_session_uuid","DBName":"","Type":"string","Comment":""},{"Name":"role","DBName":"","Type":"string","Comment":""},{"Name":"content","DBName":"","Type":"string","Comment":""},{"Name":"score","DBName":"","Type":"float","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"created_by","DBName":"","Type":"int32","Comment":""},{"Name":"updated_by","DBName":"","Type":"int32","Comment":""},{"Name":"is_deleted","DBName":"","Type":"bool","Comment":""},{"Name":"token_count","DBName":"","Type":"int32","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_session"},"Name":"ChatSession","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"uuid","DBName":"","Type":"string","Comment":""},{"Name":"topic","DBName":"","Type":"string","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"active","DBName":"","Type":"bool","Comment":""},{"Name":"model","DBName":"","Type":"string","Comment":""},{"Name":"max_length","DBName":"","Type":"int32","Comment":""},{"Name":"temperature","DBName":"","Type":"float","Comment":""},{"Name":"top_p","DBName":"","Type":"float","Comment":""},{"Name":"max_tokens","DBName":"","Type":"int32","Comment":""},{"Name":"n","DBName":"","Type":"int32","Comment":""},{"Name":"debug","DBName":"","Type":"bool","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_snapshot"},"Name":"ChatSnapshot","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"uuid","DBName":"","Type":"string","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"title","DBName":"","Type":"string","Comment":""},{"Name":"summary","DBName":"","Type":"string","Comment":""},{"Name":"model","DBName":"","Type":"string","Comment":""},{"Name":"tags","DBName":"","Type":"string","Comment":""},{"Name":"session","DBName":"","Type":"string","Comment":""},{"Name":"conversation","DBName":"","Type":"string","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"text","DBName":"","Type":"string","Comment":""},{"Name":"search_vector","DBName":"","Type":"string option","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"jwt_secrets"},"Name":"JwtSecret","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"name","DBName":"","Type":"string","Comment":""},{"Name":"secret","DBName":"","Type":"string","Comment":""},{"Name":"audience","DBName":"","Type":"string","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"user_active_chat_session"},"Name":"UserActiveChatSession","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"chat_session_uuid","DBName":"","Type":"string","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"user_chat_model_privilege"},"Name":"UserChatModelPrivilege","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"chat_model_id","DBName":"","Type":"int32","Comment":""},{"Name":"rate_limit","DBName":"","Type":"int32","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"created_by","DBName":"","Type":"int32","Comment":""},{"Name":"updated_by","DBName":"","Type":"int32","Comment":""}],"Comment":""}] \ No newline at end of file +[{"Table":{"schema":"public","name":"auth_user"},"Name":"AuthUser","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"password","DBName":"","Type":"string","Comment":""},{"Name":"last_login","DBName":"","Type":"DateTime","Comment":""},{"Name":"is_superuser","DBName":"","Type":"bool","Comment":""},{"Name":"username","DBName":"","Type":"string","Comment":""},{"Name":"first_name","DBName":"","Type":"string","Comment":""},{"Name":"last_name","DBName":"","Type":"string","Comment":""},{"Name":"email","DBName":"","Type":"string","Comment":""},{"Name":"is_staff","DBName":"","Type":"bool","Comment":""},{"Name":"is_active","DBName":"","Type":"bool","Comment":""},{"Name":"date_joined","DBName":"","Type":"DateTime","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"auth_user_management"},"Name":"AuthUserManagement","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"rate_limit","DBName":"","Type":"int32","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"authors"},"Name":"Author","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"name","DBName":"","Type":"string","Comment":""},{"Name":"bio","DBName":"","Type":"string option","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_logs"},"Name":"ChatLog","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"session","DBName":"","Type":"jsonb","Comment":""},{"Name":"question","DBName":"","Type":"jsonb","Comment":""},{"Name":"answer","DBName":"","Type":"jsonb","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_message"},"Name":"ChatMessage","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"uuid","DBName":"","Type":"string","Comment":""},{"Name":"chat_session_uuid","DBName":"","Type":"string","Comment":""},{"Name":"role","DBName":"","Type":"string","Comment":""},{"Name":"content","DBName":"","Type":"string","Comment":""},{"Name":"score","DBName":"","Type":"float","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"created_by","DBName":"","Type":"int32","Comment":""},{"Name":"updated_by","DBName":"","Type":"int32","Comment":""},{"Name":"is_deleted","DBName":"","Type":"bool","Comment":""},{"Name":"is_pin","DBName":"","Type":"bool","Comment":""},{"Name":"token_count","DBName":"","Type":"int32","Comment":""},{"Name":"raw","DBName":"","Type":"jsonb","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_model"},"Name":"ChatModel","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"name","DBName":"","Type":"string","Comment":""},{"Name":"label","DBName":"","Type":"string","Comment":""},{"Name":"is_default","DBName":"","Type":"bool","Comment":""},{"Name":"url","DBName":"","Type":"string","Comment":""},{"Name":"api_auth_header","DBName":"","Type":"string","Comment":""},{"Name":"api_auth_key","DBName":"","Type":"string","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"enable_per_mode_ratelimit","DBName":"","Type":"bool","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_prompt"},"Name":"ChatPrompt","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"uuid","DBName":"","Type":"string","Comment":""},{"Name":"chat_session_uuid","DBName":"","Type":"string","Comment":""},{"Name":"role","DBName":"","Type":"string","Comment":""},{"Name":"content","DBName":"","Type":"string","Comment":""},{"Name":"score","DBName":"","Type":"float","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"created_by","DBName":"","Type":"int32","Comment":""},{"Name":"updated_by","DBName":"","Type":"int32","Comment":""},{"Name":"is_deleted","DBName":"","Type":"bool","Comment":""},{"Name":"token_count","DBName":"","Type":"int32","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_session"},"Name":"ChatSession","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"uuid","DBName":"","Type":"string","Comment":""},{"Name":"topic","DBName":"","Type":"string","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"active","DBName":"","Type":"bool","Comment":""},{"Name":"model","DBName":"","Type":"string","Comment":""},{"Name":"max_length","DBName":"","Type":"int32","Comment":""},{"Name":"temperature","DBName":"","Type":"float","Comment":""},{"Name":"top_p","DBName":"","Type":"float","Comment":""},{"Name":"max_tokens","DBName":"","Type":"int32","Comment":""},{"Name":"n","DBName":"","Type":"int32","Comment":""},{"Name":"debug","DBName":"","Type":"bool","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"chat_snapshot"},"Name":"ChatSnapshot","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"uuid","DBName":"","Type":"string","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"title","DBName":"","Type":"string","Comment":""},{"Name":"summary","DBName":"","Type":"string","Comment":""},{"Name":"model","DBName":"","Type":"string","Comment":""},{"Name":"tags","DBName":"","Type":"jsonb","Comment":""},{"Name":"session","DBName":"","Type":"jsonb","Comment":""},{"Name":"conversation","DBName":"","Type":"jsonb","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"text","DBName":"","Type":"string","Comment":""},{"Name":"search_vector","DBName":"","Type":"string option","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"jwt_secrets"},"Name":"JwtSecret","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"name","DBName":"","Type":"string","Comment":""},{"Name":"secret","DBName":"","Type":"string","Comment":""},{"Name":"audience","DBName":"","Type":"string","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"user_active_chat_session"},"Name":"UserActiveChatSession","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"chat_session_uuid","DBName":"","Type":"string","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""}],"Comment":""},{"Table":{"schema":"public","name":"user_chat_model_privilege"},"Name":"UserChatModelPrivilege","Fields":[{"Name":"id","DBName":"","Type":"int32","Comment":""},{"Name":"user_id","DBName":"","Type":"int32","Comment":""},{"Name":"chat_model_id","DBName":"","Type":"int32","Comment":""},{"Name":"rate_limit","DBName":"","Type":"int32","Comment":""},{"Name":"created_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"updated_at","DBName":"","Type":"DateTime","Comment":""},{"Name":"created_by","DBName":"","Type":"int32","Comment":""},{"Name":"updated_by","DBName":"","Type":"int32","Comment":""}],"Comment":""}] \ No newline at end of file