database

package
v0.1.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package database contains utilities to connect to the default Postgres (+ Timescale) database. It implements the models and the API to interact with the database.

Index

Constants

View Source
const (
	RoleDefault    = "DEFAULT"
	RoleMaintainer = "MAINTAINER"
	RoleAdmin      = "ADMIN"
)

Various roles of a team member.

Variables

View Source
var (
	// ErrRecordNotFound is the error returned when the database has no matching
	// record.
	//
	// Since gorm v2 (1.20), it returns an error rather than a check using method,
	// so this error is just an alias of same error which can be used to check if
	// the record did not exist.
	ErrRecordNotFound = gorm.ErrRecordNotFound

	// ErrNilPointer is the error returned in case when the pointer is nil. This
	// can be checked using `errors.Unwrap`.
	ErrNilPointer = errors.New("pointer cannot be nil")
)

Functions

This section is empty.

Types

type Bool

type Bool rune

Bool for storing in models as a rune.

't', 'T', '1' and 1 represnt true, rest false.

const (
	True  Bool = 't'
	False Bool = 'f'
)

True and False.

func (Bool) T

func (b Bool) T() bool

T tells if b is true or false.

type Check

type Check struct {
	ID        string `gorm:"primaryKey"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index"`

	OwnerID uint
	Owner   User

	Title string `gorm:"NOT NULL"`

	Interval time.Duration `gorm:"DEFAULT:30"`
	Timeout  time.Duration `gorm:"DEFAULT:30"`

	InputType  string `gorm:"NOT NULL"`
	InputValue string `gorm:"NOT NULL"`

	OutputType  string `gorm:"NOT NULL"`
	OutputValue string `gorm:"NOT NULL"`

	TargetType  string `gorm:"NOT NULL"`
	TargetValue string `gorm:"NOT NULL"`

	Payloads []Payload `gorm:"foreignkey:CheckID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}

Check model.

type Config

type Config interface {
	GetName() string // Name of the database

	GetHost() string // Host of the database
	GetPort() uint16 // Port of the database

	GetUsername() string // Username of the database
	GetPassword() string // Password of the database

	IsSSLMode() bool // Should connect using SSL
}

Config can be used to create a connection with the database.

type Conn

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

Conn is the database connection which can be used to access the API to interact with the database.

func NewConn

func NewConn(ctx context.Context, conf Config) (*Conn, error)

NewConn creates a new connection with the database.

func (*Conn) AddChecksToPage

func (c *Conn) AddChecksToPage(ctx context.Context, ownerID, pageID uint, checkIDs []string) error

AddChecksToPage adds relationship between the checks and the page, hence inserting checks into the page.

func (*Conn) AddTeamMemberToPage

func (c *Conn) AddTeamMemberToPage(
	ctx context.Context,
	ownerID, pageID, memberID uint,
	role string,
) (*PageTeam, error)

AddTeamMemberToPage adds a new team member to the page with the given ID.

func (*Conn) CreateCheck

func (c *Conn) CreateCheck(ctx context.Context, ownerID uint, check *Check) (*Check, error)

CreateCheck creates a new check.

func (*Conn) CreateIncident

func (c *Conn) CreateIncident(ctx context.Context, ownerID, pageID uint, incident *Incident) (*Incident, error)

CreateIncident creates a new incident.

func (*Conn) CreatePage

func (c *Conn) CreatePage(ctx context.Context, ownerID uint, page *Page) (*Page, error)

CreatePage creates a new page.

func (*Conn) CreatePayload

func (c *Conn) CreatePayload(ctx context.Context, ownerID uint, checkID string, payload *Payload) (*Payload, error)

CreatePayload creates a new payload.

func (*Conn) CreateUser

func (c *Conn) CreateUser(ctx context.Context, user *User) (*User, error)

CreateUser creates a new user in the database. It simply returns the user with the same email if the user exists.

func (*Conn) DeleteCheck

func (c *Conn) DeleteCheck(ctx context.Context, ownerID uint, checkID string) error

DeleteCheck deletes the check with the given ID.

func (*Conn) DeleteIncident

func (c *Conn) DeleteIncident(ctx context.Context, ownerID, pageID, incidentID uint) error

DeleteIncident deletes the incident with the given ID.

func (*Conn) DeletePage

func (c *Conn) DeletePage(ctx context.Context, ownerID, pageID uint) error

DeletePage deletes the page with the given ID.

func (*Conn) DeletePayload

func (c *Conn) DeletePayload(ctx context.Context, ownerID, payloadID uint, checkID string) error

DeletePayload deletes the payload with the given ID.

func (*Conn) DeleteUserByEmail

func (c *Conn) DeleteUserByEmail(ctx context.Context, email string) error

DeleteUserByEmail deletes a user entry.

func (*Conn) DeleteUserByID

func (c *Conn) DeleteUserByID(ctx context.Context, id uint) error

DeleteUserByID deletes a user entry.

func (*Conn) GetCheck

func (c *Conn) GetCheck(ctx context.Context, ownerID uint, checkID string, opts GetCheckOpts) (*Check, error)

GetCheck gets a check from given checkID.

func (*Conn) GetIncident

func (c *Conn) GetIncident(
	ctx context.Context,
	ownerID, pageID, incidentID uint,
	opts GetIncidentOpts,
) (*Incident, error)

GetIncident gets an incident from given incidentID.

func (*Conn) GetPage

func (c *Conn) GetPage(ctx context.Context, ownerID, pageID uint, opts GetPageOpts) (*Page, error)

GetPage gets a page from given pageID.

func (*Conn) GetPayload

func (c *Conn) GetPayload(
	ctx context.Context,
	ownerID, payloadID uint,
	checkID string,
	opts GetPayloadOpts,
) (*Payload, error)

GetPayload gets a payload from given payloadID.

func (*Conn) GetUserByEmail

func (c *Conn) GetUserByEmail(ctx context.Context, email string, opts GetUserOpts) (*User, error)

GetUserByEmail gets user by Email.

func (*Conn) GetUserByID

func (c *Conn) GetUserByID(ctx context.Context, id uint, opts GetUserOpts) (*User, error)

GetUserByID gets user by ID.

func (*Conn) RemoveChecksFromPage

func (c *Conn) RemoveChecksFromPage(ctx context.Context, ownerID, pageID uint, checkIDs []string) error

RemoveChecksFromPage removes relationship between the checks and the page, hence deleting checks from the page.

func (*Conn) RemoveTeamMemberFromPage

func (c *Conn) RemoveTeamMemberFromPage(ctx context.Context, ownerID, pageID, memberID uint) error

RemoveTeamMemberFromPage removes the team member from the page.

func (*Conn) UpdateCheck

func (c *Conn) UpdateCheck(ctx context.Context, ownerID uint, checkID string, check *Check) (*Check, error)

UpdateCheck updates a check with the given ID.

func (*Conn) UpdateIncident

func (c *Conn) UpdateIncident(
	ctx context.Context,
	ownerID, pageID, incidentID uint,
	incident *Incident,
) (*Incident, error)

UpdateIncident updates a incident with the given ID.

func (*Conn) UpdatePage

func (c *Conn) UpdatePage(ctx context.Context, ownerID, pageID uint, page *Page) (*Page, error)

UpdatePage updates a page with the given ID.

func (*Conn) UpdatePayload

func (c *Conn) UpdatePayload(
	ctx context.Context,
	ownerID, payloadID uint,
	checkID string,
	payload *Payload,
) (*Payload, error)

UpdatePayload updates a payload with the given ID.

func (*Conn) UpdateTeamMemberRole

func (c *Conn) UpdateTeamMemberRole(
	ctx context.Context,
	ownerID, pageID, memberID uint,
	role string,
) (*PageTeam, error)

UpdateTeamMemberRole updates the role of a team member.

func (*Conn) UpdateUserByEmail

func (c *Conn) UpdateUserByEmail(ctx context.Context, email string, user *User) (*User, error)

UpdateUserByEmail updates the user for given email.

func (*Conn) UpdateUserByID

func (c *Conn) UpdateUserByID(ctx context.Context, id uint, user *User) (*User, error)

UpdateUserByID updates the user for given ID.

type GetCheckOpts

type GetCheckOpts struct {
	Owner bool

	Payloads bool
}

GetCheckOpts are the options to preload check associations.

type GetIncidentOpts

type GetIncidentOpts struct {
	Owner bool
	Page  bool
}

GetIncidentOpts are the options to preload payload associations.

type GetPageOpts

type GetPageOpts struct {
	Owner bool

	Checks    bool
	Incidents bool
	Team      bool
}

GetPageOpts are the options to preload page associations.

type GetPayloadOpts

type GetPayloadOpts struct {
	Owner bool
	Check bool
}

GetPayloadOpts are the options to preload payload associations.

type GetUserOpts

type GetUserOpts struct {
	Checks    bool
	Payloads  bool
	Pages     bool
	TeamPages bool
	Incidents bool
}

GetUserOpts are the get options for user relations. Objects are preloaded using the options set here.

type Incident

type Incident struct {
	gorm.Model

	Owner   User
	OwnerID uint

	PageID uint
	Page   Page

	Title       string `gorm:"NOT NULL"`
	Description string `gorm:"TYPE:text"`
	Resolved    Bool   `gorm:"DEFAULT:102;size:256"`

	TimeStamp time.Time     `gorm:"NOT NULL"`
	Duration  time.Duration `gorm:"NOT NULL"`
}

Incident model.

type Page

type Page struct {
	gorm.Model

	OwnerID uint
	Owner   User

	Title       string `gorm:"NOT NULL"`
	Description string `gorm:"TYPE:text"`
	Visibility  Bool   `gorm:"DEFAULT:102;size:256"`

	Checks    []Check    `gorm:"many2many:page_checks"`
	Incidents []Incident `gorm:"foreignkey:PageID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	Team      []PageTeam `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}

Page model.

type PageTeam

type PageTeam struct {
	Page   Page
	PageID uint `gorm:"primarKey;autoIncrement:false"`

	User   User
	UserID uint `gorm:"primaryKey;autoIncrement:false"`

	Role string `gorm:"NOT NULL"`
}

PageTeam model.

type Payload

type Payload struct {
	gorm.Model

	Owner   User
	OwnerID uint

	CheckID string
	Check   Check

	Type  string `gorm:"NOT NULL"`
	Value string `gorm:"NOT NULL;TYPE:text"`
}

Payload model.

type User

type User struct {
	gorm.Model

	Email string `gorm:"UNIQUE;NOT NULL"`
	Name  string `gorm:"NOT NULL"`

	Checks    []Check    `gorm:"foreignkey:OwnerID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	Payloads  []Payload  `gorm:"foreignkey:OwnerID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	Pages     []Page     `gorm:"foreignkey:OwnerID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	TeamPages []PageTeam `gorm:"foreignkey:UserID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	Incidents []Incident `gorm:"foreignkey:OwnerID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}

User model.

Jump to

Keyboard shortcuts

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