Skip to content

Commit f3bf7b7

Browse files
committed
fixed overwrite_data function
1 parent 3a9b429 commit f3bf7b7

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

src/astroidapi/surrealdb_handler.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import random
99
import string
1010
import datetime
11+
import asyncio
1112

1213
async def sync_local_files(folderpath: str, specific: bool = False):
1314
try:
@@ -164,8 +165,49 @@ async def overwrite_json(endpoint: int, json_data: dict):
164165
async with Surreal(config.SDB_URL) as db:
165166
await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS})
166167
await db.use(config.SDB_NAMESPACE, config.SDB_DATABASE)
167-
await db.query(f"UPDATE endpoints:`{endpoint}` SET data = {json_data}")
168-
return await db.query(f"SELECT * FROM endpoints:`{endpoint}`")
168+
await db.query(f"UPDATE endpoints:`{endpoint}` CONTENT {json_data}")
169+
async def set_nested_values_to_null(data, prefix="", endpoint_id=None, database=None):
170+
async def update_field(key_path):
171+
try:
172+
if "-" in key_path:
173+
parts = key_path.split('.')
174+
modified_parts = []
175+
for part in parts:
176+
if '-' in part:
177+
modified_parts.append(f'`{part}`')
178+
else:
179+
modified_parts.append(part)
180+
key_path = '.'.join(modified_parts)
181+
await database.query(f"UPDATE endpoints:`{endpoint_id}` SET {key_path} = null")
182+
except Exception as e:
183+
print(f"Error updating {key_path}: {e}")
184+
185+
async def process_nested(obj, current_prefix="", depth=0):
186+
if depth > 6: # Limit to sixth degree nesting
187+
return
188+
189+
if isinstance(obj, dict):
190+
for key, value in obj.items():
191+
current_key = f"{current_prefix}.{key}" if current_prefix else key
192+
if isinstance(value, (dict, list)):
193+
await process_nested(value, current_key, depth + 1)
194+
else:
195+
if value is None:
196+
await update_field(current_key)
197+
elif isinstance(obj, list):
198+
for i, item in enumerate(obj):
199+
current_key = f"{current_prefix}[{i}]"
200+
if isinstance(item, (dict, list)):
201+
await process_nested(item, current_key, depth + 1)
202+
else:
203+
if item is None:
204+
await update_field(current_key)
205+
206+
await process_nested(data, prefix)
207+
208+
await set_nested_values_to_null(json_data, "", endpoint, db)
209+
ret = await db.query(f"SELECT * FROM endpoints:`{endpoint}`")
210+
return ret[0]["result"][0]
169211
except Exception as e:
170212
raise errors.SurrealDBHandler.UpdateEndpointError(e)
171213

0 commit comments

Comments
 (0)