Urgent.News

What's breaking now, across thousands of outlets.

Tech

LioranDB TypeScript Series #6: Secondary Indexes and Full-Text Indexes

LioranDB TypeScript Series #6: Secondary Indexes and Full-Text Indexes LioranDB TypeScript Series: Build with a developer-first document database powered by Rust and designed for TypeScript. Indexes are where your data model meets your query patterns. LioranDB V2 currently supports secondary indexes and text indexes through the TypeScript driver. Create a secondary index await products .…

LioranDB TypeScript Series #6 delves into secondary indexes and full-text indexes. These indexes bridge your data model with query patterns. In the latest version, LioranDB V2 provides secondary indexes and text indexes via its TypeScript driver.

To create a secondary index, you can use the following code snippet:

await products.createIndex('sku', { name: 'sku_idx', unique: true, });

This code creates a secondary index on the 'sku' field of the 'products' collection, ensuring uniqueness. Compound indexes can be created by combining multiple fields in a specific order and direction:

await products.createIndex({ name: 'category_price_idx', fields: [ { field: 'category', direction: 'asc', }, { field: 'price', direction: 'desc', }, ], });

Partial indexes can be designed to index only documents matching a filter:

await products.createIndex({ name: 'active_price_idx', fields: [ 'price' ], partialFilter: { active: true, }, });

For text searches, a text index can be created:

await products.createTextIndex('title', { normalize: true, stopwords: [ 'set' ], });

The text search query looks like this:

const results = await products.find({ $text: { $search: 'Marker', $field: 'title', }, }).toArray();

To inspect indexes, you can use:

const indexes = await products.listIndexes();

console.log(indexes);

Indexes can provide information such as name, fields, uniqueness, sparse configuration, partial filters, build state, build progress, and text index status. To remove an index:

await products.dropIndex('category_price_idx');

Remember, index design should be guided by your actual query patterns rather than indexing everything. LioranDB indexing capabilities are still evolving, so treat the current release as an experimental tool for benchmarking and providing feedback.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

This story

This is one outlet's version. Read the fullest account.

Read the original at dev.to →

More in Tech

More from Saturday 15 August →