You can use the following command to create a plugin project in the working directory:
xpb plugin init <plugin_name>This command will create a Go module using plugin_name as the module name.
The key file to note is plugin.go. It will look something like this:
package plugin
import (
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbuilds/xpb"
)
type Plugin struct{}
func init() {
xpb.Register(&Plugin{})
}
func (p *Plugin) Name() string {
return "plugin"
}
var version string
func (p *Plugin) Version() string {
return version
}
func (p *Plugin) Description() string {
panic("unimplemented")
}
func (p *Plugin) Init(app core.App) error {
panic("unimplemented")
}The struct here called Plugin adheres to the xpb.Plugin interface, which allows it to be registered to the xpb runtime in the init() function (a special Go function that runs when your Go module is first loaded).
The xpb.Plugin interface looks like this:
type Plugin interface {
Init(app core.App) error
Name() string
Version() string
Description() string
}Init(app core.App) erroris called before the pocketbase application runs. Here you should register pocketbase app hooks to make your plugin do whatever you want it to do.Name() stringshould return your plugin’s unique name. Plugin names should follow a snake case naming convention, i.e. “my_plugin”.Version() stringshould return your plugin’s current version number. At build time, xpb automatically sets theversionvariable for you using the version in the build project’sgo.modfile.Description() stringshould return a short description of what your plugin is and what it does.