1
1
"""FastAPI Users database adapter for Beanie."""
2
- from typing import Any , Dict , Generic , Optional , Type , TypeVar
2
+
3
+ from typing import Any , Generic , Optional , TypeVar
3
4
4
5
import bson .errors
5
6
from beanie import Document , PydanticObjectId
@@ -23,9 +24,12 @@ class BeanieBaseUser(BaseModel):
23
24
class Settings :
24
25
email_collation = Collation ("en" , strength = 2 )
25
26
indexes = [
26
- IndexModel ("email" , unique = True ),
27
+ IndexModel ("email" ),
27
28
IndexModel (
28
- "email" , name = "case_insensitive_email_index" , collation = email_collation
29
+ "email" ,
30
+ name = "case_insensitive_email_index" ,
31
+ collation = email_collation ,
32
+ unique = True ,
29
33
),
30
34
]
31
35
@@ -59,8 +63,8 @@ class BeanieUserDatabase(
59
63
60
64
def __init__ (
61
65
self ,
62
- user_model : Type [UP_BEANIE ],
63
- oauth_account_model : Optional [Type [BaseOAuthAccount ]] = None ,
66
+ user_model : type [UP_BEANIE ],
67
+ oauth_account_model : Optional [type [BaseOAuthAccount ]] = None ,
64
68
):
65
69
self .user_model = user_model
66
70
self .oauth_account_model = oauth_account_model
@@ -90,13 +94,13 @@ async def get_by_oauth_account(
90
94
}
91
95
)
92
96
93
- async def create (self , create_dict : Dict [str , Any ]) -> UP_BEANIE :
97
+ async def create (self , create_dict : dict [str , Any ]) -> UP_BEANIE :
94
98
"""Create a user."""
95
99
user = self .user_model (** create_dict )
96
100
await user .create ()
97
101
return user
98
102
99
- async def update (self , user : UP_BEANIE , update_dict : Dict [str , Any ]) -> UP_BEANIE :
103
+ async def update (self , user : UP_BEANIE , update_dict : dict [str , Any ]) -> UP_BEANIE :
100
104
"""Update a user."""
101
105
for key , value in update_dict .items ():
102
106
setattr (user , key , value )
@@ -108,7 +112,7 @@ async def delete(self, user: UP_BEANIE) -> None:
108
112
await user .delete ()
109
113
110
114
async def add_oauth_account (
111
- self , user : UP_BEANIE , create_dict : Dict [str , Any ]
115
+ self , user : UP_BEANIE , create_dict : dict [str , Any ]
112
116
) -> UP_BEANIE :
113
117
"""Create an OAuth account and add it to the user."""
114
118
if self .oauth_account_model is None :
@@ -121,7 +125,7 @@ async def add_oauth_account(
121
125
return user
122
126
123
127
async def update_oauth_account (
124
- self , user : UP_BEANIE , oauth_account : OAP , update_dict : Dict [str , Any ]
128
+ self , user : UP_BEANIE , oauth_account : OAP , update_dict : dict [str , Any ]
125
129
) -> UP_BEANIE :
126
130
"""Update an OAuth account on a user."""
127
131
if self .oauth_account_model is None :
0 commit comments