Skip to content

collection

MongoDB collection for entry-endpoint resources.

The AsyncMongoCollection represents an asynchronous version of the equivalent MongoDB collection in optimade: MongoCollection.

AsyncMongoCollection

Bases: EntryCollection

MongoDB Collection for use with asyncio

The asynchronicity is implemented using motor and asyncio.

Source code in optimade_gateway/mongo/collection.py
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
class AsyncMongoCollection(EntryCollection):
    """MongoDB Collection for use with `asyncio`

    The asynchronicity is implemented using [`motor`](https://motor.readthedocs.io) and
    [`asyncio`](https://asyncio.readthedocs.io/).
    """

    def __init__(
        self,
        name: str,
        resource_cls: EntryResource,
        resource_mapper: BaseResourceMapper,
    ):
        """Initialize the AsyncMongoCollection for the given parameters.

        Parameters:
            name: The name of the collection.
            resource_cls: The `EntryResource` model that is stored by the collection.
            resource_mapper: A resource mapper object that handles aliases and format
                changes between deserialization and response.

        """
        from optimade_gateway.mongo.database import (
            MONGO_DB,
        )

        super().__init__(
            resource_cls=resource_cls,
            resource_mapper=resource_mapper,
            transformer=MongoTransformer(mapper=resource_mapper),
        )

        self.parser = LarkParser(version=(1, 0, 0), variant="default")
        self.collection: MongoCollection = MONGO_DB[name]

        # Check aliases do not clash with mongo operators
        self._check_aliases(self.resource_mapper.all_aliases())
        self._check_aliases(self.resource_mapper.all_length_aliases())

    def __str__(self) -> str:
        """Standard printing result for an instance."""
        return (
            f"<{self.__class__.__name__}: resource={self.resource_cls.__name__} "
            f"endpoint(mapper)={self.resource_mapper.ENDPOINT} "
            f"DB_collection={self.collection.name}>"
        )

    def __repr__(self) -> str:
        """Representation of instance."""
        return (
            f"{self.__class__.__name__}(name={self.collection.name!r}, "
            f"resource_cls={self.resource_cls!r}, "
            f"resource_mapper={self.resource_mapper!r})"
        )

    def __len__(self) -> int:
        warn(
            OptimadeGatewayWarning(
                detail=(
                    "Cannot calculate length of collection using `len()`. Use "
                    "`count()` instead."
                )
            )
        )
        return 0

    def insert(self, data: list[EntryResource]) -> None:
        raise NotImplementedError(
            "This method cannot be used with this class and is a remnant from the "
            "parent class. Use instead the asynchronous method `ainsert(data: "
            "List[EntryResource])`."
        )

    async def ainsert(self, data: list[EntryResource]) -> None:
        """Add the given entries to the underlying database.

        This is the asynchronous version of the parent class method named `insert()`.

        Arguments:
            data: The entry resource objects to add to the database.

        """
        await self.collection.insert_many(await clean_python_types(data))

    def count(self, **kwargs) -> int:
        raise NotImplementedError(
            "This method cannot be used with this class and is a remnant from the "
            "parent class. Use instead the asynchronous method `acount(params: "
            "Optional[Union[EntryListingQueryParams, SingleEntryQueryParams]], "
            "**kwargs)`."
        )

    async def acount(
        self,
        params: None | (EntryListingQueryParams | SingleEntryQueryParams) = None,
        **kwargs: Any,
    ) -> int:
        """Count documents in Collection.

        This is the asynchronous version of the parent class method named `count()`.

        Parameters:
            params: URL query parameters, either from a general entry endpoint or a
                single-entry endpoint.
            **kwargs: Query parameters as keyword arguments. Valid keys will be passed
                to the
                [`AsyncIOMotorCollection.count_documents`](https://motor.readthedocs.io/en/stable/api-asyncio/asyncio_motor_collection.html#motor.motor_asyncio.AsyncIOMotorCollection.count_documents)
                method.

        Returns:
            int: The number of entries matching the query specified by the keyword
                arguments.

        """
        if params is not None and kwargs:
            raise ValueError(
                "When 'params' is supplied, no other parameters can be supplied."
            )

        if params is not None:
            kwargs = await self.ahandle_query_params(params)

        valid_method_keys = (
            "filter",
            "skip",
            "limit",
            "hint",
            "maxTimeMS",
            "collation",
            "session",
        )
        criteria = {key: kwargs[key] for key in valid_method_keys if key in kwargs}

        if criteria.get("filter") is None:
            criteria["filter"] = {}

        return await self.collection.count_documents(**criteria)

    def find(
        self, params: EntryListingQueryParams | SingleEntryQueryParams
    ) -> tuple[
        list[EntryResource] | EntryResource | None, int, bool, set[str], set[str]
    ]:
        """
        Fetches results and indicates if more data is available.

        Also gives the total number of data available in the absence of `page_limit`.
        See
        [`EntryListingQueryParams`](https://www.optimade.org/optimade-python-tools/api_reference/server/query_params/#optimade.server.query_params.EntryListingQueryParams)
        for more information.

        Parameters:
            params: Entry listing URL query params.

        Returns:
            A tuple of various relevant values:
            (`results`, `data_returned`, `more_data_available`, `exclude_fields`,
            `include_fields`).

        """
        raise NotImplementedError(
            "This method cannot be used with this class and is a remnant from the "
            "parent class. Use instead the asynchronous method `afind(params: "
            "Optional[Union[EntryListingQueryParams, SingleEntryQueryParams]], "
            "criteria: Optional[Dict[str, Any]])`."
        )

    async def afind(
        self,
        params: None | (EntryListingQueryParams | SingleEntryQueryParams) = None,
        criteria: dict[str, Any] | None = None,
    ) -> tuple[
        list[EntryResource] | EntryResource | None, int, bool, set[str], set[str]
    ]:
        """Perform the query on the underlying MongoDB Collection, handling projection
        and pagination of the output.

        This is the asynchronous version of the parent class method named `count()`.

        Either provide `params` or `criteria`. Not both, but at least one.

        Parameters:
            params: URL query parameters, either from a general entry endpoint or a
                single-entry endpoint.
            criteria: Already handled/parsed URL query parameters.

        Returns:
            A list of entry resource objects, how much data was returned for the query,
            whether more data is available with pagination, and fields (excluded,
            included).

        """
        if (params is None and criteria is None) or (
            params is not None and criteria is not None
        ):
            raise ValueError(
                "Exacly one of either `params` and `criteria` must be specified."
            )

        # Set single_entry to False, this is done since if criteria is defined,
        # this is an unknown factor - better to then get a list of results.
        single_entry = False
        if criteria is None:
            criteria = await self.ahandle_query_params(params)
        else:
            single_entry = isinstance(params, SingleEntryQueryParams)

        response_fields = criteria.pop("fields", self.all_fields)

        results, data_returned, more_data_available = await self._arun_db_query(
            criteria=criteria,
            single_entry=single_entry,
        )

        if single_entry:
            results = results[0] if results else None  # type: ignore[assignment]

            if data_returned > 1:
                raise NotFound(
                    detail=(
                        f"Instead of a single entry, {data_returned} entries were found"
                    ),
                )

        include_fields = (
            response_fields - self.resource_mapper.TOP_LEVEL_NON_ATTRIBUTES_FIELDS
        )
        bad_optimade_fields = set()
        bad_provider_fields = set()
        for field in include_fields:
            if field not in self.resource_mapper.ALL_ATTRIBUTES:
                if field.startswith("_"):
                    if any(
                        field.startswith(f"_{prefix}_")
                        for prefix in self.resource_mapper.SUPPORTED_PREFIXES
                    ):
                        bad_provider_fields.add(field)
                else:
                    bad_optimade_fields.add(field)

        if bad_provider_fields:
            warn(
                UnknownProviderProperty(
                    detail=(
                        "Unrecognised field(s) for this provider requested in "
                        f"`response_fields`: {bad_provider_fields}."
                    )
                )
            )

        if bad_optimade_fields:
            raise BadRequest(
                detail=(
                    "Unrecognised OPTIMADE field(s) in requested `response_fields`: "
                    f"{bad_optimade_fields}."
                )
            )

        if results:
            results = await self.resource_mapper.adeserialize(results)

        return (  # type: ignore[return-value]
            results,
            data_returned,
            more_data_available,
            self.all_fields - response_fields,
            include_fields,
        )

    def handle_query_params(
        self, params: EntryListingQueryParams | SingleEntryQueryParams
    ) -> dict[str, Any]:
        """Parse and interpret the backend-agnostic query parameter models into a
        dictionary that can be used by the specific backend.

        Note:
            Currently this method returns the pymongo interpretation of the parameters,
            which will need modification for modified for other backends.

        Parameters:
            params: The initialized query parameter model from the server.

        Raises:
            Forbidden: If too large of a page limit is provided.
            BadRequest: If an invalid request is made, e.g., with incorrect fields
                or response format.

        Returns:
            A dictionary representation of the query parameters.

        """
        raise NotImplementedError(
            "This method cannot be used with this class and is a remnant from the "
            "parent class. Use instead the asynchronous method "
            "`ahandle_query_params(params: Union[EntryListingQueryParams, "
            "SingleEntryQueryParams])`."
        )

    async def ahandle_query_params(
        self, params: EntryListingQueryParams | SingleEntryQueryParams
    ) -> dict[str, Any]:
        """Parse and interpret the backend-agnostic query parameter models into a
        dictionary that can be used by the specific backend.

        This is the asynchronous version of the parent class method named
        `handle_query_params()`.

        Note:
            Currently this method returns the pymongo interpretation of the parameters,
            which will need modification for modified for other backends.

        Parameters:
            params: The initialized query parameter model from the server.

        Raises:
            Forbidden: If too large of a page limit is provided.
            BadRequest: If an invalid request is made, e.g., with incorrect fields or
                response format.

        Returns:
            A dictionary representation of the query parameters.

        """
        return super().handle_query_params(params)

    def _run_db_query(
        self, criteria: dict[str, Any], single_entry: bool = False
    ) -> tuple[list[dict[str, Any]], int, bool]:
        raise NotImplementedError(
            "This method cannot be used with this class and is a remnant from the "
            "parent class. Use instead the asynchronous method "
            "`_arun_db_query(criteria: Dict[str, Any], single_entry: bool)`."
        )

    async def _arun_db_query(
        self, criteria: dict[str, Any], single_entry: bool = False
    ) -> tuple[list[dict[str, Any]], int, bool]:
        """Run the query on the backend and collect the results.

        This is the asynchronous version of the parent class method named `count()`.

        Arguments:
            criteria: A dictionary representation of the query parameters.
            single_entry: Whether or not the caller is expecting a single entry
                response.

        Returns:
            The list of entries from the database (without any re-mapping), the total
            number of entries matching the query and a boolean for whether or not there
            is more data available.

        """
        results = []
        async for document in self.collection.find(**self._valid_find_keys(**criteria)):
            if criteria.get("projection", {}).get("_id"):
                document["_id"] = str(document["_id"])
            results.append(document)

        if single_entry:
            data_returned = len(results)
            more_data_available = False
        else:
            criteria_nolimit = criteria.copy()
            criteria_nolimit.pop("limit", None)
            data_returned = await self.acount(params=None, **criteria_nolimit)
            more_data_available = len(results) < data_returned

        return results, data_returned, more_data_available

    @staticmethod
    def _check_aliases(aliases: tuple[tuple[str, str]]) -> None:
        """Check that aliases do not clash with mongo keywords.

        Parameters:
            aliases: Tuple of tuple of aliases to be checked.

        Raises:
            RuntimeError: If any alias starts with the dollar (`$`) character.

        """
        if any(
            alias[0].startswith("$") or alias[1].startswith("$") for alias in aliases
        ):
            raise RuntimeError(f"Cannot define an alias starting with a '$': {aliases}")

    async def get_one(self, **criteria: Any) -> EntryResource:
        """Get one resource based on criteria

        Warning:
            This is not to be used for creating a REST API response,
            but is rather a utility function to easily retrieve a single resource.

        Parameters:
            **criteria: Already handled/parsed URL query parameters.

        Returns:
            A single resource from the MongoDB (mapped to pydantic models).

        """
        criteria = criteria or {}

        return self.resource_cls(
            **self.resource_mapper.map_back(
                await self.collection.find_one(**self._valid_find_keys(**criteria))
            )
        )

    async def get_multiple(self, **criteria: Any) -> list[EntryResource]:
        """Get a list of resources based on criteria

        Warning:
            This is not to be used for creating a REST API response,
            but is rather a utility function to easily retrieve a list of resources.

        Parameters:
            **criteria: Already handled/parsed URL query parameters.

        Returns:
            A list of resources from the MongoDB (mapped to pydantic models).

        """
        criteria = criteria or {}

        results = []
        async for document in self.collection.find(**self._valid_find_keys(**criteria)):
            results.append(self.resource_cls(**self.resource_mapper.map_back(document)))

        return results

    async def create_one(self, resource: EntryResourceCreate) -> EntryResource:
        """Create a new document in the MongoDB collection based on query parameters.

        Update the newly created document with an `"id"` field.
        The value will be the string representation of the `"_id"` field.
        This will only be done if `"id"` is not already present in `resource`.

        Parameters:
            resource: The resource to be created.

        Returns:
            The newly created document as a pydantic model entry resource.

        """
        resource.last_modified = datetime.now(timezone.utc)
        result = await self.collection.insert_one(
            await clean_python_types(resource.model_dump(exclude_unset=True))
        )
        LOGGER.debug(
            "Inserted resource %r in DB collection %s with ID %s",
            resource,
            self.collection.name,
            result.inserted_id,
        )

        if not resource.id:
            LOGGER.debug("Updating resource with an `id` field equal to str(id_).")
            await self.collection.update_one(
                {"_id": result.inserted_id}, {"$set": {"id": str(result.inserted_id)}}
            )

        return self.resource_cls(
            **self.resource_mapper.map_back(
                await self.collection.find_one({"_id": result.inserted_id})
            )
        )

    async def exists(self, entry_id: str) -> bool:
        """Assert whether entry_id exists in the collection (value of `"id"`)

        Parameters:
            entry_id: The `"id"` value of the entry.

        """
        return bool(await self.collection.count_documents({"id": entry_id}))

    @staticmethod
    def _valid_find_keys(**kwargs: dict[str, Any]) -> dict[str, Any]:
        """Return valid MongoDB find() keys with values from kwargs

        Note, not including deprecated flags
        (see https://pymongo.readthedocs.io/en/3.11.0/api/pymongo/collection.html#pymongo.collection.Collection.find).
        """
        valid_method_keys = (
            "filter",
            "projection",
            "session",
            "skip",
            "limit",
            "no_cursor_timeout",
            "cursor_type",
            "sort",
            "allow_partial_results",
            "batch_size",
            "collation",
            "return_key",
            "show_record_id",
            "hint",
            "max_time_ms",
            "min",
            "max",
            "comment",
            "allow_disk_use",
        )
        criteria = {key: kwargs[key] for key in valid_method_keys if key in kwargs}

        if criteria.get("filter") is None:
            # Ensure documents are included in the result set
            criteria["filter"] = {}

        return criteria

__init__(name, resource_cls, resource_mapper)

Initialize the AsyncMongoCollection for the given parameters.

Parameters:

Name Type Description Default
name str

The name of the collection.

required
resource_cls EntryResource

The EntryResource model that is stored by the collection.

required
resource_mapper BaseResourceMapper

A resource mapper object that handles aliases and format changes between deserialization and response.

required
Source code in optimade_gateway/mongo/collection.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def __init__(
    self,
    name: str,
    resource_cls: EntryResource,
    resource_mapper: BaseResourceMapper,
):
    """Initialize the AsyncMongoCollection for the given parameters.

    Parameters:
        name: The name of the collection.
        resource_cls: The `EntryResource` model that is stored by the collection.
        resource_mapper: A resource mapper object that handles aliases and format
            changes between deserialization and response.

    """
    from optimade_gateway.mongo.database import (
        MONGO_DB,
    )

    super().__init__(
        resource_cls=resource_cls,
        resource_mapper=resource_mapper,
        transformer=MongoTransformer(mapper=resource_mapper),
    )

    self.parser = LarkParser(version=(1, 0, 0), variant="default")
    self.collection: MongoCollection = MONGO_DB[name]

    # Check aliases do not clash with mongo operators
    self._check_aliases(self.resource_mapper.all_aliases())
    self._check_aliases(self.resource_mapper.all_length_aliases())

acount(params=None, **kwargs) async

Count documents in Collection.

This is the asynchronous version of the parent class method named count().

Parameters:

Name Type Description Default
params None | EntryListingQueryParams | SingleEntryQueryParams

URL query parameters, either from a general entry endpoint or a single-entry endpoint.

None
**kwargs Any

Query parameters as keyword arguments. Valid keys will be passed to the AsyncIOMotorCollection.count_documents method.

{}

Returns:

Name Type Description
int int

The number of entries matching the query specified by the keyword arguments.

Source code in optimade_gateway/mongo/collection.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
async def acount(
    self,
    params: None | (EntryListingQueryParams | SingleEntryQueryParams) = None,
    **kwargs: Any,
) -> int:
    """Count documents in Collection.

    This is the asynchronous version of the parent class method named `count()`.

    Parameters:
        params: URL query parameters, either from a general entry endpoint or a
            single-entry endpoint.
        **kwargs: Query parameters as keyword arguments. Valid keys will be passed
            to the
            [`AsyncIOMotorCollection.count_documents`](https://motor.readthedocs.io/en/stable/api-asyncio/asyncio_motor_collection.html#motor.motor_asyncio.AsyncIOMotorCollection.count_documents)
            method.

    Returns:
        int: The number of entries matching the query specified by the keyword
            arguments.

    """
    if params is not None and kwargs:
        raise ValueError(
            "When 'params' is supplied, no other parameters can be supplied."
        )

    if params is not None:
        kwargs = await self.ahandle_query_params(params)

    valid_method_keys = (
        "filter",
        "skip",
        "limit",
        "hint",
        "maxTimeMS",
        "collation",
        "session",
    )
    criteria = {key: kwargs[key] for key in valid_method_keys if key in kwargs}

    if criteria.get("filter") is None:
        criteria["filter"] = {}

    return await self.collection.count_documents(**criteria)

afind(params=None, criteria=None) async

Perform the query on the underlying MongoDB Collection, handling projection and pagination of the output.

This is the asynchronous version of the parent class method named count().

Either provide params or criteria. Not both, but at least one.

Parameters:

Name Type Description Default
params None | EntryListingQueryParams | SingleEntryQueryParams

URL query parameters, either from a general entry endpoint or a single-entry endpoint.

None
criteria dict[str, Any] | None

Already handled/parsed URL query parameters.

None

Returns:

Type Description
list[EntryResource] | EntryResource | None

A list of entry resource objects, how much data was returned for the query,

int

whether more data is available with pagination, and fields (excluded,

bool

included).

Source code in optimade_gateway/mongo/collection.py
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
async def afind(
    self,
    params: None | (EntryListingQueryParams | SingleEntryQueryParams) = None,
    criteria: dict[str, Any] | None = None,
) -> tuple[
    list[EntryResource] | EntryResource | None, int, bool, set[str], set[str]
]:
    """Perform the query on the underlying MongoDB Collection, handling projection
    and pagination of the output.

    This is the asynchronous version of the parent class method named `count()`.

    Either provide `params` or `criteria`. Not both, but at least one.

    Parameters:
        params: URL query parameters, either from a general entry endpoint or a
            single-entry endpoint.
        criteria: Already handled/parsed URL query parameters.

    Returns:
        A list of entry resource objects, how much data was returned for the query,
        whether more data is available with pagination, and fields (excluded,
        included).

    """
    if (params is None and criteria is None) or (
        params is not None and criteria is not None
    ):
        raise ValueError(
            "Exacly one of either `params` and `criteria` must be specified."
        )

    # Set single_entry to False, this is done since if criteria is defined,
    # this is an unknown factor - better to then get a list of results.
    single_entry = False
    if criteria is None:
        criteria = await self.ahandle_query_params(params)
    else:
        single_entry = isinstance(params, SingleEntryQueryParams)

    response_fields = criteria.pop("fields", self.all_fields)

    results, data_returned, more_data_available = await self._arun_db_query(
        criteria=criteria,
        single_entry=single_entry,
    )

    if single_entry:
        results = results[0] if results else None  # type: ignore[assignment]

        if data_returned > 1:
            raise NotFound(
                detail=(
                    f"Instead of a single entry, {data_returned} entries were found"
                ),
            )

    include_fields = (
        response_fields - self.resource_mapper.TOP_LEVEL_NON_ATTRIBUTES_FIELDS
    )
    bad_optimade_fields = set()
    bad_provider_fields = set()
    for field in include_fields:
        if field not in self.resource_mapper.ALL_ATTRIBUTES:
            if field.startswith("_"):
                if any(
                    field.startswith(f"_{prefix}_")
                    for prefix in self.resource_mapper.SUPPORTED_PREFIXES
                ):
                    bad_provider_fields.add(field)
            else:
                bad_optimade_fields.add(field)

    if bad_provider_fields:
        warn(
            UnknownProviderProperty(
                detail=(
                    "Unrecognised field(s) for this provider requested in "
                    f"`response_fields`: {bad_provider_fields}."
                )
            )
        )

    if bad_optimade_fields:
        raise BadRequest(
            detail=(
                "Unrecognised OPTIMADE field(s) in requested `response_fields`: "
                f"{bad_optimade_fields}."
            )
        )

    if results:
        results = await self.resource_mapper.adeserialize(results)

    return (  # type: ignore[return-value]
        results,
        data_returned,
        more_data_available,
        self.all_fields - response_fields,
        include_fields,
    )

ahandle_query_params(params) async

Parse and interpret the backend-agnostic query parameter models into a dictionary that can be used by the specific backend.

This is the asynchronous version of the parent class method named handle_query_params().

Note

Currently this method returns the pymongo interpretation of the parameters, which will need modification for modified for other backends.

Parameters:

Name Type Description Default
params EntryListingQueryParams | SingleEntryQueryParams

The initialized query parameter model from the server.

required

Raises:

Type Description
Forbidden

If too large of a page limit is provided.

BadRequest

If an invalid request is made, e.g., with incorrect fields or response format.

Returns:

Type Description
dict[str, Any]

A dictionary representation of the query parameters.

Source code in optimade_gateway/mongo/collection.py
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
async def ahandle_query_params(
    self, params: EntryListingQueryParams | SingleEntryQueryParams
) -> dict[str, Any]:
    """Parse and interpret the backend-agnostic query parameter models into a
    dictionary that can be used by the specific backend.

    This is the asynchronous version of the parent class method named
    `handle_query_params()`.

    Note:
        Currently this method returns the pymongo interpretation of the parameters,
        which will need modification for modified for other backends.

    Parameters:
        params: The initialized query parameter model from the server.

    Raises:
        Forbidden: If too large of a page limit is provided.
        BadRequest: If an invalid request is made, e.g., with incorrect fields or
            response format.

    Returns:
        A dictionary representation of the query parameters.

    """
    return super().handle_query_params(params)

ainsert(data) async

Add the given entries to the underlying database.

This is the asynchronous version of the parent class method named insert().

Parameters:

Name Type Description Default
data list[EntryResource]

The entry resource objects to add to the database.

required
Source code in optimade_gateway/mongo/collection.py
113
114
115
116
117
118
119
120
121
122
async def ainsert(self, data: list[EntryResource]) -> None:
    """Add the given entries to the underlying database.

    This is the asynchronous version of the parent class method named `insert()`.

    Arguments:
        data: The entry resource objects to add to the database.

    """
    await self.collection.insert_many(await clean_python_types(data))

create_one(resource) async

Create a new document in the MongoDB collection based on query parameters.

Update the newly created document with an "id" field. The value will be the string representation of the "_id" field. This will only be done if "id" is not already present in resource.

Parameters:

Name Type Description Default
resource EntryResourceCreate

The resource to be created.

required

Returns:

Type Description
EntryResource

The newly created document as a pydantic model entry resource.

Source code in optimade_gateway/mongo/collection.py
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
async def create_one(self, resource: EntryResourceCreate) -> EntryResource:
    """Create a new document in the MongoDB collection based on query parameters.

    Update the newly created document with an `"id"` field.
    The value will be the string representation of the `"_id"` field.
    This will only be done if `"id"` is not already present in `resource`.

    Parameters:
        resource: The resource to be created.

    Returns:
        The newly created document as a pydantic model entry resource.

    """
    resource.last_modified = datetime.now(timezone.utc)
    result = await self.collection.insert_one(
        await clean_python_types(resource.model_dump(exclude_unset=True))
    )
    LOGGER.debug(
        "Inserted resource %r in DB collection %s with ID %s",
        resource,
        self.collection.name,
        result.inserted_id,
    )

    if not resource.id:
        LOGGER.debug("Updating resource with an `id` field equal to str(id_).")
        await self.collection.update_one(
            {"_id": result.inserted_id}, {"$set": {"id": str(result.inserted_id)}}
        )

    return self.resource_cls(
        **self.resource_mapper.map_back(
            await self.collection.find_one({"_id": result.inserted_id})
        )
    )

exists(entry_id) async

Assert whether entry_id exists in the collection (value of "id")

Parameters:

Name Type Description Default
entry_id str

The "id" value of the entry.

required
Source code in optimade_gateway/mongo/collection.py
506
507
508
509
510
511
512
513
async def exists(self, entry_id: str) -> bool:
    """Assert whether entry_id exists in the collection (value of `"id"`)

    Parameters:
        entry_id: The `"id"` value of the entry.

    """
    return bool(await self.collection.count_documents({"id": entry_id}))

find(params)

Fetches results and indicates if more data is available.

Also gives the total number of data available in the absence of page_limit. See EntryListingQueryParams for more information.

Parameters:

Name Type Description Default
params EntryListingQueryParams | SingleEntryQueryParams

Entry listing URL query params.

required

Returns:

Type Description
list[EntryResource] | EntryResource | None

A tuple of various relevant values:

int

(results, data_returned, more_data_available, exclude_fields,

bool

include_fields).

Source code in optimade_gateway/mongo/collection.py
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def find(
    self, params: EntryListingQueryParams | SingleEntryQueryParams
) -> tuple[
    list[EntryResource] | EntryResource | None, int, bool, set[str], set[str]
]:
    """
    Fetches results and indicates if more data is available.

    Also gives the total number of data available in the absence of `page_limit`.
    See
    [`EntryListingQueryParams`](https://www.optimade.org/optimade-python-tools/api_reference/server/query_params/#optimade.server.query_params.EntryListingQueryParams)
    for more information.

    Parameters:
        params: Entry listing URL query params.

    Returns:
        A tuple of various relevant values:
        (`results`, `data_returned`, `more_data_available`, `exclude_fields`,
        `include_fields`).

    """
    raise NotImplementedError(
        "This method cannot be used with this class and is a remnant from the "
        "parent class. Use instead the asynchronous method `afind(params: "
        "Optional[Union[EntryListingQueryParams, SingleEntryQueryParams]], "
        "criteria: Optional[Dict[str, Any]])`."
    )

get_multiple(**criteria) async

Get a list of resources based on criteria

Warning

This is not to be used for creating a REST API response, but is rather a utility function to easily retrieve a list of resources.

Parameters:

Name Type Description Default
**criteria Any

Already handled/parsed URL query parameters.

{}

Returns:

Type Description
list[EntryResource]

A list of resources from the MongoDB (mapped to pydantic models).

Source code in optimade_gateway/mongo/collection.py
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
async def get_multiple(self, **criteria: Any) -> list[EntryResource]:
    """Get a list of resources based on criteria

    Warning:
        This is not to be used for creating a REST API response,
        but is rather a utility function to easily retrieve a list of resources.

    Parameters:
        **criteria: Already handled/parsed URL query parameters.

    Returns:
        A list of resources from the MongoDB (mapped to pydantic models).

    """
    criteria = criteria or {}

    results = []
    async for document in self.collection.find(**self._valid_find_keys(**criteria)):
        results.append(self.resource_cls(**self.resource_mapper.map_back(document)))

    return results

get_one(**criteria) async

Get one resource based on criteria

Warning

This is not to be used for creating a REST API response, but is rather a utility function to easily retrieve a single resource.

Parameters:

Name Type Description Default
**criteria Any

Already handled/parsed URL query parameters.

{}

Returns:

Type Description
EntryResource

A single resource from the MongoDB (mapped to pydantic models).

Source code in optimade_gateway/mongo/collection.py
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
async def get_one(self, **criteria: Any) -> EntryResource:
    """Get one resource based on criteria

    Warning:
        This is not to be used for creating a REST API response,
        but is rather a utility function to easily retrieve a single resource.

    Parameters:
        **criteria: Already handled/parsed URL query parameters.

    Returns:
        A single resource from the MongoDB (mapped to pydantic models).

    """
    criteria = criteria or {}

    return self.resource_cls(
        **self.resource_mapper.map_back(
            await self.collection.find_one(**self._valid_find_keys(**criteria))
        )
    )

handle_query_params(params)

Parse and interpret the backend-agnostic query parameter models into a dictionary that can be used by the specific backend.

Note

Currently this method returns the pymongo interpretation of the parameters, which will need modification for modified for other backends.

Parameters:

Name Type Description Default
params EntryListingQueryParams | SingleEntryQueryParams

The initialized query parameter model from the server.

required

Raises:

Type Description
Forbidden

If too large of a page limit is provided.

BadRequest

If an invalid request is made, e.g., with incorrect fields or response format.

Returns:

Type Description
dict[str, Any]

A dictionary representation of the query parameters.

Source code in optimade_gateway/mongo/collection.py
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
def handle_query_params(
    self, params: EntryListingQueryParams | SingleEntryQueryParams
) -> dict[str, Any]:
    """Parse and interpret the backend-agnostic query parameter models into a
    dictionary that can be used by the specific backend.

    Note:
        Currently this method returns the pymongo interpretation of the parameters,
        which will need modification for modified for other backends.

    Parameters:
        params: The initialized query parameter model from the server.

    Raises:
        Forbidden: If too large of a page limit is provided.
        BadRequest: If an invalid request is made, e.g., with incorrect fields
            or response format.

    Returns:
        A dictionary representation of the query parameters.

    """
    raise NotImplementedError(
        "This method cannot be used with this class and is a remnant from the "
        "parent class. Use instead the asynchronous method "
        "`ahandle_query_params(params: Union[EntryListingQueryParams, "
        "SingleEntryQueryParams])`."
    )