Compatmentalized functions + sync

This commit is contained in:
Simon Martens
2025-02-09 21:43:01 +01:00
parent 9554200ad1
commit bd59fdc2c2
2 changed files with 19 additions and 11 deletions

View File

@@ -44,14 +44,27 @@ func init() {
} }
func New(config Config) App { func New(config Config) App {
app := pocketbase.NewWithConfig(pocketbase.Config{ app := App{
MAConfig: config,
}
app.createPBInstance()
app.setupTestuser()
return app
}
func (app *App) createPBInstance() {
app.PB = pocketbase.NewWithConfig(pocketbase.Config{
DBConnect: func(dbPath string) (*dbx.DB, error) { DBConnect: func(dbPath string) (*dbx.DB, error) {
return dbx.Open("pb_sqlite3", dbPath) return dbx.Open("pb_sqlite3", dbPath)
}, },
DefaultDev: config.Debug, DefaultDev: app.MAConfig.Debug,
}) })
}
app.OnServe().BindFunc(func(e *core.ServeEvent) error { func (app *App) setupTestuser() {
app.PB.OnServe().BindFunc(func(e *core.ServeEvent) error {
superusersCol, err := e.App.FindCachedCollectionByNameOrId(core.CollectionNameSuperusers) superusersCol, err := e.App.FindCachedCollectionByNameOrId(core.CollectionNameSuperusers)
if err != nil { if err != nil {
return fmt.Errorf("Failed to fetch %q collection: %w.", core.CollectionNameSuperusers, err) return fmt.Errorf("Failed to fetch %q collection: %w.", core.CollectionNameSuperusers, err)
@@ -60,7 +73,7 @@ func New(config Config) App {
superuser, err := e.App.FindAuthRecordByEmail(superusersCol, TEST_SUPERUSER_MAIL) superuser, err := e.App.FindAuthRecordByEmail(superusersCol, TEST_SUPERUSER_MAIL)
if err != nil { if err != nil {
superuser = core.NewRecord(superusersCol) superuser = core.NewRecord(superusersCol)
} else if !config.AllowTestLogin { } else if !app.MAConfig.AllowTestLogin {
// INFO: we to it as a raw query here since PB does not support deleting the last superuser // INFO: we to it as a raw query here since PB does not support deleting the last superuser
_, err = e.App.DB(). _, err = e.App.DB().
NewQuery("DELETE FROM " + superusersCol.Name + " WHERE id = '" + superuser.Id + "'"). NewQuery("DELETE FROM " + superusersCol.Name + " WHERE id = '" + superuser.Id + "'").
@@ -74,15 +87,10 @@ func New(config Config) App {
superuser.SetEmail(TEST_SUPERUSER_MAIL) superuser.SetEmail(TEST_SUPERUSER_MAIL)
superuser.SetPassword(TEST_SUPERUSER_PASS) superuser.SetPassword(TEST_SUPERUSER_PASS)
if err := app.Save(superuser); err != nil { if err := e.App.Save(superuser); err != nil {
return fmt.Errorf("Failed to upsert superuser account: %w.", err) return fmt.Errorf("Failed to upsert superuser account: %w.", err)
} }
return e.Next() return e.Next()
}) })
return App{
PB: app,
MAConfig: config,
}
} }

View File

@@ -1,4 +1,4 @@
{ {
"debug": true, "debug": true,
"allow_test_login": false "allow_test_login": true
} }