IndexHandle
Read one partition of an index live from an app, with find, count, groupBy, search and nearest.
An IndexHandle reads one partition of an index from an app, from client.index(name, partition). Every read is a live query.
The index must say which apps may read it with onBeforeConnect. This one lets every app read every board.
import { defineIndex } from "luna";
import { Board } from "./board.ts";
export const BoardCards = defineIndex("BoardCards", {
partition: "board",
search: { columns: ["title"] },
from: [Board.changes.cards.map((r, l) => ({ title: r.title, done: r.done, board: l.id }))],
onBeforeConnect: () => ({}),
});
import { createClient } from "luna/client";
import type { BoardCards } from "../luna/lunas/board-cards.ts";
const client = createClient({ url: "<LUNA_URL>", key: "<LUNA_KEY>" });
const cards = client.index<typeof BoardCards>("BoardCards", "b1");
const open = cards.find({ where: { done: false }, orderBy: "title" });
open.subscribe(() => console.log(open.get().data));
Reference
client.index(name, partition)
namestring
The index's name, as given to defineIndex.
stringpartitionstring | number
The partition to read: a value of the index's partition column.
string | numberPass the index as a type argument, client.index<typeof BoardCards>, to type its rows. Without one, rows are Record<string, unknown>.
Methods
Each method returns a LiveQuery, whose get() and subscribe() work as a luna’s do.
find(opts)?LiveQuery<Row[]>
Rows that match. opts takes where, orderBy and limit.
LiveQuery<Row[]>count(opts)?LiveQuery<number>
How many rows match. opts takes where.
LiveQuery<number>groupBy(opts)?LiveQuery<IndexGroup<Row>[]>
Rows grouped by some columns, largest groups first. opts takes by, where, sum, min, max and limit.
LiveQuery<IndexGroup<Row>[]>search(text, opts)?LiveQuery<{ row: Row; score: number }[]>
Full-text search over the index's search columns, best matches first. opts takes where, limit, match and prefix.
LiveQuery<{ row: Row; score: number }[]>nearest(column, to, opts)?LiveQuery<{ row: Row; distance: number }[]>
The rows whose vector column is nearest to, nearest first. opts takes k and where.
LiveQuery<{ row: Row; distance: number }[]>read(op, opts)?LiveQuery<...>
Any of the reads above by name, such as read("search", { text: "milk" }).
LiveQuery<...>Read options
where?IndexWhere<Row>
Conditions on the rows' columns.
IndexWhere<Row>orderBy?IndexOrder<Row>
A column name, or an object such as { title: "desc" }. find only.
IndexOrder<Row>limit?number
The most rows or groups to return. search returns 20 unless set.
numberbystring[]
The columns to group by. groupBy only.
string[]sum, min, max?string[]
Columns to total or take the smallest and largest of in each group. groupBy only.
string[]match?"all" | "any" | "phrase" | "raw"
How the words combine: every word, any word, the words in order, or SQLite FTS5 query syntax. search only.
"all" | "any" | "phrase" | "raw""all"prefix?boolean
The last word also matches as a prefix, for search as you type. search only.
booleank?number
How many rows, at most 1,000. nearest only.
number10IndexWhere
Each column takes a value, null, or an object of operators.
eq?V
Equal to.
Vne?V
Not equal to.
Vlt, lte, gt, gte?V
Less than, at most, more than, at least.
Vin?V[]
Equal to one of these.
V[]isNull?boolean
true for rows where the column is null, false for rows where it is not.
booleanIndexGroup
<column>?Row[column]
The value of each column grouped by.
Row[column]countnumber
How many rows are in the group.
numbersum?Record<string, number | null>
The total of each column asked for.
Record<string, number | null>min?Record<string, number | null>
The smallest value of each column asked for.
Record<string, number | null>max?Record<string, number | null>
The largest value of each column asked for.
Record<string, number | null>Good to know
- An index without
onBeforeConnectcannot be read from an app. What the hook returns becomes the connection’sauth. - Each read runs again as the partition’s rows change, and the app hears only a result that differs from the last.
- Everyone who watches the same read of a partition shares one run, so many viewers cost about what one does.
- An index’s rows are read-only from an app. Your lunas fill it from the sources it names.
searchneeds the index’ssearchoption, andnearestneeds a vector column.
Examples
Count open cards
const left = cards.count({ where: { done: false } });
Group cards by state
const byState = cards.groupBy({ by: ["done"] });
Search as you type
const hits = cards.search("mil", { prefix: true, limit: 10 });
hits.subscribe(() => {
for (const { row, score } of hits.get().data ?? []) console.log(row.title, score);
});
Read from React
useIndex and useIndexQuery wrap these reads. See luna/react.