|
1 | 1 | """tipg.dbmodel: database events.""" |
2 | 2 |
|
| 3 | +import abc |
3 | 4 | import datetime |
4 | 5 | import re |
5 | 6 | from functools import lru_cache |
@@ -158,13 +159,12 @@ class Parameter(Column): |
158 | 159 | default: Optional[str] = None |
159 | 160 |
|
160 | 161 |
|
161 | | -class Collection(BaseModel): |
162 | | - """Model for DB Table and Function.""" |
| 162 | +class Collection(BaseModel, metaclass=abc.ABCMeta): |
| 163 | + """Collection Base Class.""" |
163 | 164 |
|
164 | 165 | type: str |
165 | 166 | id: str |
166 | 167 | table: str |
167 | | - dbschema: str = Field(alias="schema") |
168 | 168 | title: Optional[str] = None |
169 | 169 | description: Optional[str] = None |
170 | 170 | table_columns: List[Column] = [] |
@@ -298,6 +298,57 @@ def get_column(self, property_name: str) -> Optional[Column]: |
298 | 298 |
|
299 | 299 | return None |
300 | 300 |
|
| 301 | + @property |
| 302 | + def queryables(self) -> Dict: |
| 303 | + """Return the queryables.""" |
| 304 | + if self.geometry_columns: |
| 305 | + geoms = { |
| 306 | + col.name: {"$ref": geojson_schema.get(col.geometry_type.upper(), "")} |
| 307 | + for col in self.geometry_columns |
| 308 | + } |
| 309 | + else: |
| 310 | + geoms = {} |
| 311 | + |
| 312 | + props = { |
| 313 | + col.name: {"name": col.name, "type": col.json_type} |
| 314 | + for col in self.properties |
| 315 | + if col.name not in geoms |
| 316 | + } |
| 317 | + |
| 318 | + return {**geoms, **props} |
| 319 | + |
| 320 | + @abc.abstractmethod |
| 321 | + async def features(self, *args, **kwargs) -> ItemList: |
| 322 | + """Get Items.""" |
| 323 | + ... |
| 324 | + |
| 325 | + @abc.abstractmethod |
| 326 | + async def get_tile(self, *args, **kwargs) -> bytes: |
| 327 | + """Get MVT Tile.""" |
| 328 | + ... |
| 329 | + |
| 330 | + |
| 331 | +class CollectionList(TypedDict): |
| 332 | + """Collections.""" |
| 333 | + |
| 334 | + collections: List[Collection] |
| 335 | + matched: Optional[int] |
| 336 | + next: Optional[int] |
| 337 | + prev: Optional[int] |
| 338 | + |
| 339 | + |
| 340 | +class Catalog(TypedDict): |
| 341 | + """Internal Collection Catalog.""" |
| 342 | + |
| 343 | + collections: Dict[str, Collection] |
| 344 | + last_updated: datetime.datetime |
| 345 | + |
| 346 | + |
| 347 | +class PgCollection(Collection): |
| 348 | + """Model for DB Table and Function.""" |
| 349 | + |
| 350 | + dbschema: str = Field(alias="schema") |
| 351 | + |
301 | 352 | def _select_no_geo(self, properties: Optional[List[str]], addid: bool = True): |
302 | 353 | nocomma = False |
303 | 354 | columns = self.columns(properties) |
@@ -858,42 +909,9 @@ async def get_tile( |
858 | 909 | debug_query(q, *p) |
859 | 910 |
|
860 | 911 | async with pool.acquire() as conn: |
861 | | - return await conn.fetchval(q, *p) |
862 | | - |
863 | | - @property |
864 | | - def queryables(self) -> Dict: |
865 | | - """Return the queryables.""" |
866 | | - if self.geometry_columns: |
867 | | - geoms = { |
868 | | - col.name: {"$ref": geojson_schema.get(col.geometry_type.upper(), "")} |
869 | | - for col in self.geometry_columns |
870 | | - } |
871 | | - else: |
872 | | - geoms = {} |
| 912 | + tile = await conn.fetchval(q, *p) |
873 | 913 |
|
874 | | - props = { |
875 | | - col.name: {"name": col.name, "type": col.json_type} |
876 | | - for col in self.properties |
877 | | - if col.name not in geoms |
878 | | - } |
879 | | - |
880 | | - return {**geoms, **props} |
881 | | - |
882 | | - |
883 | | -class CollectionList(TypedDict): |
884 | | - """Collections.""" |
885 | | - |
886 | | - collections: List[Collection] |
887 | | - matched: Optional[int] |
888 | | - next: Optional[int] |
889 | | - prev: Optional[int] |
890 | | - |
891 | | - |
892 | | -class Catalog(TypedDict): |
893 | | - """Internal Collection Catalog.""" |
894 | | - |
895 | | - collections: Dict[str, Collection] |
896 | | - last_updated: datetime.datetime |
| 914 | + return bytes(tile) |
897 | 915 |
|
898 | 916 |
|
899 | 917 | async def get_collection_index( # noqa: C901 |
@@ -990,7 +1008,7 @@ async def get_collection_index( # noqa: C901 |
990 | 1008 | if table_conf.geomcol == c["name"] or geometry_column is None: |
991 | 1009 | geometry_column = c |
992 | 1010 |
|
993 | | - catalog[table_id] = Collection( |
| 1011 | + catalog[table_id] = PgCollection( |
994 | 1012 | type=table["entity"], |
995 | 1013 | id=table_id, |
996 | 1014 | table=table["name"], |
|
0 commit comments