sql

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2025 License: MPL-2.0 Imports: 5 Imported by: 0

README

SQL Snapshot Store

The sql is a module containing multiple sql based snapshot stores that are all based on the database/sql interface in go standard library. The different snapshot stores has specific database schemas that support the different databases.

SQLite

Supports the SQLite database https://www.sqlite.org/

Database Schema
CREATE TABLE IF NOT EXISTS snapshots (
    id              VARCHAR NOT NULL,
    type            VARCHAR,
    version         INTEGER,
    global_version  INTEGER,
    state           BLOB
);

CREATE UNIQUE INDEX IF NOT EXISTS id_type ON snapshots (id, type);
Constructor
// NewSQLite connection to database
func NewSQLite(db *sql.DB) (*SQLite, error) {
Example of use

import (
	sqldriver "database/sql"
	"github.com/hallgren/eventsourcing/snapshotstore/sql"
	_ "github.com/mattn/go-sqlite3"
)

db, err := sqldriver.Open("sqlite3", "file::memory:?locked.sqlite?cache=shared")
if err != nil {
	return nil, nil, err
}

sqliteSnapshotStore, err := sql.NewSQLite(db)
if err != nil {
	return nil, nil, err
}

Postgres

Supports the Postgres database https://www.postgresql.org

Database Schema
CREATE TABLE IF NOT EXISTS snapshots (
    id VARCHAR NOT NULL,
    type VARCHAR,
    version INTEGER,
    global_version INTEGER,
    state BYTEA
);

CREATE UNIQUE INDEX IF NOT EXISTS id_type ON snapshots (id, type);
Constructor
// NewPostgres connection to database
func NewPostgres(db *sql.DB) (*Postgres, error) {
Example of use
import (
	gosql "database/sql"
	_ "github.com/lib/pq"
	"github.com/hallgren/eventsourcing/snapshotstore/sql"
)

db, err := gosql.Open("postgres", dsn)
if err != nil {
  return err
}


postgreSnapshotStore, err := sql.NewPostgres(db)
if err != nil {
	return err
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Postgres added in v0.2.0

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

func NewPostgres added in v0.2.0

func NewPostgres(db *sql.DB) (*Postgres, error)

NewPostgres connection to database

func (*Postgres) Close added in v0.2.0

func (s *Postgres) Close()

Close the connection

func (*Postgres) Get added in v0.2.0

func (s *Postgres) Get(ctx context.Context, aggregateID, aggregateType string) (core.Snapshot, error)

Get return the snapshot data from the database

func (*Postgres) Save added in v0.2.0

func (s *Postgres) Save(snapshot core.Snapshot) error

Save persists the snapshot

type SQLite added in v0.2.0

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

func NewSQLite added in v0.2.0

func NewSQLite(db *sql.DB) (*SQLite, error)

NewSQLite connection to database

func (*SQLite) Close added in v0.2.0

func (s *SQLite) Close()

Close the connection

func (*SQLite) Get added in v0.2.0

func (s *SQLite) Get(ctx context.Context, aggregateID, aggregateType string) (core.Snapshot, error)

Get return the snapshot data from the database

func (*SQLite) Save added in v0.2.0

func (s *SQLite) Save(snapshot core.Snapshot) error

Save persists the snapshot

Jump to

Keyboard shortcuts

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