Full-Text Search
Client-side full-text search (FTS) is available using the SQLite FTS5 extension.
This requires creating a separate FTS5 table(s) to index the data, and updating the table(s) using SQLite triggers.
Note that the availability of FTS is dependent on the underlying sqlite
package used, as it is an extension that must first be enabled in the package.
Currently, FTS is supported in:
- Flutter: powersync.dart (PowerSync Flutter SDK)
- Relies on the sqlite_async package for migrations
- Web: powersync-sdk-web (JavaScript Web SDK)
- Requires version 0.5.0 or greater of the SDK (incl. version 0.2.0 of the wa-sqlite package)
React Native: SQLite recursive triggers are supported in the React Native SDK, and it should be possible to implement FTS with some additional config. This issue could be a good starting point.
Kotlin and Swift: FTS support for Kotlin and Swift is planned. Let us know if you require this for your project.
Example Implementations
FTS is implemented in the following demo apps:
We explain these implementations in more detail below. Example code is shown mainly in Dart, but references to the React equivalents are included where relevant, so you should be able to cross-reference.
Walkthrough: Full-text search in the To-Do List Demo App
Setup
FTS tables are created when instantiating the client-side PowerSync database (DB).
This occurs in powersync.dart (Flutter implementation) and SystemProvider.tsx (Web implementation).
Dart example:
Note: The Web implementation does not use migrations. It creates the FTS tables separately in utils/fts_setup.ts.
TypeScript example:
First, we need to set up the FTS tables to match the lists
and todos
tables already created in this demo app. Don’t worry if you already have data in the tables, as it will be copied into the new FTS tables.
To simplify implementation these examples make use of SQLite migrations. The migrations are run in migrations/fts_setup.dart in the Flutter implementation. Here we use the sqlite_async Dart package to generate the migrations.
The FTS tables are created in utils/fts_setup.ts in the Web implementation.
Dart example:
Copy
The createFtsMigration
function is key and corresponds to the below (Dart example):
After this is run, you should have the following tables and triggers in your SQLite DB:
FTS tables and migrations
FTS triggers
FTS Search Delegate
To show off this new functionality, we have incorporated FTS into the search button at the top of the screen in the To-Do List demo app:
Clicking on the search icon will open a search bar which will allow you to search for lists
or todos
that you have generated.
Example of searching
It uses a custom search delegate widget found in widgets/fts_search_delegate.dart (Flutter) and widgets/SearchBarWidget.tsx (Web) to display the search results.
FTS Helper
We added a helper in lib/fts_helpers.dart (Flutter) and utils/fts_helpers.ts (Web) that allows you to add additional search functionality which can be found in the SQLite FTS5 extension documentation.
Dart example:
Was this page helpful?