Documentation ¶
Overview ¶
Package store defines a common interface that any concrete storage implementation must implement. Along with some supporting types. It provides two implementations of store.Interface - MemDB and PostgresDB.
Index ¶
- type FindRequest
- type Interface
- type MemDB
- func (m *MemDB) Count() (pastes, users int64)
- func (m *MemDB) Create(p Paste) (id int64, err error)
- func (m *MemDB) Delete(id int64) error
- func (m *MemDB) Find(req FindRequest) (pastes []Paste, err error)
- func (m *MemDB) Get(id int64) (Paste, error)
- func (m *MemDB) SaveUser(usr User) (id string, err error)
- func (m *MemDB) Update(p Paste) (Paste, error)
- func (m *MemDB) User(id string) (User, error)
- type Paste
- type PostgresDB
- func (pg *PostgresDB) Count() (pastes, users int64)
- func (pg *PostgresDB) Create(p Paste) (id int64, err error)
- func (pg *PostgresDB) Delete(id int64) error
- func (pg *PostgresDB) Find(req FindRequest) (pastes []Paste, err error)
- func (pg *PostgresDB) Get(id int64) (Paste, error)
- func (pg *PostgresDB) SaveUser(usr User) (id string, err error)
- func (pg *PostgresDB) Update(p Paste) (Paste, error)
- func (pg *PostgresDB) User(id string) (User, error)
- type User
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FindRequest ¶
FindRequest is an input to the Find method
type Interface ¶
type Interface interface { Count() (pastes, users int64) // return total counts for pastes and users Create(paste Paste) (id int64, err error) // create new paste and return its id Delete(id int64) error // delete paste by id Find(req FindRequest) ([]Paste, error) // find pastes Get(id int64) (Paste, error) // get paste by id Update(paste Paste) (Paste, error) // update paste information and return updated paste SaveUser(usr User) (id string, err error) // creates or updates a user User(id string) (User, error) // get user by id }
Interface defines methods that an implementation of a concrete storage must provide.
type MemDB ¶
MemDB is a memory storage that implements the store.Interface. Because it's a transient storage you will loose all the data once the process exits. It's not completely useless though. You can use it when a temporary sharing is needed or as a cache for another storage.
func (*MemDB) Find ¶
func (m *MemDB) Find(req FindRequest) (pastes []Paste, err error)
Find return a sorted list of pastes for a given request.
type Paste ¶
type Paste struct { ID int64 `json:"id" gorm:"primaryKey"` Title string `json:"title"` Body string `json:"body"` Expires time.Time `json:"expires" gorm:"index"` DeleteAfterRead bool `json:"delete_after_read"` Privacy string `json:"privacy"` Password string `json:"password"` CreatedAt time.Time `json:"created"` Syntax string `json:"syntax"` UserID string `json:"user_id" gorm:"index default:null"` User User `json:"user"` Views int64 `json:"views"` }
Paste represents a single paste with an optional reference to its user.
func (Paste) Expiration ¶
Expiration returns a "humanized" duration between now and the expiry date stored in `Expires`. For example: "25 minutes" or "2 months" or "Never".
type PostgresDB ¶
type PostgresDB struct {
// contains filtered or unexported fields
}
PostgresDB is a Postgres SQL databasse storage that implements the store.Interface.
func NewPostgresDB ¶
func NewPostgresDB(conn string, autoMigrate bool) (*PostgresDB, error)
NewPostgresDB initialises a new instance of PostgresDB and returns. It tries to establish a database connection specified by conn and if autoMigrate is true it will try and create/alter all the tables.
func (*PostgresDB) Count ¶
func (pg *PostgresDB) Count() (pastes, users int64)
Count returns total count of pastes and users.
func (*PostgresDB) Create ¶
func (pg *PostgresDB) Create(p Paste) (id int64, err error)
Create creates and stores a new paste returning its ID.
func (*PostgresDB) Delete ¶
func (pg *PostgresDB) Delete(id int64) error
Delete deletes a paste by ID.
func (*PostgresDB) Find ¶
func (pg *PostgresDB) Find(req FindRequest) (pastes []Paste, err error)
Find return a sorted list of pastes for a given request.
func (*PostgresDB) Get ¶
func (pg *PostgresDB) Get(id int64) (Paste, error)
Get returns a paste by ID.
func (*PostgresDB) SaveUser ¶
func (pg *PostgresDB) SaveUser(usr User) (id string, err error)
SaveUser creates a new or updates an existing user.