Property Listings API
Listings for rent, sale and shortlet, and the search a property app actually runs: what is within X km of here, in my budget, with enough bedrooms.
- NestJS
- TypeScript
- PostgreSQL
- PostGIS
- TypeORM
- Docker
The problem
Built to a brief, with a clock on it: CRUD for listings, a search filtered by type, price range and bedrooms, and everything within X km of a point.
The last part is where the work is. The obvious implementation measures the distance from the search point to every listing and keeps the close ones. It returns the right answer and it reads every row in the table to do it. With two hundred listings nobody notices. With two hundred thousand, the search box is the slowest thing in the product.
What I built
- The radius search is answered from an index. The location is a PostGIS
geography(Point,4326)column with a GiST index, and the query filters withST_DWithin, which the planner can satisfy from that index.ST_Distanceappears only in theSELECT, to order and report distance for the rows that already survived the filter. - The migration is hand written, because of that. TypeORM will not emit a GiST index, so a generated migration would have left the one thing the feature depends on silently missing. Everything would still have worked, just slowly, and nothing would have said why.
- Money is an integer count of kobo, never a float, and carries a period alongside it. ₦3.5m is a normal annual rent in Lekki and an absurd monthly one, so the number means nothing without it.
- Two kinds of identifier. A UUID for machines, and a short human reference like
EL-YABA23for the agent reading it down a phone line. The alphabet drops0,O,1,IandL, and a test creates forty listings at once and checks all forty references are distinct.
The measurement that decided the design
Seeded to 50,000 listings, same radius, same machine:
- 48ms —
ST_DWithin, Bitmap Index Scan onidx_listings_location - 399ms —
ST_Distance(...) <= 3000, Parallel Seq Scan
Eight times faster at fifty thousand rows, and the gap widens with the table. That number is the whole reason for the PostGIS dependency, and it is in the README rather than in my head.
Decisions worth naming
- Agents are a real table behind a foreign key. They started as a bare
agent_idcolumn pointing at nothing, which meant a listing could name an agent who had never existed and a buyer who liked a flat got back a long random ID and nobody to call. Listings now carry the agent's name and phone. The field is optional, because an owner advertising their own place should not have to invent an agent, and a foreign key only checks a value that is there. - Validation twice, deliberately. The DTOs give a caller a useful 400;
CHECKconstraints mean a bad row cannot arrive through a migration, a seed or a console either. - The integration tests run against real PostGIS. The risk in this service is the SQL, and a mocked repository would have tested my assumptions rather than the database's. One test recomputes every distance the API returns using a different formula and agrees to within half a percent, the residual being the spheroid against the sphere.
- The test suite cannot reach the development database. Not by convention, structurally: under test the module reads a different connection string and the normal one is unreachable. That is there because a test run quietly emptied the database I was demonstrating from.
What is not finished
- There is no authentication, and the README says so in the first line of what I would do next.
agentIdcomes from the request body, so anyone can post as any agent. For a timeboxed brief I put the hours into the search and the data model and said so, rather than shipping a token check that looked like security without being it. - Caching, cursor pagination, image uploads and text search — named in the README with the reason each one matters, none of them built.