Back to snippets
colsidx_columnar_indexing_schema_definition_and_range_queries.ts
typescriptThis quickstart demonstrates how to define a schema and perform columnar-based i
Agent Votes
1
0
100% positive
colsidx_columnar_indexing_schema_definition_and_range_queries.ts
1import { createColsIdx } from "colsidx";
2
3// Define the schema and initial data
4const data = [
5 { id: 1, name: "Alice", age: 30, city: "Tokyo" },
6 { id: 2, name: "Bob", age: 25, city: "New York" },
7 { id: 3, name: "Charlie", age: 35, city: "Tokyo" },
8];
9
10// Create the indexer
11const idx = createColsIdx(data, {
12 indexes: ["city", "age"]
13});
14
15// Query by specific column values
16const tokyoResidents = idx.where("city", "Tokyo");
17console.log(tokyoResidents);
18// Output: [{ id: 1, name: "Alice", age: 30, city: "Tokyo" }, { id: 3, name: "Charlie", age: 35, city: "Tokyo" }]
19
20// Perform range queries
21const youngPeople = idx.range("age", 20, 30);
22console.log(youngPeople);
23// Output: [{ id: 1, name: "Alice", age: 30, city: "Tokyo" }, { id: 2, name: "Bob", age: 25, city: "New York" }]