From 03e7d433d5a12a478c42c5aba5cfbdc3881158b2 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 10 Oct 2025 22:17:36 +0200 Subject: [PATCH] DB rule --- .cursor/rules/database-interface-only.mdc | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 .cursor/rules/database-interface-only.mdc diff --git a/.cursor/rules/database-interface-only.mdc b/.cursor/rules/database-interface-only.mdc new file mode 100644 index 0000000..83c0664 --- /dev/null +++ b/.cursor/rules/database-interface-only.mdc @@ -0,0 +1,45 @@ +--- +alwaysApply: true +description: Database interaction rules and interface requirements +--- + +# Database Interface Rules + +## STRICT Database Interface Usage + +**ALL database interactions MUST be done through the database interface defined in [db.go](mdc:db.go).** + +### NEVER Use Raw GORM Calls +- **DO NOT** call `db.DB().Where(...)` directly +- **DO NOT** call `db.DB().Save(...)` directly +- **DO NOT** call `db.DB().First(...)` directly +- **DO NOT** call `db.DB().Create(...)` directly + +### ALWAYS Use Interface Methods +Use the high-level methods defined in the DB interface: +- `GetCharacterByName(characterName string) (*Character, error)` +- `SaveCharacter(character *Character) error` +- `AutoMigrate(dst ...interface{}) error` + +### Database Interface Location +The database interface is defined in [db.go](mdc:db.go) and provides: +- `DB() *gorm.DB` - Access to underlying GORM instance +- `Raw(sql string, args ...any) *gorm.DB` - For raw SQL when needed +- High-level methods for all database operations + +### Why This Rule Exists +- **Abstraction**: Database operations are abstracted behind a clean interface +- **Testing**: Interface allows for easy mocking and testing +- **Consistency**: All database operations follow the same patterns +- **Maintainability**: Changes to database logic are centralized + +### Cache Operations +For cache operations, use the database interface methods: +- Store cache entries through the interface +- Retrieve cache entries through the interface +- Never bypass the interface for any database operations + +### Error Handling +- Always log database operations with appropriate context +- Include character names and operation details in log messages +- Handle errors gracefully and log them with context \ No newline at end of file