Problem Statement
GistRepository uses all raw SQL via dataSource.query() for every operation (create, findNearby, findByGistId). This bypasses TypeORM's type safety, query caching, logging, and abstraction benefits.
Evidence
Backend/src/gists/gist.repository.ts — 100% raw SQL queries
- Helper functions
PaginationHelper.encodeCursor/decodeCursor used to bridge raw SQL with cursor logic
Impact
Bypasses ORM caching/type safety. Raw SQL is harder to maintain and test. Developers must know raw PostGIS SQL syntax.
Proposed Solution
- Replace INSERT with TypeORM's insert or save method + ST_MakePoint via expression
- Replace spatial SELECT with QueryBuilder using ST_DWithin, ST_X, ST_Y functions
- Replace cursor pagination with QueryBuilder's where + orderBy + take
- Keep raw SQL only for PostGIS-specific spatial functions where necessary
- Add query logging for generated SQL
Technical Requirements
- Must preserve PostGIS spatial functionality
- Must maintain cursor-based pagination
- Must not introduce N+1 queries
- Generated SQL must be equivalent to current raw queries
Acceptance Criteria
- All GistRepository operations use TypeORM QueryBuilder (not dataSource.query)
- Spatial queries (ST_DWithin, ST_MakePoint, ST_X, ST_Y) work correctly
- Cursor pagination works correctly
- All existing tests pass without modification
- Query logging shows TypeORM-generated SQL
- Performance is not regressed
File Inventory
Backend/src/gists/gist.repository.ts
Backend/src/gists/entities/gist.entity.ts
Dependencies
None.
Testing Strategy
- Run all existing integration tests
- Compare generated SQL with current raw SQL for equivalence
- Performance benchmark before and after
Security Considerations
TypeORM QueryBuilder provides SQL injection protection compared to string concatenation in raw SQL.
Definition of Done
Problem Statement
GistRepository uses all raw SQL via
dataSource.query()for every operation (create, findNearby, findByGistId). This bypasses TypeORM's type safety, query caching, logging, and abstraction benefits.Evidence
Backend/src/gists/gist.repository.ts— 100% raw SQL queriesPaginationHelper.encodeCursor/decodeCursorused to bridge raw SQL with cursor logicImpact
Bypasses ORM caching/type safety. Raw SQL is harder to maintain and test. Developers must know raw PostGIS SQL syntax.
Proposed Solution
Technical Requirements
Acceptance Criteria
File Inventory
Backend/src/gists/gist.repository.tsBackend/src/gists/entities/gist.entity.tsDependencies
None.
Testing Strategy
Security Considerations
TypeORM QueryBuilder provides SQL injection protection compared to string concatenation in raw SQL.
Definition of Done