-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_operations_example.py
More file actions
357 lines (298 loc) · 10.9 KB
/
Copy pathtable_operations_example.py
File metadata and controls
357 lines (298 loc) · 10.9 KB
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
"""
AINative SDK - Table Operations Examples
This example demonstrates how to use the Table Operations in the AINative Python SDK.
"""
from ainative import AINativeClient
import os
def main():
"""
Complete example of using ZeroDB Table Operations
"""
# Initialize the client
api_key = os.getenv("AINATIVE_API_KEY")
if not api_key:
print("Please set AINATIVE_API_KEY environment variable")
return
client = AINativeClient(api_key=api_key)
# ========================================
# 1. Create a Table
# ========================================
print("1. Creating a table...")
table_schema = {
"fields": {
"email": "string",
"name": "string",
"age": "number",
"active": "boolean",
"tags": "array",
"metadata": "object"
},
"indexes": ["email"] # Index the email field for faster queries
}
try:
table = client.zerodb.tables.create_table(
table_name="users",
schema=table_schema,
description="User data table"
)
print(f"✓ Table created: {table['table_name']} (ID: {table['table_id']})")
except Exception as e:
print(f"Table already exists or error: {e}")
# ========================================
# 2. List All Tables
# ========================================
print("\n2. Listing all tables...")
tables = client.zerodb.tables.list_tables(limit=10)
print(f"✓ Found {tables['total']} tables:")
for table in tables['tables']:
print(f" - {table['table_name']}: {table['row_count']} rows")
# ========================================
# 3. Insert Rows
# ========================================
print("\n3. Inserting rows...")
users = [
{
"email": "john.doe@example.com",
"name": "John Doe",
"age": 30,
"active": True,
"tags": ["premium", "verified"],
"metadata": {"signup_date": "2025-01-01", "referral": "organic"}
},
{
"email": "jane.smith@example.com",
"name": "Jane Smith",
"age": 25,
"active": True,
"tags": ["verified"],
"metadata": {"signup_date": "2025-01-05", "referral": "paid"}
},
{
"email": "bob.johnson@example.com",
"name": "Bob Johnson",
"age": 35,
"active": False,
"tags": ["premium"],
"metadata": {"signup_date": "2024-12-20", "referral": "organic"}
}
]
result = client.zerodb.tables.insert_rows("users", users)
print(f"✓ Inserted {result['inserted_count']} rows")
print(f" Row IDs: {result['inserted_ids'][:3]}...")
# ========================================
# 4. Query Rows - Basic
# ========================================
print("\n4. Querying all users...")
all_users = client.zerodb.tables.query_rows("users", limit=10)
print(f"✓ Found {all_users['total']} users")
for row in all_users['rows']:
user = row['data']
print(f" - {user['name']} ({user['email']}): {user['age']} years old")
# ========================================
# 5. Query Rows - With Filter
# ========================================
print("\n5. Querying active users over 25...")
active_users = client.zerodb.tables.query_rows(
"users",
filter={
"age": {"$gte": 25},
"active": True
}
)
print(f"✓ Found {active_users['total']} active users over 25")
for row in active_users['rows']:
user = row['data']
print(f" - {user['name']}: {user['age']} years old")
# ========================================
# 6. Query Rows - With Sorting
# ========================================
print("\n6. Querying users sorted by age (descending)...")
sorted_users = client.zerodb.tables.query_rows(
"users",
sort={"age": -1}, # -1 for descending, 1 for ascending
limit=5
)
print(f"✓ Top users by age:")
for row in sorted_users['rows']:
user = row['data']
print(f" - {user['name']}: {user['age']} years old")
# ========================================
# 7. Query Rows - With Projection
# ========================================
print("\n7. Querying users (name and email only)...")
projected_users = client.zerodb.tables.query_rows(
"users",
projection={"name": 1, "email": 1, "_id": 0}
)
print(f"✓ Users (projected fields):")
for row in projected_users['rows']:
user = row['data']
print(f" - {user.get('name')}: {user.get('email')}")
# ========================================
# 8. Count Rows
# ========================================
print("\n8. Counting rows...")
total_count = client.zerodb.tables.count_rows("users")
print(f"✓ Total users: {total_count}")
active_count = client.zerodb.tables.count_rows(
"users",
filter={"active": True}
)
print(f"✓ Active users: {active_count}")
# ========================================
# 9. Update Rows
# ========================================
print("\n9. Updating user age...")
update_result = client.zerodb.tables.update_rows(
"users",
filter={"email": "john.doe@example.com"},
update={"$set": {"age": 31}}
)
print(f"✓ Updated {update_result['modified_count']} users")
# Verify the update
updated_user = client.zerodb.tables.query_rows(
"users",
filter={"email": "john.doe@example.com"}
)
if updated_user['rows']:
user = updated_user['rows'][0]['data']
print(f" - {user['name']} is now {user['age']} years old")
# ========================================
# 10. Update with Increment
# ========================================
print("\n10. Incrementing age for all users...")
increment_result = client.zerodb.tables.update_rows(
"users",
filter={}, # Match all users
update={"$inc": {"age": 1}} # Increment age by 1
)
print(f"✓ Incremented age for {increment_result['modified_count']} users")
# ========================================
# 11. Update with Upsert
# ========================================
print("\n11. Updating with upsert (insert if not exists)...")
upsert_result = client.zerodb.tables.update_rows(
"users",
filter={"email": "new.user@example.com"},
update={
"$set": {
"email": "new.user@example.com",
"name": "New User",
"age": 28,
"active": True
}
},
upsert=True
)
print(f"✓ Upsert result: {upsert_result}")
# ========================================
# 12. Delete Rows
# ========================================
print("\n12. Deleting inactive users...")
delete_result = client.zerodb.tables.delete_rows(
"users",
filter={"active": False}
)
print(f"✓ Deleted {delete_result['deleted_count']} inactive users")
# ========================================
# 13. Get Table Details
# ========================================
print("\n13. Getting table details...")
table_details = client.zerodb.tables.get_table("users")
print(f"✓ Table: {table_details['table_name']}")
print(f" - Rows: {table_details['row_count']}")
print(f" - Schema: {list(table_details['schema']['fields'].keys())}")
print(f" - Created: {table_details['created_at']}")
# ========================================
# 14. Check if Table Exists
# ========================================
print("\n14. Checking if tables exist...")
users_exists = client.zerodb.tables.table_exists("users")
print(f"✓ 'users' table exists: {users_exists}")
fake_exists = client.zerodb.tables.table_exists("nonexistent_table")
print(f"✓ 'nonexistent_table' exists: {fake_exists}")
# ========================================
# 15. Delete Table (Optional - Cleanup)
# ========================================
print("\n15. Cleanup: Deleting table (optional)...")
cleanup = input("Do you want to delete the 'users' table? (yes/no): ")
if cleanup.lower() == 'yes':
delete_table_result = client.zerodb.tables.delete_table(
"users",
confirm=True # Safety confirmation required
)
print(f"✓ Table deleted: {delete_table_result['status']}")
print(f" - Rows deleted: {delete_table_result['rows_deleted']}")
else:
print("✓ Skipping table deletion")
print("\n" + "="*50)
print("✓ All table operations completed successfully!")
print("="*50)
def advanced_example():
"""
Advanced example with complex queries
"""
api_key = os.getenv("AINATIVE_API_KEY")
client = AINativeClient(api_key=api_key)
print("\n" + "="*50)
print("ADVANCED TABLE OPERATIONS")
print("="*50)
# Complex query with multiple conditions
print("\n1. Complex query with $and, $or operators...")
complex_query = client.zerodb.tables.query_rows(
"users",
filter={
"$and": [
{"age": {"$gte": 25}},
{"$or": [
{"tags": {"$in": ["premium"]}},
{"active": True}
]}
]
},
sort={"age": -1},
limit=10
)
print(f"✓ Found {complex_query['total']} users matching complex criteria")
# Batch insert with error handling
print("\n2. Batch insert with large dataset...")
large_dataset = [
{
"email": f"user{i}@example.com",
"name": f"User {i}",
"age": 20 + (i % 40),
"active": i % 2 == 0
}
for i in range(100)
]
# Insert in batches of 100
batch_size = 100
for i in range(0, len(large_dataset), batch_size):
batch = large_dataset[i:i + batch_size]
result = client.zerodb.tables.insert_rows("users", batch)
print(f"✓ Inserted batch {i//batch_size + 1}: {result['inserted_count']} rows")
# Aggregation-like query (count by condition)
print("\n3. Aggregation: Count users by age groups...")
age_groups = {
"18-25": {"$gte": 18, "$lte": 25},
"26-35": {"$gte": 26, "$lte": 35},
"36+": {"$gte": 36}
}
for group_name, age_filter in age_groups.items():
count = client.zerodb.tables.count_rows(
"users",
filter={"age": age_filter}
)
print(f"✓ {group_name}: {count} users")
print("\n" + "="*50)
print("✓ Advanced operations completed!")
print("="*50)
if __name__ == "__main__":
try:
main()
# Uncomment to run advanced examples
# advanced_example()
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()