English | 中文
goner/gorm/sqlite Component,, Gone Gorm SQLite Driver
Introduction
Gone Gorm SQLite Driver is the SQLite database driver implementation for the Gone Gorm component. It allows you to use GORM to operate SQLite databases in Gone applications, providing seamless integration with the Gone framework. SQLite is a lightweight, zero-configuration, self-contained database engine that is particularly suitable for embedded applications and development environments.
Features
- Support for basic SQLite database operations
- Seamless integration with Gone framework
- Zero configuration, easy to use
- Transaction management support
- Database migration support
- Suitable for development and testing environments
- In-memory database support
Installation
import (
"github.com/gone-io/gone/v2"
"github.com/gone-io/goner/gorm"
_ "github.com/gone-io/goner/gorm/sqlite"
)
func main() {
gone.
Loads(
gorm.Load,
sqlite.Load,
).
Run()
}
Configuration
SQLite Configuration
# SQLite Basic Configuration
gorm.sqlite.driver-name= # Driver name, optional
gorm.sqlite.dsn=gorm.db # Data source name, default is gorm.db
Usage Examples
Basic Usage
package example
import (
"github.com/gone-io/gone/v2"
"gorm.io/gorm"
)
type Note struct {
ID uint `gorm:"primaryKey"`
Title string `gorm:"size:255"`
Content string `gorm:"type:text"`
}
type NoteService struct {
gone.Flag
db *gorm.DB `gone:"*"`
}
func (s *NoteService) CreateNote(title, content string) (*Note, error) {
note := &Note{
Title: title,
Content: content,
}
if err := s.db.Create(note).Error; err != nil {
return nil, err
}
return note, nil
}
func (s *NoteService) GetNoteByID(id uint) (*Note, error) {
var note Note
if err := s.db.First(¬e, id).Error; err != nil {
return nil, err
}
return ¬e, nil
}
func (s *NoteService) UpdateNote(note *Note) error {
return s.db.Save(note).Error
}
func (s *NoteService) DeleteNote(id uint) error {
return s.db.Delete(&Note{}, id).Error
}
Using In-Memory Database
gorm.sqlite.dsn=:memory:
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
Auto Migration
type AppStart struct {
gone.Flag
db *gorm.DB `gone:"*"`
}
func (s *AppStart) AfterRevive() error {
return s.db.AutoMigrate(
&Note{},
)
}
Best Practices
-
Database Design
- Design table structure appropriately
- Use indexes properly
- Avoid overly complex relationships
-
Performance Optimization
- Use transactions for batch operations
- Perform VACUUM operations periodically
- Configure journal_mode appropriately
-
Concurrency Handling
- Be aware of SQLite's concurrency limitations
- Use appropriate transaction isolation levels
- Avoid long-lasting locks
-
Backup and Maintenance
- Backup database files regularly
- Monitor database size
- Clean up unnecessary data timely
Common Issues
-
Concurrency Access Issues
Issue: Database locks due to multiple concurrent connections
Solution: Use appropriate locking strategies, avoid long transactions, consider using WAL mode.
-
Performance Issues
Issue: Database operations becoming slow
Solution: Run VACUUM periodically, optimize indexes, use appropriate journal_mode.
-
File Permission Issues
Issue: Unable to create or access database file
Solution: Check filesystem permissions, ensure application has appropriate read/write permissions.