✨ Scaffold PocketBase app with embedded UI

Add PocketBase v0.40.6 as a dependency together with the pieces the
new backend is built from: the list of synced collections with their
client-writable fields, the initial migration that creates the five
per-user collections (entries, closed_days, closed_weeks,
settings_history, balance_adjustments), and empty Register stubs for
the tracking and legacy import packages.

Clients only get read access to their own rows over REST; writes are
meant to go through the sync push endpoint. web/build is now ignored
except for a tracked placeholder so the built SPA can be embedded.

The entry point (main.go) and the UI embedding (ui.go) are not part
of this commit: the repo root is still the legacy "assets" package,
and Go does not allow "package main" in the same directory.
This commit is contained in:
2026-11-29 16:58:41 +00:00
parent 3b024b6766
commit a2507d22d3
9 changed files with 288 additions and 31 deletions
+21
View File
@@ -0,0 +1,21 @@
package collections
import "slices"
var Synced = []string{"entries", "closed_days", "closed_weeks", "settings_history", "balance_adjustments"}
// ReadOnly collections are pulled by clients but only ever written by the server.
var ReadOnly = []string{"closed_days", "closed_weeks"}
// DataFields lists, per synced collection, the record fields a client may write through the push endpoint.
var DataFields = map[string][]string{
"entries": {"start_time", "end_time", "auto_stopped", "note", "day_key"},
"closed_days": {"day_key", "worked_ms", "kind", "closed_at"},
"closed_weeks": {"week_key", "expected_ms", "worked_ms", "delta_ms", "closed_at"},
"settings_history": {"effective_from", "hours_per_week", "workdays_mask", "timezone"},
"balance_adjustments": {"delta_ms", "note", "effective_at"},
}
func IsSynced(name string) bool { return slices.Contains(Synced, name) }
func IsReadOnly(name string) bool { return slices.Contains(ReadOnly, name) }
+9
View File
@@ -0,0 +1,9 @@
package legacyimport
import (
"github.com/pocketbase/pocketbase/core"
"github.com/spf13/cobra"
)
// Register adds the import-legacy command to the root command.
func Register(app core.App, rootCmd *cobra.Command) {}
+115
View File
@@ -0,0 +1,115 @@
package migrations
import (
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/tools/types"
)
const (
dayKeyPattern = `^\d{4}-\d{2}-\d{2}$`
weekKeyPattern = `^\d{4}-W\d{2}$`
)
func init() {
m.Register(up, down)
}
func ownerField(usersId string) *core.RelationField {
return &core.RelationField{Name: "owner", CollectionId: usersId, Required: true, CascadeDelete: true, MaxSelect: 1}
}
func autodates(c *core.Collection) {
c.Fields.Add(&core.AutodateField{Name: "created", OnCreate: true})
c.Fields.Add(&core.AutodateField{Name: "updated", OnCreate: true, OnUpdate: true})
}
// syncedCollection returns a base collection with the fields and rules shared by all synced collections.
// Clients can only read their own rows through the REST API; writes go through /api/sync/push.
func syncedCollection(name, usersId string, fields ...core.Field) *core.Collection {
c := core.NewBaseCollection(name)
c.Fields.Add(ownerField(usersId))
for _, f := range fields {
c.Fields.Add(f)
}
c.Fields.Add(&core.NumberField{Name: "client_updated_at", OnlyInt: true})
c.Fields.Add(&core.NumberField{Name: "rev", OnlyInt: true})
c.Fields.Add(&core.BoolField{Name: "deleted"})
autodates(c)
ownerRule := types.Pointer("owner = @request.auth.id")
c.ListRule = ownerRule
c.ViewRule = ownerRule
c.CreateRule = nil
c.UpdateRule = nil
c.DeleteRule = nil
c.AddIndex("idx_"+name+"_owner_updated", false, "owner, updated", "")
return c
}
func up(app core.App) error {
users, err := app.FindCollectionByNameOrId("users")
if err != nil {
return err
}
entries := syncedCollection("entries", users.Id,
&core.NumberField{Name: "start_time", OnlyInt: true, Min: types.Pointer(0.0)},
&core.NumberField{Name: "end_time", OnlyInt: true, Min: types.Pointer(0.0)},
&core.BoolField{Name: "auto_stopped"},
&core.TextField{Name: "note", Max: 5000},
&core.TextField{Name: "day_key", Required: true, Pattern: dayKeyPattern},
)
entries.AddIndex("idx_entries_owner_day", false, "owner, day_key", "")
closedDays := syncedCollection("closed_days", users.Id,
&core.TextField{Name: "day_key", Required: true, Pattern: dayKeyPattern},
&core.NumberField{Name: "worked_ms", OnlyInt: true, Min: types.Pointer(0.0)},
&core.SelectField{Name: "kind", Values: []string{"work", "holiday", "vacation", "sick"}, Required: true},
&core.NumberField{Name: "closed_at", OnlyInt: true},
)
closedDays.AddIndex("idx_closed_days_owner_day_key", true, "owner, day_key", "")
closedWeeks := syncedCollection("closed_weeks", users.Id,
&core.TextField{Name: "week_key", Required: true, Pattern: weekKeyPattern},
&core.NumberField{Name: "expected_ms", OnlyInt: true},
&core.NumberField{Name: "worked_ms", OnlyInt: true},
&core.NumberField{Name: "delta_ms", OnlyInt: true},
&core.NumberField{Name: "closed_at", OnlyInt: true},
)
closedWeeks.AddIndex("idx_closed_weeks_owner_week_key", true, "owner, week_key", "")
settingsHistory := syncedCollection("settings_history", users.Id,
&core.TextField{Name: "effective_from", Required: true, Pattern: dayKeyPattern},
&core.NumberField{Name: "hours_per_week", Min: types.Pointer(0.01)},
&core.NumberField{Name: "workdays_mask", OnlyInt: true, Min: types.Pointer(1.0), Max: types.Pointer(127.0)},
&core.TextField{Name: "timezone", Required: true, Max: 100},
)
settingsHistory.AddIndex("idx_settings_history_owner_effective_from", false, "owner, effective_from", "")
balanceAdjustments := syncedCollection("balance_adjustments", users.Id,
&core.NumberField{Name: "delta_ms", OnlyInt: true},
&core.TextField{Name: "note", Max: 5000},
&core.NumberField{Name: "effective_at", OnlyInt: true},
)
balanceAdjustments.AddIndex("idx_balance_adjustments_owner_effective_at", false, "owner, effective_at", "")
for _, c := range []*core.Collection{entries, closedDays, closedWeeks, settingsHistory, balanceAdjustments} {
if err := app.Save(c); err != nil {
return err
}
}
return nil
}
func down(app core.App) error {
for _, name := range []string{"entries", "closed_days", "closed_weeks", "settings_history", "balance_adjustments"} {
c, err := app.FindCollectionByNameOrId(name)
if err != nil {
continue
}
if err := app.Delete(c); err != nil {
return err
}
}
return nil
}
+6
View File
@@ -0,0 +1,6 @@
package tracking
import "github.com/pocketbase/pocketbase/core"
// Register wires the tracking routes, hooks and cron jobs.
func Register(app core.App) {}