-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcollection.py
36 lines (30 loc) · 1.11 KB
/
collection.py
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
import os
from pypgstac.db import PgstacDB
from pypgstac.load import Methods
from .loader import Loader
from .schemas import StacCollection
from .utils import get_db_credentials
def ingest(collection: StacCollection):
"""
Takes a collection model,
does necessary preprocessing,
and loads into the PgSTAC collection table
"""
try:
creds = get_db_credentials(os.environ["DB_SECRET_ARN"])
with PgstacDB(dsn=creds.dsn_string, debug=True) as db:
loader = Loader(db=db)
collection = [
collection.to_dict()
] # pypgstac wants either a string or an Iterable of dicts.
loader.load_collections(file=collection, insert_mode=Methods.upsert)
except Exception as e:
print(f"Encountered failure loading collection into pgSTAC: {e}")
def delete(collection_id: str):
"""
Deletes the collection from the database
"""
creds = get_db_credentials(os.environ["DB_SECRET_ARN"])
with PgstacDB(dsn=creds.dsn_string, debug=True) as db:
loader = Loader(db=db)
loader.delete_collection(collection_id)