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
- Creates a SQLite database with connection pooling
- Creates a users table with Bun ORM
- Inserts sample users (Alice, Bob, Charlie)
- Queries all users and displays them
- Updates a user (changes Alice's name)
- Performs a transaction (deletes Bob, inserts David)
- Shows health check status with latency
- 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