store

package
v0.2.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2024 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

All the error codes can be found here: https://www.postgresql.org/docs/current/errcodes-appendix.html

Index

Constants

View Source
const (
	EMAIL_VERIFICATION_CODE_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
	EMAIL_VERIFICATION_CODE_LENGTH   = 20
)
View Source
const (
	PG_ERR_UNIQUE_VIOLATION = "unique_violation"
)

Class 23 - Integrity Constraint Violation

Variables

This section is empty.

Functions

func Close

func Close() error

Close closes the database connection. Not intended to be used in production, but makes testing easier.

func Connect

func Connect(dbUrl string) error

Connect establishes a connection with the given postgresql database with the given url.

func DeleteEmailVerificationWithUserId

func DeleteEmailVerificationWithUserId(uid string) (sql.Result, error)

DeleteEmailVerificationWithUserId tries to removes any email verification code that has the given user id. IMPORTANT: this method does not use a transaction so deletion are UNSAFE.

func ExistsUserWithEmail

func ExistsUserWithEmail(email string) (bool, error)

ExistsUserWithEmail checks if there is a user with the given email stored in the database.

func Ping

func Ping() error

Ping makes sure that connection is still alives. It context.Background and timeouts in 5 seconds.

func SaveBentoEntryBatch

func SaveBentoEntryBatch(entries []BentoEntry) error

SaveBentoEntryBatch will save a batch of BentoEntry at once. The entries will be updated in place with the corresponding id, created_at and updated_at from the database.

func StartTx

func StartTx() (*sql.Tx, error)

StartTx begins a new transaction

Types

type Bento

type Bento struct {
	Id        string
	Name      string
	OwnerId   string
	PubKey    string
	CreatedAt time.Time
	UpdatedAt time.Time
}

Bento represents a bento object that has a slice with all the entries as well.

func GetBentoWithId

func GetBentoWithId(id string) (*Bento, error)

GetBentoWithId retrieves a bento with the given id.

func NewBento

func NewBento(name, ownerId, pubKey string) (*Bento, error)

NewBento will create and save a new bento into the database with the given information. This method will return an error if there is another bento with the same name from the same user. All bentos belonging to one user should have unique names.

func (*Bento) Delete

func (b *Bento) Delete(tx *sql.Tx) (sql.Result, error)

Delete removes the bento from the database.

You must call tx.Commit for it to take effect.

func (*Bento) VerifySignature

func (b *Bento) VerifySignature(signature string, challenge string) error

VerifySignature verifies the given hex encoded signature with the given challenge.

type BentoEntry

type BentoEntry struct {
	Id        int64
	Name      string
	Value     string
	BentoId   string
	CreatedAt time.Time
	UpdatedAt time.Time
}

BentoEntry represents an entry (secret) of a already preprared bento in the database.

func GetEntriesForBento

func GetEntriesForBento(bentoId string) ([]BentoEntry, error)

GetEntriesForBento will get all the entries for a given bento.

func NewBentoEntry

func NewBentoEntry(name, value, bentoId string) BentoEntry

NewBentoEntry just creates a new BentoEntry struct and it DOES NOT saves it in the database. This allows any procedure to create multiple BentoEntry if needed and save them in a batch.

type EmailVerification

type EmailVerification struct {
	Id        int64
	Code      string
	UserId    string
	ExpiresAt time.Time
	CreatedAt time.Time
}

EmailVerification represents an email verification record in the database.

func GetEmailVerificationWithCode

func GetEmailVerificationWithCode(code string) (*EmailVerification, error)

GetEmailVerificationWithCode tries to retrieves an email verification record from the database with the given code.

func NewEmailVerification

func NewEmailVerification(userId string) (*EmailVerification, error)

NewEmailVerification creates a new email verification record.

func (*EmailVerification) Delete

func (ev *EmailVerification) Delete(tx *sql.Tx) (sql.Result, error)

Delete removes the email verification from the database. You must call tx.Commit for the deletion to take effect.

func (*EmailVerification) Update

func (ev *EmailVerification) Update(tx *sql.Tx) (sql.Result, error)

Update is not implemented, but its defined to satisfy the Model interface.

type Model

type Model interface {
	// Delete is used to delete the row in the database.
	Delete(tx *sql.Tx) (sql.Result, error)
	// Update will save the current model values into the database.
	Update(tx *sql.Tx) (sql.Result, error)
}

type User

type User struct {
	Id            string
	Email         string
	Password      string
	Name          string
	EmailVerified bool
	CreatedAt     time.Time
	UpdatedAt     time.Time
}

User represents a real user store in the database.

func GetUserWithEmail

func GetUserWithEmail(email string) (*User, error)

GetUserWithEmail retrieves a user in the database with the given email.

func GetUserWithEmailAndPassword

func GetUserWithEmailAndPassword(email, password string) (*User, error)

GetUserWithEmailAndPassword tries to match a user with the given email and password. Usage for signining a user. Its better to use psql to match the password in the query.

func GetUserWithId

func GetUserWithId(id string) (*User, error)

GetUserWithId retrieves a user with the given id. An error is returned if nothing is found.

func NewUser

func NewUser(email, password, name string) (*User, error)

NewUser creates a new user with the given information. This function will save the new user in the database.

func (*User) Delete

func (u *User) Delete(tx *sql.Tx) (sql.Result, error)

Delete removes a user from the database using the id.

You must call tx.Commit for it to take effect.

func (*User) Update

func (u *User) Update(tx *sql.Tx) (sql.Result, error)

Update updates the user in the database with the current field values of the struct.

This does not update the Id, CreatedAt, and UpdatedAt fields.

You must call tx.Commit for it to take effect.

Jump to

Keyboard shortcuts

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