From bd59fdc2c2bc60a1e99dae3987c3a053ca57367b Mon Sep 17 00:00:00 2001 From: Simon Martens Date: Sun, 9 Feb 2025 21:43:01 +0100 Subject: [PATCH] Compatmentalized functions + sync --- app/pb.go | 28 ++++++++++++++++++---------- config.dev.json | 2 +- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/app/pb.go b/app/pb.go index d0e90db..a5b9723 100644 --- a/app/pb.go +++ b/app/pb.go @@ -44,14 +44,27 @@ func init() { } 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) { 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) if err != nil { 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) if err != nil { 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 _, err = e.App.DB(). NewQuery("DELETE FROM " + superusersCol.Name + " WHERE id = '" + superuser.Id + "'"). @@ -74,15 +87,10 @@ func New(config Config) App { superuser.SetEmail(TEST_SUPERUSER_MAIL) 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 e.Next() }) - - return App{ - PB: app, - MAConfig: config, - } } diff --git a/config.dev.json b/config.dev.json index a9506be..8e2cfdf 100644 --- a/config.dev.json +++ b/config.dev.json @@ -1,4 +1,4 @@ { "debug": true, - "allow_test_login": false + "allow_test_login": true }