Skip to content
Luna
Esc
↑↓navigate↵open⌘Jpreview
On this page

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)

PropType
namestring

The index's name, as given to defineIndex.

Typestring
partitionstring | number

The partition to read: a value of the index's partition column.

Typestring | number

Pass 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.

PropType
find(opts)?LiveQuery<Row[]>

Rows that match. opts takes where, orderBy and limit.

TypeLiveQuery<Row[]>
count(opts)?LiveQuery<number>

How many rows match. opts takes where.

TypeLiveQuery<number>
groupBy(opts)?LiveQuery<IndexGroup<Row>[]>

Rows grouped by some columns, largest groups first. opts takes by, where, sum, min, max and limit.

TypeLiveQuery<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.

TypeLiveQuery<{ 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.

TypeLiveQuery<{ row: Row; distance: number }[]>
read(op, opts)?LiveQuery<...>

Any of the reads above by name, such as read("search", { text: "milk" }).

TypeLiveQuery<...>

Read options

PropType
where?IndexWhere<Row>

Conditions on the rows' columns.

TypeIndexWhere<Row>
orderBy?IndexOrder<Row>

A column name, or an object such as { title: "desc" }. find only.

TypeIndexOrder<Row>
limit?number

The most rows or groups to return. search returns 20 unless set.

Typenumber
bystring[]

The columns to group by. groupBy only.

Typestring[]
sum, min, max?string[]

Columns to total or take the smallest and largest of in each group. groupBy only.

Typestring[]
match?"all" | "any" | "phrase" | "raw"

How the words combine: every word, any word, the words in order, or SQLite FTS5 query syntax. search only.

Type"all" | "any" | "phrase" | "raw"
Default"all"
prefix?boolean

The last word also matches as a prefix, for search as you type. search only.

Typeboolean
k?number

How many rows, at most 1,000. nearest only.

Typenumber
Default10

IndexWhere

Each column takes a value, null, or an object of operators.

PropType
eq?V

Equal to.

TypeV
ne?V

Not equal to.

TypeV
lt, lte, gt, gte?V

Less than, at most, more than, at least.

TypeV
in?V[]

Equal to one of these.

TypeV[]
isNull?boolean

true for rows where the column is null, false for rows where it is not.

Typeboolean

IndexGroup

PropType
<column>?Row[column]

The value of each column grouped by.

TypeRow[column]
countnumber

How many rows are in the group.

Typenumber
sum?Record<string, number | null>

The total of each column asked for.

TypeRecord<string, number | null>
min?Record<string, number | null>

The smallest value of each column asked for.

TypeRecord<string, number | null>
max?Record<string, number | null>

The largest value of each column asked for.

TypeRecord<string, number | null>

Good to know

  • An index without onBeforeConnect cannot be read from an app. What the hook returns becomes the connection’s auth.
  • 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.
  • search needs the index’s search option, and nearest needs 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.

Was this page helpful?