Skip to content

feat: Implement SQLite Database Persistence with GORM#23

Open
frye wants to merge 1 commit into
mainfrom
frye/add-sqlite-gorm-persistence
Open

feat: Implement SQLite Database Persistence with GORM#23
frye wants to merge 1 commit into
mainfrom
frye/add-sqlite-gorm-persistence

Conversation

@frye

@frye frye commented Apr 8, 2026

Copy link
Copy Markdown
Owner

Summary

Replaces the in-memory user slice with a real SQLite database using GORM, ensuring data persistence across application restarts.

Closes #15

Changes

New files

  • database/database.goInitDB (opens SQLite + auto-migrate) and SeedDB (populates default users if empty)
  • database/testhelper.go — Shared in-memory SQLite helper for tests
  • database/database_test.go — Tests for InitDB, SeedDB, and seed idempotency
  • controllers/user_controller_test.go — 12 tests covering all CRUD endpoints and edge cases

Modified files

  • models/user.go — Added GORM primaryKey tag, changed ID from string to uint, added CreateUserRequest/UpdateUserRequest DTOs
  • controllers/user_controller.go — Refactored to struct-based UserController with DB injection; all handlers now use GORM queries; implemented DeleteUser
  • api/routes.go — Accepts *gorm.DB, wires up controller methods, added DELETE /:id route
  • main.go — Initializes database, seeds data, passes DB to router
  • go.mod / go.sum — Added gorm.io/gorm and gorm.io/driver/sqlite

Testing

15 tests pass covering:

  • Database initialization and migration
  • Seed idempotency
  • Full CRUD (GET all, GET by ID, POST, PUT, DELETE)
  • Error cases (invalid ID → 400, not found → 404, missing fields → 400)

Replace in-memory user slice with SQLite database using GORM ORM.

- Add gorm.io/gorm and gorm.io/driver/sqlite dependencies
- Create database package with InitDB and SeedDB functions
- Update UserProfile model with GORM tags (uint primary key)
- Add CreateUserRequest/UpdateUserRequest DTOs for safe input binding
- Refactor controllers to struct-based UserController with DB injection
- Implement DeleteUser endpoint (DELETE /api/v1/users/:id)
- Add proper error handling: invalid ID -> 400, not found -> 404
- Use deterministic ordering (ORDER BY id) for list queries
- Add 15 unit tests covering all CRUD operations and edge cases

Closes #15

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement SQLite Database Persistence with GORM

1 participant