README
¶
ipfs-ds-postgres
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/alanshaw/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
Contribute
Feel free to dive in! Open an issue or submit PRs.
License
MIT © Alan Shaw
Documentation
¶
Index ¶
- Variables
- type Datastore
- func (d *Datastore) Batch() (ds.Batch, error)
- func (d *Datastore) Close() error
- func (d *Datastore) Delete(key ds.Key) error
- func (d *Datastore) DeleteContext(ctx context.Context, key ds.Key) error
- func (d *Datastore) Get(key ds.Key) (value []byte, err error)
- func (d *Datastore) GetContext(ctx context.Context, key ds.Key) (value []byte, err error)
- func (d *Datastore) GetSize(key ds.Key) (int, error)
- func (d *Datastore) GetSizeContext(ctx context.Context, key ds.Key) (int, error)
- func (d *Datastore) Has(key ds.Key) (bool, error)
- func (d *Datastore) HasContext(ctx context.Context, key ds.Key) (bool, error)
- func (d *Datastore) PgxPool() *pgxpool.Pool
- func (d *Datastore) Put(key ds.Key, value []byte) error
- func (d *Datastore) PutContext(ctx context.Context, key ds.Key, value []byte) error
- func (d *Datastore) Query(q dsq.Query) (dsq.Results, error)
- func (d *Datastore) QueryContext(ctx context.Context, q dsq.Query) (dsq.Results, error)
- func (d *Datastore) Sync(key ds.Key) error
- type Option
- type Options
Constants ¶
This section is empty.
Variables ¶
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 ¶
NewDatastore creates a new PostgreSQL datastore
func (*Datastore) DeleteContext ¶
DeleteContext removes a row from the PostgreSQL database by the given key.
func (*Datastore) GetContext ¶
GetContext retrieves a value from the PostgreSQL database by the given key.
func (*Datastore) GetSizeContext ¶
GetSizeContext determines the size in bytes of the value for a given key. Returns -1 if not found or other error occurs.
func (*Datastore) Has ¶
Has determines if a value for the given key exists in the PostgreSQL database.
func (*Datastore) HasContext ¶
HasContext determines if a value for the given key exists in the PostgreSQL database.
func (*Datastore) PgxPool ¶ added in v0.1.1
PgxPool exposes the underlying pool of connections to Postgres.
func (*Datastore) PutContext ¶
PutContext "upserts" a row into the PostgreSQL database.
func (*Datastore) Query ¶
Query returns multiple rows from the SQL database based on the passed query parameters.
func (*Datastore) QueryContext ¶
QueryContext returns multiple rows from the SQL database based on the passed query parameters.