Skip to content

Commit 821cc6e

Browse files
Merge #262
262: [Feature] Support NDJson and CSV r=brunoocasali a=ahmednfwela Fixes #102 Depends on #261 - [x] Tests Added Co-authored-by: Ahmed Fwela <[email protected]>
2 parents 9498a4f + 073dfbf commit 821cc6e

File tree

7 files changed

+873
-197
lines changed

7 files changed

+873
-197
lines changed

lib/src/http_request.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,23 @@ abstract class HttpRequest {
2929
String path, {
3030
Object? data,
3131
Map<String, Object?>? queryParameters,
32+
String contentType,
3233
});
3334

3435
/// POST method
3536
Future<Response<T>> postMethod<T>(
3637
String path, {
3738
Object? data,
3839
Map<String, Object?>? queryParameters,
40+
String contentType,
3941
});
4042

4143
/// PUT method
4244
Future<Response<T>> putMethod<T>(
4345
String path, {
4446
Object? data,
4547
Map<String, Object?>? queryParameters,
48+
String contentType,
4649
});
4750

4851
/// DELETE method

lib/src/http_request_impl.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class HttpRequestImpl implements HttpRequest {
1717
if (_kIsWeb) Version.qualifiedVersionWeb
1818
].join(',')
1919
},
20-
contentType: 'application/json',
2120
responseType: ResponseType.json,
2221
connectTimeout: connectTimeout ?? Duration(seconds: 5),
2322
),
@@ -61,12 +60,16 @@ class HttpRequestImpl implements HttpRequest {
6160
String path, {
6261
Object? data,
6362
Map<String, Object?>? queryParameters,
63+
String contentType = Headers.jsonContentType,
6464
}) async {
6565
try {
6666
return await dio.post<T>(
6767
path,
6868
data: data,
6969
queryParameters: queryParameters,
70+
options: Options(
71+
contentType: contentType,
72+
),
7073
);
7174
} on DioError catch (e) {
7275
return throwException(e);
@@ -78,12 +81,16 @@ class HttpRequestImpl implements HttpRequest {
7881
String path, {
7982
Object? data,
8083
Map<String, Object?>? queryParameters,
84+
String contentType = Headers.jsonContentType,
8185
}) async {
8286
try {
8387
return await dio.patch<T>(
8488
path,
8589
data: data,
8690
queryParameters: queryParameters,
91+
options: Options(
92+
contentType: contentType,
93+
),
8794
);
8895
} on DioError catch (e) {
8996
return throwException(e);
@@ -95,12 +102,16 @@ class HttpRequestImpl implements HttpRequest {
95102
String path, {
96103
Object? data,
97104
Map<String, Object?>? queryParameters,
105+
String contentType = Headers.jsonContentType,
98106
}) async {
99107
try {
100108
return await dio.put<T>(
101109
path,
102110
data: data,
103111
queryParameters: queryParameters,
112+
options: Options(
113+
contentType: contentType,
114+
),
104115
);
105116
} on DioError catch (e) {
106117
return throwException(e);

lib/src/index.dart

Lines changed: 129 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,161 @@ abstract class MeiliSearchIndex {
5555
/// Return a list of all existing documents in the index.
5656
Future<Result<Map<String, Object?>>> getDocuments({DocumentsQuery? params});
5757

58+
/// {@template meili.add_docs}
5859
/// Add a list of documents by given [documents] and optional [primaryKey] parameter.
59-
/// If index is not exists tries to create a new index and adds documents.
60+
/// {@endtemplate}
61+
///
62+
/// {@template meili.index_upsert}
63+
/// If the index does not exist, tries to create a new index and adds documents.
64+
/// {@endtemplate}
6065
Future<Task> addDocuments(
6166
List<Map<String, Object?>> documents, {
6267
String? primaryKey,
6368
});
6469

70+
/// {@macro meili.add_docs}
71+
///
72+
/// * The passed [documents] must be a valid JSON string representing an array of objects.
73+
/// *
74+
/// {@macro meili.index_upsert}
75+
Future<Task> addDocumentsJson(
76+
String documents, {
77+
String? primaryKey,
78+
});
79+
80+
/// {@macro meili.add_docs}
81+
///
82+
/// *
83+
/// {@template meili.csv}
84+
/// The passed documents must be a valid CSV string, where the first line contains objects' keys and types, and each subsequent line corresponds to an object.
85+
/// [see relevant documentation](https://docs.meilisearch.com/learn/core_concepts/documents.html#csv)
86+
/// {@endtemplate}
87+
///
88+
/// *
89+
/// {@macro meili.index_upsert}
90+
Future<Task> addDocumentsCsv(
91+
String documents, {
92+
String? primaryKey,
93+
});
94+
95+
/// {@macro meili.add_docs}
96+
///
97+
/// * The passed [documents] must be a valid Newline Delimited Json (NdJson) string, where each line corresponds to an object.
98+
/// *
99+
/// {@macro meili.index_upsert}
100+
Future<Task> addDocumentsNdjson(
101+
String documents, {
102+
String? primaryKey,
103+
});
104+
105+
/// {@template meili.add_docs_batches}
65106
/// Add a list of documents in batches of size [batchSize] by given [documents] and optional [primaryKey] parameter.
66-
/// If the index does not exist try to create a new index and add documents.
107+
/// {@endtemplate}
108+
///
109+
/// {@macro meili.index_upsert}
67110
Future<List<Task>> addDocumentsInBatches(
68111
List<Map<String, Object?>> documents, {
69112
int batchSize = 1000,
70113
String? primaryKey,
71114
});
72115

116+
/// {@macro meili.add_docs_batches}
117+
///
118+
/// *
119+
/// {@macro meili.csv}
120+
/// *
121+
/// {@macro meili.index_upsert}
122+
Future<List<Task>> addDocumentsCsvInBatches(
123+
String documents, {
124+
String? primaryKey,
125+
int batchSize = 1000,
126+
});
127+
128+
/// {@macro meili.add_docs_batches}
129+
///
130+
/// * The passed [documents] must be a valid Newline Delimited Json (NdJson) string, where each line corresponds to an object.
131+
/// *
132+
/// {@macro meili.index_upsert}
133+
Future<List<Task>> addDocumentsNdjsonInBatches(
134+
String documents, {
135+
String? primaryKey,
136+
int batchSize = 1000,
137+
});
138+
139+
/// {@template meili.update_docs}
73140
/// Add a list of documents or update them if they already exist by given [documents] and optional [primaryKey] parameter.
74-
/// If index is not exists tries to create a new index and adds documents.
141+
/// {@endtemplate}
142+
///
143+
/// {@macro meili.index_upsert}
75144
Future<Task> updateDocuments(
76145
List<Map<String, Object?>> documents, {
77146
String? primaryKey,
78147
});
79148

149+
/// {@macro meili.update_docs}
150+
///
151+
/// * the passed [documents] must be a valid JSON string representing an array of objects.
152+
/// *
153+
/// {@macro meili.index_upsert}
154+
Future<Task> updateDocumentsJson(
155+
String documents, {
156+
String? primaryKey,
157+
});
158+
159+
/// {@macro meili.update_docs}
160+
///
161+
/// * The passed [documents] must be a valid Newline Delimited Json (NdJson) string, where each line corresponds to an object.
162+
/// *
163+
/// {@macro meili.index_upsert}
164+
Future<Task> updateDocumentsNdjson(
165+
String documents, {
166+
String? primaryKey,
167+
});
168+
169+
/// {@macro meili.update_docs}
170+
///
171+
/// *
172+
/// {@macro meili.csv}
173+
/// *
174+
/// {@macro meili.index_upsert}
175+
Future<Task> updateDocumentsCsv(
176+
String documents, {
177+
String? primaryKey,
178+
});
179+
180+
/// {@template meili.update_docs_batches}
80181
/// Add a list of documents or update them if they already exist in batches of size [batchSize] by given [documents] and optional [primaryKey] parameter.
81-
/// If index is not exists tries to create a new index and adds documents.
182+
/// {@endtemplate}
183+
///
184+
/// {@macro meili.index_upsert}
82185
Future<List<Task>> updateDocumentsInBatches(
83186
List<Map<String, Object?>> documents, {
84187
int batchSize = 1000,
85188
String? primaryKey,
86189
});
87190

191+
/// {@macro meili.update_docs_batches}
192+
///
193+
/// * The passed [documents] must be a valid CSV string, where each line corresponds to an object.
194+
/// *
195+
/// {@macro meili.index_upsert}
196+
Future<List<Task>> updateDocumentsCsvInBatches(
197+
String documents, {
198+
String? primaryKey,
199+
int batchSize = 1000,
200+
});
201+
202+
/// {@macro meili.update_docs_batches}
203+
///
204+
/// * The passed [documents] must be a valid Newline Delimited Json (NdJson) string, where each line corresponds to an object.
205+
/// *
206+
/// {@macro meili.index_upsert}
207+
Future<List<Task>> updateDocumentsNdjsonInBatches(
208+
String documents, {
209+
String? primaryKey,
210+
int batchSize = 1000,
211+
});
212+
88213
/// Delete one document by given [id].
89214
Future<Task> deleteDocument(Object id);
90215

0 commit comments

Comments
 (0)