Skip to content

refactor: replace raw SQL with TypeORM QueryBuilder in GistRepository #46

@snowrugar-beep

Description

@snowrugar-beep

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

  1. Replace INSERT with TypeORM's insert or save method + ST_MakePoint via expression
  2. Replace spatial SELECT with QueryBuilder using ST_DWithin, ST_X, ST_Y functions
  3. Replace cursor pagination with QueryBuilder's where + orderBy + take
  4. Keep raw SQL only for PostGIS-specific spatial functions where necessary
  5. 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

  1. All GistRepository operations use TypeORM QueryBuilder (not dataSource.query)
  2. Spatial queries (ST_DWithin, ST_MakePoint, ST_X, ST_Y) work correctly
  3. Cursor pagination works correctly
  4. All existing tests pass without modification
  5. Query logging shows TypeORM-generated SQL
  6. 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

  • Raw SQL replaced with QueryBuilder
  • All tests passing
  • Performance not regressed
  • Spatial queries working

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions