npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

svelter-search

v0.1.2

Published

Client first full-text search library

Downloads

127

Readme

Svelter Search | Client First Full Text Search Library

Svelter Search is a JavaScript library that synchronizes data from a search API into the browser's IndexedDB for efficient and offline-capable full-text searching. By performing searches against the IndexedDB, it reduces the number of server requests, enhancing performance and user experience.

Features

  • Synchronizes data from a remote API to IndexedDB.
  • Supports incremental or full data updates.
  • Handles deletions using "soft deletes" with the is_deleted field.
  • Configurable update intervals.
  • Option to automatically update data before searching.
  • Full-text search capabilities using IndexedDB.
  • Easy integration and customization.

Installation

Install via npm:

npm install svelter-search dexie

Note: dexie is a required dependency for IndexedDB operations.

Usage

Import the Library

import SvelterSearch from 'svelter-search';

Create an Instance

const searchInstance = new SvelterSearch({
  search_id: 'mySearch', // Unique ID for the search (used as table name)
  update_interval: 3600000, // Update interval in milliseconds (e.g., 1 hour)
  data_url: 'https://api.example.com/search-data', // URL to fetch data
  date_url: 'https://api.example.com/data-updated-at', // (Optional) URL to check last update date
  auto_update: true, // Whether to automatically update data before searching
  incrementalUpdate: true, // Whether updates are incremental (true) or full (false)
  result_limit: 10, // Limit the number of results returned on the search (default 50)
});

Options

  • search_id (String): Unique identifier for the search. This is used as the table name within the svelter_search database.
  • update_interval (Number): Interval in milliseconds to check if data synchronization is needed.
  • data_url (String): URL to fetch data from.
  • date_url (String): URL to check the last update date of the data.
  • auto_update (Boolean): If true, the library will check if an update is needed before performing a search and update if necessary. Default is false.
  • incrementalUpdate (Boolean): Indicates whether updates are incremental (true) or full (false). Default is false.

Methods

update(data_url)

Triggers a data update. If data_url is provided as a parameter, it will use that URL; otherwise, it will use the data_url specified during initialization.

await searchInstance.update(); // Uses the data_url specified during initialization

// Or with a custom data_url
await searchInstance.update('https://api.example.com/alternative-data');
  • If incrementalUpdate is false, a full update is performed:
    • The corresponding table in IndexedDB is cleared.
    • All data is fetched from the endpoint.
  • If incrementalUpdate is true, an incremental update is performed:
    • Only new and updated data since the last update is fetched.
    • Deletions are handled using "soft deletes" with the is_deleted field.

search(text)

Performs a full-text search in IndexedDB for items that match the provided text.

const results = await searchInstance.search('search term');
console.log(results);
  • If auto_update is true, the library checks if an update is needed based on the update_interval and the last update before performing the search.
  • If an update is needed, it is performed before searching.

clear()

Clears all data from the IndexedDB table associated with the search_id and removes the last update timestamp.

await searchInstance.clear();

Data Format

The data fetched from data_url should be an array of items with the following format:

[
  {
    "id": "unique-item-id",
    "updated_at": "2023-10-15T12:34:56Z",
    "search_value": "Text to be searched",
    "data": { /* Additional data as needed */ },
    "is_deleted": false // (Optional) Indicates if the item has been deleted
  },
  // ... more items
]
  • Note: If is_deleted is true, the item will be deleted from IndexedDB during an incremental update.

date_url Response Format

When using the date_url option, the endpoint should return a JSON object containing an updated_at field, which is a date string in a format that can be parsed by JavaScript's Date constructor.

Example:

{
  "updated_at": "2023-10-15T12:34:56Z"
}
  • The updated_at field represents the last time the data at data_url was updated.
  • The library uses this date to determine if it needs to fetch new data.
  • If date_url is not provided, the library will assume that the data may have changed and will proceed with the update.

Example

import SvelterSearch from 'svelter-search';

const searchInstance = new SvelterSearch({
  search_id: 'products',
  update_interval: 60000, // Check for updates every minute
  data_url: 'https://api.example.com/products',
  date_url: 'https://api.example.com/products-updated-at',
  auto_update: true, // Automatically update before searching
  incrementalUpdate: true, // Use incremental updates
});

// Manually trigger an update
await searchInstance.update();

// Perform a full-text search for products containing 'laptop' and 'gaming'
const results = await searchInstance.search('laptop gaming');
console.log(results);

// Clear the database table for this search_id
await searchInstance.clear();

Contribution

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the MIT License.