You've already forked pocketbase
mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-12-14 20:36:02 +02:00
initial v0.8 pre-release
This commit is contained in:
@@ -9,56 +9,36 @@ import (
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
)
|
||||
|
||||
// SettingsUpsert specifies a [core.Settings] upsert (create/update) form.
|
||||
// SettingsUpsert is a [core.Settings] upsert (create/update) form.
|
||||
type SettingsUpsert struct {
|
||||
*core.Settings
|
||||
|
||||
config SettingsUpsertConfig
|
||||
}
|
||||
|
||||
// SettingsUpsertConfig is the [SettingsUpsert] factory initializer config.
|
||||
//
|
||||
// NB! App is required struct member.
|
||||
type SettingsUpsertConfig struct {
|
||||
App core.App
|
||||
Dao *daos.Dao
|
||||
LogsDao *daos.Dao
|
||||
app core.App
|
||||
dao *daos.Dao
|
||||
}
|
||||
|
||||
// NewSettingsUpsert creates a new [SettingsUpsert] form with initializer
|
||||
// config created from the provided [core.App] instance.
|
||||
//
|
||||
// If you want to submit the form as part of another transaction, use
|
||||
// [NewSettingsUpsertWithConfig] with explicitly set Dao.
|
||||
// If you want to submit the form as part of a transaction,
|
||||
// you can change the default Dao via [SetDao()].
|
||||
func NewSettingsUpsert(app core.App) *SettingsUpsert {
|
||||
return NewSettingsUpsertWithConfig(SettingsUpsertConfig{
|
||||
App: app,
|
||||
})
|
||||
}
|
||||
|
||||
// NewSettingsUpsertWithConfig creates a new [SettingsUpsert] form
|
||||
// with the provided config or panics on invalid configuration.
|
||||
func NewSettingsUpsertWithConfig(config SettingsUpsertConfig) *SettingsUpsert {
|
||||
form := &SettingsUpsert{config: config}
|
||||
|
||||
if form.config.App == nil {
|
||||
panic("Missing required config.App instance.")
|
||||
}
|
||||
|
||||
if form.config.Dao == nil {
|
||||
form.config.Dao = form.config.App.Dao()
|
||||
}
|
||||
|
||||
if form.config.LogsDao == nil {
|
||||
form.config.LogsDao = form.config.App.LogsDao()
|
||||
form := &SettingsUpsert{
|
||||
app: app,
|
||||
dao: app.Dao(),
|
||||
}
|
||||
|
||||
// load the application settings into the form
|
||||
form.Settings, _ = config.App.Settings().Clone()
|
||||
form.Settings, _ = app.Settings().Clone()
|
||||
|
||||
return form
|
||||
}
|
||||
|
||||
// SetDao replaces the default form Dao instance with the provided one.
|
||||
func (form *SettingsUpsert) SetDao(dao *daos.Dao) {
|
||||
form.dao = dao
|
||||
}
|
||||
|
||||
// Validate makes the form validatable by implementing [validation.Validatable] interface.
|
||||
func (form *SettingsUpsert) Validate() error {
|
||||
return form.Settings.Validate()
|
||||
@@ -75,10 +55,10 @@ func (form *SettingsUpsert) Submit(interceptors ...InterceptorFunc) error {
|
||||
return err
|
||||
}
|
||||
|
||||
encryptionKey := os.Getenv(form.config.App.EncryptionEnv())
|
||||
encryptionKey := os.Getenv(form.app.EncryptionEnv())
|
||||
|
||||
return runInterceptors(func() error {
|
||||
saveErr := form.config.Dao.SaveParam(
|
||||
saveErr := form.dao.SaveParam(
|
||||
models.ParamAppSettings,
|
||||
form.Settings,
|
||||
encryptionKey,
|
||||
@@ -88,11 +68,11 @@ func (form *SettingsUpsert) Submit(interceptors ...InterceptorFunc) error {
|
||||
}
|
||||
|
||||
// explicitly trigger old logs deletion
|
||||
form.config.LogsDao.DeleteOldRequests(
|
||||
form.app.LogsDao().DeleteOldRequests(
|
||||
time.Now().AddDate(0, 0, -1*form.Settings.Logs.MaxDays),
|
||||
)
|
||||
|
||||
// merge the application settings with the form ones
|
||||
return form.config.App.Settings().Merge(form.Settings)
|
||||
return form.app.Settings().Merge(form.Settings)
|
||||
}, interceptors...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user