ormDB

Geographic Search in ormDB

Use ormDB's built-in R-tree geo index for spatial queries. Find nearby records, search within polygons, and combine geo filters with graph fetches.

Difficulty: intermediateTime: 12 minutes

ormDB is a relational database engine written in Rust that replaces PostgreSQL, MySQL, and SQLite. Geographic search is built directly into the engine using R-tree indexes for fast spatial queries. There is no extension to install. Point-based queries, radius search, bounding box queries, and polygon containment all work out of the box.

Most application geo queries follow the same patterns: find things near a location, find things within a region, sort by distance. ormDB handles these natively, and the results compose with graph fetches so you get nearby entities with their full relational context in a single round-trip.

Defining Geo Columns

Add a geographic point column to your entity:

entity Restaurant {
  id: uuid @primary
  name: text
  cuisine: text
  rating: float
  location: geopoint @index(rtree)
  owner_id: uuid @relation(User)
}

The geopoint type stores a latitude/longitude coordinate pair. The @index(rtree) annotation creates an R-tree spatial index for fast queries.

Find entities within a distance of a point:

const nearby = await db.geoSearch('Restaurant', {
  field: 'location',
  near: { lat: 37.7749, lng: -122.4194 }, // San Francisco
  radius: 5000, // 5 km
  where: { rating: { gte: 4.0 } },
  order_by: 'distance',
  limit: 20,
})

Results include the distance from the query point:

{
  records: [
    {
      id: 'rest_001',
      name: 'Nopa',
      cuisine: 'Californian',
      rating: 4.5,
      location: { lat: 37.7752, lng: -122.4372 },
      distance: 1847, // meters
    },
    // ...
  ]
}

Find entities within a rectangular region:

const inArea = await db.geoSearch('Restaurant', {
  field: 'location',
  within: {
    type: 'bbox',
    min: { lat: 37.75, lng: -122.45 },
    max: { lat: 37.80, lng: -122.40 },
  },
})

Find entities within an arbitrary polygon:

const inZone = await db.geoSearch('Restaurant', {
  field: 'location',
  within: {
    type: 'polygon',
    coordinates: [
      { lat: 37.78, lng: -122.42 },
      { lat: 37.79, lng: -122.40 },
      { lat: 37.77, lng: -122.39 },
      { lat: 37.76, lng: -122.41 },
    ],
  },
})

Geo Search with Graph Fetch

Combine geo search with graph fetches for rich location-based results:

const nearby = await db.geoSearch('Restaurant', {
  field: 'location',
  near: { lat: 37.7749, lng: -122.4194 },
  radius: 3000,
  order_by: 'distance',
  limit: 10,
  include: {
    owner: {
      fields: ['id', 'name'],
    },
    menu_items: {
      where: { available: true },
      order_by: { popularity: 'desc' },
      limit: 5,
    },
    reviews: {
      limit: 3,
      order_by: { created_at: 'desc' },
      include: {
        author: { fields: ['id', 'name'] },
      },
    },
  },
})

This single request finds the 10 nearest restaurants within 3 km, and for each includes the owner, top 5 available menu items, and 3 most recent reviews with their authors. With a traditional database like PostgreSQL (requiring PostGIS) and a separate geo service, this would require multiple queries across multiple systems.

Indexing Performance

R-tree indexes provide logarithmic query time regardless of dataset size. For more on tuning query performance, see the performance optimization guide. A table with 10 million geo-indexed records responds to radius queries in the same time frame as a table with 10,000 records. The index is maintained automatically as records are inserted, updated, or deleted within ACID transactions.

Geographic search in ormDB is designed for the queries applications actually make: “what is near me” and “what is in this area.” It composes naturally with the rest of ormDB’s query capabilities, including full-text search and vector search, keeping your geo data and relational data in one engine.

Frequently Asked Questions

How does ormDB's geo search compare to PostGIS?

ormDB's geo search is built into the engine and requires no extension. It supports point-based queries (radius search, bounding box, polygon containment) using R-tree indexes. PostGIS is a full GIS system with support for complex geometries, projections, and spatial analysis. ormDB covers the most common application geo queries natively.

What coordinate system does ormDB use?

ormDB uses WGS 84 (EPSG:4326) coordinates, the same system used by GPS and most mapping APIs. Coordinates are specified as latitude/longitude pairs.

Can I combine geo search with graph fetches?

Yes. Geo search can be the entry point for a graph fetch. Find nearby restaurants and include their menus, reviews, and photos in a single round-trip.

Does geo search support polygon queries?

Yes. You can search for records within an arbitrary polygon defined by a list of coordinates. This enables queries like 'find all stores within this delivery zone.'

How accurate is the distance calculation?

ormDB uses the Haversine formula for distance calculations, which accounts for the curvature of the Earth. Results are accurate for distances from meters to thousands of kilometers.

Related Content

Try ormDB today

Open source, MIT licensed. Install and start building.