Documentation ¶
Overview ¶
Package models implements all PocketBase DB models and DTOs.
Index ¶
- Constants
- type Admin
- type BackupFileInfo
- type BaseModel
- func (m *BaseModel) GetCreated() types.DateTime
- func (m *BaseModel) GetId() string
- func (m *BaseModel) GetUpdated() types.DateTime
- func (m *BaseModel) HasId() bool
- func (m *BaseModel) IsNew() bool
- func (m *BaseModel) MarkAsNew()
- func (m *BaseModel) MarkAsNotNew()
- func (m *BaseModel) PostScan() error
- func (m *BaseModel) RefreshCreated()
- func (m *BaseModel) RefreshId()
- func (m *BaseModel) RefreshUpdated()
- func (m *BaseModel) SetId(id string)
- type Collection
- func (m *Collection) AuthOptions() CollectionAuthOptions
- func (m *Collection) BaseFilesPath() string
- func (m *Collection) BaseOptions() CollectionBaseOptions
- func (m *Collection) DecodeOptions(result any) error
- func (m *Collection) IsAuth() bool
- func (m *Collection) IsBase() bool
- func (m *Collection) IsView() bool
- func (m Collection) MarshalJSON() ([]byte, error)
- func (m *Collection) NormalizeOptions() error
- func (m *Collection) SetOptions(typedOptions any) error
- func (m *Collection) TableName() string
- func (m *Collection) ViewOptions() CollectionViewOptions
- type CollectionAuthOptions
- type CollectionBaseOptions
- type CollectionViewOptions
- type ColumnValueMapper
- type ExternalAuth
- type FilesManager
- type Log
- type Model
- type Param
- type Record
- func (m *Record) BaseFilesPath() string
- func (m *Record) CleanCopy() *Record
- func (m *Record) Collection() *Collection
- func (m *Record) ColumnValueMap() map[string]any
- func (m *Record) Email() string
- func (m *Record) EmailVisibility() bool
- func (m *Record) Expand() map[string]any
- func (m *Record) ExpandedAll(relField string) []*Record
- func (m *Record) ExpandedOne(relField string) *Record
- func (m *Record) FindFileFieldByFile(filename string) *schema.SchemaField
- func (m *Record) Get(key string) any
- func (m *Record) GetBool(key string) bool
- func (m *Record) GetDateTime(key string) types.DateTime
- func (m *Record) GetFloat(key string) float64
- func (m *Record) GetInt(key string) int
- func (m *Record) GetString(key string) string
- func (m *Record) GetStringSlice(key string) []string
- func (m *Record) GetTime(key string) time.Time
- func (m *Record) IgnoreEmailVisibility(state bool)
- func (m *Record) LastResetSentAt() types.DateTime
- func (m *Record) LastVerificationSentAt() types.DateTime
- func (m *Record) Load(data map[string]any)
- func (m Record) MarshalJSON() ([]byte, error)
- func (m *Record) MergeExpand(expand map[string]any)
- func (m *Record) OriginalCopy() *Record
- func (m *Record) PasswordHash() string
- func (m *Record) PublicExport() map[string]any
- func (m *Record) RefreshTokenKey() error
- func (m *Record) ReplaceModifers(data map[string]any) map[string]any
- func (m *Record) SchemaData() map[string]any
- func (m *Record) Set(key string, value any)
- func (m *Record) SetEmail(email string) error
- func (m *Record) SetEmailVisibility(visible bool) error
- func (m *Record) SetExpand(expand map[string]any)
- func (m *Record) SetLastResetSentAt(dateTime types.DateTime) error
- func (m *Record) SetLastVerificationSentAt(dateTime types.DateTime) error
- func (m *Record) SetPassword(password string) error
- func (m *Record) SetTokenKey(key string) error
- func (m *Record) SetUsername(username string) error
- func (m *Record) SetVerified(verified bool) error
- func (m *Record) TableName() string
- func (m *Record) TokenKey() string
- func (m *Record) UnknownData() map[string]any
- func (m *Record) UnmarshalJSON(data []byte) error
- func (m *Record) UnmarshalJSONField(key string, result any) error
- func (m *Record) Username() string
- func (m *Record) ValidatePassword(password string) bool
- func (m *Record) Verified() bool
- func (m *Record) WithUnknownData(state bool)
- type Requestdeprecated
- type RequestInfo
- type TableInfoRow
Constants ¶
const ( // DefaultIdLength is the default length of the generated model id. DefaultIdLength = 15 // DefaultIdAlphabet is the default characters set used for generating the model id. DefaultIdAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789" )
const ( CollectionTypeBase = "base" CollectionTypeAuth = "auth" CollectionTypeView = "view" )
const ( RequestAuthGuest = "guest" RequestAuthAdmin = "admin" RequestAuthRecord = "authRecord" )
list with the supported values for `Request.Auth`
const (
ParamAppSettings = "settings"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Admin ¶
type Admin struct { BaseModel Avatar int `db:"avatar" json:"avatar"` Email string `db:"email" json:"email"` TokenKey string `db:"tokenKey" json:"-"` PasswordHash string `db:"passwordHash" json:"-"` LastResetSentAt types.DateTime `db:"lastResetSentAt" json:"-"` }
func (*Admin) RefreshTokenKey ¶ added in v0.8.0
RefreshTokenKey generates and sets new random token key.
func (*Admin) SetPassword ¶ added in v0.8.0
SetPassword sets cryptographically secure string to `model.Password`.
Additionally this method also resets the LastResetSentAt and the TokenKey fields.
func (*Admin) ValidatePassword ¶ added in v0.8.0
ValidatePassword validates a plain password against the model's password.
type BackupFileInfo ¶ added in v0.16.0
type BaseModel ¶
type BaseModel struct { Id string `db:"id" json:"id"` Created types.DateTime `db:"created" json:"created"` Updated types.DateTime `db:"updated" json:"updated"` // contains filtered or unexported fields }
BaseModel defines common fields and methods used by all other models.
func (*BaseModel) GetCreated ¶
GetCreated returns the model Created datetime.
func (*BaseModel) GetUpdated ¶
GetUpdated returns the model Updated datetime.
func (*BaseModel) IsNew ¶ added in v0.4.0
IsNew indicates what type of db query (insert or update) should be used with the model instance.
func (*BaseModel) MarkAsNew ¶ added in v0.4.0
func (m *BaseModel) MarkAsNew()
MarkAsNew marks the model as "new" (aka. enforces m.IsNew() to be true).
func (*BaseModel) MarkAsNotNew ¶ added in v0.9.0
func (m *BaseModel) MarkAsNotNew()
MarkAsNotNew marks the model as "not new" (aka. enforces m.IsNew() to be false)
func (*BaseModel) PostScan ¶ added in v0.9.0
PostScan implements the dbx.PostScanner interface.
It is executed right after the model was populated with the db row values.
func (*BaseModel) RefreshCreated ¶
func (m *BaseModel) RefreshCreated()
RefreshCreated updates the model Created field with the current datetime.
func (*BaseModel) RefreshId ¶
func (m *BaseModel) RefreshId()
RefreshId generates and sets a new model id.
The generated id is a cryptographically random 15 characters length string.
func (*BaseModel) RefreshUpdated ¶
func (m *BaseModel) RefreshUpdated()
RefreshUpdated updates the model Updated field with the current datetime.
type Collection ¶
type Collection struct { BaseModel Name string `db:"name" json:"name"` Type string `db:"type" json:"type"` System bool `db:"system" json:"system"` Schema schema.Schema `db:"schema" json:"schema"` Indexes types.JsonArray[string] `db:"indexes" json:"indexes"` // rules ListRule *string `db:"listRule" json:"listRule"` ViewRule *string `db:"viewRule" json:"viewRule"` CreateRule *string `db:"createRule" json:"createRule"` UpdateRule *string `db:"updateRule" json:"updateRule"` DeleteRule *string `db:"deleteRule" json:"deleteRule"` Options types.JsonMap `db:"options" json:"options"` }
func (*Collection) AuthOptions ¶ added in v0.8.0
func (m *Collection) AuthOptions() CollectionAuthOptions
AuthOptions decodes the current collection options and returns them as new CollectionAuthOptions instance.
func (*Collection) BaseFilesPath ¶
func (m *Collection) BaseFilesPath() string
BaseFilesPath returns the storage dir path used by the collection.
func (*Collection) BaseOptions ¶ added in v0.8.0
func (m *Collection) BaseOptions() CollectionBaseOptions
BaseOptions decodes the current collection options and returns them as new CollectionBaseOptions instance.
func (*Collection) DecodeOptions ¶ added in v0.8.0
func (m *Collection) DecodeOptions(result any) error
DecodeOptions decodes the current collection options into the provided "result" (must be a pointer).
func (*Collection) IsAuth ¶ added in v0.8.0
func (m *Collection) IsAuth() bool
IsAuth checks if the current collection has "auth" type.
func (*Collection) IsBase ¶ added in v0.8.0
func (m *Collection) IsBase() bool
IsBase checks if the current collection has "base" type.
func (*Collection) IsView ¶ added in v0.13.0
func (m *Collection) IsView() bool
IsView checks if the current collection has "view" type.
func (Collection) MarshalJSON ¶ added in v0.8.0
func (m Collection) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
func (*Collection) NormalizeOptions ¶ added in v0.8.0
func (m *Collection) NormalizeOptions() error
NormalizeOptions updates the current collection options with a new normalized state based on the collection type.
func (*Collection) SetOptions ¶ added in v0.8.0
func (m *Collection) SetOptions(typedOptions any) error
SetOptions normalizes and unmarshals the specified options into m.Options.
func (*Collection) TableName ¶
func (m *Collection) TableName() string
TableName returns the Collection model SQL table name.
func (*Collection) ViewOptions ¶ added in v0.13.0
func (m *Collection) ViewOptions() CollectionViewOptions
ViewOptions decodes the current collection options and returns them as new CollectionViewOptions instance.
type CollectionAuthOptions ¶ added in v0.8.0
type CollectionAuthOptions struct { ManageRule *string `form:"manageRule" json:"manageRule"` AllowOAuth2Auth bool `form:"allowOAuth2Auth" json:"allowOAuth2Auth"` AllowUsernameAuth bool `form:"allowUsernameAuth" json:"allowUsernameAuth"` AllowEmailAuth bool `form:"allowEmailAuth" json:"allowEmailAuth"` RequireEmail bool `form:"requireEmail" json:"requireEmail"` ExceptEmailDomains []string `form:"exceptEmailDomains" json:"exceptEmailDomains"` OnlyVerified bool `form:"onlyVerified" json:"onlyVerified"` OnlyEmailDomains []string `form:"onlyEmailDomains" json:"onlyEmailDomains"` MinPasswordLength int `form:"minPasswordLength" json:"minPasswordLength"` }
CollectionAuthOptions defines the "auth" Collection.Options fields.
func (CollectionAuthOptions) Validate ¶ added in v0.8.0
func (o CollectionAuthOptions) Validate() error
Validate implements validation.Validatable interface.
type CollectionBaseOptions ¶ added in v0.8.0
type CollectionBaseOptions struct { }
CollectionBaseOptions defines the "base" Collection.Options fields.
func (CollectionBaseOptions) Validate ¶ added in v0.8.0
func (o CollectionBaseOptions) Validate() error
Validate implements validation.Validatable interface.
type CollectionViewOptions ¶ added in v0.13.0
type CollectionViewOptions struct {
Query string `form:"query" json:"query"`
}
CollectionViewOptions defines the "view" Collection.Options fields.
func (CollectionViewOptions) Validate ¶ added in v0.13.0
func (o CollectionViewOptions) Validate() error
Validate implements validation.Validatable interface.
type ColumnValueMapper ¶
type ColumnValueMapper interface { // ColumnValueMap returns the data to be used when persisting the model. ColumnValueMap() map[string]any }
ColumnValueMapper defines an interface for custom db model data serialization.
type ExternalAuth ¶ added in v0.6.0
type ExternalAuth struct { BaseModel CollectionId string `db:"collectionId" json:"collectionId"` RecordId string `db:"recordId" json:"recordId"` Provider string `db:"provider" json:"provider"` ProviderId string `db:"providerId" json:"providerId"` }
func (*ExternalAuth) TableName ¶ added in v0.6.0
func (m *ExternalAuth) TableName() string
type FilesManager ¶ added in v0.3.0
type FilesManager interface { // BaseFilesPath returns the storage dir path used by the interface instance. BaseFilesPath() string }
FilesManager defines an interface with common methods that files manager models should implement.
type Log ¶ added in v0.20.0
type Model ¶
type Model interface { TableName() string IsNew() bool MarkAsNew() MarkAsNotNew() HasId() bool GetId() string SetId(id string) GetCreated() types.DateTime GetUpdated() types.DateTime RefreshId() RefreshCreated() RefreshUpdated() }
Model defines an interface with common methods that all db models should have.
type Param ¶
type Record ¶
type Record struct { BaseModel // contains filtered or unexported fields }
func NewRecord ¶
func NewRecord(collection *Collection) *Record
NewRecord initializes a new empty Record model.
func NewRecordFromNullStringMap ¶
func NewRecordFromNullStringMap(collection *Collection, data dbx.NullStringMap) *Record
NewRecordFromNullStringMap initializes a single new Record model with data loaded from the provided NullStringMap.
Note that this method is intended to load and Scan data from a database result and calls PostScan() which marks the record as "not new".
func NewRecordsFromNullStringMaps ¶
func NewRecordsFromNullStringMaps(collection *Collection, rows []dbx.NullStringMap) []*Record
NewRecordsFromNullStringMaps initializes a new Record model for each row in the provided NullStringMap slice.
Note that this method is intended to load and Scan data from a database result and calls PostScan() for each record marking them as "not new".
func (*Record) BaseFilesPath ¶
BaseFilesPath returns the storage dir path used by the record.
func (*Record) CleanCopy ¶ added in v0.12.0
CleanCopy returns a copy of the current record model populated only with its LATEST data state and everything else reset to the defaults.
func (*Record) Collection ¶
func (m *Record) Collection() *Collection
Collection returns the Collection model associated to the current Record model.
func (*Record) ColumnValueMap ¶
ColumnValueMap implements ColumnValueMapper interface.
func (*Record) EmailVisibility ¶ added in v0.8.0
Verified returns the "emailVisibility" auth record data value.
func (*Record) Expand ¶ added in v0.8.0
Expand returns a shallow copy of the current Record model expand data.
func (*Record) ExpandedAll ¶ added in v0.17.0
ExpandedAll retrieves a slice of relation Records from the already loaded expand data of the current model.
If the requested expand relation is single, this method normalizes the return result and will wrap the single model as a slice.
Returns nil slice if there is no such expand relation loaded.
func (*Record) ExpandedOne ¶ added in v0.17.0
ExpandedOne retrieves a single relation Record from the already loaded expand data of the current model.
If the requested expand relation is multiple, this method returns only first available Record from the expanded relation.
Returns nil if there is no such expand relation loaded.
func (*Record) FindFileFieldByFile ¶
func (m *Record) FindFileFieldByFile(filename string) *schema.SchemaField
FindFileFieldByFile returns the first file type field for which any of the record's data contains the provided filename.
func (*Record) Get ¶ added in v0.8.0
Get returns a normalized single record model data value for "key".
func (*Record) GetDateTime ¶ added in v0.8.0
GetDateTime returns the data value for "key" as a DateTime instance.
func (*Record) GetStringSlice ¶ added in v0.8.0
GetStringSlice returns the data value for "key" as a slice of unique strings.
func (*Record) GetTime ¶ added in v0.8.0
GetTime returns the data value for "key" as a time.Time instance.
func (*Record) IgnoreEmailVisibility ¶ added in v0.8.0
IgnoreEmailVisibility toggles the flag to ignore the auth record email visibility check.
func (*Record) LastResetSentAt ¶ added in v0.8.0
LastResetSentAt returns the "lastResentSentAt" auth record data value.
func (*Record) LastVerificationSentAt ¶ added in v0.8.0
LastVerificationSentAt returns the "lastVerificationSentAt" auth record data value.
func (Record) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
Only the data exported by `PublicExport()` will be serialized.
func (*Record) MergeExpand ¶ added in v0.10.2
MergeExpand merges recursively the provided expand data into the current model's expand (if any).
Note that if an expanded prop with the same key is a slice (old or new expand) then both old and new records will be merged into a new slice (aka. a :merge: [b,c] => [a,b,c]). Otherwise the "old" expanded record will be replace with the "new" one (aka. a :merge: aNew => aNew).
func (*Record) OriginalCopy ¶ added in v0.9.0
OriginalCopy returns a copy of the current record model populated with its ORIGINAL data state (aka. the initially loaded) and everything else reset to the defaults.
func (*Record) PasswordHash ¶ added in v0.8.0
PasswordHash returns the "passwordHash" auth record data value.
func (*Record) PublicExport ¶
PublicExport exports only the record fields that are safe to be public.
For auth records, to force the export of the email field you need to set `m.IgnoreEmailVisibility(true)`.
func (*Record) RefreshTokenKey ¶ added in v0.8.0
RefreshTokenKey generates and sets new random auth record "tokenKey".
Returns an error if the record is not from an auth collection.
func (*Record) ReplaceModifers ¶ added in v0.11.0
ReplaceModifers returns a new map with applied modifier values based on the current record and the specified data.
The resolved modifier keys will be removed.
Multiple modifiers will be applied one after another, while reusing the previous base key value result (eg. 1; -5; +2 => -2).
Example usage:
newData := record.ReplaceModifers(data) // record: {"field": 10} // data: {"field+": 5} // newData: {"field": 15}
func (*Record) SchemaData ¶ added in v0.8.0
SchemaData returns a shallow copy ONLY of the defined record schema fields data.
func (*Record) Set ¶ added in v0.8.0
Set sets the provided key-value data pair for the current Record model.
If the record collection has field with name matching the provided "key", the value will be further normalized according to the field rules.
func (*Record) SetEmail ¶ added in v0.8.0
SetEmail sets the "email" auth record data value.
This method doesn't check whether the provided value is a valid email.
Returns an error if the record is not from an auth collection.
func (*Record) SetEmailVisibility ¶ added in v0.8.0
SetEmailVisibility sets the "emailVisibility" auth record data value.
Returns an error if the record is not from an auth collection.
func (*Record) SetExpand ¶
SetExpand shallow copies the provided data to the current Record model's expand.
func (*Record) SetLastResetSentAt ¶ added in v0.8.0
SetLastResetSentAt sets the "lastResentSentAt" auth record data value.
Returns an error if the record is not from an auth collection.
func (*Record) SetLastVerificationSentAt ¶ added in v0.8.0
SetLastVerificationSentAt sets an "lastVerificationSentAt" auth record data value.
Returns an error if the record is not from an auth collection.
func (*Record) SetPassword ¶ added in v0.8.0
SetPassword sets cryptographically secure string to the auth record "password" field. This method also resets the "lastResetSentAt" and the "tokenKey" fields.
Returns an error if the record is not from an auth collection or an empty password is provided.
func (*Record) SetTokenKey ¶ added in v0.8.0
SetTokenKey sets the "tokenKey" auth record data value.
Returns an error if the record is not from an auth collection.
func (*Record) SetUsername ¶ added in v0.8.0
SetUsername sets the "username" auth record data value.
This method doesn't check whether the provided value is a valid username.
Returns an error if the record is not from an auth collection.
func (*Record) SetVerified ¶ added in v0.8.0
SetVerified sets the "verified" auth record data value.
Returns an error if the record is not from an auth collection.
func (*Record) UnknownData ¶ added in v0.8.0
UnknownData returns a shallow copy ONLY of the unknown record fields data, aka. fields that are neither one of the base and special system ones, nor defined by the collection schema.
func (*Record) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
func (*Record) UnmarshalJSONField ¶ added in v0.8.0
Retrieves the "key" json field value and unmarshals it into "result".
Example
result := struct { FirstName string `json:"first_name"` }{} err := m.UnmarshalJSONField("my_field_name", &result)
func (*Record) ValidatePassword ¶ added in v0.8.0
ValidatePassword validates a plain password against the auth record password.
Returns false if the password is incorrect or record is not from an auth collection.
func (*Record) WithUnknownData ¶ added in v0.15.0
WithUnknownData toggles the export/serialization of unknown data fields (false by default).
type Request
deprecated
type Request struct { BaseModel Url string `db:"url" json:"url"` Method string `db:"method" json:"method"` Status int `db:"status" json:"status"` Auth string `db:"auth" json:"auth"` UserIp string `db:"userIp" json:"userIp"` RemoteIp string `db:"remoteIp" json:"remoteIp"` Referer string `db:"referer" json:"referer"` UserAgent string `db:"userAgent" json:"userAgent"` Meta types.JsonMap `db:"meta" json:"meta"` }
Deprecated: Replaced by the Log model and will be removed in a future version.
type RequestInfo ¶ added in v0.17.0
type RequestInfo struct { Query map[string]any `json:"query"` Data map[string]any `json:"data"` Headers map[string]any `json:"headers"` AuthRecord *Record `json:"authRecord"` Admin *Admin `json:"admin"` Method string `json:"method"` }
RequestInfo defines a HTTP request data struct, usually used as part of the `@request.*` filter resolver.
func (*RequestInfo) HasModifierDataKeys ¶ added in v0.17.0
func (r *RequestInfo) HasModifierDataKeys() bool
HasModifierDataKeys loosely checks if the current struct has any modifier Data keys.