drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
23.61k stars 582 forks source link

[FEATURE]: sqlite CREATE VIRTUAL TABLE and R*Tree extension #2046

Open aaroned opened 6 months ago

aaroned commented 6 months ago

Describe what you want

sqlite implements geospatial queries using the R*Tree extension (https://www.sqlite.org/rtree.html), and full text search using the FTS5 extension (https://www.sqlite.org/fts5.html). Both extensions are enabled by default on Turso. (https://docs.turso.tech/extensions)

Use of these modules requires the CREATE VIRTUAL TABLE statement. (https://www.sqlite.org/vtab.html)

The schema for an R*Tree virtual table might look like:

export const suburbSpatialIndex = sqliteRTree('suburbSpatialIndex', 'id', ['minLon', 'maxLon'], ['minLat', 'maxLat'])

this would create a migration:

CREATE VIRTUAL TABLE suburbSpatialIndex USING rtree(id, minLon, maxLon, minLat, maxLat);

Querying the virtual table would be just like querying a regular table:

db.select().from(suburbSpatialIndex).where(and(lte(suburbSpatialIndex.minLon, 144.5), gte(suburbSpatialIndex.maxLon, 144.5), lte(suburbSpatialIndex.minLat, -37.5), gte(suburbSpatialIndex.maxLat, -37.5)))

this would generate the sql:

SELECT * FROM suburbSpatialIndex WHERE minLon <= 144.5 AND maxLon >= 144.5 AND minLat <= -37.5 AND maxLat >= -37.5

jamiehaywood commented 3 months ago

Would be great to have this feature. How do Drizzle recommend we query spatial data at the moment?