Skip to content

Commit bc0c14e

Browse files
committed
**feat(sqlite): enhance JSON handling and update type mapping**
- **JSON Type Mapping**: - Replace `datatypes.JSON` with `datatypes.JSONMap` for more versatile JSON and JSONB handling in SQLite mappings. - **E2E Tests**: - Add `json_col` to the test schema for JSONB field support. - Update test records with new JSON field initialization, inserts, and updates. - **Custom Type Mapping**: - Implement support for dynamic type mapping by merging user-defined type maps with the default SQLite type map. This update improves JSON handling, extends test coverage, and adds flexibility for overriding type mappings.
1 parent c47c9be commit bc0c14e

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

e2e_sqlite_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func TestEndToEndSQLite(t *testing.T) {
5353
ts_col TIMESTAMP,
5454
numeric_col NUMERIC,
5555
decimal_col DECIMAL,
56-
duration_col DURATION
56+
duration_col DURATION,
57+
json_col JSONB
5758
);`,
5859
// second table to exercise relation via ExtraFields (one-to-many)
5960
`CREATE TABLE IF NOT EXISTS child (
@@ -172,19 +173,22 @@ Sqlitedbpath = %q
172173
import (
173174
"fmt"
174175
"time"
176+
"gorm.io/datatypes"
175177
g "%s/%s"
176178
m "%s/%s/models"
177179
)
178180
func main(){
179181
g.DbInit(%q)
180182
// Insert
181-
a := &m.%s{BoolCol: ptrBool(true), Tiny1: ptrStr("1"), IntCol: ptrI64(42), BigCol: ptrI64(4200), RealCol: ptrF64(1.5), DoubleCol: ptrF64(2.5), FloatCol: ptrF32(3.5), TextCol: ptrStr("hello"), VarcharCol: ptrStr("v"), CharCol: ptrStr("c"), BlobCol: ptrBytes([]byte{1,2,3}), DateCol: ptrTime(1700000000), DatetimeCol: ptrTime(1700000100), TsCol: ptrTime(1700000200), NumericCol: ptrF64(10.5), DecimalCol: ptrF64(20.5), DurationCol: ptrDur(1234567890)}
183+
js := datatypes.JSONMap(map[string]any{"a": 1, "b": 2})
184+
a := &m.%s{BoolCol: ptrBool(true), Tiny1: ptrStr("1"), IntCol: ptrI64(42), BigCol: ptrI64(4200), RealCol: ptrF64(1.5), DoubleCol: ptrF64(2.5), FloatCol: ptrF32(3.5), TextCol: ptrStr("hello"), VarcharCol: ptrStr("v"), CharCol: ptrStr("c"), BlobCol: ptrBytes([]byte{1,2,3}), DateCol: ptrTime(1700000000), DatetimeCol: ptrTime(1700000100), TsCol: ptrTime(1700000200), NumericCol: ptrF64(10.5), DecimalCol: ptrF64(20.5), DurationCol: ptrDur(1234567890), JSONCol: &js}
182185
if err := g.DB.Create(a).Error; err != nil { panic(err) }
183186
// Read
184187
var got m.%s
185188
if err := g.DB.First(&got, a.ID).Error; err != nil { panic(err) }
186189
// Update each field
187190
b := false
191+
jsu := datatypes.JSONMap(map[string]any{"c": 3, "d": 4})
188192
if err := g.DB.Model(&got).Updates(map[string]any{
189193
"bool_col": &b,
190194
"tiny1": ptrStr("0"),
@@ -203,6 +207,7 @@ func main(){
203207
"numeric_col": ptrF64(11.5),
204208
"decimal_col": ptrF64(21.5),
205209
"duration_col": ptrDur(987654321),
210+
"json_col": &jsu,
206211
}).Error; err != nil { panic(err) }
207212
var after m.%s
208213
if err := g.DB.First(&after, a.ID).Error; err != nil { panic(err) }

sqlite.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ func sqliteToGorm(cfg ConversionConfig) {
4444
// JSON tag strategy same as Postgres generator
4545
g.WithJSONTagNameStrategy(func(col string) (tag string) { return strcase.ToLowerCamel(col) })
4646
// Use SQLite-specific type map (no Postgres materialized views handling)
47-
g.WithDataTypeMap(sqlitetype.TypeMap)
47+
dtMaps := sqlitetype.TypeMap
48+
for k, v := range cfg.TypeMap {
49+
dtMaps[k] = func(columnType gorm.ColumnType) string { return v }
50+
}
51+
g.WithDataTypeMap(dtMaps)
4852
g.WithImportPkgPath("gorm.io/datatypes")
4953
g.UseDB(db)
5054

sqlitetype/sqlitetype.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ var TypeMap = map[string]func(gorm.ColumnType) string{
4848
"CHAR": func(ct gorm.ColumnType) string { n, _ := ct.Nullable(); return nullablePtr(n, "string") },
4949
"CLOB": func(ct gorm.ColumnType) string { n, _ := ct.Nullable(); return nullablePtr(n, "string") },
5050
"UUID": func(ct gorm.ColumnType) string { return "datatypes.UUID" },
51-
"JSON": func(ct gorm.ColumnType) string { return "datatypes.JSON" },
52-
"JSONB": func(ct gorm.ColumnType) string { return "datatypes.JSON" },
51+
"JSON": func(ct gorm.ColumnType) string { return "datatypes.JSONMap" },
52+
"JSONB": func(ct gorm.ColumnType) string { return "datatypes.JSONMap" },
5353

5454
// ---- bytes ----
5555
"BLOB": func(gorm.ColumnType) string { return "[]byte" },

0 commit comments

Comments
 (0)