@@ -176,52 +176,59 @@ defmodule Mongo.Repo do
176
176
def all ( module , filter \\ % { } , opts \\ [ ] ) do
177
177
collection = module . __collection__ ( :collection )
178
178
179
- @ topology
180
- |> Mongo . find ( collection , filter , opts )
181
- |> Enum . map ( & module . load / 1 )
179
+ case Mongo . find ( @ topology , collection , filter , opts ) do
180
+ { :error , _reason } = error -> error
181
+ cursor -> Enum . map ( cursor , & module . load / 1 )
182
+ end
182
183
end
183
184
184
185
def stream ( module , filter \\ % { } , opts \\ [ ] ) do
185
186
collection = module . __collection__ ( :collection )
186
187
187
- @ topology
188
- |> Mongo . find ( collection , module . dump_part ( filter ) , opts )
189
- |> Stream . map ( & module . load / 1 )
188
+ case Mongo . find ( @ topology , collection , module . dump_part ( filter ) , opts ) do
189
+ { :error , _reason } = error -> error
190
+ cursor -> Stream . map ( cursor , & module . load / 1 )
191
+ end
190
192
end
191
193
192
194
def aggregate ( module , pipeline , opts \\ [ ] ) do
193
195
collection = module . __collection__ ( :collection )
194
196
195
- @ topology
196
- |> Mongo . aggregate ( collection , pipeline , opts )
197
- |> Enum . map ( & module . load / 1 )
197
+ case Mongo . aggregate ( @ topology , collection , pipeline , opts ) do
198
+ { :error , _reason } = error -> error
199
+ cursor -> Enum . map ( cursor , & module . load / 1 )
200
+ end
198
201
end
199
202
200
203
def get ( module , id , opts \\ [ ] ) do
201
204
collection = module . __collection__ ( :collection )
202
205
203
- @ topology
204
- |> Mongo . find_one ( collection , % { _id: id } , opts )
205
- |> module . load ( )
206
+ case Mongo . find_one ( @ topology , collection , % { _id: id } , opts ) do
207
+ { :error , _reason } = error -> error
208
+ value -> module . load ( value )
209
+ end
206
210
end
207
211
208
212
def get_by ( module , filter \\ % { } , opts \\ [ ] ) do
209
213
collection = module . __collection__ ( :collection )
210
214
211
- @ topology
212
- |> Mongo . find_one ( collection , module . dump_part ( filter ) , opts )
213
- |> module . load ( )
215
+ case Mongo . find_one ( @ topology , collection , module . dump_part ( filter ) , opts ) do
216
+ { :error , _reason } = error -> error
217
+ value -> module . load ( value )
218
+ end
214
219
end
215
220
216
221
def fetch ( module , id , opts \\ [ ] ) do
217
222
case get ( module , id , opts ) do
223
+ { :error , _reason } = error -> error
218
224
nil -> { :error , :not_found }
219
225
doc -> { :ok , doc }
220
226
end
221
227
end
222
228
223
229
def fetch_by ( module , filter \\ % { } , opts \\ [ ] ) do
224
230
case get_by ( module , module . dump_part ( filter ) , opts ) do
231
+ { :error , _reason } = error -> error
225
232
nil -> { :error , :not_found }
226
233
doc -> { :ok , doc }
227
234
end
@@ -297,7 +304,7 @@ defmodule Mongo.Repo do
297
304
MyApp.Repo.all(Post, %{title: title}, batch_size: 2)
298
305
"""
299
306
@ callback all ( module :: module ( ) , filter :: BSON . document ( ) , opts :: Keyword . t ( ) ) ::
300
- list ( Mongo.Collection . t ( ) )
307
+ list ( Mongo.Collection . t ( ) ) | { :error , any ( ) }
301
308
302
309
@ doc """
303
310
Selects documents for the collection defined in the given module and returns a stream of collection
@@ -311,7 +318,7 @@ defmodule Mongo.Repo do
311
318
MyApp.Repo.stream(Post, %{title: title}, batch_size: 2)
312
319
"""
313
320
@ callback stream ( module :: module ( ) , filter :: BSON . document ( ) , opts :: Keyword . t ( ) ) ::
314
- Enumerable . t ( )
321
+ Enumerable . t ( ) | { :error , any ( ) }
315
322
316
323
@ doc """
317
324
Performs aggregation operation using the aggregation pipeline on the given collection module and returns
@@ -328,7 +335,7 @@ defmodule Mongo.Repo do
328
335
])
329
336
"""
330
337
@ callback aggregate ( module :: module ( ) , pipeline :: BSON . document ( ) , opts :: Keyword . t ( ) ) ::
331
- list ( Mongo.Collection . t ( ) )
338
+ list ( Mongo.Collection . t ( ) ) | { :error , any ( ) }
332
339
333
340
@ doc """
334
341
Returns the count of documents in the given collection module for the given filter.
0 commit comments