Skip to content

Commit

Permalink
Fix jsonb (#30)
Browse files Browse the repository at this point in the history
* update

* update

* update
  • Loading branch information
swuecho authored May 14, 2023
1 parent 7d3f599 commit a899d21
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
8 changes: 4 additions & 4 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion templates/model.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
2 changes: 1 addition & 1 deletion templates/query.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand Down
4 changes: 2 additions & 2 deletions testdata/gen/chat_snapshot.sql.fs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type ChatSnapshotMetaByUserIDRow = {
Uuid: string;
Title: string;
Summary: string;
Tags: jsonb;
Tags: string;
CreatedAt: DateTime;
}

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion testdata/gen/model.json
Original file line number Diff line number Diff line change
@@ -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":""}]
[{"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":""}]

0 comments on commit a899d21

Please sign in to comment.