Skip to content

Commit

Permalink
Add a way to pass fields to getDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoocasali committed Jul 5, 2022
1 parent 1ab0a55 commit 3616ae4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/src/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ abstract class MeiliSearchIndex {
});

/// Return the document in the index by given [id].
Future<Map<String, dynamic>?> getDocument(dynamic id);
Future<Map<String, dynamic>?> getDocument(dynamic id, { List<String> fields });

/// Return a list of all existing documents in the index.
Future<Result> getDocuments({DocumentsQuery? params});
Expand Down
4 changes: 3 additions & 1 deletion lib/src/index_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,11 @@ class MeiliSearchIndexImpl implements MeiliSearchIndex {
}

@override
Future<Map<String, dynamic>?> getDocument(id) async {
Future<Map<String, dynamic>?> getDocument(id, { List<String> fields: const [] }) async {
final params = DocumentsQuery(fields: fields);
final response = await http.getMethod<Map<String, dynamic>>(
'/indexes/$uid/documents/$id',
queryParameters: params.toQuery()
);

return response.data;
Expand Down
8 changes: 8 additions & 0 deletions test/documents_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,13 @@ void main() {
expect(docs.results[0]['book_id'], isNotNull);
expect(docs.results[0]['title'], isNull);
});

test('Get document with fields', () async {
final index = await createBooksIndex();
final doc = await index.getDocument(1, fields: ['book_id']);

expect(doc?['book_id'], isNotNull);
expect(doc?['title'], isNull);
});
});
}

0 comments on commit 3616ae4

Please sign in to comment.