motorhead.service
BaseService
Bases: Generic[TInsert, TUpdate, TPrimaryKey]
Base service with typed utility methods for MongoDB (motor
asyncio).
The service provides a limited subset of motor
's capabilities.
For undocumented keyword arguments, please see the motor
or pymongo
documentation.
For delete rule support, see DeleteRule
, delete_many()
, and delete_one()
.
For insert and update data validation, see Validator
, _validate_insert()
, and _validate_update()
Class attributes
collection_name: The name of the collection the service operates on. Must be set by subclasses.
collection_options: Optional CollectionOptions
dict.
Source code in motorhead/service.py
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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 |
|
client: AgnosticClient
property
The database client.
collection: AgnosticCollection
property
The collection instance of the service.
collection_name: str
class-attribute
The name of the collection the service operates on. Must be set by subclasses.
collection_options: CollectionOptions | None = None
class-attribute
Optional CollectionOptions
dict.
indexes: dict[str, IndexData] | None = None
class-attribute
The full description of the indexes (if any) of the collection.
service_config: ServiceConfig = {}
class-attribute
Service configuration. Partial override of parent class configuration is not supported.
__init__(database)
Initialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
database
|
AgnosticDatabase
|
The database driver. |
required |
Source code in motorhead/service.py
_convert_for_insert(data)
async
Converts the given piece of the into its database representation.
The default implementation is self._mongo_dump(data)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
TInsert
|
The data to be inserted. |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
The MongoDB-compatible, insertable data. |
Raises:
Type | Description |
---|---|
Exception
|
If the data is invalid. |
Source code in motorhead/service.py
_convert_for_update(data)
async
Converts the given piece of data into an update object.
The default implementation is {"$set": self._mongo_dump(data)}
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
TUpdate
|
The update data. |
required |
Returns:
Type | Description |
---|---|
UpdateObject
|
The MongoDB-compatible update object. |
Raises:
Type | Description |
---|---|
Exception
|
If the data is invalid. |
Source code in motorhead/service.py
_create_collection()
Creates a new AgnosticCollection
instance for the service.
_delete_rules()
Generator that yields the delete rules that are registered on this service
in the order they are present in __class__.__dict__
.
Source code in motorhead/service.py
_get_session_context_manager(session)
Returns a session context manager
Source code in motorhead/service.py
_has_delete_rules()
Returns whether the service has any delete rules.
_mongo_dump(data, *, exclude_unset)
Dumps the given model instance for consumption by MongoDB.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
BaseModel
|
The model instance to dump. |
required |
exclude_unset
|
bool
|
Whether to exclude fields that have not been explicitly set on the data. |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
The MongoDB-compatible, dumped dictionary. |
Source code in motorhead/service.py
_prepare_for_insert(data, query=None)
async
Validates the given piece of data and converts it into its database representation if validation was successful.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
TInsert
|
The data to be inserted. |
required |
query
|
ClauseOrMongoQuery | None
|
Query that matches the documents that will be updated. |
None
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
The MongoDB-compatible, insertable data. |
Raises:
Type | Description |
---|---|
Exception
|
If the data is invalid. |
Source code in motorhead/service.py
_prepare_for_update(data, query)
async
Validates the given piece of data and converts it into an update object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
TUpdate
|
The update data. |
required |
query
|
ClauseOrMongoQuery | None
|
Query that matches the documents that will be updated. |
required |
Returns:
Type | Description |
---|---|
UpdateObject
|
The MongoDB-compatible update object. |
Raises:
Type | Description |
---|---|
Exception
|
If the data is invalid. |
Source code in motorhead/service.py
_validate_deny_delete(session, ids)
async
Executes all "deny" delete rules.
See DeleteRule
for more information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
AgnosticClientSession
|
The current database session. |
required |
ids
|
Sequence[TPrimaryKey]
|
The IDs that will be removed. |
required |
Raises:
Type | Description |
---|---|
DeleteError
|
if one of the executed delete rules prevent the operation. |
Source code in motorhead/service.py
_validate_insert(data, query=None)
async
Validates the given piece of data for insertion by executing all insert validators.
See Validator
for more information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
ClauseOrMongoQuery | None
|
Query that matches the documents that will be updated. |
None
|
data
|
TInsert
|
The data to validate. |
required |
Raises:
Type | Description |
---|---|
ValidationError
|
If validation failed. |
Source code in motorhead/service.py
_validate_post_delete(session, ids)
async
Executes all "post" delete rules.
See DeleteRule
for more information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
AgnosticClientSession
|
The current database session. |
required |
ids
|
Sequence[TPrimaryKey]
|
The IDs that will be removed. |
required |
Raises:
Type | Description |
---|---|
DeleteError
|
if one of the executed delete rules fail. |
Source code in motorhead/service.py
_validate_pre_delete(session, ids)
async
Executes all "pre" delete rules.
See DeleteRule
for more information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
AgnosticClientSession
|
The current database session. |
required |
ids
|
Sequence[TPrimaryKey]
|
The IDs that will be removed. |
required |
Raises:
Type | Description |
---|---|
DeleteError
|
if one of the executed delete rules fail. |
Source code in motorhead/service.py
_validate_update(data, query=None)
async
Validates the given piece of data for update by executing all update validators.
See Validator
for more information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
TUpdate
|
The data to validate. |
required |
query
|
ClauseOrMongoQuery | None
|
Query that matches the documents that will be updated. |
None
|
Raises:
Type | Description |
---|---|
ValidationError
|
If validation failed. |
Source code in motorhead/service.py
_validators()
Generator that yields the validators that are registered on this service
in the order they are present in __class__.__dict__
.
Source code in motorhead/service.py
aggregate(pipeline, session=None, **kwargs)
Performs an aggregation.
For undocumented keyword arguments, see the documentation of
pymongo.collection.Collection.aggregate()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pipeline
|
Sequence[dict[str, Any]]
|
The aggregation pipeline. |
required |
session
|
AgnosticClientSession | None
|
An optional session to use. |
None
|
Source code in motorhead/service.py
count_documents(query=None, *, options=None)
async
Returns the number of documents that match the given query.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
ClauseOrMongoQuery | None
|
The query object. |
None
|
options
|
FindOptions | None
|
Query options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
int
|
The number of matching documents. |
Source code in motorhead/service.py
create(data, *, options=None)
async
Inserts the given data into the collection and immediately queries and returns the created document.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
TInsert
|
The data to be inserted. |
required |
options
|
InsertOneOptions | None
|
Insert options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
The result of the operation. |
Raises:
Type | Description |
---|---|
ServiceException
|
If the service failed to access the newly created document. |
Exception
|
If the data is invalid. |
Source code in motorhead/service.py
create_index(keys, *, name, unique=False, session=None, background=False, collation=None, sparse=False, **kwargs)
async
Creates the specified index on collection of the service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keys
|
str | Sequence[tuple[str, int | str | Mapping[str, Any]]]
|
Index description. |
required |
name
|
str
|
Index name. |
required |
unique
|
bool
|
Whether to create a uniqueness constraint on the index. |
False
|
session
|
AgnosticClientSession | None
|
An optional session to use. |
None
|
background
|
bool
|
Whether the index should be created in the background. |
False
|
collation
|
Collation | None
|
A |
None
|
sparse
|
bool
|
Whether to omit documents from the index that doesn't have the indexed field. |
False
|
Source code in motorhead/service.py
create_indexes(session=None)
async
Creates all declared indexes (see cls.indexes) on the collection of the service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
AgnosticClientSession | None
|
An optional session to use. |
None
|
Source code in motorhead/service.py
delete_by_id(id, *, options=None)
async
Deletes the document with the given ID.
This method is just a convenience wrapper around delete_one()
, see that
method for more details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id
|
TPrimaryKey
|
The ID of the document to delete. |
required |
options
|
DeleteOptions | None
|
Delete options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
DeleteResult
|
The result of the operation. |
Source code in motorhead/service.py
delete_many(query, *, options=None)
async
The default delete_many()
implementation of the service.
The method enforces delete rules and executes the operation as follows:
- Enforce
"deny"
delete rules. - Enforce
"pre"
delete rules. - Execute the delete operation.
- Enforce
"post"
delete rules.
See DeleteRule
for more information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
ClauseOrMongoQuery | None
|
Query object that matches the documents that should be deleted. |
required |
options
|
DeleteOptions | None
|
Delete options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
DeleteResult
|
The result of the operation. |
Source code in motorhead/service.py
delete_one(query, *, options=None)
async
The default delete_one()
implementation of the service.
The method enforces delete rules and executes the operation as follows:
- Enforce
"deny"
delete rules. - Enforce
"pre"
delete rules. - Execute the delete operation.
- Enforce
"post"
delete rules.
See DeleteRule
for more information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
ClauseOrMongoQuery | None
|
Query object that matches the document that should be deleted. |
required |
options
|
DeleteOptions | None
|
Delete options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
DeleteResult
|
The result of the operation. |
Source code in motorhead/service.py
drop_index(index_or_name, session=None, **kwargs)
async
Drops the given index from the collection of the service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index_or_name
|
str | Sequence[tuple[str, int | str | Mapping[str, Any]]]
|
The index to drop. |
required |
session
|
AgnosticClientSession | None
|
An optional session to use. |
None
|
Source code in motorhead/service.py
drop_indexes(session=None, **kwargs)
async
Drops all indexes from the collection of the service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
AgnosticClientSession | None
|
An optional session to use. |
None
|
Source code in motorhead/service.py
exists(id, *, options=None)
async
Returns whether the document with the given ID exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id
|
TPrimaryKey
|
The ID of the document to check. |
required |
options
|
FindOptions | None
|
Query options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
bool
|
Whether the document with the given ID exists. |
Source code in motorhead/service.py
find(query=None, projection=None, *, options=None)
The default find()
implementation of the service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
ClauseOrMongoQuery | None
|
The query object. |
None
|
projection
|
MongoProjection | None
|
Optional projection. |
None
|
options
|
FindOptions | None
|
Query options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
AgnosticCursor
|
An async database cursor. |
Source code in motorhead/service.py
find_ids(query, *, session=None)
async
Returns the IDs of all documents that match the given query.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
ClauseOrMongoQuery | None
|
The query object. |
required |
session
|
AgnosticClientSession | None
|
An optional database session to use. |
None
|
Returns:
Type | Description |
---|---|
list[TPrimaryKey]
|
The IDs of all matching documents. |
Source code in motorhead/service.py
find_one(query=None, projection=None, *, options=None)
async
The default find_one()
implementation of the service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
ClauseOrMongoQuery | None
|
The query object. |
None
|
projection
|
MongoProjection | None
|
Optional projection. |
None
|
options
|
FindOptions | None
|
Query options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
dict[str, Any] | None
|
A single matching document or |
Source code in motorhead/service.py
get_by_id(id, projection=None, *, options=None)
async
Returns the document with the given ID if it exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id
|
TPrimaryKey
|
The ID of the queried document. |
required |
projection
|
MongoProjection | None
|
Optional projection. |
None
|
options
|
FindOptions | None
|
Query options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
dict[str, Any] | None
|
The queried document if such a document exists. |
Source code in motorhead/service.py
get_objectid_fields(model)
classmethod
Returns the names of all ObjectId
fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
type[BaseModel]
|
The model to collect |
required |
Source code in motorhead/service.py
insert_many(data, *, options=None)
async
Inserts all the given objects into the collection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Iterable[TInsert]
|
The documents to be inserted. |
required |
options
|
InsertManyOptions | None
|
Insert options, see the arguments of |
None
|
Returns The result of the operation.
Raises:
Type | Description |
---|---|
Exception
|
If any of the documents is invalid. |
Source code in motorhead/service.py
insert_one(data, *, options=None)
async
Inserts the given data into the collection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
TInsert
|
The data to be inserted. |
required |
options
|
InsertOneOptions | None
|
Insert options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
InsertOneResult
|
The result of the operation. |
Raises:
Type | Description |
---|---|
Exception
|
If the data is invalid. |
Source code in motorhead/service.py
list_indexes(session=None, **kwargs)
Returns a cursor over the indexes of the collection of the service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
AgnosticClientSession | None
|
An optional session to use. |
None
|
Source code in motorhead/service.py
supports_transactions()
async
Queries the database if it supports transactions or not.
Note: transactions are only supported in replica set configuration.
Source code in motorhead/service.py
update(id, changes, *, options=None)
async
Updates the document with the given ID and immediately queries and returns the updated document.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id
|
TPrimaryKey
|
The ID of the document to update. |
required |
changes
|
TUpdate
|
The changes to make. |
required |
options
|
UpdateOneOptions | None
|
Update options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
The updated document. |
Raises:
Type | Description |
---|---|
ServiceException
|
If the service failed to query the updated document. |
Exception
|
If the data is invalid. |
Source code in motorhead/service.py
update_by_id(id, changes, *, options=None)
async
Updates the document with the given ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id
|
TPrimaryKey
|
The ID of the document to update. |
required |
changes
|
TUpdate
|
The changes to make. |
required |
options
|
UpdateOneOptions | None
|
Update options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
UpdateResult
|
The result of the operation. |
Raises:
Type | Description |
---|---|
Exception
|
If the data is invalid. |
Source code in motorhead/service.py
update_many(query, changes, *, options=None)
async
The default delete_many()
implementation of the service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
ClauseOrMongoQuery | None
|
Query that matches the documents that should be updated. |
required |
changes
|
TUpdate
|
The changes to make. |
required |
options
|
UpdateManyOptions | None
|
Update options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
UpdateResult
|
The result of the operation. |
Raises:
Type | Description |
---|---|
Exception
|
If the data is invalid. |
Source code in motorhead/service.py
update_one(query, changes, *, options=None)
async
The default delete_one()
implementation of the service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
ClauseOrMongoQuery | None
|
Query that matches the document that should be updated. |
required |
changes
|
TUpdate
|
The changes to make. |
required |
options
|
UpdateOneOptions | None
|
Update options, see the arguments of |
None
|
Returns:
Type | Description |
---|---|
UpdateResult
|
The result of the operation. |
Raises:
Type | Description |
---|---|
Exception
|
If the data is invalid. |
Source code in motorhead/service.py
Service
Bases: BaseService[TInsert, TUpdate, ObjectId]
Base service for documents with an ObjectId
_id
.
Source code in motorhead/service.py
ServiceConfig
Bases: TypedDict
Service configuration.
Source code in motorhead/service.py
exclude_unset_on_insert: bool
instance-attribute
Whether to exclude fields that have not been explicitly set on the data during insert operations.
ServiceException
_default_service_config
Default service configuration.
Source code in motorhead/service.py
exclude_unset_on_insert: bool = False
class-attribute
See ServiceConfig.exclude_unset_on_insert
for details.