Spaces:
Running
Running
File size: 61,320 Bytes
2a0bc63 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 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 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 |
# Copyright DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import logging
from types import TracebackType
from typing import Any, Dict, List, Optional, Type, Union, TYPE_CHECKING
from astrapy.core.db import AstraDB, AsyncAstraDB
from astrapy.exceptions import (
CollectionAlreadyExistsException,
DataAPIFaultyResponseException,
DevOpsAPIException,
MultiCallTimeoutManager,
recast_method_sync,
recast_method_async,
base_timeout_info,
)
from astrapy.cursors import AsyncCommandCursor, CommandCursor
from astrapy.info import (
DatabaseInfo,
CollectionDescriptor,
CollectionVectorServiceOptions,
)
from astrapy.admin import parse_api_endpoint, fetch_database_info
if TYPE_CHECKING:
from astrapy.collection import AsyncCollection, Collection
from astrapy.admin import AstraDBDatabaseAdmin
logger = logging.getLogger(__name__)
def _validate_create_collection_options(
dimension: Optional[int],
metric: Optional[str],
service: Optional[Union[CollectionVectorServiceOptions, Dict[str, Any]]],
indexing: Optional[Dict[str, Any]],
default_id_type: Optional[str],
additional_options: Optional[Dict[str, Any]],
) -> None:
if additional_options:
if "vector" in additional_options:
raise ValueError(
"`additional_options` dict parameter to create_collection "
"cannot have a `vector` key. Please use the specific "
"method parameter."
)
if "indexing" in additional_options:
raise ValueError(
"`additional_options` dict parameter to create_collection "
"cannot have a `indexing` key. Please use the specific "
"method parameter."
)
if "defaultId" in additional_options and default_id_type is not None:
# this leaves the workaround to pass more info in the defaultId
# should that become part of the specs:
raise ValueError(
"`additional_options` dict parameter to create_collection "
"cannot have a `defaultId` key when passing the "
"`default_id_type` parameter as well."
)
is_vector: bool
if service is not None or dimension is not None:
is_vector = True
else:
is_vector = False
if not is_vector and metric is not None:
raise ValueError(
"Cannot specify `metric` for non-vector collections in the "
"create_collection method."
)
class Database:
"""
A Data API database. This is the entry-point object for doing database-level
DML, such as creating/deleting collections, and for obtaining Collection
objects themselves. This class has a synchronous interface.
A Database comes with an "API Endpoint", which implies a Database object
instance reaches a specific region (relevant point in case of multi-region
databases).
Args:
api_endpoint: the full "API Endpoint" string used to reach the Data API.
Example: "https://<database_id>-<region>.apps.astra.datastax.com"
token: an Access Token to the database. Example: "AstraCS:xyz..."
namespace: this is the namespace all method calls will target, unless
one is explicitly specified in the call. If no namespace is supplied
when creating a Database, the name "default_namespace" is set.
caller_name: name of the application, or framework, on behalf of which
the Data API calls are performed. This ends up in the request user-agent.
caller_version: version of the caller.
api_path: path to append to the API Endpoint. In typical usage, this
should be left to its default of "/api/json".
api_version: version specifier to append to the API path. In typical
usage, this should be left to its default of "v1".
Example:
>>> from astrapy import DataAPIClient
>>> my_client = astrapy.DataAPIClient("AstraCS:...")
>>> my_db = my_client.get_database_by_api_endpoint(
... "https://01234567-....apps.astra.datastax.com"
... )
Note:
creating an instance of Database does not trigger actual creation
of the database itself, which should exist beforehand. To create databases,
see the AstraDBAdmin class.
"""
def __init__(
self,
api_endpoint: str,
token: str,
*,
namespace: Optional[str] = None,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
api_path: Optional[str] = None,
api_version: Optional[str] = None,
) -> None:
self._astra_db = AstraDB(
token=token,
api_endpoint=api_endpoint,
api_path=api_path,
api_version=api_version,
namespace=namespace,
caller_name=caller_name,
caller_version=caller_version,
)
self._name: Optional[str] = None
def __getattr__(self, collection_name: str) -> Collection:
return self.get_collection(name=collection_name)
def __getitem__(self, collection_name: str) -> Collection:
return self.get_collection(name=collection_name)
def __repr__(self) -> str:
return (
f'{self.__class__.__name__}(api_endpoint="{self._astra_db.api_endpoint}", '
f'token="{self._astra_db.token[:12]}...", namespace="{self._astra_db.namespace}")'
)
def __eq__(self, other: Any) -> bool:
if isinstance(other, Database):
return self._astra_db == other._astra_db
else:
return False
def _copy(
self,
*,
api_endpoint: Optional[str] = None,
token: Optional[str] = None,
namespace: Optional[str] = None,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
api_path: Optional[str] = None,
api_version: Optional[str] = None,
) -> Database:
return Database(
api_endpoint=api_endpoint or self._astra_db.api_endpoint,
token=token or self._astra_db.token,
namespace=namespace or self._astra_db.namespace,
caller_name=caller_name or self._astra_db.caller_name,
caller_version=caller_version or self._astra_db.caller_version,
api_path=api_path or self._astra_db.api_path,
api_version=api_version or self._astra_db.api_version,
)
def with_options(
self,
*,
namespace: Optional[str] = None,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
) -> Database:
"""
Create a clone of this database with some changed attributes.
Args:
namespace: this is the namespace all method calls will target, unless
one is explicitly specified in the call. If no namespace is supplied
when creating a Database, the name "default_namespace" is set.
caller_name: name of the application, or framework, on behalf of which
the Data API calls are performed. This ends up in the request user-agent.
caller_version: version of the caller.
Returns:
a new `Database` instance.
Example:
>>> my_db_2 = my_db.with_options(
... namespace="the_other_namespace",
... caller_name="the_caller",
... caller_version="0.1.0",
... )
"""
return self._copy(
namespace=namespace,
caller_name=caller_name,
caller_version=caller_version,
)
def to_async(
self,
*,
api_endpoint: Optional[str] = None,
token: Optional[str] = None,
namespace: Optional[str] = None,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
api_path: Optional[str] = None,
api_version: Optional[str] = None,
) -> AsyncDatabase:
"""
Create an AsyncDatabase from this one. Save for the arguments
explicitly provided as overrides, everything else is kept identical
to this database in the copy.
Args:
api_endpoint: the full "API Endpoint" string used to reach the Data API.
Example: "https://<database_id>-<region>.apps.astra.datastax.com"
token: an Access Token to the database. Example: "AstraCS:xyz..."
namespace: this is the namespace all method calls will target, unless
one is explicitly specified in the call. If no namespace is supplied
when creating a Database, the name "default_namespace" is set.
caller_name: name of the application, or framework, on behalf of which
the Data API calls are performed. This ends up in the request user-agent.
caller_version: version of the caller.
api_path: path to append to the API Endpoint. In typical usage, this
should be left to its default of "/api/json".
api_version: version specifier to append to the API path. In typical
usage, this should be left to its default of "v1".
Returns:
the new copy, an `AsyncDatabase` instance.
Example:
>>> my_async_db = my_db.to_async()
>>> asyncio.run(my_async_db.list_collection_names())
"""
return AsyncDatabase(
api_endpoint=api_endpoint or self._astra_db.api_endpoint,
token=token or self._astra_db.token,
namespace=namespace or self._astra_db.namespace,
caller_name=caller_name or self._astra_db.caller_name,
caller_version=caller_version or self._astra_db.caller_version,
api_path=api_path or self._astra_db.api_path,
api_version=api_version or self._astra_db.api_version,
)
def set_caller(
self,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
) -> None:
"""
Set a new identity for the application/framework on behalf of which
the Data API calls are performed (the "caller").
Args:
caller_name: name of the application, or framework, on behalf of which
the Data API calls are performed. This ends up in the request user-agent.
caller_version: version of the caller.
Example:
>>> my_db.set_caller(caller_name="the_caller", caller_version="0.1.0")
"""
logger.info(f"setting caller to {caller_name}/{caller_version}")
self._astra_db.set_caller(
caller_name=caller_name,
caller_version=caller_version,
)
def info(self) -> DatabaseInfo:
"""
Additional information on the database as a DatabaseInfo instance.
Some of the returned properties are dynamic throughout the lifetime
of the database (such as raw_info["keyspaces"]). For this reason,
each invocation of this method triggers a new request to the DevOps API.
Example:
>>> my_db.info().region
'eu-west-1'
>>> my_db.info().raw_info['datacenters'][0]['dateCreated']
'2023-01-30T12:34:56Z'
Note:
see the DatabaseInfo documentation for a caveat about the difference
between the `region` and the `raw_info["region"]` attributes.
"""
logger.info("getting database info")
database_info = fetch_database_info(
self._astra_db.api_endpoint,
token=self._astra_db.token,
namespace=self.namespace,
)
if database_info is not None:
logger.info("finished getting database info")
return database_info
else:
raise DevOpsAPIException(
"Database is not in a supported environment for this operation."
)
@property
def id(self) -> str:
"""
The ID of this database.
Example:
>>> my_db.id
'01234567-89ab-cdef-0123-456789abcdef'
"""
parsed_api_endpoint = parse_api_endpoint(self._astra_db.api_endpoint)
if parsed_api_endpoint is not None:
return parsed_api_endpoint.database_id
else:
raise DevOpsAPIException(
"Database is not in a supported environment for this operation."
)
def name(self) -> str:
"""
The name of this database. Note that this bears no unicity guarantees.
Calling this method the first time involves a request
to the DevOps API (the resulting database name is then cached).
See the `info()` method for more details.
Example:
>>> my_db.name()
'the_application_database'
"""
if self._name is None:
self._name = self.info().name
return self._name
@property
def namespace(self) -> str:
"""
The namespace this database uses as target for all commands when
no method-call-specific namespace is specified.
Example:
>>> my_db.namespace
'the_keyspace'
"""
return self._astra_db.namespace
def get_collection(
self, name: str, *, namespace: Optional[str] = None
) -> Collection:
"""
Spawn a `Collection` object instance representing a collection
on this database.
Creating a `Collection` instance does not have any effect on the
actual state of the database: in other words, for the created
`Collection` instance to be used meaningfully, the collection
must exist already (for instance, it should have been created
previously by calling the `create_collection` method).
Args:
name: the name of the collection.
namespace: the namespace containing the collection. If no namespace
is specified, the general setting for this database is used.
Returns:
a `Collection` instance, representing the desired collection
(but without any form of validation).
Example:
>>> my_col = my_db.get_collection("my_collection")
>>> my_col.count_documents({}, upper_bound=100)
41
Note:
The attribute and indexing syntax forms achieve the same effect
as this method. In other words, the following are equivalent:
my_db.get_collection("coll_name")
my_db.coll_name
my_db["coll_name"]
"""
# lazy importing here against circular-import error
from astrapy.collection import Collection
_namespace = namespace or self._astra_db.namespace
return Collection(self, name, namespace=_namespace)
@recast_method_sync
def create_collection(
self,
name: str,
*,
namespace: Optional[str] = None,
dimension: Optional[int] = None,
metric: Optional[str] = None,
service: Optional[Union[CollectionVectorServiceOptions, Dict[str, Any]]] = None,
indexing: Optional[Dict[str, Any]] = None,
default_id_type: Optional[str] = None,
additional_options: Optional[Dict[str, Any]] = None,
check_exists: Optional[bool] = None,
max_time_ms: Optional[int] = None,
) -> Collection:
"""
Creates a collection on the database and return the Collection
instance that represents it.
This is a blocking operation: the method returns when the collection
is ready to be used. As opposed to the `get_collection` instance,
this method triggers causes the collection to be actually created on DB.
Args:
name: the name of the collection.
namespace: the namespace where the collection is to be created.
If not specified, the general setting for this database is used.
dimension: for vector collections, the dimension of the vectors
(i.e. the number of their components).
metric: the similarity metric used for vector searches.
Allowed values are `VectorMetric.DOT_PRODUCT`, `VectorMetric.EUCLIDEAN`
or `VectorMetric.COSINE` (default).
service: a dictionary describing a service for
embedding computation, e.g. `{"provider": "ab", "modelName": "xy"}`.
Alternatively, a CollectionVectorServiceOptions object to the same effect.
NOTE: This feature is under current development.
indexing: optional specification of the indexing options for
the collection, in the form of a dictionary such as
{"deny": [...]}
or
{"allow": [...]}
default_id_type: this sets what type of IDs the API server will
generate when inserting documents that do not specify their
`_id` field explicitly. Can be set to any of the values
`DefaultIdType.UUID`, `DefaultIdType.OBJECTID`,
`DefaultIdType.UUIDV6`, `DefaultIdType.UUIDV7`,
`DefaultIdType.DEFAULT`.
additional_options: any further set of key-value pairs that will
be added to the "options" part of the payload when sending
the Data API command to create a collection.
check_exists: whether to run an existence check for the collection
name before attempting to create the collection:
If check_exists is True, an error is raised when creating
an existing collection.
If it is False, the creation is attempted. In this case, for
preexisting collections, the command will succeed or fail
depending on whether the options match or not.
max_time_ms: a timeout, in milliseconds, for the underlying HTTP request.
Returns:
a (synchronous) `Collection` instance, representing the
newly-created collection.
Example:
>>> new_col = my_db.create_collection("my_v_col", dimension=3)
>>> new_col.insert_one({"name": "the_row"}, vector=[0.4, 0.5, 0.7])
InsertOneResult(raw_results=..., inserted_id='e22dd65e-...-...-...')
Note:
A collection is considered a vector collection if at least one of
`dimension` or `service` are provided and not null. In that case,
and only in that case, is `metric` an accepted parameter.
Note, moreover, that if passing both these parameters, then
the dimension must be compatible with the chosen service.
"""
_validate_create_collection_options(
dimension=dimension,
metric=metric,
service=service,
indexing=indexing,
default_id_type=default_id_type,
additional_options=additional_options,
)
_options = {
**(additional_options or {}),
**({"indexing": indexing} if indexing else {}),
**({"defaultId": {"type": default_id_type}} if default_id_type else {}),
}
timeout_manager = MultiCallTimeoutManager(overall_max_time_ms=max_time_ms)
if check_exists is None:
_check_exists = True
else:
_check_exists = check_exists
existing_names: List[str]
if _check_exists:
logger.info(f"checking collection existence for '{name}'")
existing_names = self.list_collection_names(
namespace=namespace,
max_time_ms=timeout_manager.remaining_timeout_ms(),
)
else:
existing_names = []
driver_db = self._astra_db.copy(namespace=namespace)
if name in existing_names:
raise CollectionAlreadyExistsException(
text=f"CollectionInvalid: collection {name} already exists",
namespace=driver_db.namespace,
collection_name=name,
)
service_dict: Optional[Dict[str, Any]]
if service is not None:
service_dict = service if isinstance(service, dict) else service.as_dict()
else:
service_dict = None
logger.info(f"creating collection '{name}'")
driver_db.create_collection(
name,
options=_options,
dimension=dimension,
metric=metric,
service_dict=service_dict,
timeout_info=timeout_manager.remaining_timeout_info(),
)
logger.info(f"finished creating collection '{name}'")
return self.get_collection(name, namespace=namespace)
@recast_method_sync
def drop_collection(
self,
name_or_collection: Union[str, Collection],
*,
max_time_ms: Optional[int] = None,
) -> Dict[str, Any]:
"""
Drop a collection from the database, along with all documents therein.
Args:
name_or_collection: either the name of a collection or
a `Collection` instance.
max_time_ms: a timeout, in milliseconds, for the underlying HTTP request.
Returns:
a dictionary in the form {"ok": 1} if the command succeeds.
Example:
>>> my_db.list_collection_names()
['a_collection', 'my_v_col', 'another_col']
>>> my_db.drop_collection("my_v_col")
{'ok': 1}
>>> my_db.list_collection_names()
['a_collection', 'another_col']
Note:
when providing a collection name, it is assumed that the collection
is to be found in the namespace set at database instance level.
"""
# lazy importing here against circular-import error
from astrapy.collection import Collection
if isinstance(name_or_collection, Collection):
_namespace = name_or_collection.namespace
_name: str = name_or_collection.name
logger.info(f"dropping collection '{_name}'")
dc_response = self._astra_db.copy(namespace=_namespace).delete_collection(
_name,
timeout_info=base_timeout_info(max_time_ms),
)
logger.info(f"finished dropping collection '{_name}'")
return dc_response.get("status", {}) # type: ignore[no-any-return]
else:
logger.info(f"dropping collection '{name_or_collection}'")
dc_response = self._astra_db.delete_collection(
name_or_collection,
timeout_info=base_timeout_info(max_time_ms),
)
logger.info(f"finished dropping collection '{name_or_collection}'")
return dc_response.get("status", {}) # type: ignore[no-any-return]
@recast_method_sync
def list_collections(
self,
*,
namespace: Optional[str] = None,
max_time_ms: Optional[int] = None,
) -> CommandCursor[CollectionDescriptor]:
"""
List all collections in a given namespace for this database.
Args:
namespace: the namespace to be inspected. If not specified,
the general setting for this database is assumed.
max_time_ms: a timeout, in milliseconds, for the underlying HTTP request.
Returns:
a `CommandCursor` to iterate over CollectionDescriptor instances,
each corresponding to a collection.
Example:
>>> ccur = my_db.list_collections()
>>> ccur
<astrapy.cursors.CommandCursor object at ...>
>>> list(ccur)
[CollectionDescriptor(name='my_v_col', options=CollectionOptions())]
>>> for coll_dict in my_db.list_collections():
... print(coll_dict)
...
CollectionDescriptor(name='my_v_col', options=CollectionOptions())
"""
if namespace:
_client = self._astra_db.copy(namespace=namespace)
else:
_client = self._astra_db
logger.info("getting collections")
gc_response = _client.get_collections(
options={"explain": True}, timeout_info=base_timeout_info(max_time_ms)
)
if "collections" not in gc_response.get("status", {}):
raise DataAPIFaultyResponseException(
text="Faulty response from get_collections API command.",
raw_response=gc_response,
)
else:
# we know this is a list of dicts, to marshal into "descriptors"
logger.info("finished getting collections")
return CommandCursor(
address=self._astra_db.base_url,
items=[
CollectionDescriptor.from_dict(col_dict)
for col_dict in gc_response["status"]["collections"]
],
)
@recast_method_sync
def list_collection_names(
self,
*,
namespace: Optional[str] = None,
max_time_ms: Optional[int] = None,
) -> List[str]:
"""
List the names of all collections in a given namespace of this database.
Args:
namespace: the namespace to be inspected. If not specified,
the general setting for this database is assumed.
max_time_ms: a timeout, in milliseconds, for the underlying HTTP request.
Returns:
a list of the collection names as strings, in no particular order.
Example:
>>> my_db.list_collection_names()
['a_collection', 'another_col']
"""
logger.info("getting collection names")
gc_response = self._astra_db.copy(namespace=namespace).get_collections(
timeout_info=base_timeout_info(max_time_ms)
)
if "collections" not in gc_response.get("status", {}):
raise DataAPIFaultyResponseException(
text="Faulty response from get_collections API command.",
raw_response=gc_response,
)
else:
# we know this is a list of strings
logger.info("finished getting collection names")
return gc_response["status"]["collections"] # type: ignore[no-any-return]
@recast_method_sync
def command(
self,
body: Dict[str, Any],
*,
namespace: Optional[str] = None,
collection_name: Optional[str] = None,
max_time_ms: Optional[int] = None,
) -> Dict[str, Any]:
"""
Send a POST request to the Data API for this database with
an arbitrary, caller-provided payload.
Args:
body: a JSON-serializable dictionary, the payload of the request.
namespace: the namespace to use. Requests always target a namespace:
if not specified, the general setting for this database is assumed.
collection_name: if provided, the collection name is appended at the end
of the endpoint. In this way, this method allows collection-level
arbitrary POST requests as well.
max_time_ms: a timeout, in milliseconds, for the underlying HTTP request.
Returns:
a dictionary with the response of the HTTP request.
Example:
>>> my_db.command({"findCollections": {}})
{'status': {'collections': ['my_coll']}}
>>> my_db.command({"countDocuments": {}}, collection_name="my_coll")
{'status': {'count': 123}}
"""
if namespace:
_client = self._astra_db.copy(namespace=namespace)
else:
_client = self._astra_db
if collection_name:
_collection = _client.collection(collection_name)
logger.info(f"issuing custom command to API (on '{collection_name}')")
req_response = _collection.post_raw_request(
body=body,
timeout_info=base_timeout_info(max_time_ms),
)
logger.info(
f"finished issuing custom command to API (on '{collection_name}')"
)
return req_response
else:
logger.info("issuing custom command to API")
req_response = _client.post_raw_request(
body=body,
timeout_info=base_timeout_info(max_time_ms),
)
logger.info("finished issuing custom command to API")
return req_response
def get_database_admin(
self,
*,
token: Optional[str] = None,
dev_ops_url: Optional[str] = None,
dev_ops_api_version: Optional[str] = None,
) -> AstraDBDatabaseAdmin:
"""
Return an AstraDBDatabaseAdmin object corresponding to this database, for
use in admin tasks such as managing namespaces.
Args:
token: an access token with enough permission on the database to
perform the desired tasks. If omitted (as it can generally be done),
the token of this Database is used.
dev_ops_url: in case of custom deployments, this can be used to specify
the URL to the DevOps API, such as "https://api.astra.datastax.com".
Generally it can be omitted. The environment (prod/dev/...) is
determined from the API Endpoint.
dev_ops_api_version: this can specify a custom version of the DevOps API
(such as "v2"). Generally not needed.
Returns:
An AstraDBDatabaseAdmin instance targeting this database.
Example:
>>> my_db_admin = my_db.get_database_admin()
>>> if "new_namespace" not in my_db_admin.list_namespaces():
... my_db_admin.create_namespace("new_namespace")
>>> my_db_admin.list_namespaces()
['default_keyspace', 'new_namespace']
"""
# lazy importing here to avoid circular dependency
from astrapy.admin import AstraDBDatabaseAdmin
return AstraDBDatabaseAdmin.from_api_endpoint(
api_endpoint=self._astra_db.api_endpoint,
token=token or self._astra_db.token,
caller_name=self._astra_db.caller_name,
caller_version=self._astra_db.caller_version,
dev_ops_url=dev_ops_url,
dev_ops_api_version=dev_ops_api_version,
)
class AsyncDatabase:
"""
A Data API database. This is the entry-point object for doing database-level
DML, such as creating/deleting collections, and for obtaining Collection
objects themselves. This class has an asynchronous interface.
A Database comes with an "API Endpoint", which implies a Database object
instance reaches a specific region (relevant point in case of multi-region
databases).
Args:
api_endpoint: the full "API Endpoint" string used to reach the Data API.
Example: "https://<database_id>-<region>.apps.astra.datastax.com"
token: an Access Token to the database. Example: "AstraCS:xyz..."
namespace: this is the namespace all method calls will target, unless
one is explicitly specified in the call. If no namespace is supplied
when creating a Database, the name "default_namespace" is set.
caller_name: name of the application, or framework, on behalf of which
the Data API calls are performed. This ends up in the request user-agent.
caller_version: version of the caller.
api_path: path to append to the API Endpoint. In typical usage, this
should be left to its default of "/api/json".
api_version: version specifier to append to the API path. In typical
usage, this should be left to its default of "v1".
Example:
>>> from astrapy import DataAPIClient
>>> my_client = astrapy.DataAPIClient("AstraCS:...")
>>> my_db = my_client.get_async_database_by_api_endpoint(
... "https://01234567-....apps.astra.datastax.com"
... )
Note:
creating an instance of AsyncDatabase does not trigger actual creation
of the database itself, which should exist beforehand. To create databases,
see the AstraDBAdmin class.
"""
def __init__(
self,
api_endpoint: str,
token: str,
*,
namespace: Optional[str] = None,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
api_path: Optional[str] = None,
api_version: Optional[str] = None,
) -> None:
self._astra_db = AsyncAstraDB(
token=token,
api_endpoint=api_endpoint,
api_path=api_path,
api_version=api_version,
namespace=namespace,
caller_name=caller_name,
caller_version=caller_version,
)
self._name: Optional[str] = None
def __getattr__(self, collection_name: str) -> AsyncCollection:
return self.to_sync().get_collection(name=collection_name).to_async()
def __getitem__(self, collection_name: str) -> AsyncCollection:
return self.to_sync().get_collection(name=collection_name).to_async()
def __repr__(self) -> str:
return (
f'{self.__class__.__name__}(api_endpoint="{self._astra_db.api_endpoint}", '
f'token="{self._astra_db.token[:12]}...", namespace="{self._astra_db.namespace}")'
)
def __eq__(self, other: Any) -> bool:
if isinstance(other, AsyncDatabase):
return self._astra_db == other._astra_db
else:
return False
async def __aenter__(self) -> AsyncDatabase:
return self
async def __aexit__(
self,
exc_type: Optional[Type[BaseException]] = None,
exc_value: Optional[BaseException] = None,
traceback: Optional[TracebackType] = None,
) -> None:
await self._astra_db.__aexit__(
exc_type=exc_type,
exc_value=exc_value,
traceback=traceback,
)
def _copy(
self,
*,
api_endpoint: Optional[str] = None,
token: Optional[str] = None,
namespace: Optional[str] = None,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
api_path: Optional[str] = None,
api_version: Optional[str] = None,
) -> AsyncDatabase:
return AsyncDatabase(
api_endpoint=api_endpoint or self._astra_db.api_endpoint,
token=token or self._astra_db.token,
namespace=namespace or self._astra_db.namespace,
caller_name=caller_name or self._astra_db.caller_name,
caller_version=caller_version or self._astra_db.caller_version,
api_path=api_path or self._astra_db.api_path,
api_version=api_version or self._astra_db.api_version,
)
def with_options(
self,
*,
namespace: Optional[str] = None,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
) -> AsyncDatabase:
"""
Create a clone of this database with some changed attributes.
Args:
namespace: this is the namespace all method calls will target, unless
one is explicitly specified in the call. If no namespace is supplied
when creating a Database, the name "default_namespace" is set.
caller_name: name of the application, or framework, on behalf of which
the Data API calls are performed. This ends up in the request user-agent.
caller_version: version of the caller.
Returns:
a new `AsyncDatabase` instance.
Example:
>>> my_async_db_2 = my_async_db.with_options(
... namespace="the_other_namespace",
... caller_name="the_caller",
... caller_version="0.1.0",
... )
"""
return self._copy(
namespace=namespace,
caller_name=caller_name,
caller_version=caller_version,
)
def to_sync(
self,
*,
api_endpoint: Optional[str] = None,
token: Optional[str] = None,
namespace: Optional[str] = None,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
api_path: Optional[str] = None,
api_version: Optional[str] = None,
) -> Database:
"""
Create a (synchronous) Database from this one. Save for the arguments
explicitly provided as overrides, everything else is kept identical
to this database in the copy.
Args:
api_endpoint: the full "API Endpoint" string used to reach the Data API.
Example: "https://<database_id>-<region>.apps.astra.datastax.com"
token: an Access Token to the database. Example: "AstraCS:xyz..."
namespace: this is the namespace all method calls will target, unless
one is explicitly specified in the call. If no namespace is supplied
when creating a Database, the name "default_namespace" is set.
caller_name: name of the application, or framework, on behalf of which
the Data API calls are performed. This ends up in the request user-agent.
caller_version: version of the caller.
api_path: path to append to the API Endpoint. In typical usage, this
should be left to its default of "/api/json".
api_version: version specifier to append to the API path. In typical
usage, this should be left to its default of "v1".
Returns:
the new copy, a `Database` instance.
Example:
>>> my_sync_db = my_async_db.to_sync()
>>> my_sync_db.list_collection_names()
['a_collection', 'another_collection']
"""
return Database(
api_endpoint=api_endpoint or self._astra_db.api_endpoint,
token=token or self._astra_db.token,
namespace=namespace or self._astra_db.namespace,
caller_name=caller_name or self._astra_db.caller_name,
caller_version=caller_version or self._astra_db.caller_version,
api_path=api_path or self._astra_db.api_path,
api_version=api_version or self._astra_db.api_version,
)
def set_caller(
self,
caller_name: Optional[str] = None,
caller_version: Optional[str] = None,
) -> None:
"""
Set a new identity for the application/framework on behalf of which
the Data API calls are performed (the "caller").
Args:
caller_name: name of the application, or framework, on behalf of which
the Data API calls are performed. This ends up in the request user-agent.
caller_version: version of the caller.
Example:
>>> my_db.set_caller(caller_name="the_caller", caller_version="0.1.0")
"""
logger.info(f"setting caller to {caller_name}/{caller_version}")
self._astra_db.set_caller(
caller_name=caller_name,
caller_version=caller_version,
)
def info(self) -> DatabaseInfo:
"""
Additional information on the database as a DatabaseInfo instance.
Some of the returned properties are dynamic throughout the lifetime
of the database (such as raw_info["keyspaces"]). For this reason,
each invocation of this method triggers a new request to the DevOps API.
Example:
>>> my_async_db.info().region
'eu-west-1'
>>> my_async_db.info().raw_info['datacenters'][0]['dateCreated']
'2023-01-30T12:34:56Z'
Note:
see the DatabaseInfo documentation for a caveat about the difference
between the `region` and the `raw_info["region"]` attributes.
"""
logger.info("getting database info")
database_info = fetch_database_info(
self._astra_db.api_endpoint,
token=self._astra_db.token,
namespace=self.namespace,
)
if database_info is not None:
logger.info("finished getting database info")
return database_info
else:
raise DevOpsAPIException(
"Database is not in a supported environment for this operation."
)
@property
def id(self) -> str:
"""
The ID of this database.
Example:
>>> my_async_db.id
'01234567-89ab-cdef-0123-456789abcdef'
"""
parsed_api_endpoint = parse_api_endpoint(self._astra_db.api_endpoint)
if parsed_api_endpoint is not None:
return parsed_api_endpoint.database_id
else:
raise DevOpsAPIException(
"Database is not in a supported environment for this operation."
)
def name(self) -> str:
"""
The name of this database. Note that this bears no unicity guarantees.
Calling this method the first time involves a request
to the DevOps API (the resulting database name is then cached).
See the `info()` method for more details.
Example:
>>> my_async_db.name()
'the_application_database'
"""
if self._name is None:
self._name = self.info().name
return self._name
@property
def namespace(self) -> str:
"""
The namespace this database uses as target for all commands when
no method-call-specific namespace is specified.
Example:
>>> my_async_db.namespace
'the_keyspace'
"""
return self._astra_db.namespace
async def get_collection(
self, name: str, *, namespace: Optional[str] = None
) -> AsyncCollection:
"""
Spawn an `AsyncCollection` object instance representing a collection
on this database.
Creating an `AsyncCollection` instance does not have any effect on the
actual state of the database: in other words, for the created
`AsyncCollection` instance to be used meaningfully, the collection
must exist already (for instance, it should have been created
previously by calling the `create_collection` method).
Args:
name: the name of the collection.
namespace: the namespace containing the collection. If no namespace
is specified, the setting for this database is used.
Returns:
an `AsyncCollection` instance, representing the desired collection
(but without any form of validation).
Example:
>>> async def count_docs(adb: AsyncDatabase, c_name: str) -> int:
... async_col = await adb.get_collection(c_name)
... return await async_col.count_documents({}, upper_bound=100)
...
>>> asyncio.run(count_docs(my_async_db, "my_collection"))
45
Note: the attribute and indexing syntax forms achieve the same effect
as this method, returning an AsyncCollection, albeit
in a synchronous way. In other words, the following are equivalent:
await my_async_db.get_collection("coll_name")
my_async_db.coll_name
my_async_db["coll_name"]
"""
# lazy importing here against circular-import error
from astrapy.collection import AsyncCollection
_namespace = namespace or self._astra_db.namespace
return AsyncCollection(self, name, namespace=_namespace)
@recast_method_async
async def create_collection(
self,
name: str,
*,
namespace: Optional[str] = None,
dimension: Optional[int] = None,
metric: Optional[str] = None,
service: Optional[Union[CollectionVectorServiceOptions, Dict[str, Any]]] = None,
indexing: Optional[Dict[str, Any]] = None,
default_id_type: Optional[str] = None,
additional_options: Optional[Dict[str, Any]] = None,
check_exists: Optional[bool] = None,
max_time_ms: Optional[int] = None,
) -> AsyncCollection:
"""
Creates a collection on the database and return the AsyncCollection
instance that represents it.
This is a blocking operation: the method returns when the collection
is ready to be used. As opposed to the `get_collection` instance,
this method triggers causes the collection to be actually created on DB.
Args:
name: the name of the collection.
namespace: the namespace where the collection is to be created.
If not specified, the general setting for this database is used.
dimension: for vector collections, the dimension of the vectors
(i.e. the number of their components).
metric: the similarity metric used for vector searches.
Allowed values are `VectorMetric.DOT_PRODUCT`, `VectorMetric.EUCLIDEAN`
or `VectorMetric.COSINE` (default).
service: a dictionary describing a service for
embedding computation, e.g. `{"provider": "ab", "modelName": "xy"}`.
Alternatively, a CollectionVectorServiceOptions object to the same effect.
NOTE: This feature is under current development.
indexing: optional specification of the indexing options for
the collection, in the form of a dictionary such as
{"deny": [...]}
or
{"allow": [...]}
default_id_type: this sets what type of IDs the API server will
generate when inserting documents that do not specify their
`_id` field explicitly. Can be set to any of the values
`DefaultIdType.UUID`, `DefaultIdType.OBJECTID`,
`DefaultIdType.UUIDV6`, `DefaultIdType.UUIDV7`,
`DefaultIdType.DEFAULT`.
additional_options: any further set of key-value pairs that will
be added to the "options" part of the payload when sending
the Data API command to create a collection.
check_exists: whether to run an existence check for the collection
name before attempting to create the collection:
If check_exists is True, an error is raised when creating
an existing collection.
If it is False, the creation is attempted. In this case, for
preexisting collections, the command will succeed or fail
depending on whether the options match or not.
max_time_ms: a timeout, in milliseconds, for the underlying HTTP request.
Returns:
an `AsyncCollection` instance, representing the newly-created collection.
Example:
>>> async def create_and_insert(adb: AsyncDatabase) -> Dict[str, Any]:
... new_a_col = await adb.create_collection("my_v_col", dimension=3)
... return await new_a_col.insert_one(
... {"name": "the_row"},
... vector=[0.4, 0.5, 0.7],
... )
...
>>> asyncio.run(create_and_insert(my_async_db))
InsertOneResult(raw_results=..., inserted_id='08f05ecf-...-...-...')
Note:
A collection is considered a vector collection if at least one of
`dimension` or `service` are provided and not null. In that case,
and only in that case, is `metric` an accepted parameter.
Note, moreover, that if passing both these parameters, then
the dimension must be compatible with the chosen service.
"""
_validate_create_collection_options(
dimension=dimension,
metric=metric,
service=service,
indexing=indexing,
default_id_type=default_id_type,
additional_options=additional_options,
)
_options = {
**(additional_options or {}),
**({"indexing": indexing} if indexing else {}),
**({"defaultId": {"type": default_id_type}} if default_id_type else {}),
}
timeout_manager = MultiCallTimeoutManager(overall_max_time_ms=max_time_ms)
if check_exists is None:
_check_exists = True
else:
_check_exists = check_exists
existing_names: List[str]
if _check_exists:
logger.info(f"checking collection existence for '{name}'")
existing_names = await self.list_collection_names(
namespace=namespace,
max_time_ms=timeout_manager.remaining_timeout_ms(),
)
else:
existing_names = []
driver_db = self._astra_db.copy(namespace=namespace)
if name in existing_names:
raise CollectionAlreadyExistsException(
text=f"CollectionInvalid: collection {name} already exists",
namespace=driver_db.namespace,
collection_name=name,
)
service_dict: Optional[Dict[str, Any]]
if service is not None:
service_dict = service if isinstance(service, dict) else service.as_dict()
else:
service_dict = None
logger.info(f"creating collection '{name}'")
await driver_db.create_collection(
name,
options=_options,
dimension=dimension,
metric=metric,
service_dict=service_dict,
timeout_info=timeout_manager.remaining_timeout_info(),
)
logger.info(f"finished creating collection '{name}'")
return await self.get_collection(name, namespace=namespace)
@recast_method_async
async def drop_collection(
self,
name_or_collection: Union[str, AsyncCollection],
*,
max_time_ms: Optional[int] = None,
) -> Dict[str, Any]:
"""
Drop a collection from the database, along with all documents therein.
Args:
name_or_collection: either the name of a collection or
an `AsyncCollection` instance.
max_time_ms: a timeout, in milliseconds, for the underlying HTTP request.
Returns:
a dictionary in the form {"ok": 1} if the command succeeds.
Example:
>>> asyncio.run(my_async_db.list_collection_names())
['a_collection', 'my_v_col', 'another_col']
>>> asyncio.run(my_async_db.drop_collection("my_v_col"))
{'ok': 1}
>>> asyncio.run(my_async_db.list_collection_names())
['a_collection', 'another_col']
Note:
when providing a collection name, it is assumed that the collection
is to be found in the namespace set at database instance level.
"""
# lazy importing here against circular-import error
from astrapy.collection import AsyncCollection
if isinstance(name_or_collection, AsyncCollection):
_namespace = name_or_collection.namespace
_name = name_or_collection.name
logger.info(f"dropping collection '{_name}'")
dc_response = await self._astra_db.copy(
namespace=_namespace
).delete_collection(
_name,
timeout_info=base_timeout_info(max_time_ms),
)
logger.info(f"finished dropping collection '{_name}'")
return dc_response.get("status", {}) # type: ignore[no-any-return]
else:
logger.info(f"dropping collection '{name_or_collection}'")
dc_response = await self._astra_db.delete_collection(
name_or_collection,
timeout_info=base_timeout_info(max_time_ms),
)
logger.info(f"finished dropping collection '{name_or_collection}'")
return dc_response.get("status", {}) # type: ignore[no-any-return]
@recast_method_sync
def list_collections(
self,
*,
namespace: Optional[str] = None,
max_time_ms: Optional[int] = None,
) -> AsyncCommandCursor[CollectionDescriptor]:
"""
List all collections in a given namespace for this database.
Args:
namespace: the namespace to be inspected. If not specified,
the general setting for this database is assumed.
max_time_ms: a timeout, in milliseconds, for the underlying HTTP request.
Returns:
an `AsyncCommandCursor` to iterate over CollectionDescriptor instances,
each corresponding to a collection.
Example:
>>> async def a_list_colls(adb: AsyncDatabase) -> None:
... a_ccur = adb.list_collections()
... print("* a_ccur:", a_ccur)
... print("* list:", [coll async for coll in a_ccur])
... async for coll in adb.list_collections():
... print("* coll:", coll)
...
>>> asyncio.run(a_list_colls(my_async_db))
* a_ccur: <astrapy.cursors.AsyncCommandCursor object at ...>
* list: [CollectionDescriptor(name='my_v_col', options=CollectionOptions())]
* coll: CollectionDescriptor(name='my_v_col', options=CollectionOptions())
"""
_client: AsyncAstraDB
if namespace:
_client = self._astra_db.copy(namespace=namespace)
else:
_client = self._astra_db
logger.info("getting collections")
gc_response = _client.to_sync().get_collections(
options={"explain": True},
timeout_info=base_timeout_info(max_time_ms),
)
if "collections" not in gc_response.get("status", {}):
raise DataAPIFaultyResponseException(
text="Faulty response from get_collections API command.",
raw_response=gc_response,
)
else:
# we know this is a list of dicts, to marshal into "descriptors"
logger.info("finished getting collections")
return AsyncCommandCursor(
address=self._astra_db.base_url,
items=[
CollectionDescriptor.from_dict(col_dict)
for col_dict in gc_response["status"]["collections"]
],
)
@recast_method_async
async def list_collection_names(
self,
*,
namespace: Optional[str] = None,
max_time_ms: Optional[int] = None,
) -> List[str]:
"""
List the names of all collections in a given namespace of this database.
Args:
namespace: the namespace to be inspected. If not specified,
the general setting for this database is assumed.
max_time_ms: a timeout, in milliseconds, for the underlying HTTP request.
Returns:
a list of the collection names as strings, in no particular order.
Example:
>>> asyncio.run(my_async_db.list_collection_names())
['a_collection', 'another_col']
"""
logger.info("getting collection names")
gc_response = await self._astra_db.copy(namespace=namespace).get_collections(
timeout_info=base_timeout_info(max_time_ms)
)
if "collections" not in gc_response.get("status", {}):
raise DataAPIFaultyResponseException(
text="Faulty response from get_collections API command.",
raw_response=gc_response,
)
else:
# we know this is a list of strings
logger.info("finished getting collection names")
return gc_response["status"]["collections"] # type: ignore[no-any-return]
@recast_method_async
async def command(
self,
body: Dict[str, Any],
*,
namespace: Optional[str] = None,
collection_name: Optional[str] = None,
max_time_ms: Optional[int] = None,
) -> Dict[str, Any]:
"""
Send a POST request to the Data API for this database with
an arbitrary, caller-provided payload.
Args:
body: a JSON-serializable dictionary, the payload of the request.
namespace: the namespace to use. Requests always target a namespace:
if not specified, the general setting for this database is assumed.
collection_name: if provided, the collection name is appended at the end
of the endpoint. In this way, this method allows collection-level
arbitrary POST requests as well.
max_time_ms: a timeout, in milliseconds, for the underlying HTTP request.
Returns:
a dictionary with the response of the HTTP request.
Example:
>>> asyncio.run(my_async_db.command({"findCollections": {}}))
{'status': {'collections': ['my_coll']}}
>>> asyncio.run(my_async_db.command(
... {"countDocuments": {}},
... collection_name="my_coll",
... )
{'status': {'count': 123}}
"""
if namespace:
_client = self._astra_db.copy(namespace=namespace)
else:
_client = self._astra_db
if collection_name:
_collection = await _client.collection(collection_name)
logger.info(f"issuing custom command to API (on '{collection_name}')")
req_response = await _collection.post_raw_request(
body=body,
timeout_info=base_timeout_info(max_time_ms),
)
logger.info(
f"finished issuing custom command to API (on '{collection_name}')"
)
return req_response
else:
logger.info("issuing custom command to API")
req_response = await _client.post_raw_request(
body=body,
timeout_info=base_timeout_info(max_time_ms),
)
logger.info("finished issuing custom command to API")
return req_response
def get_database_admin(
self,
*,
token: Optional[str] = None,
dev_ops_url: Optional[str] = None,
dev_ops_api_version: Optional[str] = None,
) -> AstraDBDatabaseAdmin:
"""
Return an AstraDBDatabaseAdmin object corresponding to this database, for
use in admin tasks such as managing namespaces.
Args:
token: an access token with enough permission on the database to
perform the desired tasks. If omitted (as it can generally be done),
the token of this Database is used.
dev_ops_url: in case of custom deployments, this can be used to specify
the URL to the DevOps API, such as "https://api.astra.datastax.com".
Generally it can be omitted. The environment (prod/dev/...) is
determined from the API Endpoint.
dev_ops_api_version: this can specify a custom version of the DevOps API
(such as "v2"). Generally not needed.
Returns:
An AstraDBDatabaseAdmin instance targeting this database.
Example:
>>> my_db_admin = my_async_db.get_database_admin()
>>> if "new_namespace" not in my_db_admin.list_namespaces():
... my_db_admin.create_namespace("new_namespace")
>>> my_db_admin.list_namespaces()
['default_keyspace', 'new_namespace']
"""
# lazy importing here to avoid circular dependency
from astrapy.admin import AstraDBDatabaseAdmin
return AstraDBDatabaseAdmin.from_api_endpoint(
api_endpoint=self._astra_db.api_endpoint,
token=token or self._astra_db.token,
caller_name=self._astra_db.caller_name,
caller_version=self._astra_db.caller_version,
dev_ops_url=dev_ops_url,
dev_ops_api_version=dev_ops_api_version,
)
|