types
import types "ganigeorgiev.com/pocketbase/pocketbase/tools/types"
Package types implements some commonly used db serializable types like datetime, json, etc.
Source: tools/types.
Constants
const DefaultDateLayout = "2006-01-02 15:04:05.000Z"
DefaultDateLayout specifies the default app date strings layout.
Functions
Pointer
func Pointer[T any](val T) *T
Pointer is a generic helper that returns val as *T.
Types
DateTime
type DateTime struct {
// contains filtered or unexported fields
}
DateTime represents a time.Time instance in UTC that is wrapped and serialized using the app default date layout.
NowDateTime
func NowDateTime() DateTime
NowDateTime returns new DateTime instance with the current local time.
ParseDateTime
func ParseDateTime(value any) (DateTime, error)
ParseDateTime creates a new DateTime from the provided value (could be cast.ToTime supported string, time.Time, etc.).
DateTime.Add
func (d DateTime) Add(duration time.Duration) DateTime
Add returns a new DateTime based on the current DateTime + the specified duration.
DateTime.AddDate
func (d DateTime) AddDate(years, months, days int) DateTime
AddDate returns a new DateTime based on the current one + duration.
It follows the same rules as time.AddDate.
DateTime.After
func (d DateTime) After(u DateTime) bool
After reports whether the current DateTime instance is after u.
DateTime.Before
func (d DateTime) Before(u DateTime) bool
Before reports whether the current DateTime instance is before u.
DateTime.Compare
func (d DateTime) Compare(u DateTime) int
Compare compares the current DateTime instance with u. If the current instance is before u, it returns -1. If the current instance is after u, it returns +1. If they're the same, it returns 0.
DateTime.Equal
func (d DateTime) Equal(u DateTime) bool
Equal reports whether the current DateTime and u represent the same time instant. Two DateTime can be equal even if they are in different locations. For example, 6:00 +0200 and 4:00 UTC are Equal.
DateTime.IsZero
func (d DateTime) IsZero() bool
IsZero checks whether the current DateTime instance has zero time value.
DateTime.MarshalJSON
func (d DateTime) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
DateTime.Scan
func (d *DateTime) Scan(value any) error
Scan implements [sql.Scanner] interface to scan the provided value into the current DateTime instance.
DateTime.String
func (d DateTime) String() string
String serializes the current DateTime instance into a formatted UTC date string.
The zero value is serialized to an empty string.
DateTime.Sub
func (d DateTime) Sub(u DateTime) time.Duration
Sub returns a time.Duration by subtracting the specified DateTime from the current one.
If the result exceeds the maximum (or minimum) value that can be stored in a time.Duration, the maximum (or minimum) duration will be returned.
DateTime.Time
func (d DateTime) Time() time.Time
Time returns the internal time.Time instance.
DateTime.Unix
func (d DateTime) Unix() int64
Unix returns the current DateTime as a Unix time, aka. the number of seconds elapsed since January 1, 1970 UTC.
DateTime.UnmarshalJSON
func (d *DateTime) UnmarshalJSON(b []byte) error
UnmarshalJSON implements the json.Unmarshaler interface.
DateTime.Value
func (d DateTime) Value() (driver.Value, error)
Value implements the driver.Valuer interface.
GeoPoint
type GeoPoint struct {
Lon float64 `form:"lon" json:"lon"`
Lat float64 `form:"lat" json:"lat"`
}
GeoPoint defines a struct for storing geo coordinates as serialized json object (e.g. {lon:0,lat:0}).
Note: using object notation and not a plain array to avoid the confusion as there doesn't seem to be a fixed standard for the coordinates order.
GeoPoint.AsMap
func (p GeoPoint) AsMap() map[string]any
AsMap implements [core.mapExtractor] and returns a value suitable to be used in an API rule expression.
GeoPoint.Scan
func (p *GeoPoint) Scan(value any) error
Scan implements [sql.Scanner] interface to scan the provided value into the current GeoPoint instance.
The value argument could be nil (no-op), another GeoPoint instance, map or serialized json object with lat-lon props.
GeoPoint.String
func (p GeoPoint) String() string
String returns the string representation of the current GeoPoint instance.
GeoPoint.Value
func (p GeoPoint) Value() (driver.Value, error)
Value implements the driver.Valuer interface.
JSONArray
type JSONArray[T any] []T
JSONArray defines a slice that is safe for json and db read/write.
JSONArray.MarshalJSON
func (m JSONArray[T]) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
JSONArray.Scan
func (m *JSONArray[T]) Scan(value any) error
Scan implements [sql.Scanner] interface to scan the provided value into the current JSONArray[T] instance.
JSONArray.String
func (m JSONArray[T]) String() string
String returns the string representation of the current json array.
JSONArray.Value
func (m JSONArray[T]) Value() (driver.Value, error)
Value implements the driver.Valuer interface.
JSONMap
type JSONMap[T any] map[string]T
JSONMap defines a map that is safe for json and db read/write.
JSONMap.Get
func (m JSONMap[T]) Get(key string) T
Get retrieves a single value from the current JSONMap[T].
This helper was added primarily to assist the goja integration since custom map types don't have direct access to the map keys (https://pkg.go.dev/github.com/dop251/goja#hdr-Maps_with_methods).
JSONMap.MarshalJSON
func (m JSONMap[T]) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
JSONMap.Scan
func (m *JSONMap[T]) Scan(value any) error
Scan implements [sql.Scanner] interface to scan the provided value into the current JSONMap[T] instance.
JSONMap.Set
func (m JSONMap[T]) Set(key string, value T)
Set sets a single value in the current JSONMap[T].
This helper was added primarily to assist the goja integration since custom map types don't have direct access to the map keys (https://pkg.go.dev/github.com/dop251/goja#hdr-Maps_with_methods).
JSONMap.String
func (m JSONMap[T]) String() string
String returns the string representation of the current json map.
JSONMap.Value
func (m JSONMap[T]) Value() (driver.Value, error)
Value implements the driver.Valuer interface.
JSONRaw
type JSONRaw []byte
JSONRaw defines a json value type that is safe for db read/write.
ParseJSONRaw
func ParseJSONRaw(value any) (JSONRaw, error)
ParseJSONRaw creates a new JSONRaw instance from the provided value (could be JSONRaw, int, float, string, []byte, etc.).
JSONRaw.MarshalJSON
func (j JSONRaw) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
JSONRaw.Scan
func (j *JSONRaw) Scan(value any) error
Scan implements [sql.Scanner] interface to scan the provided value into the current JSONRaw instance.
JSONRaw.String
func (j JSONRaw) String() string
String returns the current JSONRaw instance as a json encoded string.
JSONRaw.UnmarshalJSON
func (j *JSONRaw) UnmarshalJSON(b []byte) error
UnmarshalJSON implements the json.Unmarshaler interface.
JSONRaw.Value
func (j JSONRaw) Value() (driver.Value, error)
Value implements the driver.Valuer interface.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.