jsvm

package
v0.37.1 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 49 Imported by: 52

Documentation

Overview

Package jsvm implements pluggable utilities for binding a JS goja runtime to the PocketBase instance (loading migrations, attaching to app hooks, etc.).

The package also exports several reusable bindings so that users can utilize them as part of their own custom goja runtime setup.

Example:

jsvm.MustRegister(app, jsvm.Config{
	HooksWatch: true,
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindApis added in v0.37.0

func BindApis(vm *goja.Runtime)

BindApis registers $apis.* namespaced object with reusable Web API handlers, middlewares and other related helpers.

See https://pocketbase.io/jsvm/modules/_apis.html.

func BindCore added in v0.37.0

func BindCore(vm *goja.Runtime)

BindCore registers common core objects and functions such as sleep, toString, DynamicModel, etc. into the provided runtime.

func BindDbx added in v0.37.0

func BindDbx(vm *goja.Runtime)

BindDbx registers $dbx.* namespaced object with dbx database builder related methods.

See https://pocketbase.io/jsvm/modules/_dbx.html.

func BindFilepath added in v0.37.0

func BindFilepath(vm *goja.Runtime)

BindFilepath registers $filepath.* namespaced object with common std Go filepath package related exports.

See https://pocketbase.io/jsvm/modules/_filepath.html.

func BindFilesystem added in v0.37.0

func BindFilesystem(vm *goja.Runtime)

BindFilesystem registers $filesystem.* namespaced object with common filesystem package related helpers.

See https://pocketbase.io/jsvm/modules/_filesystem.html.

func BindForms added in v0.37.0

func BindForms(vm *goja.Runtime)

BindForms registers various application form constructors. These bindings are mostly used internally and/or preserved for backward compatibility with earlier versions.

func BindHTTP added in v0.37.0

func BindHTTP(vm *goja.Runtime)

BindHTTP registers $http.* namespaced object with common utils for sending HTTP requests.

See https://pocketbase.io/jsvm/modules/_http.html.

func BindMails added in v0.37.0

func BindMails(vm *goja.Runtime)

BindMails registers $mail.* namespaced object with common mail related helpers.

See https://pocketbase.io/jsvm/modules/_mails.html.

func BindOS added in v0.37.0

func BindOS(vm *goja.Runtime)

BindOS registers $os.* namespaced object with common std Go os package related exports.

See https://pocketbase.io/jsvm/modules/_os.html.

func BindSecurity added in v0.37.0

func BindSecurity(vm *goja.Runtime)

BindSecurity registers $security.* namespaced object with common security related helpers.

See https://pocketbase.io/jsvm/modules/_security.html.

func MustRegister added in v0.17.0

func MustRegister(app core.App, config Config)

MustRegister registers the jsvm plugin in the provided app instance and panics if it fails.

Example usage:

jsvm.MustRegister(app, jsvm.Config{
	OnInit: func(vm *goja.Runtime) {
		// register custom bindings
		vm.Set("myCustomVar", 123)
	},
})

func Register added in v0.17.0

func Register(app core.App, config Config) error

Register registers the jsvm plugin in the provided app instance.

Types

type Config added in v0.17.0

type Config struct {
	// OnInit is an optional function that will be called
	// after a JS runtime is initialized, allowing you to
	// attach custom Go variables and functions.
	OnInit func(vm *goja.Runtime)

	// HooksWatch enables auto app restarts when a JS app hook file changes.
	//
	// Note that currently the application cannot be automatically restarted on Windows
	// because the restart process relies on execve.
	HooksWatch bool

	// HooksDir specifies the JS app hooks directory.
	//
	// If not set it fallbacks to a relative "pb_data/../pb_hooks" directory.
	HooksDir string

	// HooksFilesPattern specifies a regular expression pattern that
	// identify which file to load by the hook vm(s).
	//
	// If not set it fallbacks to `^.*(\.pb\.js|\.pb\.ts)$`, aka. any
	// HookdsDir file ending in ".pb.js" or ".pb.ts" (the last one is to enforce IDE linters).
	HooksFilesPattern string

	// HooksPoolSize specifies how many goja.Runtime instances to prewarm
	// and keep for the JS app hooks gorotines execution.
	//
	// Zero or negative value means that it will create a new goja.Runtime
	// on every fired goroutine.
	HooksPoolSize int

	// MigrationsDir specifies the JS migrations directory.
	//
	// If not set it fallbacks to a relative "pb_data/../pb_migrations" directory.
	MigrationsDir string

	// If not set it fallbacks to `^.*(\.js|\.ts)$`, aka. any MigrationDir file
	// ending in ".js" or ".ts" (the last one is to enforce IDE linters).
	MigrationsFilesPattern string

	// TypesDir specifies the directory where to store the embedded
	// TypeScript declarations file.
	//
	// If not set it fallbacks to "pb_data".
	//
	// Note: Avoid using the same directory as the HooksDir when HooksWatch is enabled
	// to prevent unnecessary app restarts when the types file is initially created.
	TypesDir string
}

Config defines the config options of the jsvm plugin.

type FieldMapper

type FieldMapper struct {
}

FieldMapper provides custom mapping between Go and JavaScript property names.

It is similar to the builtin "uncapFieldNameMapper" but also converts all uppercase identifiers to their lowercase equivalent (eg. "GET" -> "get").

func (FieldMapper) FieldName

func (u FieldMapper) FieldName(_ reflect.Type, f reflect.StructField) string

FieldName implements the [FieldNameMapper.FieldName] interface method.

func (FieldMapper) MethodName

func (u FieldMapper) MethodName(_ reflect.Type, m reflect.Method) string

MethodName implements the [FieldNameMapper.MethodName] interface method.

type FormData added in v0.22.4

type FormData map[string][]any

FormData represents an interface similar to the browser's FormData.

The value of each FormData entry must be a string or *filesystem.File instance.

It is intended to be used together with the JSVM `$http.send` when sending multipart/form-data requests.

func (FormData) Append added in v0.22.4

func (data FormData) Append(key string, value any)

Append appends a new value onto an existing key inside the current FormData, or adds the key if it does not already exist.

func (FormData) Delete added in v0.22.4

func (data FormData) Delete(key string)

Delete deletes a key and its value(s) from the current FormData.

func (FormData) Entries added in v0.22.4

func (data FormData) Entries() [][]any

Entries returns a [key, value] slice pair for each FormData entry.

func (FormData) Get added in v0.22.4

func (data FormData) Get(key string) any

Get returns the first value associated with a given key from within the current FormData.

If you expect multiple values and want all of them, use the FormData.GetAll method instead.

func (FormData) GetAll added in v0.22.4

func (data FormData) GetAll(key string) []any

GetAll returns all the values associated with a given key from within the current FormData.

func (FormData) Has added in v0.22.4

func (data FormData) Has(key string) bool

Has returns whether a FormData object contains a certain key.

func (FormData) Keys added in v0.22.4

func (data FormData) Keys() []string

Keys returns all keys contained in the current FormData.

func (FormData) Set added in v0.22.4

func (data FormData) Set(key string, value any)

Set sets a new value for an existing key inside the current FormData, or adds the key/value if it does not already exist.

func (FormData) Values added in v0.22.4

func (data FormData) Values() []any

Keys returns all values contained in the current FormData.

Directories

Path Synopsis
internal
types command

Jump to

Keyboard shortcuts

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