Database Service
MongoDB database service for person data management.
Database Class
app.services.database.Database
MongoDB database service for person data management.
Manages connections to MongoDB, handles CRUD operations for Person objects, and maintains a cached reference to the user/patient (role=USER) person.
Attributes:
| Name | Type | Description |
|---|---|---|
current_client |
Optional[MongoClient]
|
Class-level MongoDB client instance. |
url |
str
|
MongoDB connection URL. |
name_db |
str
|
Database name. |
collection_name |
str
|
Collection name within the database. |
patient |
Optional[Person]
|
Cached reference to the user/patient person. |
Source code in backend/app/services/database.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 | |
Attributes
is_connected
property
Check if database connection is active.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if connection is active and responsive, False otherwise. |
Functions
__init__
Initialize Database instance and establish connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
MongoDB connection URL. |
required |
name
|
str
|
Database name. |
required |
collection
|
str
|
Collection name. |
required |
Raises:
| Type | Description |
|---|---|
ConnectionFailure
|
If connection to MongoDB fails. |
ValueError
|
If connection URL conflicts with existing connection. |
Source code in backend/app/services/database.py
add_person
Add a new person to the database.
Inserts person document into MongoDB. Ensures only one USER role person exists. Updates person.id with the inserted document ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
person
|
Person
|
Person object to insert. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Person] | None
|
Optional[Person]: Person object with assigned ID if successful, None otherwise. |
Raises:
| Type | Description |
|---|---|
DuplicateKeyError
|
If duplicate key constraint violation occurs. |
WriteConcernError
|
If write operation fails. |
ConnectionFailure
|
If database connection is lost. |
Source code in backend/app/services/database.py
check_patient_existence
Check if a patient (role=USER) exists in the database.
Returns cached patient if available, otherwise queries the database.
Returns:
| Type | Description |
|---|---|
Optional[Person] | None
|
Optional[Person]: Patient person if found, None otherwise. |
Source code in backend/app/services/database.py
close_connection
classmethod
Close MongoDB connection and reset client.
Closes the shared MongoDB client connection and sets it to None.
Source code in backend/app/services/database.py
convert_to_objectid
staticmethod
Convert string ID to MongoDB ObjectId.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_string
|
str
|
String representation of MongoDB ObjectId. |
required |
Returns:
| Type | Description |
|---|---|
Optional[ObjectId]
|
Optional[ObjectId]: ObjectId if valid, None otherwise. |
Source code in backend/app/services/database.py
drop_database
Drop the entire database and close connection.
⚠️ Deprecated: This method is not currently used in the application. Reserved for testing and maintenance purposes. Use with extreme caution as it permanently deletes all data.
Permanently deletes the database and all its collections. Resets patient cache and closes connection.
Source code in backend/app/services/database.py
get_all_encodings
Extract all face encodings from all people in the database.
⚠️ Deprecated: This method was used with the old face_recognition library.
The current implementation uses FaceEngine which directly processes Person objects
with encodings stored in the encoding field. Use get_all_people() instead and
access encodings directly from Person objects.
Returns:
| Type | Description |
|---|---|
tuple[list[str], list[ndarray]]
|
tuple[list[str], list[np.ndarray]]: (known_ids, known_encodings) where known_ids is a list of person IDs and known_encodings is a list of numpy arrays representing face embeddings. |
Source code in backend/app/services/database.py
get_all_people
Retrieve all people from the database.
Uses projection to load only necessary fields for performance optimization.
Returns:
| Type | Description |
|---|---|
list[Person]
|
list[Person]: List of all Person objects in the database. |
Source code in backend/app/services/database.py
get_collection
Get MongoDB collection instance.
Returns:
| Type | Description |
|---|---|
Collection | None
|
pymongo.collection.Collection | None: MongoDB collection instance, or None if connection fails. |
Source code in backend/app/services/database.py
get_connection
classmethod
Get or create MongoDB client connection.
Creates a new connection if none exists, or validates existing connection. Uses singleton pattern to share connection across instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
MongoDB connection URL. |
required |
Returns:
| Type | Description |
|---|---|
MongoClient | None
|
pymongo.MongoClient | None: MongoDB client instance, or None if URL is invalid. |
Raises:
| Type | Description |
|---|---|
ConnectionFailure
|
If connection fails. |
ConfigurationError
|
If connection configuration is invalid. |
ValueError
|
If URL conflicts with existing connection. |
Source code in backend/app/services/database.py
get_person
Retrieve a person by ID.
⚠️ Deprecated: This method is not currently used in the application.
The application primarily works with get_all_people() to load all persons
for face recognition. Consider implementing REST API endpoints if individual
person retrieval is needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
person_id
|
str
|
Person's MongoDB ObjectId as string. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Person]
|
Optional[Person]: Person object if found, None otherwise. |
Source code in backend/app/services/database.py
remove_person
Remove a person from the database by ID.
⚠️ Deprecated: This method is not currently used in the application. Consider implementing proper deletion endpoints if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
person_id
|
str
|
Person's MongoDB ObjectId as string. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if person was deleted, False otherwise. |
Source code in backend/app/services/database.py
update_people
Update multiple people in the database.
⚠️ Deprecated: This method is not currently used in the application.
Person management is primarily done through add_person() during initial setup.
Consider implementing batch update functionality via REST API if needed.
Adds new people (id=None) or updates existing ones based on their ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
people
|
list
|
List of Person objects to add or update. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of successfully processed people. |
Source code in backend/app/services/database.py
update_person
Update a person's data in the database.
⚠️ Deprecated: This method is only used internally by update_people() which
itself is not used. Consider implementing REST API endpoints for person updates
if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
person_id
|
str
|
Person's MongoDB ObjectId as string. |
required |
update_data
|
Person | dict
|
Person object or dictionary with update fields. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Person]
|
Optional[Person]: Updated Person object if successful, None otherwise. |