Skip to content

Commit a899d21

Browse files
authored
Fix jsonb (#30)
* update * update * update
1 parent 7d3f599 commit a899d21

File tree

6 files changed

+13
-9
lines changed

6 files changed

+13
-9
lines changed

query.go

+4
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ func type2readerFunc(t string) string {
172172
t = "float"
173173
}
174174

175+
if t == "jsonb" {
176+
t = "string"
177+
}
178+
175179
return sdk.ToLowerCamelCase(strings.Replace(t, " option", "OrNone", 1))
176180
}
177181

result.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func buildStructs(req *plugin.CodeGenRequest) []Struct {
3333
for _, column := range table.Columns {
3434
s.Fields = append(s.Fields, Field{
3535
Name: FieldName(column.Name, req.Settings),
36-
Type: jsonb2Str(fsType(req, column)),
36+
Type: fsType(req, column),
3737
Comment: column.Comment,
3838
})
3939
}
@@ -83,7 +83,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
8383
p := query.Params[0]
8484
gq.Arg = QueryValue{
8585
Name: paramName(p),
86-
Typ: jsonb2Str(fsType(req, p.Column)),
86+
Typ: fsType(req, p.Column),
8787
}
8888
} else if len(query.Params) > 1 {
8989
var cols []column
@@ -128,7 +128,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
128128
}
129129
gq.Ret = QueryValue{
130130
Name: name,
131-
Typ: jsonb2Str(fsType(req, c)),
131+
Typ: fsType(req, c),
132132
}
133133
} else if len(query.Columns) > 1 {
134134
var gs *Struct
@@ -142,7 +142,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
142142
for i, f := range s.Fields {
143143
c := query.Columns[i]
144144
sameName := f.Name == columnName(c, i)
145-
sameType := f.Type == jsonb2Str(fsType(req, c))
145+
sameType := f.Type == fsType(req, c)
146146
sameTable := sdk.SameTableName(c.Table, &s.Table, req.Catalog.DefaultSchema)
147147
if !sameName || !sameType || !sameTable {
148148
same = false

templates/model.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ open System
1313
{{- end -}}
1414
type {{.Name}} = {{ "{" }}
1515
{{range .Fields -}}
16-
{{" "}} {{.Name | pascalCase }}: {{.Type}} {{- if .Comment -}} // {{.Comment}} {{- end }}
16+
{{" "}} {{.Name | pascalCase }}: {{.Type | json2str }} {{- if .Comment -}} // {{.Comment}} {{- end }}
1717
{{end -}}
1818
{{ "}\n" }}
1919
{{end}}

templates/query.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type {{.Arg.Type}} = { {{- range .Arg.UniqueFields}}
3030

3131
{{if .Ret.EmitStruct}}
3232
type {{.Ret.Type}} = { {{- range .Ret.Struct.Fields}}
33-
{{.Name | pascalCase }}: {{.Type}};
33+
{{.Name | pascalCase }}: {{.Type | json2str }};
3434
{{- end}}
3535
}
3636
{{- end -}}

testdata/gen/chat_snapshot.sql.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ type ChatSnapshotMetaByUserIDRow = {
105105
Uuid: string;
106106
Title: string;
107107
Summary: string;
108-
Tags: jsonb;
108+
Tags: string;
109109
CreatedAt: DateTime;
110110
}
111111

@@ -115,7 +115,7 @@ let ChatSnapshotMetaByUserID (db: NpgsqlConnection) (userId: int32) =
115115
Uuid = read.string "uuid"
116116
Title = read.string "title"
117117
Summary = read.string "summary"
118-
Tags = read.jsonb "tags"
118+
Tags = read.string "tags"
119119
CreatedAt = read.dateTime "created_at"}
120120
db
121121
|> Sql.existingConnection

testdata/gen/model.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +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":""}]
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":"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 commit comments

Comments
 (0)