You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Finally, create a new `GeospatialIndex` within your `convex/` folder, and point it to the installed component:
57
+
Finally, create a new `GeospatialIndex` within your `convex/` folder, and point
58
+
it to the installed component:
51
59
52
60
```ts
53
61
// convex/index.ts
@@ -59,9 +67,10 @@ const geospatial = new GeospatialIndex(components.geospatial);
59
67
60
68
## Inserting points
61
69
62
-
After installing the component, you can `insert`, `get`, and `remove` points from the index. You can specify
63
-
a `filterKeys` record for filtering at query time and optionally a `sortKey` for the query result order. We
64
-
currently only support ascending order on the `sortKey`.
70
+
After installing the component, you can `insert`, `get`, and `remove` points
71
+
from the index. You can specify a `filterKeys` record for filtering at query
72
+
time and optionally a `sortKey` for the query result order. We currently only
73
+
support ascending order on the `sortKey`.
65
74
66
75
```ts
67
76
// convex/index.ts
@@ -85,9 +94,11 @@ const example = mutation({
85
94
});
86
95
```
87
96
88
-
If you would like some more typesafety, you can specify a type argument for the `GeospatialIndex` class. This
89
-
will also provide you with auto-complete for the `filterKeys` and `sortKey` parameters.
90
-
Above the key was "American Museum of Natural History" but most commonly the `key` will be an ID in another table of yours.
97
+
If you would like some more typesafety, you can specify a type argument for the
98
+
`GeospatialIndex` class. This will also provide you with auto-complete for the
99
+
`filterKeys` and `sortKey` parameters. Above the key was "American Museum of
100
+
Natural History" but most commonly the `key` will be an ID in another table of
101
+
yours.
91
102
92
103
```ts
93
104
// convex/index.ts
@@ -125,13 +136,13 @@ const example = query({
125
136
});
126
137
```
127
138
128
-
This query will find all points that lie within the query rectangle, sort them in ascending
129
-
`sortKey` order, and return at most 16 results.
139
+
This query will find all points that lie within the query rectangle, sort them
140
+
in ascending `sortKey` order, and return at most 16 results.
130
141
131
142
You can optionally add filter conditions to queries.
132
143
133
-
The first type of filter condition is an `in()` filter, which requires that a matching
134
-
document have a filter field with a value in a specified set.
144
+
The first type of filter condition is an `in()` filter, which requires that a
145
+
matching document have a filter field with a value in a specified set.
135
146
136
147
```ts
137
148
// convex/index.ts
@@ -153,8 +164,8 @@ const example = query({
153
164
});
154
165
```
155
166
156
-
The second type of filter condition is an `eq()` filter, which requires that a matching
157
-
document have a filter field with a value equal to a specified value.
167
+
The second type of filter condition is an `eq()` filter, which requires that a
168
+
matching document have a filter field with a value equal to a specified value.
158
169
159
170
```ts
160
171
// convex/index.ts
@@ -170,7 +181,9 @@ const example = query({
170
181
});
171
182
```
172
183
173
-
The final type of filter condition allows you to specify ranges over the `sortKey`. We currently only support (optional) inclusive lower bounds and exclusive upper bounds.
184
+
The final type of filter condition allows you to specify ranges over the
185
+
`sortKey`. We currently only support (optional) inclusive lower bounds and
186
+
exclusive upper bounds.
174
187
175
188
```ts
176
189
// convex/index.ts
@@ -192,10 +205,13 @@ const example = query({
192
205
});
193
206
```
194
207
195
-
Queries take in a `limit`, which bounds the maximum number of rows returned. If this limit is hit,
196
-
the query will return a `nextCursor` for continuation. The query may also return a `nextCursor` with fewer than `limit` results if it runs out of its IO budget while executing.
208
+
Queries take in a `limit`, which bounds the maximum number of rows returned. If
209
+
this limit is hit, the query will return a `nextCursor` for continuation. The
210
+
query may also return a `nextCursor` with fewer than `limit` results if it runs
211
+
out of its IO budget while executing.
197
212
198
-
In either case, you can continue the stream by passing `nextCursor` to the next call's `cursor` parameter.
213
+
In either case, you can continue the stream by passing `nextCursor` to the next
214
+
call's `cursor` parameter.
199
215
200
216
```ts
201
217
// convex/index.ts
@@ -234,11 +250,13 @@ const example = query({
234
250
});
235
251
```
236
252
237
-
**Note: you typically pass the `nextCursor` in from a client that is paginating through results, to avoid loading too much data in a single query.**
253
+
**Note: you typically pass the `nextCursor` in from a client that is paginating
254
+
through results, to avoid loading too much data in a single query.**
238
255
239
256
## Querying the points nearest a query point
240
257
241
-
You can also query for the points closest to a given point, optionally limiting to a maximum distance (in meters).
258
+
You can also query for the points closest to a given point, optionally limiting
259
+
to a maximum distance (in meters).
242
260
243
261
```ts
244
262
// convex/index.ts
@@ -252,7 +270,7 @@ const example = query({
252
270
{ latitude: 40.7813, longitude: -73.9737 },
253
271
maxResults,
254
272
{
255
-
maxDistance,
273
+
maxDistance,
256
274
filter: (q) =>q.eq("category", "coffee"),
257
275
},
258
276
);
@@ -261,11 +279,19 @@ const example = query({
261
279
});
262
280
```
263
281
264
-
The fourth argument can either be a numeric `maxDistance` (for backwards compatibility) or an options object. When you pass an options object you can combine `maxDistance` with the same filter builder used by `query`, including `eq`, `in`, `gte`, and `lt` conditions. These filters are enforced through the indexed `pointsByFilterKey` range before documents are loaded, so the database does the heavy lifting and the query avoids reading unrelated points. Pairing that with a sensible `maxDistance` further constrains the search space and can speed up lookups.
282
+
The fourth argument can either be a numeric `maxDistance` (for backwards
283
+
compatibility) or an options object. When you pass an options object you can
284
+
combine `maxDistance` with the same filter builder used by `query`, including
285
+
`eq`, `in`, `gte`, and `lt` conditions. These filters are enforced through the
286
+
indexed `pointsByFilterKey` range before documents are loaded, so the database
287
+
does the heavy lifting and the query avoids reading unrelated points. Pairing
288
+
that with a sensible `maxDistance` further constrains the search space and can
289
+
greatly speed up searching the index.
265
290
266
291
## Example
267
292
268
-
See [`example/`](./example/) for a full example with a [Leaflet](https://leafletjs.com/)-based frontend.
293
+
See [`example/`](./example/) for a full example with a
294
+
[Leaflet](https://leafletjs.com/)-based frontend.
269
295
270
296
## Development
271
297
@@ -278,7 +304,8 @@ npm install
278
304
npm run dev
279
305
```
280
306
281
-
The component definition is in `src/` and reflects what users of the component will install. The example app,
282
-
which is entirely independent, lives in `example/`.
307
+
The component definition is in `src/` and reflects what users of the component
308
+
will install. The example app, which is entirely independent, lives in
309
+
`example/`.
283
310
284
311
<!-- END: Include on https://convex.dev/components -->
0 commit comments