Documentation ¶
Overview ¶
Package db contains useful functions related to the Firestore Database
Index ¶
- Variables
- func ApplyConcurrentOperationsInTransaction(tx *firestore.Transaction, operators []Operation) error
- func CheckSubjectVerified(DB Database, userHash, subHash string) error
- type BatchObject
- type Database
- func (db Database) BatchWrite(objs []BatchObject) error
- func (db Database) Insert(obj Inserter, collection string) error
- func (db Database) Restore(documentHash string) (*firestore.DocumentSnapshot, error)
- func (db Database) RestoreBatch(documentHashes []string) ([]*firestore.DocumentSnapshot, error)
- func (db Database) RestoreCollection(collection string) ([]*firestore.DocumentSnapshot, error)
- func (db Database) RestoreCollectionRefs(collection string) ([]*firestore.DocumentRef, error)
- func (db Database) Update(obj Updater, collection string) error
- type Inserter
- type Operation
- type Updater
- type Writer
Constants ¶
This section is empty.
Variables ¶
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
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 ¶
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 (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) 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 "/"
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