Table API
The Table class provides a convenient interface for performing CRUD operations on a specific database table using the synchronous API.
Class Documentation
Table
Database table with CRUD operations.
Source code in manticore_cockroachdb/crud/table.py
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 | |
Functions
__init__(name, db=None, schema=None, if_not_exists=True)
Initialize table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Table name |
required |
db
|
Optional[Database]
|
Database instance |
None
|
schema
|
Optional[Dict[str, str]]
|
Column definitions {name: type} |
None
|
if_not_exists
|
bool
|
Whether to create table only if it doesn't exist |
True
|
Source code in manticore_cockroachdb/crud/table.py
initialize(schema=None, if_not_exists=True)
Initialize table schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Dict[str, str]
|
Column definitions {name: type} |
None
|
if_not_exists
|
bool
|
Whether to create table only if it doesn't exist |
True
|
Source code in manticore_cockroachdb/crud/table.py
create(data)
Create a new record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
Record data |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Created record |
read(id)
Read a record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Record ID |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Record data or None if not found |
Source code in manticore_cockroachdb/crud/table.py
update(id, data)
Update a record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Record ID |
required |
data
|
Dict[str, Any]
|
Update data |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Updated record |
delete(id)
Delete a record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Record ID |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if record was deleted |
list(where=None, order_by=None, limit=None)
List records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where
|
Optional[Dict[str, Any]]
|
Filter conditions |
None
|
order_by
|
Optional[str]
|
Order by clause |
None
|
limit
|
Optional[int]
|
Result limit |
None
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of records |
Source code in manticore_cockroachdb/crud/table.py
count(where=None)
Count records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
where
|
Optional[Dict[str, Any]]
|
Filter conditions |
None
|
Returns:
| Type | Description |
|---|---|
int
|
Number of records |
Source code in manticore_cockroachdb/crud/table.py
batch_create(records)
Create multiple records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
List[Dict[str, Any]]
|
Records to create |
required |
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
Created records |
Source code in manticore_cockroachdb/crud/table.py
batch_update(records, key_column='id')
Update multiple records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
List[Dict[str, Any]]
|
Records to update |
required |
key_column
|
str
|
Column to use as key |
'id'
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
Updated records |
Source code in manticore_cockroachdb/crud/table.py
__enter__()
Usage Examples
from manticore_cockroachdb import Database, Table
# Connect to database
db = Database(database="example_db")
# Create a table
db.create_table(
"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
)
# Create a Table instance
users = Table("users", db=db)
# Create a user
user = users.create({
"name": "John Doe",
"email": "john@example.com",
"age": 30
})
print(f"Created user with ID: {user['id']}")
# Read a user
retrieved_user = users.read(user["id"])
print(f"Retrieved user: {retrieved_user['name']}")
# Update a user
updated_user = users.update(user["id"], {"age": 31})
print(f"Updated user age: {updated_user['age']}")
# List all users
all_users = users.list()
print(f"Total users: {len(all_users)}")
# Filter users
active_users = users.list(where={"active": True})
print(f"Active users: {len(active_users)}")
# Count users
user_count = users.count()
print(f"User count: {user_count}")
# Delete a user
users.delete(user["id"])
print("User deleted")
# Batch operations
batch_users = [
{"name": "User 1", "email": "user1@example.com", "age": 21},
{"name": "User 2", "email": "user2@example.com", "age": 22},
{"name": "User 3", "email": "user3@example.com", "age": 23}
]
# Create multiple users in a batch
created_batch = users.batch_create(batch_users)
print(f"Created {len(created_batch)} users in batch")
# Update multiple users in a batch
updates = [
{"id": created_batch[0]["id"], "age": 31},
{"id": created_batch[1]["id"], "age": 32},
{"id": created_batch[2]["id"], "age": 33}
]
updated_batch = users.batch_update(updates)
print(f"Updated {len(updated_batch)} users in batch")
# Delete multiple users in a batch
ids_to_delete = [user["id"] for user in created_batch]
users.batch_delete(ids_to_delete)
print(f"Deleted {len(ids_to_delete)} users in batch")