Skip to content

tinh-tinh/mongoose

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

79 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Mongoose for Tinh Tinh

Tinh Tinh Logo

Overview

Mongoose for Tinh Tinh is a powerful MongoDB integration package designed specifically for the Tinh Tinh framework. It provides a clean, efficient, and type-safe way to work with MongoDB databases in your Go applications.

Features

  • πŸš€ Simple and intuitive MongoDB integration
  • πŸ“¦ BSON document handling
  • πŸ”„ Automatic model serialization/deserialization
  • 🎯 Type-safe query building
  • πŸ› οΈ Advanced features including:
    • Collection operations
    • Aggregation pipelines
    • Index management
    • Transaction support
    • Change streams
    • GridFS support

Installation

To install the package, use:

go get -u github.com/tinh-tinh/mongoose/v2

Quick Start

package main

import (
    "context"
    "github.com/tinh-tinh/mongoose/v2"
)

// User represents your MongoDB document
type User struct {
    ID       string `bson:"_id,omitempty"`
    Name     string `bson:"name"`
    Email    string `bson:"email"`
    Age      int    `bson:"age"`
    IsActive bool   `bson:"is_active"`
}

func main() {
    // Initialize MongoDB connection
    client := mongoose.New(&mongoose.Config{
        URI:      "mongodb://localhost:27017",
        Database: "myapp",
    })

    // Get a collection
    collection := client.Collection("users")

    // Insert a document
    user := User{
        Name:     "John Doe",
        Email:    "[email protected]",
        Age:      30,
        IsActive: true,
    }
    
    result, err := collection.InsertOne(context.Background(), user)
    if err != nil {
        panic(err)
    }
}

Configuration

The package supports various configuration options:

type Config struct {
    URI              string        // MongoDB connection URI
    Database         string        // Database name
    MaxPoolSize      uint64        // Maximum number of connections
    MinPoolSize      uint64        // Minimum number of connections
    ConnectTimeout   time.Duration // Connection timeout
    MaxConnIdleTime  time.Duration // Maximum idle connection time
    RetryWrites     bool          // Enable retry writes
    RetryReads      bool          // Enable retry reads
    DirectConnection bool          // Use direct connection
}

Key Features

Collection Operations

// Find documents
users, err := collection.Find(ctx, bson.M{"age": bson.M{"$gt": 25}})

// Update documents
update := bson.M{"$set": bson.M{"is_active": false}}
result, err := collection.UpdateMany(ctx, bson.M{"age": bson.M{"$lt": 18}}, update)

// Delete documents
result, err := collection.DeleteOne(ctx, bson.M{"email": "[email protected]"})

Aggregation Pipeline

pipeline := mongo.Pipeline{
    {{$match: {"age": {"$gt": 25}}}},
    {{$group: {"_id": "$city", "count": {"$sum": 1}}}}
}
cursor, err := collection.Aggregate(ctx, pipeline)

Transactions

err := client.UseTransaction(ctx, func(sessCtx mongo.SessionContext) error {
    // Perform operations within transaction
    _, err := collection.InsertOne(sessCtx, newUser)
    if err != nil {
        return err
    }
    _, err = collection.UpdateOne(sessCtx, filter, update)
    return err
})

Best Practices

  1. Connection Management

    • Always close connections when done
    • Use appropriate pool sizes
    • Handle connection errors properly
  2. Error Handling

    • Check for both operation and connection errors
    • Implement proper retry logic
    • Log relevant error information
  3. Performance Optimization

    • Use appropriate indexes
    • Implement efficient queries
    • Monitor query performance

Documentation

For detailed documentation and examples, please visit:

Contributing

We welcome contributions! Here's how you can help:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Support

If you encounter any issues or need help, you can:

  • Open an issue in the GitHub repository
  • Check our documentation
  • Join our community discussions

About

πŸƒ Mongo ODM module for Tinh Tinh framework

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages