initial pase store definitions

This commit is contained in:
juancwu 2026-05-04 18:39:25 +00:00
commit b947535795
10 changed files with 723 additions and 0 deletions

54
store/jsonb.go Normal file
View file

@ -0,0 +1,54 @@
package store
import (
"database/sql/driver"
"fmt"
)
// JSONB is a nullable JSON value backed by raw bytes.
// Scans from jsonb (Postgres) or TEXT (SQLite). Empty means SQL NULL.
type JSONB []byte
// Scan implements sql.Scanner.
func (j *JSONB) Scan(src any) error {
if src == nil {
*j = nil
return nil
}
switch v := src.(type) {
case []byte:
// Copy: drivers may reuse the buffer between rows.
*j = append((*j)[:0], v...)
case string:
*j = []byte(v)
default:
return fmt.Errorf("pase: cannot scan %T into JSONB", src)
}
return nil
}
// Value implements driver.Valuer.
func (j JSONB) Value() (driver.Value, error) {
if len(j) == 0 {
return nil, nil
}
return []byte(j), nil
}
// MarshalJSON makes JSONB transparent in API responses.
func (j JSONB) MarshalJSON() ([]byte, error) {
if len(j) == 0 {
return []byte("null"), nil
}
return []byte(j), nil
}
// UnmarshalJSON lets you decode directly into JSONB.
func (j *JSONB) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
*j = nil
return nil
}
*j = append((*j)[:0], data...)
return nil
}