The Transaction type represents a database transaction.
Transaction lifecycle
Commit
Commits the transaction.
func (t *Transaction) Commit() 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
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)
Document to insert (typically a map or struct)
ID of the inserted document
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)
Pointer to store the result
True if the document was found
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
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
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
Pointer to a slice to store the results
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)
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 of the collection to create
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 of the collection to drop
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
Current name of the collection
New name for the collection
Error if the collection could not be renamed
Example:
err := tx.RenameCollection("users", "customers")
if err != nil {
log.Fatal(err)
}