Dynamic funtion in convex
how can ı write this codes's convex funtion ?
query = "SELECT * FROM flights WHERE 1 = 1"
params = []
if departure_airport:
query += " AND departure_airport = ?"
params.append(departure_airport)
if arrival_airport:
query += " AND arrival_airport = ?"
params.append(arrival_airport)
if start_time:
query += " AND scheduled_departure >= ?"
params.append(start_time)
if end_time:
query += " AND scheduled_departure <= ?"
params.append(end_time)
4 Replies
When running this hypothetical sql query, what index(es) are on the flights table?
If there are no indexes, the equivalent Convex code would use
filter
https://stack.convex.dev/complex-filters-in-convex
Stack
Using TypeScript to Write Complex Query Filters
There’s a new Convex helper to perform generic TypeScript filters, with the same performance as built-in Convex filters, and unlimited potential.
when ı tried without indexes getting limit error so ı added index but no idea for with indexes:
flights: defineTable({
actual_arrival: v.union(v.null(), v.string()),
actual_departure: v.union(v.null(), v.string()),
aircraft_code: v.string(),
arrival_airport: v.string(),
departure_airport: v.string(),
flight_id: v.float64(),
flight_no: v.string(),
scheduled_arrival: v.string(),
scheduled_departure: v.string(),
status: v.string(),
}).index("by_departure_airport", ["departure_airport"])
.index("by_arrival_airport", ["arrival_airport"])
.index("by_actual_departure", ["actual_departure"])
.index("by_actual_arrival", ["actual_arrival"]),
})
You should pick an index that narrows down your search space as much as possible. See https://stack.convex.dev/databases-are-spreadsheets to think about how to design your indexes and pick one
Stack
Databases are Spreadsheets
I want to share my mental model of databases:
- Databases are just big spreadsheets
- An index is just a view of the spreadsheet sorted by one or mor...