Skip to content

Commit 2ea43ef

Browse files
Refactor DuckDBStore to handle JSON data and improve service retrieval logic
1 parent d702527 commit 2ea43ef

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

titiler/openeo/services/duckdb.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""titiler.openeo.services duckDB."""
22

3+
import json
34
import uuid
45
from typing import Any, Dict, List, Optional
56

@@ -9,7 +10,7 @@
910
from .base import ServicesStore
1011

1112

12-
@define(kw_only=True, init=False)
13+
@define(kw_only=True)
1314
class DuckDBStore(ServicesStore):
1415
"""DuckDB Service Store."""
1516

@@ -45,23 +46,23 @@ def get_service(self, service_id: str) -> Optional[Dict]:
4546

4647
return {
4748
"id": result[0],
48-
**result[1],
49+
**json.loads(result[1]),
4950
}
5051

5152
def get_services(self, **kwargs) -> List[Dict]:
5253
"""Return All Services."""
5354
with duckdb.connect(self.store) as con:
5455
results = con.execute(
5556
"""
56-
SELECT service_id, service
57+
SELECT service_id, json(service)
5758
FROM services
5859
"""
5960
).fetchall()
6061

6162
return [
6263
{
6364
"id": result[0],
64-
**result[1],
65+
**json.loads(result[1]),
6566
}
6667
for result in results
6768
]
@@ -71,20 +72,17 @@ def get_user_services(self, user_id: str, **kwargs) -> List[Dict]:
7172
with duckdb.connect(self.store) as con:
7273
results = con.execute(
7374
"""
74-
SELECT service_id, service
75+
SELECT service_id, service::JSON
7576
FROM services
7677
WHERE user_id = ?
7778
""",
7879
[user_id],
7980
).fetchall()
8081

81-
if not results:
82-
raise ValueError(f"Could not find service for user: {user_id}")
83-
8482
return [
8583
{
8684
"id": result[0],
87-
**result[1],
85+
**json.loads(result[1]),
8886
}
8987
for result in results
9088
]

0 commit comments

Comments
 (0)