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.
is_deleted
field.Install via npm:
npm install svelter-search dexie
Note: dexie
is a required dependency for IndexedDB operations.
import SvelterSearch from 'svelter-search';
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)
});
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
.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');
incrementalUpdate
is false
, a full update is performed:incrementalUpdate
is true
, an incremental update is performed: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);
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.clear()
Clears all data from the IndexedDB table associated with the search_id
and removes the last update timestamp.
await searchInstance.clear();
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
]
is_deleted
is true
, the item will be deleted from IndexedDB during an incremental update.date_url
Response FormatWhen 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"
}
updated_at
field represents the last time the data at data_url
was updated.date_url
is not provided, the library will assume that the data may have changed and will proceed with the update.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();
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License.