|
8 | 8 | import random |
9 | 9 | import string |
10 | 10 | import datetime |
| 11 | +import asyncio |
11 | 12 |
|
12 | 13 | async def sync_local_files(folderpath: str, specific: bool = False): |
13 | 14 | try: |
@@ -164,8 +165,49 @@ async def overwrite_json(endpoint: int, json_data: dict): |
164 | 165 | async with Surreal(config.SDB_URL) as db: |
165 | 166 | await db.signin({"user": config.SDB_USER, "pass": config.SDB_PASS}) |
166 | 167 | 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] |
169 | 211 | except Exception as e: |
170 | 212 | raise errors.SurrealDBHandler.UpdateEndpointError(e) |
171 | 213 |
|
|
0 commit comments