pgbase

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: MIT Imports: 13 Imported by: 0

README

简介

成功实现了多写, 通过自定义 DBConnectPocketBase 的存储后端由 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)
		},
		DataMaxOpenConns: core.DefaultDataMaxOpenConns,
		DataMaxIdleConns: core.DefaultDataMaxIdleConns,
	}
	app := pocketbase.NewWithConfig(cfg)
	try.To(app.Bootstrap())

	// required!
	// required!
	// required!
	// postgres support concurrent, reset max conns
	if db, ok := app.NonconcurrentDB().(*dbx.DB); ok {
		db.DB().SetMaxOpenConns(cfg.DataMaxOpenConns)
		db.DB().SetMaxIdleConns(cfg.DataMaxIdleConns)
	}
	try.To(app.Start())
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cache = otter.Must(&otter.Options[string, string]{
	MaximumSize: 1000,
})
View Source
var ErrBaseDriverNotExecer = fmt.Errorf("base driver don't support driver.ExecerContext")
View Source
var ErrNoTranslatedQuery = fmt.Errorf("no translated query")

Functions

func NewSqliteBuilder

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

func Open

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

Types

type Builder

type Builder struct {
	dbx.Builder
}

func (*Builder) GeneratePlaceholder

func (*Builder) GeneratePlaceholder(i int) string

type Conn

type Conn struct {
	driver.Conn
}

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
}

func (*Connecter) Connect

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

type Driver

type Driver struct {
	driver.Driver
	Cache *otter.Cache[string, string]
}

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