restorev1

import restorev1 "pocketone.dev/ext/contracts/restore/v1"

Package restorev1 is the restore/v1 contract: put an app's data back as it was at a moment. Its providers differ in how close they get: pitr restores any second in its window (continuous history); the default restores the latest backup at or before the moment. Both answer the same calls, so a client (the dashboard, the CLI, an agent) asks for "Tuesday 15:00" and gets the closest the app can do, and is told which point it got.

A wire contract at /api/_c/restore/v1; a multisite host may provide it for its instances (from host-level snapshots).

Source: ext/contracts/restore/v1.

Constants

const (
	Continuous = "continuous" // any moment inside Window
	Snapshots  = "snapshots"  // only the moments in Snapshots
)

Kinds of points.

Variables

var Contract = extkit.NewContract[Restorer]("restore/v1", extkit.One).HostProvidable().JS(extkit.JSHooks, nil).JSTypes(jsTypes)

Contract is restore/v1.

var ErrBusy = &extkit.Error{Code: "busy", Message: "a restore is already running", Status: 409}

ErrBusy: a restore is already running.

var ErrNoPoint = &extkit.Error{Code: "no_point", Message: "nothing to restore from at or before that moment", Status: 422}

ErrNoPoint: nothing to restore from at or before the moment asked for.

var W = extkit.NewWire(Contract, "restore", func(inner Restorer, g *extkit.Guard[Restorer]) Restorer {
	return &guardRestorer{inner, g}
},
	extkit.Op[Restorer]{
		Name:   "points",
		Desc:   "Says what the app's data can be restored to: a window of time (continuous history) or a list of moments (backups), and whether a restore is running.",
		Method: "GET",
		Path:   "",
		Scope:  "restore:read",
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Restorer, a extkit.Auth, req extkit.OpRequest) (any, error) {
			return impl.Points(ctx, a)
		},
	},
	extkit.Op[Restorer]{
		Name:   "restore_to",
		Desc:   "Puts the app's data back as it was at the given moment, from the latest point at or before it. The app restarts to apply it; its current data is kept aside until the next restore.",
		Method: "POST",
		Path:   "restore",
		Scope:  "restore",
		In:     reflect.TypeOf(RestoreInput{}),
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Restorer, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in RestoreInput
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.RestoreTo(ctx, a, in)
		},
	},
)

W is restore/v1's wire side: routes at /api/_c/restore/v1/<op>, action hooks, MCP tools and the client.

Functions

OnPoints

func OnPoints(owner any, fn func(e *extkit.CallEvent) error)

OnPoints binds fn to points calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse.

OnRestoreTo

func OnRestoreTo(owner any, fn func(e *extkit.CallEvent, in *RestoreInput) error)

OnRestoreTo binds fn to restore_to calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.

Types

Client

type Client struct {
	// contains filtered or unexported fields
}

Client is restore/v1 over HTTP, for the CLI and other apps. It implements Restorer; the token it was made with is the credential, so the extkit.Auth argument is ignored.

NewClient

func NewClient(baseURL, token string, hc *http.Client) *Client

NewClient calls restore/v1 on the app at baseURL with token (a PocketBase auth token or an API token).

Client.Points

func (c *Client) Points(ctx context.Context, _ extkit.Auth) (Points, error)

Client.RestoreTo

func (c *Client) RestoreTo(ctx context.Context, _ extkit.Auth, in RestoreInput) (RestoreResult, error)

Points

type Points struct {
	Provider  string     `json:"provider"` // who answers: "pitr", "backups", a host
	Kind      string     `json:"kind"`     // Continuous or Snapshots
	Window    *Window    `json:"window,omitempty"`
	Snapshots []Snapshot `json:"snapshots,omitempty"` // newest first
	Restoring bool       `json:"restoring"`
	Last      *Restored  `json:"last,omitempty"` // the last restore, if any
	// Details are the provider's own facts (pitr: where the history is kept and how big it is), for display only.
	Details map[string]any `json:"details,omitempty"`
}

Points is what the app can be restored to.

RestoreInput

type RestoreInput struct {
	At time.Time `json:"at"`
}

RestoreInput is the moment to restore to.

RestoreResult

type RestoreResult struct {
	Restoring bool      `json:"restoring"`
	Point     time.Time `json:"point"` // the point it restores from: At itself (continuous) or the latest snapshot before it
	Name      string    `json:"name,omitempty"`
	Message   string    `json:"message"`
}

RestoreResult says what a restore will use.

Restored

type Restored struct {
	Asked time.Time `json:"asked"` // the moment asked for
	Point time.Time `json:"point"` // the point the data came from
	Done  time.Time `json:"done"`
}

Restored is a finished restore.

Restorer

type Restorer interface {
	// Points says what the app's data can be restored to: a window of time (continuous history) or a list of
	// moments (backups), and whether a restore is running.
	//
	//op: GET / scope=restore:read
	Points(ctx context.Context, a extkit.Auth) (Points, error)

	// RestoreTo puts the app's data back as it was at the given moment, from the latest point at or before it. The
	// app restarts to apply it; its current data is kept aside until the next restore.
	//
	//op: POST restore scope=restore
	RestoreTo(ctx context.Context, a extkit.Auth, in RestoreInput) (RestoreResult, error)
}

Restorer puts an app's data back as it was at a moment.

Snapshot

type Snapshot struct {
	At   time.Time `json:"at"`
	Name string    `json:"name"` // the provider's name for it (a backup file)
	Size int64     `json:"size,omitempty"`
}

Snapshot is one moment that can be restored.

Window

type Window struct {
	Earliest time.Time `json:"earliest"`
	Latest   time.Time `json:"latest"`
}

Window is a span of time any moment of which can be restored.