Skip to main content
The Transaction type represents a database transaction.

Transaction lifecycle

Commit

Commits the transaction.
func (t *Transaction) Commit() error
error
error
Error if the transaction could not be committed
Example:
tx, err := db.BeginTransaction()
if err != nil {
    log.Fatal(err)
}
defer tx.Rollback()

// ... perform operations ...

err = tx.Commit()
if err != nil {
    log.Fatal(err)
}

Rollback

Rolls back the transaction. Safe to call multiple times.
func (t *Transaction) Rollback()
Example:
tx, err := db.BeginTransaction()
if err != nil {
    log.Fatal(err)
}
defer tx.Rollback() // Automatically rollback if not committed

// ... perform operations ...

IsActive

Returns whether the transaction is still active.
func (t *Transaction) IsActive() bool
bool
bool
True if the transaction is active

Document operations

Insert

Inserts a document into a collection.
func (t *Transaction) Insert(collectionName string, doc interface{}) (string, error)
collectionName
string
required
Name of the collection
doc
interface{}
required
Document to insert (typically a map or struct)
string
string
ID of the inserted document
error
error
Error if the document could not be inserted
Example:
tx, err := db.BeginTransaction()
if err != nil {
    log.Fatal(err)
}
defer tx.Rollback()

doc := map[string]interface{}{
    "name": "Alice",
    "age":  30,
    "email": "alice@example.com",
}
id, err := tx.Insert("users", doc)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Inserted document with ID: %s\n", id)

tx.Commit()

FindByID

Finds a document by its ID.
func (t *Transaction) FindByID(collectionName, id string, result interface{}) (bool, error)
collectionName
string
required
Name of the collection
id
string
required
Document ID to find
result
interface{}
required
Pointer to store the result
bool
bool
True if the document was found
error
error
Error if the operation failed
Example:
var result map[string]interface{}
found, err := tx.FindByID("users", "user123", &result)
if err != nil {
    log.Fatal(err)
}
if found {
    fmt.Printf("Found: %+v\n", result)
} else {
    fmt.Println("Document not found")
}

UpdateByID

Updates a document by its ID.
func (t *Transaction) UpdateByID(collectionName, id string, doc interface{}) error
collectionName
string
required
Name of the collection
id
string
required
Document ID to update
doc
interface{}
required
Updated document data
error
error
Error if the document could not be updated
Example:
updatedDoc := map[string]interface{}{
    "name": "Alice Smith",
    "age":  31,
    "email": "alice@example.com",
}
err := tx.UpdateByID("users", "user123", updatedDoc)
if err != nil {
    log.Fatal(err)
}

DeleteByID

Deletes a document by its ID.
func (t *Transaction) DeleteByID(collectionName, id string) error
collectionName
string
required
Name of the collection
id
string
required
Document ID to delete
error
error
Error if the document could not be deleted
Example:
err := tx.DeleteByID("users", "user123")
if err != nil {
    log.Fatal(err)
}

FindAll

Finds all documents in a collection.
func (t *Transaction) FindAll(collectionName string, result interface{}) error
collectionName
string
required
Name of the collection
result
interface{}
required
Pointer to a slice to store the results
error
error
Error if the operation failed
Example:
var all []map[string]interface{}
err := tx.FindAll("users", &all)
if err != nil {
    log.Fatal(err)
}
for _, doc := range all {
    fmt.Printf("User: %+v\n", doc)
}

Count

Returns the number of documents in a collection.
func (t *Transaction) Count(collectionName string) (int64, error)
collectionName
string
required
Name of the collection
int64
int64
Number of documents
error
error
Error if the count could not be retrieved
Example:
count, err := tx.Count("users")
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Total users: %d\n", count)

Collection management

CreateCollection

Creates a new collection within the transaction.
func (t *Transaction) CreateCollection(name string) error
name
string
required
Name of the collection to create
error
error
Error if the collection could not be created
Example:
err := tx.CreateCollection("products")
if err != nil {
    log.Fatal(err)
}

DropCollection

Drops a collection within the transaction.
func (t *Transaction) DropCollection(name string) error
name
string
required
Name of the collection to drop
error
error
Error if the collection could not be dropped
Example:
err := tx.DropCollection("old_data")
if err != nil {
    log.Fatal(err)
}

RenameCollection

Renames a collection within the transaction.
func (t *Transaction) RenameCollection(oldName, newName string) error
oldName
string
required
Current name of the collection
newName
string
required
New name for the collection
error
error
Error if the collection could not be renamed
Example:
err := tx.RenameCollection("users", "customers")
if err != nil {
    log.Fatal(err)
}

Build docs developers (and LLMs) love