Documentation
¶
Overview ¶
Package fields provides [tidal.Model] column types for JSON, string arrays, and normalized timestamps.
Use them as fields on [tidal.Model] structs. They implement database/sql.Scanner and database/sql/driver.Valuer, so [tidal.CRUD] queries read and write them without extra conversion code. Backing columns should store JSON (JSONB, BYTEA, BLOB, or TEXT depending on your database).
Example:
type Document struct {
tidal.BaseModel
Metadata fields.JSONB
Tags fields.StringArray
SeenAt fields.Timestamp
}
Index ¶
- Constants
- Variables
- type JSONB
- type NullJSONB
- type NullStringArray
- type StringArray
- type Timestamp
- func (t Timestamp) Add(d time.Duration) Timestamp
- func (t Timestamp) After(other Timestamp) bool
- func (t Timestamp) Before(other Timestamp) bool
- func (t Timestamp) Compare(other Timestamp) int
- func (t Timestamp) Equal(other Timestamp) bool
- func (t Timestamp) Format(layout string) string
- func (t Timestamp) IsZero() bool
- func (t Timestamp) MarshalJSON() ([]byte, error)
- func (t *Timestamp) Now()
- func (t *Timestamp) Scan(src any) (err error)
- func (t *Timestamp) Set(ts time.Time)
- func (t Timestamp) Since(other Timestamp) time.Duration
- func (t Timestamp) String() string
- func (t Timestamp) Sub(other Timestamp) time.Duration
- func (t Timestamp) Time() time.Time
- func (t Timestamp) UTC() time.Time
- func (t Timestamp) Unix() int64
- func (t Timestamp) UnixMilli() int64
- func (t *Timestamp) UnmarshalJSON(data []byte) (err error)
- func (t Timestamp) Until(other Timestamp) time.Duration
- func (t Timestamp) Value() (driver.Value, error)
Constants ¶
const ISO8601Milli = "2006-01-02T15:04:05.000Z07:00"
Variables ¶
var JSONNull = []byte("null")
JSONNull is the JSON literal null as bytes.
Functions ¶
This section is empty.
Types ¶
type JSONB ¶
type JSONB json.RawMessage
JSONB stores raw JSON in a NOT NULL column. SQL NULL and JSON "null" scan as nil.
func (JSONB) Equal ¶ added in v1.2.0
Equal compares JSON values after normalization so key order differences do not matter.
func (*JSONB) MarshalFrom ¶
MarshalFrom JSON-encodes src into the field. A nil src clears the field.
func (JSONB) Normalize ¶
Normalizes the JSON bytes for hashing: values round-trip through encoding/json so object keys are emitted in sorted order. Invalid JSON is returned unchanged (copy). Numbers become float64 when decoded as any; empty input returns nil.
func (JSONB) UnmarshalTo ¶
UnmarshalTo decodes the JSON into dst. A nil or empty value is a no-op.
type NullJSONB ¶
NullJSONB stores JSON in a nullable column. Check Valid after scanning.
func (NullJSONB) Equal ¶ added in v1.2.0
Equal compares nullable JSON values with SQL NULL normalization.
func (*NullJSONB) MarshalFrom ¶
MarshalFrom JSON-encodes src and sets Valid. Nil or JSON null becomes SQL NULL.
func (*NullJSONB) Scan ¶
Scan implements database/sql.Scanner.
func (NullJSONB) UnmarshalTo ¶
UnmarshalTo decodes the JSON when Valid is true.
type NullStringArray ¶
type NullStringArray struct {
Valid bool
StringArray StringArray
}
NullStringArray stores a list of strings in a nullable column. Check Valid after scanning.
func (NullStringArray) Equal ¶ added in v1.2.0
func (n NullStringArray) Equal(other NullStringArray) bool
Equal compares nullable arrays, normalizing empty arrays to SQL NULL semantics.
func (*NullStringArray) Scan ¶
func (n *NullStringArray) Scan(src any) (err error)
Scan implements database/sql.Scanner.
func (NullStringArray) Value ¶
func (n NullStringArray) Value() (driver.Value, error)
Value implements database/sql/driver.Valuer.
type StringArray ¶
type StringArray []string
StringArray stores a list of strings as a JSON array in a NOT NULL column.
func (StringArray) Equal ¶ added in v1.2.0
func (s StringArray) Equal(other StringArray) bool
Equal compares two arrays, treating nil and empty arrays as equivalent.
func (*StringArray) Scan ¶
func (s *StringArray) Scan(src any) (err error)
Scan implements database/sql.Scanner.
func (StringArray) Value ¶
func (s StringArray) Value() (driver.Value, error)
Value implements database/sql/driver.Valuer.
type Timestamp ¶ added in v1.5.0
type Timestamp struct {
// contains filtered or unexported fields
}
Timestamp wraps a time.Time value in order to ensure that the underlying database always has a timestamp stored in the UTC timezone and truncated to millisecond precision. It also treats zero valued timestamps as NULL values so there is no need for a NullTime type. This timestamp is not well suited for all use cases, but generally can unify timestamp behavior across different databases.
func TimeNow ¶ added in v1.5.1
func TimeNow() Timestamp
Returns a new Timestamp with the current time.
func (Timestamp) Add ¶ added in v1.5.0
Returns a new normalized Timestamp by adding the given duration.
func (Timestamp) After ¶ added in v1.5.1
Returns true if the timestamp is after the other timestamp.
func (Timestamp) Before ¶ added in v1.5.1
Returns true if the timestamp is before the other timestamp.
func (Timestamp) Compare ¶ added in v1.5.0
Returns the result of comparing the two timestamps (0 if equal, -1 if less, 1 if greater).
func (Timestamp) Equal ¶ added in v1.5.0
Returns true if the timestamp is equal to the other timestamp.
func (Timestamp) MarshalJSON ¶ added in v1.5.0
func (*Timestamp) Now ¶ added in v1.5.0
func (t *Timestamp) Now()
Sets the timestamp to the current UTC time.
func (*Timestamp) Set ¶ added in v1.5.0
Set the timestamp to the given time in the UTC timezone, truncated to millisecond precision.
func (Timestamp) Since ¶ added in v1.5.1
Returns the duration since the other timestamp. Equivalent to Timestamp.Sub.
func (Timestamp) Time ¶ added in v1.5.0
Returns the underlying time.Time value (already normalized when set).
func (*Timestamp) UnmarshalJSON ¶ added in v1.5.0
func (Timestamp) Until ¶ added in v1.5.1
Returns the duration until the other timestamp. Equivalent to argument-inverted Timestamp.Sub.