Dev.to · 2 min read

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

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.createIndex("sku", { name: "sku_idx", unique: true, }); Now sku has an explicit index and the uniqueness constraint can protect the collection from duplicate values. Compound indexes Queries often depend on more than one field. await products.createIndex({ name: "category_price_idx", fields: [ { field: "category", direction: "asc", }, { field: "price", direction: "desc", }, ], }); Design compound indexes around actual query patterns rather than creating every field combination imaginable. Indexes aren't Pokémon. You don't need to collect them all. Partial indexes You can also index only documents matching a filter: await products.createIndex({ name: "active_price_idx", fields: ["price"], partialFilter: { active: true, }, }); For the right workload, this can keep an index focused on the subset of documents that matters. Text indexes Create a text index: await products.createTextIndex("title", { normalize: true, stopwords: ["set"], }); Then search: const results = await products.find({ $text: { $search: "Marker", $field: "title", }, }).toArray(); Inspect indexes const indexes = await products.listIndexes(); console.log(indexes); Index definitions can expose information including: name fields uniqueness sparse configuration partial filters build state build progress whether the index is a text index Drop an index await products.dropIndex("category_price_idx"); Index design rule Start from the query: await products.find({ category: "stationery", price: { $gte: 100, }, }); Then ask what index structure helps that query. Don't start by indexing everything and hoping the database sorts out the consequences later. Pre-alpha note Indexing behavior and capabilities will continue evolving as LioranDB moves from pre-alpha toward alpha. Treat the current release as something to experiment with, benchmark against your own workload and give feedback on. Resources Documentation: https://docs.liorandb.com Website: https://liorandb.com Creator: https://github.com/UltronTheAI Previous: Part 5 → Cursors & Text Search Next: Part 7 → Aggregation Pipelines

This is a summary aggregated from Dev.to. Read the complete article on the original site:

Read full article at Dev.to

More Programming & Dev News