2022-07-06 23:19:05 +02:00
|
|
|
// Package models implements all PocketBase DB models.
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pocketbase/pocketbase/tools/security"
|
|
|
|
"github.com/pocketbase/pocketbase/tools/types"
|
|
|
|
)
|
|
|
|
|
2022-08-14 18:30:45 +02:00
|
|
|
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"
|
|
|
|
)
|
2022-08-06 17:15:18 +02:00
|
|
|
|
2022-07-06 23:19:05 +02:00
|
|
|
// ColumnValueMapper defines an interface for custom db model data serialization.
|
|
|
|
type ColumnValueMapper interface {
|
|
|
|
// ColumnValueMap returns the data to be used when persisting the model.
|
|
|
|
ColumnValueMap() map[string]any
|
|
|
|
}
|
|
|
|
|
2022-07-18 11:04:27 +02:00
|
|
|
// FilesManager defines an interface with common methods that files manager models should implement.
|
|
|
|
type FilesManager interface {
|
|
|
|
// BaseFilesPath returns the storage dir path used by the interface instance.
|
|
|
|
BaseFilesPath() string
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:19:05 +02:00
|
|
|
// Model defines an interface with common methods that all db models should have.
|
|
|
|
type Model interface {
|
|
|
|
TableName() string
|
2022-08-05 05:00:38 +02:00
|
|
|
IsNew() bool
|
|
|
|
MarkAsNew()
|
|
|
|
UnmarkAsNew()
|
2022-07-06 23:19:05 +02:00
|
|
|
HasId() bool
|
|
|
|
GetId() string
|
2022-08-07 10:14:49 +02:00
|
|
|
SetId(id string)
|
2022-07-06 23:19:05 +02:00
|
|
|
GetCreated() types.DateTime
|
|
|
|
GetUpdated() types.DateTime
|
|
|
|
RefreshId()
|
|
|
|
RefreshCreated()
|
|
|
|
RefreshUpdated()
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
// BaseModel
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
|
|
|
// BaseModel defines common fields and methods used by all other models.
|
|
|
|
type BaseModel struct {
|
2022-08-05 05:00:38 +02:00
|
|
|
isNewFlag bool
|
|
|
|
|
2022-07-06 23:19:05 +02:00
|
|
|
Id string `db:"id" json:"id"`
|
|
|
|
Created types.DateTime `db:"created" json:"created"`
|
|
|
|
Updated types.DateTime `db:"updated" json:"updated"`
|
|
|
|
}
|
|
|
|
|
2022-08-03 15:26:24 +02:00
|
|
|
// HasId returns whether the model has a nonzero id.
|
2022-07-06 23:19:05 +02:00
|
|
|
func (m *BaseModel) HasId() bool {
|
|
|
|
return m.GetId() != ""
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:14:49 +02:00
|
|
|
// GetId returns the model id.
|
2022-07-06 23:19:05 +02:00
|
|
|
func (m *BaseModel) GetId() string {
|
|
|
|
return m.Id
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:14:49 +02:00
|
|
|
// SetId sets the model id to the provided string value.
|
2022-08-05 05:00:38 +02:00
|
|
|
func (m *BaseModel) SetId(id string) {
|
|
|
|
m.Id = id
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:14:49 +02:00
|
|
|
// MarkAsNew sets the model isNewFlag enforcing [m.IsNew()] to be true.
|
2022-08-05 05:00:38 +02:00
|
|
|
func (m *BaseModel) MarkAsNew() {
|
|
|
|
m.isNewFlag = true
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:14:49 +02:00
|
|
|
// UnmarkAsNew resets the model isNewFlag.
|
2022-08-05 05:00:38 +02:00
|
|
|
func (m *BaseModel) UnmarkAsNew() {
|
|
|
|
m.isNewFlag = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNew indicates what type of db query (insert or update)
|
|
|
|
// should be used with the model instance.
|
|
|
|
func (m *BaseModel) IsNew() bool {
|
|
|
|
return m.isNewFlag || !m.HasId()
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:14:49 +02:00
|
|
|
// GetCreated returns the model Created datetime.
|
2022-07-06 23:19:05 +02:00
|
|
|
func (m *BaseModel) GetCreated() types.DateTime {
|
|
|
|
return m.Created
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:14:49 +02:00
|
|
|
// GetUpdated returns the model Updated datetime.
|
2022-07-06 23:19:05 +02:00
|
|
|
func (m *BaseModel) GetUpdated() types.DateTime {
|
|
|
|
return m.Updated
|
|
|
|
}
|
|
|
|
|
|
|
|
// RefreshId generates and sets a new model id.
|
|
|
|
//
|
2022-08-03 15:26:24 +02:00
|
|
|
// The generated id is a cryptographically random 15 characters length string.
|
2022-07-06 23:19:05 +02:00
|
|
|
func (m *BaseModel) RefreshId() {
|
2022-10-30 10:28:14 +02:00
|
|
|
if m.Id == "" { // no previous id
|
|
|
|
m.MarkAsNew()
|
|
|
|
}
|
2022-08-14 18:30:45 +02:00
|
|
|
m.Id = security.RandomStringWithAlphabet(DefaultIdLength, DefaultIdAlphabet)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
2022-08-07 10:14:49 +02:00
|
|
|
// RefreshCreated updates the model Created field with the current datetime.
|
2022-07-06 23:19:05 +02:00
|
|
|
func (m *BaseModel) RefreshCreated() {
|
|
|
|
m.Created = types.NowDateTime()
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:14:49 +02:00
|
|
|
// RefreshUpdated updates the model Updated field with the current datetime.
|
2022-07-06 23:19:05 +02:00
|
|
|
func (m *BaseModel) RefreshUpdated() {
|
|
|
|
m.Updated = types.NowDateTime()
|
|
|
|
}
|