models

package
Version: v0.7.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2022 License: MIT Imports: 13 Imported by: 53

Documentation

Overview

Package models implements all PocketBase DB models.

Index

Constants

View Source
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"
)
View Source
const (
	RequestAuthGuest = "guest"
	RequestAuthUser  = "user"
	RequestAuthAdmin = "admin"
)

list with the supported values for `Request.Auth`

View Source
const (
	// ProfileCollectionName is the name of the system user profiles collection.
	ProfileCollectionName = "profiles"

	// ProfileCollectionUserFieldName is the name of the user field from the system user profiles collection.
	ProfileCollectionUserFieldName = "userId"
)
View Source
const (
	ParamAppSettings = "settings"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Admin

type Admin struct {
	BaseAccount

	Avatar int `db:"avatar" json:"avatar"`
}

func (*Admin) TableName

func (m *Admin) TableName() string

type BaseAccount

type BaseAccount struct {
	BaseModel

	Email           string         `db:"email" json:"email"`
	TokenKey        string         `db:"tokenKey" json:"-"`
	PasswordHash    string         `db:"passwordHash" json:"-"`
	LastResetSentAt types.DateTime `db:"lastResetSentAt" json:"lastResetSentAt"`
}

BaseAccount defines common fields and methods used by auth models (aka. users and admins).

func (*BaseAccount) RefreshTokenKey

func (m *BaseAccount) RefreshTokenKey()

RefreshTokenKey generates and sets new random token key.

func (*BaseAccount) SetPassword

func (m *BaseAccount) SetPassword(password string) error

SetPassword sets cryptographically secure string to `model.Password`.

Additionally this method also resets the LastResetSentAt and the TokenKey fields.

func (*BaseAccount) ValidatePassword

func (m *BaseAccount) ValidatePassword(password string) bool

ValidatePassword validates a plain password against the model's password.

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

func (m *BaseModel) GetCreated() types.DateTime

GetCreated returns the model Created datetime.

func (*BaseModel) GetId

func (m *BaseModel) GetId() string

GetId returns the model id.

func (*BaseModel) GetUpdated

func (m *BaseModel) GetUpdated() types.DateTime

GetUpdated returns the model Updated datetime.

func (*BaseModel) HasId

func (m *BaseModel) HasId() bool

HasId returns whether the model has a nonzero id.

func (*BaseModel) IsNew added in v0.4.0

func (m *BaseModel) IsNew() bool

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 sets the model isNewFlag enforcing [m.IsNew()] to be true.

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.

func (*BaseModel) SetId added in v0.4.0

func (m *BaseModel) SetId(id string)

SetId sets the model id to the provided string value.

func (*BaseModel) UnmarkAsNew added in v0.4.0

func (m *BaseModel) UnmarkAsNew()

UnmarkAsNew resets the model isNewFlag.

type Collection

type Collection struct {
	BaseModel

	Name       string        `db:"name" json:"name"`
	System     bool          `db:"system" json:"system"`
	Schema     schema.Schema `db:"schema" json:"schema"`
	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"`
}

func (*Collection) BaseFilesPath

func (m *Collection) BaseFilesPath() string

BaseFilesPath returns the storage dir path used by the collection.

func (*Collection) TableName

func (m *Collection) TableName() string

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

	UserId     string `db:"userId" json:"userId"`
	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 Model

type Model interface {
	TableName() string
	IsNew() bool
	MarkAsNew()
	UnmarkAsNew()
	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 Param struct {
	BaseModel

	Key   string        `db:"key" json:"key"`
	Value types.JsonRaw `db:"value" json:"value"`
}

func (*Param) TableName

func (m *Param) TableName() string

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.

func NewRecordsFromNullStringMaps

func NewRecordsFromNullStringMaps(collection *Collection, rows []dbx.NullStringMap) []*Record

NewRecordsFromNullStringMaps initializes a new Record model for each row in the provided NullStringMap slice.

func (*Record) BaseFilesPath

func (m *Record) BaseFilesPath() string

BaseFilesPath returns the storage dir path used by the record.

func (*Record) Collection

func (m *Record) Collection() *Collection

Collection returns the Collection model associated to the current Record model.

func (*Record) ColumnValueMap

func (m *Record) ColumnValueMap() map[string]any

ColumnValueMap implements ColumnValueMapper interface.

func (*Record) Data

func (m *Record) Data() map[string]any

Data returns a shallow copy of the currently loaded record's data.

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) GetBoolDataValue

func (m *Record) GetBoolDataValue(key string) bool

GetBoolDataValue returns the data value for `key` as a bool.

func (*Record) GetDataValue

func (m *Record) GetDataValue(key string) any

GetDataValue returns the current record's data value for `key`.

Returns nil if data value with `key` is not found or set.

func (*Record) GetDateTimeDataValue

func (m *Record) GetDateTimeDataValue(key string) types.DateTime

GetDateTimeDataValue returns the data value for `key` as a DateTime instance.

func (*Record) GetExpand

func (m *Record) GetExpand() map[string]any

GetExpand returns a shallow copy of the optional `expand` data attached to the current Record model.

func (*Record) GetFloatDataValue

func (m *Record) GetFloatDataValue(key string) float64

GetFloatDataValue returns the data value for `key` as a float64.

func (*Record) GetIntDataValue

func (m *Record) GetIntDataValue(key string) int

GetIntDataValue returns the data value for `key` as an int.

func (*Record) GetStringDataValue

func (m *Record) GetStringDataValue(key string) string

GetStringDataValue returns the data value for `key` as a string.

func (*Record) GetStringSliceDataValue

func (m *Record) GetStringSliceDataValue(key string) []string

GetStringSliceDataValue returns the data value for `key` as a slice of unique strings.

func (*Record) GetTimeDataValue

func (m *Record) GetTimeDataValue(key string) time.Time

GetTimeDataValue returns the data value for `key` as a time.Time instance.

func (*Record) Load

func (m *Record) Load(data map[string]any) error

Load bulk loads the provided data into the current Record model.

func (Record) MarshalJSON

func (m Record) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

Only the data exported by `PublicExport()` will be serialized.

func (*Record) PublicExport

func (m *Record) PublicExport() map[string]any

PublicExport exports only the record fields that are safe to be public.

This method also skips the "hidden" fields, aka. fields prefixed with `#`.

func (*Record) SetDataValue

func (m *Record) SetDataValue(key string, value any)

SetDataValue sets the provided key-value data pair for the current Record model.

This method does nothing if the record doesn't have a `key` field.

func (*Record) SetExpand

func (m *Record) SetExpand(data map[string]any)

SetExpand assigns the provided data to `record.expand`.

func (*Record) TableName

func (m *Record) TableName() string

TableName returns the table name associated to the current Record model.

func (*Record) UnmarshalJSON

func (m *Record) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Request

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"`
}

func (*Request) TableName

func (m *Request) TableName() string

type User

type User struct {
	BaseAccount

	Verified               bool           `db:"verified" json:"verified"`
	LastVerificationSentAt types.DateTime `db:"lastVerificationSentAt" json:"lastVerificationSentAt"`

	// profile rel
	Profile *Record `db:"-" json:"profile"`
}

func (*User) AsMap

func (m *User) AsMap() (map[string]any, error)

AsMap returns the current user data as a plain map (including the profile relation, if loaded).

func (*User) TableName

func (m *User) TableName() string

Directories

Path Synopsis
Package schema implements custom Schema and SchemaField datatypes for handling the Collection schema definitions.
Package schema implements custom Schema and SchemaField datatypes for handling the Collection schema definitions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL