import_export

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2025 License: MIT Imports: 24 Imported by: 0

README

Import Export

An xpb plugin for Pocketbase that provides a suite of cli commands for importing and exporting records and collections.

Installation

  1. Install XPB.
  2. Use the builder:
xpb build --with github.com/pocketbuilds/import_export@latest

Plugin Config

# pocketbuilds.toml

[import_export]
# Determines if an automatic database backup should be made prior to an import.
#   - flag: auto_backup
#   - default: true
auto_backup = true
# Path to directory for collections schema files.
#   - flag: collections_dir
#   - default: pb_data/../migrations/collections
collections_dir = ""
# Encoding to use for collection imports and exports.
#   - options: json, yml, toml, or any community plugin options installed
#   - flag: --json, --yml, --toml, etc.
#   - default: json
collections_encoding = "json"
# Optional prefix to prepend the commands to avoid possible name collisions.
#   - default: "" (no prefix)
command_prefix = ""
# Determines if to include oauth2 config in collections export.
#   - flag: TODO
#   - default: false
include_oauth2 = false
# Path to directory for records data files.
#   - flag: records_dir
#   - default: pb_data/../migrations/records
records_dir = ""
# Encoding to use for records imports and exports.
#   - options: csv, json, yml, toml, or any community plugin options installed
#   - flag: --csv, --json, --yml, --toml, etc.
#   - default: csv
records_encoding = "csv"
# Determines if record imports should skip validation.
#   - flag: no_validate
#   - default: false
no_validate = false
# Determines if verified state should be overriden.
#   - options: true, false, null (do not override)
#   - flag: override_verified
#   - default: null
override_verified = true
# Determines if email visibility should be overriden.
#   - options: true, false, null (do not override)
#   - flag: override_email_visibility
#   - default: null
override_email_visibility = true
# Determines if measures are taken to reduce git diff. Currently, just sets
# updated to the zero datetime.
#   - default: false
reduce_git_diff = false
# Determines if to include system collections.
#   - flag: system
#   - default: false
system = false

[import_export_csv]
# Delimiter character to use for the csv.
#   - default: ","
delimiter = ","

[import_export_json]
# Indent prefix to be used for json collection exports.
#   - default: "" (no indent prefix)
collection_prefix = ""
# Indent to be used for json collection exports.
#   - default: "\t" (tab)
collection_indent = "\t"
# Indent prefix to be used for json record exports.
#   - default: "" (no indent prefix)
records_prefix = ""
# Indent to be used for json records exports.
#   - default: "" (no indent)
records_indent = ""

[import_export_toml]
# Indent to be used for toml collection exports.
#   - default: "  "
collection_indent = "  "
# Indent to be used for toml record exports.
#   - default: "  "
records_indent = "  "
# Key for records array, since toml cannot have root level arrays.
#   - default: "records"
records_array_key = "records"

[import_export_yml]
# Number of spaces to use for indentation in yaml collection exports.
#   - default: 2
collection_indent = 2
# Number of spaces to use for indentation in yaml record exports.
#   - default: 2
records_indent = 2

Creating Community Encoding Handler

  1. Look at the examples in handlers/ directory.
  2. Create struct that implements the xpb.Plugin interface as well as the import_export.RecordsHandler and/or import_export.CollectionHandler interfaces.
  3. Register the plugin and handler on init():
    func init() {
        myPlugin := &Plugin{}
        xpb.Register(myPlugin)
        import_export.RegisterHandler(myPlugin)
    }

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoCollectionHandler = errors.New("no collection encoding handler was installed")
	ErrNoRecordsHandler    = errors.New("no records encoding handler was installed")
)

Functions

func RegisterHandler

func RegisterHandler(h Handler)

Types

type CollectionHandler

type CollectionHandler interface {
	Handler
	EncodeCollection(collection *core.Collection, writer io.Writer) error
	DecodeCollection(reader io.Reader) (map[string]any, error)
}

type Handler

type Handler interface {
	FileExtension() string
}

type Plugin

type Plugin struct {
	// Determines if an automatic database backup should be made prior to an import.
	//   - flag: auto_backup
	//   - default: true
	AutoBackup bool `json:"auto_backup"`
	// Path to directory for collections schema files.
	//   - flag: collections_dir
	//   - default: pb_data/../migrations/collections
	CollectionsDir string `json:"collections_dir"`
	// Encoding to use for collection imports and exports.
	//   - options: json, yml, toml, or any community plugin options installed
	//   - flag: --json, --yml, --toml, etc.
	//   - default: json
	CollectionsEncoding *flags.RadioValue `json:"collections_encoding"`
	// Optional prefix to prepend the commands to avoid possible name collisions.
	//   - default: "" (no prefix)
	CommandPrefix string `json:"command_prefix"`
	// Determines if to include oauth2 config in collections export.
	//   - flag: TODO
	//   - default: false
	IncludeOauth2 bool `json:"include_oauth2"`
	// Path to directory for records data files.
	//   - flag: records_dir
	//   - default: pb_data/../migrations/records
	RecordsDir string `json:"records_dir"`
	// Encoding to use for records imports and exports.
	//   - options: csv, json, yml, toml, or any community plugin options installed
	//   - flag: --csv, --json, --yml, --toml, etc.
	//   - default: csv
	RecordsEncoding *flags.RadioValue `json:"records_encoding"`
	// Determines if record imports should skip validation.
	//   - flag: no_validate
	//   - default: false
	NoValidate bool `json:"no_validate"`
	// Determines if verified state should be overriden.
	//   - options: true, false, null (do not override)
	//   - flag: override_verified
	//   - default: null
	OverrideVerified *flags.OptionalBoolValue `json:"override_verified"`
	// Determines if email visibility should be overriden.
	//   - options: true, false, null (do not override)
	//   - flag: override_email_visibility
	//   - default: null
	OverrideEmailVisibility *flags.OptionalBoolValue `json:"override_email_visibility"`
	// Determines if measures are taken to reduce git diff. Currently, just sets
	// updated to the zero datetime.
	//   - default: false
	ReduceGitDiff bool `json:"reduce_git_diff"`
	// Determines if to include system collections.
	//   - flag: system
	//   - default: false
	System bool `json:"system"`
}

func (*Plugin) Description

func (p *Plugin) Description() string

Description implements xpb.Plugin.

func (*Plugin) ExportCollectionsCommand

func (p *Plugin) ExportCollectionsCommand(app core.App) *cobra.Command

func (*Plugin) ExportCommand

func (p *Plugin) ExportCommand(app core.App) *cobra.Command

func (*Plugin) ExportRecordsCommand

func (p *Plugin) ExportRecordsCommand(app core.App) *cobra.Command

func (*Plugin) ImportCollectionsCommand

func (p *Plugin) ImportCollectionsCommand(app core.App) *cobra.Command

func (*Plugin) ImportCommand

func (p *Plugin) ImportCommand(app core.App) *cobra.Command

func (*Plugin) ImportRecordsCommand

func (p *Plugin) ImportRecordsCommand(app core.App) *cobra.Command

func (*Plugin) Init

func (p *Plugin) Init(app core.App) error

Init implements xpb.Plugin.

func (*Plugin) Name

func (p *Plugin) Name() string

Name implements xpb.Plugin.

func (*Plugin) PreValidate

func (p *Plugin) PreValidate(app core.App) error

PreValidate implements xpb.PreValidator.

func (*Plugin) Validate

func (p *Plugin) Validate() error

Validate implements validation.Validatable.

func (*Plugin) Version

func (p *Plugin) Version() string

Version implements xpb.Plugin.

type RecordsHandler

type RecordsHandler interface {
	Handler
	EncodeRecords(records []*core.Record, writer io.Writer) error
	DecodeRecords(collection *core.Collection, reader io.Reader) ([]*core.Record, error)
}

Directories

Path Synopsis
handlers
csv
yml

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL