masamune_model_algolia 3.2.3
masamune_model_algolia: ^3.2.3 copied to clipboard
Masamune plugin package that includes a model adapter to retrieve data from Algolia.
Masamune Model Algolia
[GitHub] | [YouTube] | [Packages] | [X] | [LinkedIn] | [mathru.net]
Masamune Model Algolia #
Usage #
- Add the package to your project.
flutter pub add masamune_model_algolia
Run flutter pub get after editing pubspec.yaml manually.
- Combine the adapter with an existing Firestore model adapter. Algolia handles collection loading, while Firestore continues to manage mutations and document access.
import 'package:masamune_model_algolia/masamune_model_algolia.dart';
import 'package:katana_model_firestore/katana_model_firestore.dart';
final modelAdapter = AlgoliaModelAdapter(
firestoreModelAdapter: const FirestoreModelAdapter(
options: DefaultFirebaseOptions.currentPlatform,
),
applicationId: "YOUR_ALGOLIA_APP_ID",
apiKey: "YOUR_ALGOLIA_SEARCH_API_KEY",
);
- Use filters to search and paginate data. The
likefilter sends the search keyword to Algolia, whilelimitTocontrols pagination.
Using standard filters (for exact matches, ordering, etc.)
// In your widget or controller
final collection = appRef.model(
UserModel.collection()
.name.equal("John") // Filter by exact match
.limitTo(20), // Limit to 20 results per page
)..load();
// Access via next() for pagination
await collection.next();
Using full-text search with Algolia
For full-text search, Algolia looks for the like filter in the query. The simplest way is to use models that extend SearchableCollectionMixin:
// Extend your collection with SearchableCollectionMixin in your model file
// Then use the search method:
final collection = appRef.model(UserModel.collection());
await collection.load();
await collection.search(keyword); // Trigger Algolia full-text search
The search method automatically creates a like filter with the @search field (or your custom search field). Make sure your Algolia index includes bigram-tokenized data for the search field.
Advanced: Direct filter usage
If you need more control, you can use the raw filter approach by extending the generated collection query with custom filters. However, this is not the recommended approach for typical use cases.
Ensure your Algolia index stores entries containing the Masamune model @uid so AlgoliaModelAdapter can map hits back to documents.
GitHub Sponsors #
Sponsors are always welcome. Thank you for your support!