Skip to content

Commit d035774

Browse files
authored
Allow specifying insert method for bulk transactions (#614)
* Added method parameter to bulk_item_insert route in order to allow more flexibility in how data is loaded into the DB * Add method param to AsyncBaseBulkTransactionsClient, which pgstac inherits from * Moved method on to the Items base model for bulk transaction request * Updated docs and changelog with bulk transactions method parameter
1 parent 2cb8359 commit d035774

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Forward `x-forwarded-host` ([#586](https://github.com/stac-utils/stac-fastapi/pull/586))
66
* Add CQL2-json to filter conformance class ([#611](https://github.com/stac-utils/stac-fastapi/issues/611))
7+
* Add `method` parameter to Bulk Transactions extension in order to support upserting bulk data ([#614](https://github.com/stac-utils/stac-fastapi/pull/614))
78

89
## [2.4.8] - 2023-06-07
910

stac_fastapi/extensions/stac_fastapi/extensions/third_party/bulk_transactions.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Bulk transactions extension."""
22
import abc
3+
from enum import Enum
34
from typing import Any, Dict, List, Optional, Union
45

56
import attr
@@ -11,10 +12,18 @@
1112
from stac_fastapi.types.extension import ApiExtension
1213

1314

15+
class BulkTransactionMethod(str, Enum):
16+
"""Bulk Transaction Methods."""
17+
18+
INSERT = "insert"
19+
UPSERT = "upsert"
20+
21+
1422
class Items(BaseModel):
1523
"""A group of STAC Item objects, in the form of a dictionary from Item.id -> Item."""
1624

1725
items: Dict[str, Any]
26+
method: BulkTransactionMethod = BulkTransactionMethod.INSERT
1827

1928
def __iter__(self):
2029
"""Return an iterable of STAC Item objects."""
@@ -36,7 +45,10 @@ def _chunks(lst, n):
3645

3746
@abc.abstractmethod
3847
def bulk_item_insert(
39-
self, items: Items, chunk_size: Optional[int] = None, **kwargs
48+
self,
49+
items: Items,
50+
chunk_size: Optional[int] = None,
51+
**kwargs,
4052
) -> str:
4153
"""Bulk creation of items.
4254
@@ -55,7 +67,11 @@ class AsyncBaseBulkTransactionsClient(abc.ABC):
5567
"""BulkTransactionsClient."""
5668

5769
@abc.abstractmethod
58-
async def bulk_item_insert(self, items: Items, **kwargs) -> str:
70+
async def bulk_item_insert(
71+
self,
72+
items: Items,
73+
**kwargs,
74+
) -> str:
5975
"""Bulk creation of items.
6076
6177
Args:
@@ -77,11 +93,19 @@ class BulkTransactionExtension(ApiExtension):
7793
attribute "items", that has a value that is an object with a group of
7894
attributes that are the ids of each Item, and the value is the Item entity.
7995
96+
Optionally, clients can specify a "method" attribute that is either "insert"
97+
or "upsert". If "insert", then the items will be inserted if they do not
98+
exist, and an error will be returned if they do. If "upsert", then the items
99+
will be inserted if they do not exist, and updated if they do. This defaults
100+
to "insert".
101+
80102
{
81-
"items": {
82-
"id1": { "type": "Feature", ... },
83-
"id2": { "type": "Feature", ... },
84-
"id3": { "type": "Feature", ... }
103+
"items": {
104+
"id1": { "type": "Feature", ... },
105+
"id2": { "type": "Feature", ... },
106+
"id3": { "type": "Feature", ... }
107+
},
108+
"method": "insert"
85109
}
86110
"""
87111

0 commit comments

Comments
 (0)