-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmeta_operations.py
More file actions
209 lines (171 loc) · 7.77 KB
/
meta_operations.py
File metadata and controls
209 lines (171 loc) · 7.77 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
"""
Meta API operations examples for NocoDB Simple Client.
This example demonstrates how to manage NocoDB structure including
workspaces, bases, tables, columns, and views programmatically.
"""
from nocodb_simple_client import NocoDBMetaClient
from nocodb_simple_client.columns import NocoDBColumns
from nocodb_simple_client.views import NocoDBViews
# Configuration
NOCODB_BASE_URL = "https://your-nocodb-instance.com"
API_TOKEN = "your-api-token-here"
def main():
"""Demonstrate Meta API operations."""
# Initialize Meta API client
meta_client = NocoDBMetaClient(base_url=NOCODB_BASE_URL, api_token=API_TOKEN)
try:
# ===================================================================
# WORKSPACE OPERATIONS
# ===================================================================
print("=" * 60)
print("WORKSPACE OPERATIONS")
print("=" * 60)
# List workspaces
print("\n1. Listing workspaces...")
workspaces = meta_client.list_workspaces()
for workspace in workspaces:
print(f" • {workspace.get('title')} (ID: {workspace.get('id')})")
# ===================================================================
# BASE OPERATIONS
# ===================================================================
print("\n" + "=" * 60)
print("BASE OPERATIONS")
print("=" * 60)
# List bases
print("\n2. Listing bases...")
bases = meta_client.list_bases()
for base in bases:
print(f" • {base.get('title')} (ID: {base.get('id')})")
if bases:
base_id = bases[0]["id"]
print(f"\n Using base: {bases[0].get('title')} ({base_id})")
# ===================================================================
# TABLE OPERATIONS
# ===================================================================
print("\n" + "=" * 60)
print("TABLE OPERATIONS")
print("=" * 60)
# List tables
print("\n3. Listing tables...")
tables = meta_client.list_tables(base_id)
for table in tables:
print(f" • {table.get('title')} (ID: {table.get('id')})")
# Create a new table
print("\n4. Creating a new table...")
table_data = {
"title": "Demo Table",
"table_name": "demo_table",
"columns": [
{"title": "Title", "column_name": "title", "uidt": "SingleLineText"},
{
"title": "Status",
"column_name": "status",
"uidt": "SingleSelect",
"dtxp": [
{"title": "New", "color": "#3498db"},
{"title": "In Progress", "color": "#f39c12"},
{"title": "Done", "color": "#2ecc71"},
],
},
],
}
new_table = meta_client.create_table(base_id, table_data)
table_id = new_table["id"]
print(f" ✓ Created table: {new_table.get('title')} (ID: {table_id})")
# Get table info
print("\n5. Getting table information...")
table_info = meta_client.get_table_info(table_id)
print(f" • Table: {table_info.get('title')}")
print(f" • Columns: {len(table_info.get('columns', []))}")
# ===================================================================
# COLUMN OPERATIONS
# ===================================================================
print("\n" + "=" * 60)
print("COLUMN OPERATIONS")
print("=" * 60)
columns_manager = NocoDBColumns(meta_client)
# Add various column types
print("\n6. Adding columns...")
# Text column
text_col = columns_manager.create_text_column(
table_id, title="Description", max_length=1000
)
print(f" ✓ Added text column: {text_col.get('title')}")
# Number column
number_col = columns_manager.create_number_column(
table_id, title="Priority", precision=3
)
print(f" ✓ Added number column: {number_col.get('title')}")
# Checkbox column
checkbox_col = columns_manager.create_checkbox_column(
table_id, title="Is Important", default_value=False
)
print(f" ✓ Added checkbox column: {checkbox_col.get('title')}")
# Date column
date_col = columns_manager.create_date_column(
table_id, title="Due Date", date_format="YYYY-MM-DD"
)
print(f" ✓ Added date column: {date_col.get('title')}")
# List all columns
print("\n7. Listing all columns...")
columns = columns_manager.get_columns(table_id)
for col in columns:
print(f" • {col.get('title')} ({col.get('uidt')})")
# ===================================================================
# VIEW OPERATIONS
# ===================================================================
print("\n" + "=" * 60)
print("VIEW OPERATIONS")
print("=" * 60)
views_manager = NocoDBViews(meta_client)
# Create views
print("\n8. Creating views...")
# Grid view
grid_view = views_manager.create_view(table_id, title="All Items", view_type="grid")
print(f" ✓ Created grid view: {grid_view.get('title')}")
# Gallery view
gallery_view = views_manager.create_view(
table_id, title="Gallery View", view_type="gallery"
)
print(f" ✓ Created gallery view: {gallery_view.get('title')}")
# Form view
form_view = views_manager.create_view(table_id, title="Submit Form", view_type="form")
print(f" ✓ Created form view: {form_view.get('title')}")
# List all views
print("\n9. Listing all views...")
views = views_manager.get_views(table_id)
for view in views:
print(f" • {view.get('title')} ({view.get('type')})")
# Add filter to a view (if we have the column IDs)
if columns:
status_column = next((c for c in columns if c.get("title") == "Status"), None)
if status_column and views:
print("\n10. Adding filter to grid view...")
views_manager.create_view_filter(
table_id,
grid_view["id"],
column_id=status_column["id"],
comparison_op="eq",
value="Done",
logical_op="and",
)
print(" ✓ Added filter to view")
# ===================================================================
# CLEANUP
# ===================================================================
print("\n" + "=" * 60)
print("CLEANUP")
print("=" * 60)
# Delete the demo table
print("\n11. Cleaning up (deleting demo table)...")
meta_client.delete_table(table_id)
print(" ✓ Deleted demo table")
else:
print("\n No bases found. Please create a base in NocoDB first.")
except Exception as e:
print(f"❌ Error: {e}")
finally:
meta_client.close()
print("\n✓ Meta client session closed")
if __name__ == "__main__":
main()