database

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 1 Imported by: 0

README

Database Integration

This package provides storage interfaces and implementations for time series data.

Overview

The database package provides:

  • A generic Storage interface for time series data operations
  • InfluxDBStorage implementation (scaffold)
  • TimescaleDBStorage implementation (scaffold)

Status

Current State: Initial scaffolding - ready for full implementation

Completed
  • ✅ Created Storage interface with CRUD operations
  • ✅ Created InfluxDBStorage struct and skeleton methods
  • ✅ Created TimescaleDBStorage struct and skeleton methods
  • ✅ Added basic tests for storage creation
TODO (Requires Full Implementation)
InfluxDB Integration
  • Add InfluxDB Go client dependency (influxdb-client-go)
  • Implement StoreCandle() with actual InfluxDB write
  • Implement StoreCandles() with batch writes
  • Implement GetCandles() with Flux query
  • Implement GetLatestCandles() with sorting
  • Implement DeleteSymbol() with delete query
  • Implement Close() with proper connection cleanup
  • Add connection pooling and retry logic
  • Add unit tests with testcontainers
TimescaleDB Integration
  • Add PostgreSQL/TimescaleDB Go client dependency (pgx or lib/pq)
  • Create SQL schema/migrations for time series tables
  • Implement StoreCandle() with SQL INSERT
  • Implement StoreCandles() with batch COPY
  • Implement GetCandles() with time range query
  • Implement GetLatestCandles() with LIMIT/OFFSET
  • Implement DeleteSymbol() with SQL DELETE
  • Implement Close() with connection cleanup
  • Add connection pooling (pgxpool)
  • Add unit tests with testcontainers
Integration Tests
  • Set up Docker Compose for local testing
  • Add integration tests for InfluxDB
  • Add integration tests for TimescaleDB
  • Add performance benchmarks for both databases
Documentation
  • Add README with setup instructions
  • Add examples for common use cases
  • Document DSN/connection string formats
  • Add troubleshooting guide

Usage Examples

// InfluxDB
storage, err := database.NewInfluxDBStorage("http://localhost:8086", "trading")
if err != nil {
    log.Fatal(err)
}
defer storage.Close()

// TimescaleDB
storage, err := database.NewTimescaleDBStorage("postgres://user:pass@localhost/trading")
if err != nil {
    log.Fatal(err)
}
defer storage.Close()

// Store candle
candle := &database.Candle{
    Symbol:    "BTC",
    Timestamp: time.Now().UnixNano(),
    Open:       100.0,
    High:       105.0,
    Low:        95.0,
    Close:      102.0,
    Volume:     1000.0,
}
err = storage.StoreCandle("BTC", candle)

Dependencies Required

InfluxDB
import "github.com/influxdata/influxdb-client-go/v2"
TimescaleDB
import "github.com/jackc/pgx/v5"

Next Steps

  1. Choose primary database for initial implementation
  2. Add necessary dependencies to go.mod
  3. Implement full CRUD operations
  4. Add integration tests
  5. Document performance characteristics
  6. Add to CI/CD pipeline

Notes

  • The current implementation is a scaffold that compiles but returns "not yet implemented" errors
  • Full implementation requires database servers to be available for testing
  • Consider using testcontainers for integration tests
  • Both databases support efficient time series queries but have different trade-offs:
    • InfluxDB: Purpose-built for time series, write-optimized
    • TimescaleDB: Postgres extension, SQL-compatible, ACID guarantees

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Candle

type Candle struct {
	Symbol    string
	Timestamp int64
	Open      float64
	High      float64
	Low       float64
	Close     float64
	Volume    float64
}

Candle represents OHLCV data stored in database

type InfluxDBStorage

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

InfluxDBStorage implements Storage interface using InfluxDB

func NewInfluxDBStorage

func NewInfluxDBStorage(dsn, database string) (*InfluxDBStorage, error)

NewInfluxDBStorage creates a new InfluxDB storage instance

func (*InfluxDBStorage) Close

func (s *InfluxDBStorage) Close() error

Close closes InfluxDB connection TODO: Implement InfluxDB close logic

func (*InfluxDBStorage) DeleteSymbol

func (s *InfluxDBStorage) DeleteSymbol(symbol string) error

DeleteSymbol removes all data for a symbol from InfluxDB TODO: Implement InfluxDB delete logic

func (*InfluxDBStorage) GetCandles

func (s *InfluxDBStorage) GetCandles(symbol string, startTime int64, endTime int64) ([]*Candle, error)

GetCandles retrieves candles from InfluxDB TODO: Implement InfluxDB query logic

func (*InfluxDBStorage) GetLatestCandles

func (s *InfluxDBStorage) GetLatestCandles(symbol string, limit int) ([]*Candle, error)

GetLatestCandles retrieves recent candles from InfluxDB TODO: Implement InfluxDB latest query logic

func (*InfluxDBStorage) StoreCandle

func (s *InfluxDBStorage) StoreCandle(symbol string, candle *Candle) error

StoreCandle stores a single candle in InfluxDB TODO: Implement InfluxDB client connection and write logic

func (*InfluxDBStorage) StoreCandles

func (s *InfluxDBStorage) StoreCandles(symbol string, candles []*Candle) error

StoreCandles stores multiple candles in InfluxDB TODO: Implement InfluxDB batch write logic

type Storage

type Storage interface {
	// StoreCandle stores a single candle in the database
	StoreCandle(symbol string, candle *Candle) error

	// StoreCandles stores multiple candles in the database
	StoreCandles(symbol string, candles []*Candle) error

	// GetCandles retrieves candles for a symbol within a time range
	GetCandles(symbol string, startTime int64, endTime int64) ([]*Candle, error)

	// GetLatestCandles retrieves the most recent N candles for a symbol
	GetLatestCandles(symbol string, limit int) ([]*Candle, error)

	// DeleteSymbol removes all data for a symbol
	DeleteSymbol(symbol string) error

	// Close closes the database connection
	Close() error
}

Storage represents a generic time series storage interface

type TimescaleDBStorage

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

TimescaleDBStorage implements Storage interface using TimescaleDB (PostgreSQL extension)

func NewTimescaleDBStorage

func NewTimescaleDBStorage(dsn string) (*TimescaleDBStorage, error)

NewTimescaleDBStorage creates a new TimescaleDB storage instance

func (*TimescaleDBStorage) Close

func (s *TimescaleDBStorage) Close() error

Close closes TimescaleDB connection TODO: Implement TimescaleDB close logic

func (*TimescaleDBStorage) DeleteSymbol

func (s *TimescaleDBStorage) DeleteSymbol(symbol string) error

DeleteSymbol removes all data for a symbol from TimescaleDB TODO: Implement TimescaleDB delete logic

func (*TimescaleDBStorage) GetCandles

func (s *TimescaleDBStorage) GetCandles(symbol string, startTime int64, endTime int64) ([]*Candle, error)

GetCandles retrieves candles from TimescaleDB TODO: Implement TimescaleDB query logic

func (*TimescaleDBStorage) GetLatestCandles

func (s *TimescaleDBStorage) GetLatestCandles(symbol string, limit int) ([]*Candle, error)

GetLatestCandles retrieves recent candles from TimescaleDB TODO: Implement TimescaleDB latest query logic

func (*TimescaleDBStorage) StoreCandle

func (s *TimescaleDBStorage) StoreCandle(symbol string, candle *Candle) error

StoreCandle stores a single candle in TimescaleDB TODO: Implement TimescaleDB client connection and insert logic

func (*TimescaleDBStorage) StoreCandles

func (s *TimescaleDBStorage) StoreCandles(symbol string, candles []*Candle) error

StoreCandles stores multiple candles in TimescaleDB TODO: Implement TimescaleDB batch insert logic

Jump to

Keyboard shortcuts

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