AsyncDatabase API
The AsyncDatabase class provides an asynchronous interface for interacting with CockroachDB. It handles connection management, SQL execution, and transaction support using Python's async/await syntax.
Class Documentation
AsyncDatabase
Main interface for asynchronous CockroachDB operations.
Source code in manticore_cockroachdb/async_database.py
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 | |
Functions
__init__(database='defaultdb', host='localhost', port=26257, user='root', password=None, sslmode='disable', min_connections=2, max_connections=10, application_name='manticore-async-db')
Initialize database connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
str
|
Database name |
'defaultdb'
|
host
|
str
|
Database host |
'localhost'
|
port
|
int
|
Database port |
26257
|
user
|
str
|
Database user |
'root'
|
password
|
Optional[str]
|
Database password |
None
|
sslmode
|
str
|
SSL mode (disable, verify-ca, verify-full) |
'disable'
|
min_connections
|
int
|
Minimum number of connections in pool |
2
|
max_connections
|
int
|
Maximum number of connections in pool |
10
|
application_name
|
str
|
Application name for connection |
'manticore-async-db'
|
Source code in manticore_cockroachdb/async_database.py
_build_dsn()
Build database connection string.
Returns:
| Type | Description |
|---|---|
str
|
Database connection string |
Source code in manticore_cockroachdb/async_database.py
connect()
async
Connect to database.
Source code in manticore_cockroachdb/async_database.py
close()
async
Close database connection pool.
Source code in manticore_cockroachdb/async_database.py
execute(query, params=None, fetch=True)
async
Execute a query and optionally return results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SQL query to execute |
required |
params
|
Optional[tuple]
|
Query parameters |
None
|
fetch
|
bool
|
Whether to return results |
True
|
Returns:
| Type | Description |
|---|---|
Optional[List[Dict[str, Any]]]
|
Query results if fetch=True, None otherwise |
Source code in manticore_cockroachdb/async_database.py
create_database(name)
async
Create a new database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Database name |
required |
Source code in manticore_cockroachdb/async_database.py
create_table(name, columns, if_not_exists=True)
async
Create a new table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Table name |
required |
columns
|
Dict[str, str]
|
Column definitions {name: type} |
required |
if_not_exists
|
bool
|
Whether to create table only if it doesn't exist |
True
|
Source code in manticore_cockroachdb/async_database.py
drop_table(name, if_exists=True)
async
Drop a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Table name |
required |
if_exists
|
bool
|
Whether to drop table only if it exists |
True
|
Source code in manticore_cockroachdb/async_database.py
insert(table, data)
async
Insert a record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str
|
Table name |
required |
data
|
Dict[str, Any]
|
Record data |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Inserted record |
Source code in manticore_cockroachdb/async_database.py
select(table, columns=None, where=None, order_by=None, limit=None, offset=None)
async
Select records from a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str
|
Table name |
required |
columns
|
Optional[List[str]]
|
Columns to select |
None
|
where
|
Optional[Dict[str, Any]]
|
Where conditions {column: value} |
None
|
order_by
|
Optional[str]
|
Order by clause |
None
|
limit
|
Optional[int]
|
Result limit |
None
|
offset
|
Optional[int]
|
Result offset |
None
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
Selected records |
Source code in manticore_cockroachdb/async_database.py
update(table, data, where)
async
Update records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str
|
Table name |
required |
data
|
Dict[str, Any]
|
Update data |
required |
where
|
Dict[str, Any]
|
Filter conditions |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Updated record |
Source code in manticore_cockroachdb/async_database.py
delete(table, where)
async
Delete records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str
|
Table name |
required |
where
|
Dict[str, Any]
|
Filter conditions |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether any records were deleted |
Source code in manticore_cockroachdb/async_database.py
from_url(url)
classmethod
Create database instance from URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Database URL |
required |
Returns:
| Type | Description |
|---|---|
AsyncDatabase
|
Database instance |
Source code in manticore_cockroachdb/async_database.py
__aenter__()
async
__aexit__(exc_type, exc_val, exc_tb)
async
transaction()
async
Start a transaction.
Returns:
| Type | Description |
|---|---|
AsyncTransaction
|
Transaction context manager |
Source code in manticore_cockroachdb/async_database.py
run_in_transaction(operation, max_retries=3)
async
Run an operation in a transaction with retry logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
Callable[[AsyncConnection], T]
|
Operation to run |
required |
max_retries
|
int
|
Maximum number of retry attempts |
3
|
Returns:
| Type | Description |
|---|---|
T
|
Operation result |
Raises:
| Type | Description |
|---|---|
ValueError
|
If transaction fails after max retries |
Source code in manticore_cockroachdb/async_database.py
batch_insert(table, records)
async
Insert multiple records into a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str
|
Table name |
required |
records
|
List[Dict[str, Any]]
|
Records to insert |
required |
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
Inserted records |
Source code in manticore_cockroachdb/async_database.py
batch_update(table, records, key_column='id')
async
Update multiple records in a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str
|
Table name |
required |
records
|
List[Dict[str, Any]]
|
Records to update |
required |
key_column
|
str
|
Primary key column |
'id'
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
Updated records |
Source code in manticore_cockroachdb/async_database.py
Usage Examples
import asyncio
from manticore_cockroachdb import AsyncDatabase
async def main():
# Connect to database
db = AsyncDatabase(
host="localhost",
port=26257,
database="example_db",
user="root",
password="",
ssl_mode="disable"
)
# Or connect using a URL
# db = AsyncDatabase.from_url("postgresql://root@localhost:26257/example_db?sslmode=disable")
# Connect to the database
await db.connect()
try:
# Create a table
await db.create_table(
"async_users",
{
"id": "UUID PRIMARY KEY DEFAULT gen_random_uuid()",
"name": "TEXT NOT NULL",
"email": "TEXT UNIQUE NOT NULL",
"age": "INTEGER",
"active": "BOOLEAN DEFAULT TRUE"
},
if_not_exists=True
)
# Insert data
user_id = await db.insert("async_users", {
"name": "John Doe",
"email": "john@example.com",
"age": 30
})
# Select data
user = await db.select_one("async_users", where={"id": user_id})
print(f"User: {user['name']}, Email: {user['email']}")
# Update data
await db.update("async_users", {"age": 31}, where={"id": user_id})
# Delete data
await db.delete("async_users", where={"id": user_id})
# Execute raw SQL
results = await db.execute("SELECT * FROM async_users WHERE age > %s", [25])
# Use transactions
async with db.transaction():
await db.insert("async_users", {"name": "Alice", "email": "alice@example.com", "age": 25})
await db.insert("async_users", {"name": "Bob", "email": "bob@example.com", "age": 28})
finally:
# Close the connection
await db.close()
# Run the async function
asyncio.run(main())