-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmeta_api_version_example.py
More file actions
375 lines (306 loc) · 11.1 KB
/
meta_api_version_example.py
File metadata and controls
375 lines (306 loc) · 11.1 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""
Example demonstrating NocoDB Meta API v2 and v3 usage.
This example shows how to use the Meta API client with both versions
for managing database structure (tables, columns, views, webhooks).
"""
from nocodb_simple_client import NocoDBMetaClient
# ============================================================================
# Meta API v2 Example (Default)
# ============================================================================
print("=" * 60)
print("Meta API v2 Example (Default)")
print("=" * 60)
# Create Meta API client with v2 (default)
meta_v2 = NocoDBMetaClient(
base_url="https://app.nocodb.com",
db_auth_token="your-api-token-here",
# api_version="v2" # Default, can be omitted
)
print(f"Client API Version: {meta_v2.api_version}")
# List all bases
bases = meta_v2.list_bases()
print(f"Found {len(bases)} bases")
# List tables in a base
tables = meta_v2.list_tables(base_id="base_abc123")
print(f"Found {len(tables)} tables")
# Get table metadata (v2 - no base_id required)
table_info = meta_v2.get_table_info(table_id="tbl_xyz789")
print(f"Table: {table_info.get('title', 'Unknown')}")
# List columns in a table (v2 - no base_id required)
columns = meta_v2.list_columns(table_id="tbl_xyz789")
print(f"Table has {len(columns)} columns")
# Create a new column
new_column = {
"title": "Status",
"uidt": "SingleSelect", # UI Data Type
"dtxp": "Active,Inactive,Pending", # Options
}
column = meta_v2.create_column(
table_id="tbl_xyz789",
column_data=new_column,
)
print(f"Created column: {column.get('title', 'Unknown')}")
# List views
views = meta_v2.list_views(table_id="tbl_xyz789")
print(f"Table has {len(views)} views")
# List webhooks
webhooks = meta_v2.list_webhooks(table_id="tbl_xyz789")
print(f"Table has {len(webhooks)} webhooks")
# ============================================================================
# Meta API v3 Example
# ============================================================================
print("\n" + "=" * 60)
print("Meta API v3 Example")
print("=" * 60)
# Create Meta API client with v3
meta_v3 = NocoDBMetaClient(
base_url="https://app.nocodb.com",
db_auth_token="your-api-token-here",
api_version="v3",
base_id="base_abc123", # Default base_id for all operations
)
print(f"Client API Version: {meta_v3.api_version}")
print(f"Default Base ID: {meta_v3.base_id}")
# ============================================================================
# Workspace & Base Operations (Same for v2 and v3)
# ============================================================================
print("\n" + "=" * 60)
print("Workspace & Base Operations (v2/v3 Compatible)")
print("=" * 60)
# List workspaces (same endpoint for v2 and v3)
workspaces = meta_v3.list_workspaces()
print(f"Found {len(workspaces)} workspaces")
# Get workspace details
if workspaces:
workspace = meta_v3.get_workspace(workspaces[0]["id"])
print(f"Workspace: {workspace.get('title', 'Unknown')}")
# List all bases
bases_v3 = meta_v3.list_bases()
print(f"Found {len(bases_v3)} bases")
# Get base details
base = meta_v3.get_base(base_id="base_abc123")
print(f"Base: {base.get('title', 'Unknown')}")
# ============================================================================
# Table Operations (v3 with base_id)
# ============================================================================
print("\n" + "=" * 60)
print("Table Operations (v3)")
print("=" * 60)
# List tables (base_id already known)
tables_v3 = meta_v3.list_tables(base_id="base_abc123")
print(f"Found {len(tables_v3)} tables")
# Get table info (v3 - uses default base_id)
table_info_v3 = meta_v3.get_table_info(table_id="tbl_xyz789")
print(f"Table: {table_info_v3.get('title', 'Unknown')}")
# Or override with specific base_id
table_info_alt = meta_v3.get_table_info(
table_id="tbl_different",
base_id="base_different123",
)
print(f"Alternate table: {table_info_alt.get('title', 'Unknown')}")
# Create a new table
table_data = {
"title": "Customers",
"description": "Customer database",
"columns": [
{
"title": "Name",
"uidt": "SingleLineText",
"pv": True, # Primary value
},
{
"title": "Email",
"uidt": "Email",
},
{
"title": "Phone",
"uidt": "PhoneNumber",
},
{
"title": "Created At",
"uidt": "DateTime",
"dtxp": "YYYY-MM-DD HH:mm:ss",
},
],
}
new_table = meta_v3.create_table(
base_id="base_abc123",
table_data=table_data,
)
print(f"Created table: {new_table.get('title', 'Unknown')}")
# Update table metadata
updated_table = meta_v3.update_table(
table_id=new_table["id"],
table_data={"description": "Updated customer database"},
# base_id automatically used from client's default
)
print("Updated table description")
# ============================================================================
# Column Operations (v3 - columns are called "fields")
# ============================================================================
print("\n" + "=" * 60)
print("Column/Field Operations (v3)")
print("=" * 60)
# List columns (v3 - automatically uses default base_id)
columns_v3 = meta_v3.list_columns(table_id="tbl_xyz789")
print(f"Table has {len(columns_v3)} columns/fields")
# Create a new column/field
column_data = {
"title": "Priority",
"uidt": "SingleSelect",
"dtxp": "Low,Medium,High,Urgent", # Options
"dtxs": "Medium", # Default value
}
new_column_v3 = meta_v3.create_column(
table_id="tbl_xyz789",
column_data=column_data,
# base_id automatically used
)
print(f"Created field: {new_column_v3.get('title', 'Unknown')}")
# Update column (v3 requires base_id for column_id operations)
updated_column = meta_v3.update_column(
column_id=new_column_v3["id"],
column_data={"title": "Task Priority"},
base_id="base_abc123", # Required for v3 when using column_id
)
print("Updated field title")
# ============================================================================
# View Operations (v3)
# ============================================================================
print("\n" + "=" * 60)
print("View Operations (v3)")
print("=" * 60)
# List views for a table
views_v3 = meta_v3.list_views(table_id="tbl_xyz789")
print(f"Table has {len(views_v3)} views")
# Create a new view
view_data = {
"title": "Active Customers",
"type": "Grid",
"show_system_fields": False,
"lock_type": "collaborative",
}
new_view = meta_v3.create_view(
table_id="tbl_xyz789",
view_data=view_data,
# base_id automatically used
)
print(f"Created view: {new_view.get('title', 'Unknown')}")
# Update view (requires base_id for view_id operations)
updated_view = meta_v3.update_view(
view_id=new_view["id"],
view_data={"title": "All Active Customers"},
base_id="base_abc123", # Required for v3
)
print("Updated view title")
# Get view details
view_details = meta_v3.get_view(
view_id=new_view["id"],
base_id="base_abc123",
)
print(f"View type: {view_details.get('type', 'Unknown')}")
# ============================================================================
# Webhook Operations (v3)
# ============================================================================
print("\n" + "=" * 60)
print("Webhook Operations (v3)")
print("=" * 60)
# List webhooks for a table
webhooks_v3 = meta_v3.list_webhooks(table_id="tbl_xyz789")
print(f"Table has {len(webhooks_v3)} webhooks")
# Create a webhook
webhook_data = {
"title": "New Customer Notification",
"event": "after",
"operation": "insert",
"notification": {
"type": "URL",
"payload": {
"method": "POST",
"url": "https://hooks.example.com/customer-webhook",
"body": '{"customer": "{{Name}}", "email": "{{Email}}"}',
"headers": [{"name": "Content-Type", "value": "application/json"}],
},
},
"condition": [], # No conditions, trigger on all inserts
"active": True,
}
new_webhook = meta_v3.create_webhook(
table_id="tbl_xyz789",
webhook_data=webhook_data,
# base_id automatically used
)
print(f"Created webhook: {new_webhook.get('title', 'Unknown')}")
# Update webhook
updated_webhook = meta_v3.update_webhook(
hook_id=new_webhook["id"],
webhook_data={"active": False},
base_id="base_abc123", # Required for v3
)
print("Disabled webhook")
# Test webhook
test_result = meta_v3.test_webhook(
hook_id=new_webhook["id"],
base_id="base_abc123",
)
print(f"Webhook test result: {test_result.get('status', 'Unknown')}")
# ============================================================================
# Migration: Comparing v2 and v3 Side-by-Side
# ============================================================================
print("\n" + "=" * 60)
print("Migration Example: v2 vs v3 Comparison")
print("=" * 60)
# Same operation in v2 and v3
print("\nGetting table info:")
# v2 - no base_id needed
table_v2 = meta_v2.get_table_info(table_id="tbl_xyz789")
print(f"v2: {table_v2.get('title', 'Unknown')}")
# v3 - uses default base_id from client
table_v3 = meta_v3.get_table_info(table_id="tbl_xyz789")
print(f"v3: {table_v3.get('title', 'Unknown')}")
print("\nListing columns:")
# v2 - no base_id needed
cols_v2 = meta_v2.list_columns(table_id="tbl_xyz789")
print(f"v2: {len(cols_v2)} columns")
# v3 - uses default base_id, columns are called "fields"
cols_v3 = meta_v3.list_columns(table_id="tbl_xyz789")
print(f"v3: {len(cols_v3)} fields")
# ============================================================================
# Advanced: Using Multiple Bases in v3
# ============================================================================
print("\n" + "=" * 60)
print("Advanced: Working with Multiple Bases (v3)")
print("=" * 60)
# Client configured with default base
meta_multi = NocoDBMetaClient(
base_url="https://app.nocodb.com",
db_auth_token="your-api-token-here",
api_version="v3",
base_id="base_primary",
)
# Use default base
tables_primary = meta_multi.list_tables(base_id="base_primary")
print(f"Primary base: {len(tables_primary)} tables")
# Override for different base
tables_secondary = meta_multi.list_tables(base_id="base_secondary")
print(f"Secondary base: {len(tables_secondary)} tables")
# Table operations with explicit base_id override
table_from_other_base = meta_multi.get_table_info(
table_id="tbl_from_secondary",
base_id="base_secondary", # Override default
)
print(f"Table from secondary base: {table_from_other_base.get('title', 'Unknown')}")
# ============================================================================
# Best Practices Summary
# ============================================================================
print("\n" + "=" * 60)
print("BEST PRACTICES")
print("=" * 60)
print("1. v2: Simple, no base_id needed for table operations")
print("2. v3: Set base_id in constructor for cleaner code")
print("3. v3: For column/view/webhook operations by ID, always provide base_id")
print("4. Migration: Test v3 in parallel before switching")
print("5. Columns → Fields: v3 terminology (PathBuilder handles this)")
print("6. Workspace/Base endpoints: Same for v2 and v3")
print("=" * 60)
print("\n✅ All Meta API examples completed!")