db

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2023 License: AGPL-3.0 Imports: 8 Imported by: 4

Documentation

Overview

Package db contains useful functions related to the Firestore Database

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSubjectNotFound = errors.New("subject does not exist")
	ErrNoPermission    = errors.New("user has not done subject")
	ErrCommentNotFound = errors.New("comment not found")
)

DB operation errors

Functions

func ApplyConcurrentOperationsInTransaction

func ApplyConcurrentOperationsInTransaction(tx *firestore.Transaction, operators []Operation) error

ApplyConcurrentOperationsInTransaction takes a transaction and a list of operations and applies them using concurrency

It returns an error in case any operation fails Use this function at the end of a transaction to ensure write operations are done in the end of the transaction

func CheckSubjectVerified added in v1.6.0

func CheckSubjectVerified(DB Database, userHash, subHash string) error

CheckSubjectVerified takes a user hash and a subject hash and checks whether the user has done this subject

Types

type BatchObject

type BatchObject struct {
	Collection string
	Doc        string

	WriteData  Writer
	UpdateData []firestore.Update

	Preconditions []firestore.Precondition
	SetOptions    []firestore.SetOption
}

BatchObject is used for batched writes that can contain different types that implement Inserter Set Doc to empty string if you'd like to use a random Hash

type Database

type Database struct {
	Client *firestore.Client
	Ctx    context.Context
}

Database is passed to /server/dao functions that require DB operations

func InitFireStore

func InitFireStore() Database

InitFireStore initiates the DB Environment (requires some environment variables to work)

func SetupDB

func SetupDB() Database

SetupDB wraps the Firestore initialization

func (Database) BatchWrite

func (db Database) BatchWrite(objs []BatchObject) error

BatchWrite will perform operations atomically

For a batch of more than 500 documents, batch write will perform each of these batches sequentially TODO: Apply batches concurrently

func (Database) Insert

func (db Database) Insert(obj Inserter, collection string) error

Insert inserts an entity that implements Inserter into a DB collection

func (Database) Restore

func (db Database) Restore(documentHash string) (*firestore.DocumentSnapshot, error)

Restore restores a document with a specific hash

If the document is not found, returns an error which can be checked with status.Code(err) == codes.NotFound

Besides, the Exists method for this Ref will return false

func (Database) RestoreBatch

func (db Database) RestoreBatch(documentHashes []string) ([]*firestore.DocumentSnapshot, error)

RestoreBatch is similar to Env.Restore, but restores a batch of documents concurrently

If any document is not found, the Exists method for that snap will return false

It is guaranteed that snapshots are returned in the same order as passed hashes

func (Database) RestoreCollection

func (db Database) RestoreCollection(collection string) ([]*firestore.DocumentSnapshot, error)

RestoreCollection is similar to Env.Restore, but restores all documents from a collection

Collection cannot end in "/"

func (Database) RestoreCollectionRefs

func (db Database) RestoreCollectionRefs(collection string) ([]*firestore.DocumentRef, error)

RestoreCollectionRefs is similar to RestoreCollection, but uses DocRefs that allow missing documents inside the query

Collection cannot end in "/"

func (Database) Update

func (db Database) Update(obj Updater, collection string) error

Update updates entity in firestore with data in object variable

type Inserter

type Inserter interface {
	Insert(db Database, collection string) error
}

Inserter will be implemented by almost all entities

type Operation

type Operation struct {
	Ref     *firestore.DocumentRef
	Method  string
	Payload interface{}

	Err error
}

Operation is used as a generic operation to be applied on a document

It is mostly used inside transactions to provide an easy way to store operations to be executed after reads

type Updater

type Updater interface {
	Update(db Database, collection string) error
}

Updater will be implemented by almost all entities

type Writer

type Writer interface {
	Inserter
	Updater
}

Writer implements Inserter and Updater (InserterUpdater is a bad name)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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