store

package
v0.0.9-alpha Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2021 License: MIT Imports: 11 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FindRequest

type FindRequest struct {
	UserID string
	Sort   string
	Since  time.Time
	Limit  int
	Skip   int
}

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

type MemDB struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

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 NewMemDB

func NewMemDB() *MemDB

NewMemDB initialises and returns an instance of MemDB.

func (*MemDB) Count

func (m *MemDB) Count() (pastes, users int64)

Count returns total count of pastes and users.

func (*MemDB) Create

func (m *MemDB) Create(p Paste) (id int64, err error)

Create creates and stores a new paste returning its ID.

func (*MemDB) Delete

func (m *MemDB) Delete(id int64) error

Delete deletes a paste by ID.

func (*MemDB) Find

func (m *MemDB) Find(req FindRequest) (pastes []Paste, err error)

Find return a sorted list of pastes for a given request.

func (*MemDB) Get

func (m *MemDB) Get(id int64) (Paste, error)

Get returns a paste by ID.

func (*MemDB) SaveUser

func (m *MemDB) SaveUser(usr User) (id string, err error)

SaveUser creates a new or updates an existing user.

func (*MemDB) Update

func (m *MemDB) Update(p Paste) (Paste, error)

Update updates existing paste.

func (*MemDB) User

func (m *MemDB) User(id string) (User, error)

User returns a user by ID.

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

func (p Paste) Expiration() string

Expiration returns a "humanized" duration between now and the expiry date stored in `Expires`. For example: "25 minutes" or "2 months" or "Never".

func (Paste) URL

func (p Paste) URL() string

URL generates a base62 encoded string from the paste ID. This string is used as a unique URL for the paste, hence the name.

func (Paste) URL2ID

func (p Paste) URL2ID(url string) (int64, error)

URL2ID decodes the previously generated URL string into a paste ID.

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.

func (*PostgresDB) Update

func (pg *PostgresDB) Update(p Paste) (Paste, error)

Update saves the paste into database and returns it

func (*PostgresDB) User

func (pg *PostgresDB) User(id string) (User, error)

User returns a user by ID.

type User

type User struct {
	ID    string `json:"id" gorm:"primaryKey"`
	Name  string `json:"name" gorm:"index"`
	Email string `json:"email" gorm:"index"`
	IP    string `json:"ip,omitempty"`
	Admin bool   `json:"admin"`
}

User represents a single user.

Jump to

Keyboard shortcuts

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