library-example

command
v0.0.6-beta Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT, MIT Imports: 18 Imported by: 0

README ΒΆ

Library Usage Example

This directory contains a standalone example of how to use mongork as a library in your own Go projects.

What's Included

  • main.go - Complete example showing how to:
    • Load configuration
    • Connect to MongoDB
    • Create and register migrations
    • Run migrations up/down
    • Check migration status

How to Use

1. Build the Example
# From the mongork root directory
go build -o examples/library-example/library-example examples/library-example/main.go

# Or using make
make test-examples
2. Run the Example
# Set required environment variables first
export MONGO_URL="mongodb://localhost:27017"
export MONGO_DATABASE="example_db"

# Run the example
./examples/library-example/library-example
3. Use in Your Own Project
# Initialize your Go module
go mod init your-project-name

# Add mongork dependency
go get github.com/drewjocham/mongork@latest

# Copy and adapt the example code from main.go

Example Migration

The example includes a sample migration that:

  • Creates a sample_collection with a document
  • Adds an index on created_at
  • Demonstrates rollback by dropping the collection

Configuration

The example uses environment variables or falls back to default values:

  • MONGO_URL - MongoDB connection string (default: mongodb://localhost:27017)
  • MONGO_DATABASE - Database name (default: standalone_example)
  • MIGRATIONS_COLLECTION - Collection for migration tracking (default: schema_migrations)

Output

When run successfully, you'll see:

πŸš€ mongork Standalone Example
=====================================
ℹ️  Using default configuration (no .env file found)
πŸ”— Connecting to MongoDB: mongodb://localhost:27017/standalone_example
βœ… Connected to MongoDB successfully

πŸ“Š Migration Status:
   20240109_001     ❌ No    Example migration - creates sample_collection with index

⬆️  Running migrations up...
βœ… Created sample_collection with index
βœ… All migrations applied successfully

πŸ“Š Updated Migration Status:
   20240109_001     βœ… Yes   Example migration - creates sample_collection with index

⬇️  Rolling back last migration...
βœ… Dropped sample_collection
βœ… Rolled back migration: 20240109_001

πŸŽ‰ Standalone example completed successfully!

This demonstrates the complete lifecycle of using mongork as a library.

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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