1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-12-01 11:01:04 +02:00
pocketbase/models/base.go

123 lines
3.3 KiB
Go
Raw Normal View History

// Package models implements all PocketBase DB models and DTOs.
2022-07-06 23:19:05 +02:00
package models
import (
"github.com/pocketbase/pocketbase/tools/security"
"github.com/pocketbase/pocketbase/tools/types"
)
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-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
}
// 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()
MarkAsNotNew()
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 {
isNotNew bool
2022-08-05 05:00:38 +02:00
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
}
// MarkAsNew marks the model as "new" (aka. enforces m.IsNew() to be true).
2022-08-05 05:00:38 +02:00
func (m *BaseModel) MarkAsNew() {
m.isNotNew = false
2022-08-05 05:00:38 +02:00
}
// MarkAsNotNew marks the model as "not new" (aka. enforces m.IsNew() to be false)
func (m *BaseModel) MarkAsNotNew() {
m.isNotNew = true
2022-08-05 05:00:38 +02:00
}
// IsNew indicates what type of db query (insert or update)
// should be used with the model instance.
func (m *BaseModel) IsNew() bool {
return !m.isNotNew
2022-08-05 05:00:38 +02:00
}
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() {
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()
}
// PostScan implements the [dbx.PostScanner] interface.
//
// It is executed right after the model was populated with the db row values.
func (m *BaseModel) PostScan() error {
m.MarkAsNotNew()
return nil
}