Functions¶
DynamoDB utility functions.
convert_decimals_to_floats ¶
convert_decimals_to_floats(item, in_place=True)
Convert all Decimal values in a dictionary to floats.
DynamoDB returns numeric values as Decimal objects. This function recursively converts them to standard Python floats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Dict[str, Any]
|
Dictionary potentially containing Decimal values. |
required |
in_place
|
bool
|
If True, modify the dictionary in place. If False, create a copy first. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
The dictionary with all Decimals converted to floats. |
Source code in src/aibs_informatics_aws_utils/dynamodb/functions.py
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 | |
convert_floats_to_decimals ¶
convert_floats_to_decimals(item, in_place=True)
Convert all float values in a dictionary to Decimals.
DynamoDB requires numeric values to be Decimal objects. This function recursively converts Python floats to Decimals for storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Dict[str, Any]
|
Dictionary potentially containing float values. |
required |
in_place
|
bool
|
If True, modify the dictionary in place. If False, create a copy first. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
The dictionary with all floats converted to Decimals. |
Source code in src/aibs_informatics_aws_utils/dynamodb/functions.py
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 | |
execute_partiql_statement ¶
execute_partiql_statement(statement, region=None)
Execute a PartiQL statement against DynamoDB.
Handles pagination automatically to retrieve all results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statement
|
str
|
The PartiQL statement to execute. |
required |
region
|
Optional[str]
|
AWS region. Defaults to None (uses default region). |
None
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of items returned by the statement. |
Source code in src/aibs_informatics_aws_utils/dynamodb/functions.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
table_as_resource ¶
table_as_resource(table, region=None)
Get a DynamoDB Table resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str
|
Name of the table. |
required |
region
|
Optional[str]
|
AWS region. Defaults to None (uses default region). |
None
|
Returns:
| Type | Description |
|---|---|
|
A boto3 DynamoDB Table resource. |
Source code in src/aibs_informatics_aws_utils/dynamodb/functions.py
450 451 452 453 454 455 456 457 458 459 460 461 | |
table_delete_item ¶
table_delete_item(
table_name,
key,
condition_expression=None,
return_values="NONE",
**kwargs
)
Delete an item from a DynamoDB table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Name of the table. |
required |
key
|
Mapping[str, Any]
|
Dictionary of key attribute(s) identifying the item to delete. |
required |
condition_expression
|
Optional[ConditionBase]
|
Optional condition that must be satisfied for the delete to succeed. |
None
|
return_values
|
Literal['NONE', 'ALL_OLD']
|
What to return after the delete. Options:
|
'NONE'
|
**kwargs
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
The deleted item's attributes if |
Source code in src/aibs_informatics_aws_utils/dynamodb/functions.py
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 | |
table_get_item ¶
table_get_item(table_name, key, attrs=None)
Get a single item from a DynamoDB table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Name of the table. |
required |
key
|
Mapping[str, Any]
|
Dictionary of key attribute(s) identifying the item to get. |
required |
attrs
|
Optional[str]
|
Optional projection expression specifying which attributes to retrieve. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
The item if found, None otherwise. |
Source code in src/aibs_informatics_aws_utils/dynamodb/functions.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
table_get_items ¶
table_get_items(table_name, keys, attrs=None, region=None)
Batch get multiple items from a DynamoDB table.
Handles pagination automatically when more than 100 keys are provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Name of the table. |
required |
keys
|
List[Mapping[str, Any]]
|
List of key dictionaries identifying the items to get. |
required |
attrs
|
Optional[str]
|
Optional projection expression specifying which attributes to retrieve. |
None
|
region
|
Optional[str]
|
AWS region. Defaults to None (uses default region). |
None
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of items found. |
Source code in src/aibs_informatics_aws_utils/dynamodb/functions.py
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 | |
table_get_key_schema ¶
table_get_key_schema(table_name)
Get the key schema for a DynamoDB table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Name of the table. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict[str, str]
|
Mapping of key type to attribute name. |
|
Example |
Dict[str, str]
|
|
Source code in src/aibs_informatics_aws_utils/dynamodb/functions.py
411 412 413 414 415 416 417 418 419 420 421 422 | |
table_put_item ¶
table_put_item(
table_name, item, condition_expression=None, **kwargs
)
Put an item into a DynamoDB table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Name of the table. |
required |
item
|
Dict[str, Any]
|
Dictionary representing the item to put. |
required |
condition_expression
|
Optional[ConditionBase]
|
Optional condition that must be satisfied for the put to succeed. |
None
|
**kwargs
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
PutItemOutputTableTypeDef
|
Response from the put_item operation. |
Source code in src/aibs_informatics_aws_utils/dynamodb/functions.py
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 | |
table_query ¶
table_query(
table_name,
key_condition_expression,
index_name=None,
filter_expression=None,
region=None,
consistent_read=False,
)
Query a DynamoDB table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Name of the table. |
required |
key_condition_expression
|
ConditionBase
|
Key condition expression for the query. |
required |
index_name
|
Optional[str]
|
Index name. Defaults to None (query the main table). |
None
|
filter_expression
|
Optional[ConditionBase]
|
Filter expression to apply after the query. Defaults to None. |
None
|
region
|
Optional[str]
|
AWS region. Defaults to None (uses default region). |
None
|
consistent_read
|
bool
|
Whether a strongly consistent read should be used. Defaults to False. Note: Strongly consistent reads are not supported for global secondary indexes. |
False
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of items matching the query. |
Source code in src/aibs_informatics_aws_utils/dynamodb/functions.py
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 | |
table_scan ¶
table_scan(
table_name,
index_name=None,
filter_expression=None,
region=None,
consistent_read=False,
)
Scan a DynamoDB table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Name of the table. |
required |
index_name
|
Optional[str]
|
Index name. Defaults to None (scan the main table). |
None
|
filter_expression
|
Optional[ConditionBase]
|
Filter expression to apply. Defaults to None. |
None
|
region
|
Optional[str]
|
AWS region. Defaults to None (uses default region). |
None
|
consistent_read
|
bool
|
Whether a strongly consistent read should be used. Defaults to False. Note: Strongly consistent reads are not supported for secondary indexes. |
False
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of items from the scan. |
Source code in src/aibs_informatics_aws_utils/dynamodb/functions.py
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 | |
table_update_item ¶
table_update_item(
table_name,
key,
attributes,
return_values="NONE",
**kwargs
)
Update an item in a DynamoDB table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Name of the table. |
required |
key
|
Mapping[str, Any]
|
Dictionary of key attribute(s) identifying the item to update. |
required |
attributes
|
Mapping[str, Any]
|
Dictionary of attributes to update. |
required |
return_values
|
Literal['NONE', 'ALL_OLD', 'UPDATED_OLD', 'ALL_NEW', 'UPDATED_NEW']
|
What to return after the update. Options:
|
'NONE'
|
**kwargs
|
Additional arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
The attributes based on |
Source code in src/aibs_informatics_aws_utils/dynamodb/functions.py
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 | |