pgds

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2022 License: MIT Imports: 6 Imported by: 1

README

ipfs-ds-postgres

Build Status Coverage Standard README GoDoc golang version Go Report Card

An implementation of the datastore interface for PostgreSQL that uses the pgx PostgreSQL driver.

Note: Currently implements Datastore and Batching interfaces.

Install

go get github.com/ipfs/ipfs-ds-postgres

Usage

Ensure a database is created and a table exists that has the following structure (replacing table_name with the name of the table the datastore will use - by default this is blocks):

CREATE TABLE IF NOT EXISTS table_name (key TEXT NOT NULL UNIQUE, data BYTEA)

It's recommended to create a text_pattern_ops index on the table:

CREATE INDEX IF NOT EXISTS table_name_key_text_pattern_ops_idx ON table_name (key text_pattern_ops)

Import and use in your application:

package main

import (
	"context"
	pgds "github.com/alanshaw/ipfs-ds-postgres"
)

const (
	connString = "postgresql://user:pass@host:12345/database?sslmode=require"
	tableName  = "blocks" // (default)
)

func main() {
	ds, err := pgds.NewDatastore(context.Background(), connString, pgds.Table(tableName))
	if err != nil {
		panic(err)
	}
}

API

GoDoc Reference

Contribute

Feel free to dive in! Open an issue or submit PRs.

License

MIT © Alan Shaw

Documentation

Index

Constants

This section is empty.

Variables

View Source
var OptionDefaults = func(o *Options) error {
	o.Table = "blocks"
	return nil
}

OptionDefaults are the default datastore options. This option will be automatically prepended to any options you pass to the Hydra Head constructor.

Functions

This section is empty.

Types

type Datastore

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

Datastore is a PostgreSQL backed datastore.

func NewDatastore

func NewDatastore(ctx context.Context, connString string, options ...Option) (*Datastore, error)

NewDatastore creates a new PostgreSQL datastore

func (*Datastore) Batch

func (d *Datastore) Batch(_ context.Context) (ds.Batch, error)

Batch creates a set of deferred updates to the database.

func (*Datastore) Close

func (d *Datastore) Close() error

Close closes the underying PostgreSQL database.

func (*Datastore) Delete

func (d *Datastore) Delete(ctx context.Context, key ds.Key) error

Delete removes a row from the PostgreSQL database by the given key.

func (*Datastore) Get

func (d *Datastore) Get(ctx context.Context, key ds.Key) (value []byte, err error)

Get retrieves a value from the PostgreSQL database by the given key.

func (*Datastore) GetSize

func (d *Datastore) GetSize(ctx context.Context, key ds.Key) (int, error)

GetSize determines the size in bytes of the value for a given key.

func (*Datastore) Has

func (d *Datastore) Has(ctx context.Context, key ds.Key) (bool, error)

Has determines if a value for the given key exists in the PostgreSQL database.

func (*Datastore) PgxPool

func (d *Datastore) PgxPool() *pgxpool.Pool

PgxPool exposes the underlying pool of connections to Postgres.

func (*Datastore) Put

func (d *Datastore) Put(ctx context.Context, key ds.Key, value []byte) error

Put "upserts" a row into the SQL database.

func (*Datastore) Query

func (d *Datastore) Query(ctx context.Context, q dsq.Query) (dsq.Results, error)

Query returns multiple rows from the SQL database based on the passed query parameters.

func (*Datastore) Sync

func (d *Datastore) Sync(ctx context.Context, key ds.Key) error

Sync is noop for PostgreSQL databases.

type Option

type Option func(*Options) error

Option is the Datastore option type.

func Table

func Table(t string) Option

Table configures the name of the postgres database table to store data in. Defaults to "blocks".

type Options

type Options struct {
	Table string
}

Options are Datastore options

func (*Options) Apply

func (o *Options) Apply(opts ...Option) error

Apply applies the given options to this Option.

Jump to

Keyboard shortcuts

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