Slice Index
A slice index groups nodes in a sorted list based on a specific value
new Garak.SliceIndex(
"name",
Store,
{
groupOn: string,
indexOn: string,
}
);
Name
A unique name for the slice, identifying it in the database. No two slice indexes may have the same name across the silo, regardless of store.
Group on
Slice indexes are sorted into groups, groups will be determined by the value of the groupOn field (similar to a categorization index). To form a universal group across the silo, "$" may be provided.
Index on
The field to sort the slice and query from. This may be any scalar, and will be converted to a number internally for querying.
Number => remains the same
Boolean => 0 for false and 1 for true
String => Converted to number using fnv1a64
Due to the fact fnv1a64 is used to convert strings for slices, alphabetical order is NOT guaranteed.
Include
Optionally, an include option may be specified, limiting which nodes get included in the categorization:
new Garak.SliceIndex(
"name",
Store,
{
groupOn: string,
indexOn: string,
include: {
"status": {
$eq: "active"
}
}
}
);
Nodes will only be added to the slice if the include selector resolves to true. If a node originally had the include selector match, but has an update that results in the selector no longer matching, it will be removed from the index.
Querying
Three methods can be used to query a slice. Note that for group, if your index is set to group on all you may pass "$" here.
Query from a starting point
const mySlice = ...;
mySlice.before(tx, group, before, limit);
mySlice.after(tx, group, after, limit);
Note that after/before are inclusive, so querying after: 30 will return nodes where the value is 30. The name can be a bit deceptive, but think of after as >= and before as <=.
Query a range
const mySlice = ...;
mySlice.until(tx, group, from, to);
When querying a range, note that a theoretically unlimited number of nodes can be returned. Be careful with your ranges.
Working with a query handle:
const myHandle = mySlice.before(...); // or mySlice.after(...) or mySlice.until(...)
await myHandle.lookup(); // { nodes: number[] }
await myHandle.fetch(); // Array<{ node_id: number, value: T | null }>