database-demo

command
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

README ΒΆ

Database Extension Demo

This example demonstrates the Database Extension with SQLite.

Features Demonstrated

  • Database configuration and setup
  • Bun ORM integration
  • CRUD operations (Create, Read, Update, Delete)
  • Transactions
  • Health checks
  • Connection pool statistics

Running the Example

cd examples/database-demo
go run main.go

What It Does

  1. Creates a SQLite database with connection pooling
  2. Creates a users table with Bun ORM
  3. Inserts sample users (Alice, Bob, Charlie)
  4. Queries all users and displays them
  5. Updates a user (changes Alice's name)
  6. Performs a transaction (deletes Bob, inserts David)
  7. Shows health check status with latency
  8. Displays connection pool stats

Output

βœ“ Inserted users

πŸ“‹ Users:
  - Alice (alice@example.com)
  - Bob (bob@example.com)
  - Charlie (charlie@example.com)

βœ“ Updated Alice's name
βœ“ Transaction completed

πŸ“Š Total users: 3

πŸ’š Health Check:
  - primary: βœ“ healthy (latency: 245Β΅s)

πŸ“ˆ Connection Pool Stats:
  - Open: 1
  - In Use: 0
  - Idle: 1

βœ… Database demo completed!

Configuration

The example uses SQLite with these settings:

database.Config{
    Databases: []database.DatabaseConfig{
        {
            Name:            "primary",
            Type:            database.TypeSQLite,
            DSN:             "file:demo.db?cache=shared",
            MaxOpenConns:    10,
            MaxIdleConns:    5,
            ConnMaxLifetime: 5 * time.Minute,
        },
    },
}

Database Types Supported

  • Postgres: database.TypePostgres
  • MySQL: database.TypeMySQL
  • SQLite: database.TypeSQLite
  • MongoDB: database.TypeMongoDB

Advanced Usage

See the design doc at v2/design/028-database-extension.md for:

  • PostgreSQL and MySQL setup
  • MongoDB usage
  • Multiple database connections
  • Migrations with Bun Migrate
  • Advanced transaction patterns

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