Help us shape the future of Go! Take the 2026 Go Developer Survey

pgbase

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: MIT Imports: 14 Imported by: 0

README

简介

成功实现了多写, 通过自定义 DBConnect 将 PocketBase 的存储后端由 sqlite 切换到 postgres

原理

使用 Polyglot 将 sqlite 语句翻译成 postgres 语句

测试

docker-compose up -d
cd ./example
# download libpolyglot_sql_ffi.so in https://github.com/tobilg/polyglot/releases
go run -v . serve

示例

package main

import (
	"strings"

	"github.com/pocketbase/dbx"
	"github.com/pocketbase/pocketbase"
	"github.com/pocketbase/pocketbase/core"
	"github.com/shynome/err0/try"
	"github.com/shynome/pgbase"
	polyglot "github.com/tobilg/polyglot/packages/go"
)

func main() {
	tc := try.To1(polyglot.OpenDefault())
	polyglot.SetDefaultClient(tc)
	cfg := pocketbase.Config{
		DBConnect: func(dbPath string) (*dbx.DB, error) {
			if strings.HasSuffix(dbPath, "data.db") {
				return pgbase.Open("postgres://user:pass@127.0.0.1:5432/postgres?sslmode=disable")
			}
			return core.DefaultDBConnect(dbPath)
		},
	}
	app := pocketbase.NewWithConfig(cfg)
	try.To(app.Bootstrap())

	// required!
	// required!
	// required!
	pgbase.EnableConcurrentWrites(app, cfg)

	try.To(app.Start())
}


参考

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBaseDriverNotExecer = fmt.Errorf("base driver don't support driver.ExecerContext")
View Source
var ErrNoTranslatedQuery = fmt.Errorf("no translated query")

Functions

func EnableConcurrentWrites added in v0.0.2

func EnableConcurrentWrites(app core.App, config pocketbase.Config)

func NewSqliteBuilder

func NewSqliteBuilder(db *dbx.DB, executor dbx.Executor) dbx.Builder

func Open

func Open(dsn string) (*dbx.DB, error)

Types

type Base added in v0.0.2

type Base struct {
	Cache *otter.Cache[string, string]
	// contains filtered or unexported fields
}

func (*Base) GetPolyglotClient added in v0.0.2

func (b *Base) GetPolyglotClient() (*polyglot.Client, error)

func (*Base) SetPolyglotClient added in v0.0.2

func (b *Base) SetPolyglotClient(tc *polyglot.Client)

type Builder

type Builder struct {
	dbx.Builder
}

func (*Builder) GeneratePlaceholder

func (*Builder) GeneratePlaceholder(i int) string

type Conn

type Conn struct {
	driver.Conn
	*Base
}

func (*Conn) ExecContext

func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)

func (*Conn) Prepare

func (c *Conn) Prepare(query string) (driver.Stmt, error)

type Connecter

type Connecter struct {
	driver.Connector
	*Base
	// contains filtered or unexported fields
}

func (*Connecter) Connect

func (c *Connecter) Connect(ctx context.Context) (driver.Conn, error)

func (*Connecter) Driver added in v0.0.2

func (c *Connecter) Driver() driver.Driver

type Driver

type Driver struct {
	driver.Driver
	*Base
}

func GetDefaultDriver added in v0.0.2

func GetDefaultDriver() *Driver

func (*Driver) Open

func (d *Driver) Open(name string) (driver.Conn, error)

func (*Driver) OpenConnector

func (d *Driver) OpenConnector(name string) (driver.Connector, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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