db

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package db provides database abstraction layer for MongoDB operations. It defines interfaces and implementations for database connectivity, querying, and transaction management used throughout the Go IAM application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckAndRunMigrations

func CheckAndRunMigrations(ctx context.Context, db DB) error

CheckAndRunMigrations checks if migrations need to be run and executes them. This function should be called during application startup to ensure the database schema is up to date. It applies migrations in the order they were registered.

Parameters:

  • ctx: Context for the migration operations
  • db: Database interface to execute migrations against

Returns an error if any migration fails to apply or if there are database issues. Successfully applied migrations are recorded in the migrations collection.

func IsMigrationApplied

func IsMigrationApplied(ctx context.Context, db DB, version string) (bool, error)

IsMigrationApplied checks if a specific migration has been applied to the database. This function can be used to conditionally run migrations or check database state.

Parameters:

  • ctx: Context for the database operation
  • db: Database interface to query
  • version: Version identifier of the migration to check

Returns:

  • bool: true if the migration has been applied, false otherwise
  • error: An error if the database query fails

func RegisterMigration

func RegisterMigration(migration MigrationInfo)

RegisterMigration registers a new migration to be tracked and executed. Migrations should be registered in the order they should be applied.

Parameters:

  • migration: MigrationInfo containing all migration details

func ResetMigrations

func ResetMigrations()

ResetMigrations clears all registered migrations. This function is primarily intended for testing purposes to reset migration state. Use with caution in production environments.

Types

type DB

type DB interface {
	DbQuerier
	DbClient
}

DB is the main database interface that combines querying and client operations. It provides a unified interface for all database operations in the application.

func GetDbFromContext

func GetDbFromContext(ctx context.Context) DB

GetDbFromContext retrieves the database connection from the provided context. This function is used by handlers and services to access the database.

Parameters:

  • ctx: The context containing the database connection

Returns the DB interface implementation stored in the context. Panics if no database connection is found in the context.

type DbClient

type DbClient interface {
	// SetDbInContext stores the database connection in the given context
	SetDbInContext(ctx context.Context) context.Context

	// Disconnect closes the database connection
	Disconnect(ctx context.Context) error
}

DbClient defines the interface for database client operations. It handles database connection lifecycle and context management. DbClient defines the interface for database client operations. It handles database connection lifecycle and context management.

type DbCollection

type DbCollection interface {
	Name() string   // Returns the collection name
	DbName() string // Returns the database name
}

DbCollection represents a database collection with its metadata. Implementations should provide the collection name and database name.

type DbCtxKey

type DbCtxKey struct{}

DbCtxKey is the context key type used for storing database connections in context.

type DbQuerier

type DbQuerier interface {
	// FindOne executes a find operation that returns a single document
	FindOne(ctx context.Context, col DbCollection, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult

	// Find executes a find operation that can return multiple documents
	Find(ctx context.Context, col DbCollection, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)

	// InsertOne inserts a single document into the collection
	InsertOne(ctx context.Context, col DbCollection, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)

	// UpdateOne updates a single document in the collection
	UpdateOne(ctx context.Context, col DbCollection, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

	// DeleteOne deletes a single document from the collection
	DeleteOne(ctx context.Context, col DbCollection, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

	// Aggregate executes an aggregation pipeline
	Aggregate(ctx context.Context, col DbCollection, filter interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error)

	// CountDocuments counts the number of documents matching the filter
	CountDocuments(ctx context.Context, col DbCollection, filter interface{}, opts ...*options.CountOptions) (int64, error)

	// BulkWrite executes multiple write operations in bulk
	BulkWrite(ctx context.Context, col DbCollection, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error)

	// UpdateMany updates multiple documents in the collection
	UpdateMany(ctx context.Context, col DbCollection, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
}

DbQuerier defines the interface for database query operations. All MongoDB query operations are abstracted through this interface.

type MigrationFunc

type MigrationFunc func(ctx context.Context, db DB) error

MigrationFunc represents a function that performs a database migration. It receives a context and database interface to execute migration operations.

type MigrationInfo

type MigrationInfo struct {
	Version     string        // Unique version identifier for the migration
	Name        string        // Human-readable name of the migration
	Description string        // Detailed description of what the migration does
	Up          MigrationFunc // Function to apply the migration
	Down        MigrationFunc // Function to rollback the migration
}

MigrationInfo contains metadata and implementation for a database migration. Each migration should have a unique version identifier and both up/down functions.

func GetMigrations

func GetMigrations() []MigrationInfo

GetMigrations returns a copy of all registered migrations. This function is useful for debugging, testing, or displaying migration status.

Returns a slice containing all registered MigrationInfo structs.

type MongoConnection

type MongoConnection struct {
	// contains filtered or unexported fields
}

MongoConnection implements the DB interface for MongoDB. It wraps a MongoDB client and provides all database operations.

func NewMongoConnection

func NewMongoConnection(url string) (*MongoConnection, error)

NewMongoConnection creates a new MongoDB connection using the provided connection URL. It establishes a connection to MongoDB and returns a MongoConnection instance.

Parameters:

  • url: MongoDB connection string (e.g., "mongodb://localhost:27017")

Returns:

  • *MongoConnection: A new MongoDB connection instance
  • error: An error if the connection cannot be established

The connection uses MongoDB Server API version 1 for compatibility.

func (*MongoConnection) Aggregate

func (m *MongoConnection) Aggregate(ctx context.Context, col DbCollection, filter interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error)

Aggregate executes an aggregation pipeline operation. This is a wrapper around MongoDB's Aggregate operation.

Parameters:

  • ctx: Context for the operation
  • col: Collection to aggregate
  • filter: Aggregation pipeline stages
  • opts: Optional aggregation options

Returns a Cursor for iterating over aggregation results and an error if the operation fails.

func (*MongoConnection) BulkWrite

BulkWrite executes multiple write operations in a single bulk operation. This is a wrapper around MongoDB's BulkWrite operation for improved performance.

Parameters:

  • ctx: Context for the operation
  • col: Collection to perform bulk operations on
  • models: Slice of write models (InsertOne, UpdateOne, DeleteOne, etc.)
  • opts: Optional bulk write options

Returns BulkWriteResult containing information about the bulk operation and an error if it fails.

func (*MongoConnection) CountDocuments

func (m *MongoConnection) CountDocuments(ctx context.Context, col DbCollection, filter interface{}, opts ...*options.CountOptions) (int64, error)

CountDocuments counts the number of documents in the collection that match the filter. This is a wrapper around MongoDB's CountDocuments operation.

Parameters:

  • ctx: Context for the operation
  • col: Collection to count documents in
  • filter: Query filter to match documents
  • opts: Optional count options

Returns the number of matching documents and an error if the operation fails.

func (*MongoConnection) DeleteOne

func (m *MongoConnection) DeleteOne(ctx context.Context, col DbCollection, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

DeleteOne deletes a single document from the collection. This is a wrapper around MongoDB's DeleteOne operation.

Parameters:

  • ctx: Context for the operation
  • col: Collection to delete from
  • filter: Query filter to match the document to delete
  • opts: Optional delete options

Returns DeleteResult containing information about the delete operation and an error if it fails.

func (*MongoConnection) Disconnect

func (m *MongoConnection) Disconnect(ctx context.Context) error

Disconnect closes the MongoDB connection. This should be called when shutting down the application to properly clean up resources.

Parameters:

  • ctx: Context for the disconnect operation

Returns an error if the disconnect operation fails.

func (*MongoConnection) Find

func (m *MongoConnection) Find(ctx context.Context, col DbCollection, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)

Find executes a find operation that can return multiple documents. This is a wrapper around MongoDB's Find operation.

Parameters:

  • ctx: Context for the operation
  • col: Collection to query
  • filter: Query filter
  • opts: Optional find options

Returns a Cursor for iterating over results and an error if the operation fails.

func (*MongoConnection) FindOne

func (m *MongoConnection) FindOne(ctx context.Context, col DbCollection, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult

FindOne executes a find operation that returns at most one document. This is a wrapper around MongoDB's FindOne operation.

Parameters:

  • ctx: Context for the operation
  • col: Collection to query
  • filter: Query filter
  • opts: Optional find options

Returns a SingleResult that can be decoded into a document.

func (*MongoConnection) InsertOne

func (m *MongoConnection) InsertOne(ctx context.Context, col DbCollection, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)

InsertOne inserts a single document into the collection. This is a wrapper around MongoDB's InsertOne operation.

Parameters:

  • ctx: Context for the operation
  • col: Collection to insert into
  • document: Document to insert
  • opts: Optional insert options

Returns InsertOneResult containing the inserted document's ID and an error if the operation fails.

func (MongoConnection) SetDbInContext

func (m MongoConnection) SetDbInContext(ctx context.Context) context.Context

SetDbInContext stores this MongoConnection instance in the provided context. This allows handlers and services to access the database connection.

Parameters:

  • ctx: The context to store the database connection in

Returns a new context with the database connection stored.

func (*MongoConnection) UpdateMany

func (m *MongoConnection) UpdateMany(ctx context.Context, col DbCollection, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

UpdateMany updates multiple documents in the collection that match the filter. This is a wrapper around MongoDB's UpdateMany operation.

Parameters:

  • ctx: Context for the operation
  • col: Collection to update
  • filter: Query filter to match documents
  • update: Update operations to apply to all matching documents
  • opts: Optional update options

Returns UpdateResult containing information about the update operation and an error if it fails.

func (*MongoConnection) UpdateOne

func (m *MongoConnection) UpdateOne(ctx context.Context, col DbCollection, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

UpdateOne updates a single document in the collection. This is a wrapper around MongoDB's UpdateOne operation.

Parameters:

  • ctx: Context for the operation
  • col: Collection to update
  • filter: Query filter to match documents
  • update: Update operations to apply
  • opts: Optional update options

Returns UpdateResult containing information about the update operation and an error if it fails.

Directories

Path Synopsis
Package models provides database model definitions and access patterns for the Go IAM system.
Package models provides database model definitions and access patterns for the Go IAM system.

Jump to

Keyboard shortcuts

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