postgres

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// DSN is the PostgreSQL Data Source Name
	// Example: "host=localhost user=postgres password=secret dbname=agent port=5432 sslmode=disable"
	DSN string

	// TableName is the name of the table to use for storage
	TableName string

	// MaxIdleConns is the maximum number of idle connections
	MaxIdleConns int

	// MaxOpenConns is the maximum number of open connections
	MaxOpenConns int

	// ConnMaxLifetime is the maximum lifetime of a connection
	ConnMaxLifetime time.Duration

	// LogLevel is the GORM log level
	LogLevel logger.LogLevel

	// AutoMigrate enables automatic table creation
	AutoMigrate bool
}

Config holds configuration for PostgreSQL store

func ApplyPostgresOptions added in v0.2.0

func ApplyPostgresOptions(config *Config, opts ...PostgresOption) *Config

ApplyPostgresOptions 应用选项到配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default PostgreSQL configuration

type PostgresOption added in v0.2.0

type PostgresOption func(*Config)

PostgresOption 定义 PostgreSQL Store 配置选项

func WithAutoMigrate added in v0.2.0

func WithAutoMigrate(enabled bool) PostgresOption

WithAutoMigrate 设置是否自动迁移

func WithConnMaxLifetime added in v0.2.0

func WithConnMaxLifetime(d time.Duration) PostgresOption

WithConnMaxLifetime 设置连接最大生命周期

func WithLogLevel added in v0.2.0

func WithLogLevel(level logger.LogLevel) PostgresOption

WithLogLevel 设置 GORM 日志级别

func WithMaxIdleConns added in v0.2.0

func WithMaxIdleConns(n int) PostgresOption

WithMaxIdleConns 设置最大空闲连接数

func WithMaxOpenConns added in v0.2.0

func WithMaxOpenConns(n int) PostgresOption

WithMaxOpenConns 设置最大打开连接数

func WithTableName added in v0.2.0

func WithTableName(tableName string) PostgresOption

WithTableName 设置表名

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a PostgreSQL-backed implementation of the store.Store interface.

Features:

  • JSONB storage for flexible data types
  • Efficient indexing on namespace and key
  • ACID compliance for data integrity
  • Powerful search with JSONB queries
  • Connection pooling

Suitable for:

  • Production deployments
  • Large-scale data storage
  • Complex queries
  • Distributed systems with shared database

func New

func New(dsn string, opts ...PostgresOption) (*Store, error)

New creates a new PostgreSQL-backed store with options

Example:

store, err := postgres.New("host=localhost user=postgres dbname=mydb",
    postgres.WithMaxOpenConns(50),
    postgres.WithAutoMigrate(true),
)

func NewFromDB

func NewFromDB(db *gorm.DB, config *Config) (*Store, error)

NewFromDB creates a Store from an existing GORM DB

func (*Store) Clear

func (s *Store) Clear(ctx context.Context, namespace []string) error

Clear removes all values within a namespace

func (*Store) Close

func (s *Store) Close() error

Close closes the database connection

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, namespace []string, key string) error

Delete removes a value by namespace and key

func (*Store) Get

func (s *Store) Get(ctx context.Context, namespace []string, key string) (*store.Value, error)

Get retrieves a value by namespace and key

func (*Store) List

func (s *Store) List(ctx context.Context, namespace []string) ([]string, error)

List returns all keys within a namespace

func (*Store) Ping

func (s *Store) Ping(ctx context.Context) error

Ping tests the connection to PostgreSQL

func (*Store) Put

func (s *Store) Put(ctx context.Context, namespace []string, key string, value interface{}) error

Put stores a value with the given namespace and key

func (*Store) Search

func (s *Store) Search(ctx context.Context, namespace []string, filter map[string]interface{}) ([]*store.Value, error)

Search finds values matching the filter within a namespace

func (*Store) Size

func (s *Store) Size(ctx context.Context) (int64, error)

Size returns the total number of stored values

Jump to

Keyboard shortcuts

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