Skip to content

Commit d5b5009

Browse files
committed
db logic helpers
1 parent d3d4b16 commit d5b5009

File tree

4 files changed

+52
-52
lines changed

4 files changed

+52
-52
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
ElasticsearchSettings as SyncElasticsearchSettings,
2222
)
2323
from stac_fastapi.sfeos_helpers import filter
24+
from stac_fastapi.sfeos_helpers.database_logic_helpers import (
25+
create_index_templates_shared,
26+
)
2427
from stac_fastapi.sfeos_helpers.mappings import (
2528
COLLECTIONS_INDEX,
2629
DEFAULT_SORT,
27-
ES_COLLECTIONS_MAPPINGS,
28-
ES_ITEMS_MAPPINGS,
29-
ES_ITEMS_SETTINGS,
3030
ITEM_INDICES,
3131
ITEMS_INDEX_PREFIX,
3232
Geometry,
@@ -51,22 +51,7 @@ async def create_index_templates() -> None:
5151
None
5252
5353
"""
54-
client = AsyncElasticsearchSettings().create_client
55-
await client.indices.put_index_template(
56-
name=f"template_{COLLECTIONS_INDEX}",
57-
body={
58-
"index_patterns": [f"{COLLECTIONS_INDEX}*"],
59-
"template": {"mappings": ES_COLLECTIONS_MAPPINGS},
60-
},
61-
)
62-
await client.indices.put_index_template(
63-
name=f"template_{ITEMS_INDEX_PREFIX}",
64-
body={
65-
"index_patterns": [f"{ITEMS_INDEX_PREFIX}*"],
66-
"template": {"settings": ES_ITEMS_SETTINGS, "mappings": ES_ITEMS_MAPPINGS},
67-
},
68-
)
69-
await client.close()
54+
await create_index_templates_shared(settings=AsyncElasticsearchSettings())
7055

7156

7257
async def create_collection_index() -> None:

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
)
2222
from stac_fastapi.opensearch.config import OpensearchSettings as SyncSearchSettings
2323
from stac_fastapi.sfeos_helpers import filter
24+
from stac_fastapi.sfeos_helpers.database_logic_helpers import (
25+
create_index_templates_shared,
26+
)
2427
from stac_fastapi.sfeos_helpers.mappings import (
2528
COLLECTIONS_INDEX,
2629
DEFAULT_SORT,
@@ -51,23 +54,7 @@ async def create_index_templates() -> None:
5154
None
5255
5356
"""
54-
client = AsyncSearchSettings().create_client
55-
await client.indices.put_template(
56-
name=f"template_{COLLECTIONS_INDEX}",
57-
body={
58-
"index_patterns": [f"{COLLECTIONS_INDEX}*"],
59-
"mappings": ES_COLLECTIONS_MAPPINGS,
60-
},
61-
)
62-
await client.indices.put_template(
63-
name=f"template_{ITEMS_INDEX_PREFIX}",
64-
body={
65-
"index_patterns": [f"{ITEMS_INDEX_PREFIX}*"],
66-
"settings": ES_ITEMS_SETTINGS,
67-
"mappings": ES_ITEMS_MAPPINGS,
68-
},
69-
)
70-
await client.close()
57+
await create_index_templates_shared(settings=AsyncSearchSettings())
7158

7259

7360
async def create_collection_index() -> None:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Shared code for elasticsearch/ opensearch database logic."""
2+
3+
from typing import Any
4+
5+
from stac_fastapi.sfeos_helpers.mappings import (
6+
COLLECTIONS_INDEX,
7+
ES_COLLECTIONS_MAPPINGS,
8+
ES_ITEMS_MAPPINGS,
9+
ES_ITEMS_SETTINGS,
10+
ITEMS_INDEX_PREFIX,
11+
)
12+
13+
14+
async def create_index_templates_shared(settings: Any) -> None:
15+
"""
16+
Create index templates for the Collection and Item indices.
17+
18+
Returns:
19+
None
20+
21+
"""
22+
client = settings.create_client
23+
await client.indices.put_index_template(
24+
name=f"template_{COLLECTIONS_INDEX}",
25+
body={
26+
"index_patterns": [f"{COLLECTIONS_INDEX}*"],
27+
"template": {"mappings": ES_COLLECTIONS_MAPPINGS},
28+
},
29+
)
30+
await client.indices.put_index_template(
31+
name=f"template_{ITEMS_INDEX_PREFIX}",
32+
body={
33+
"index_patterns": [f"{ITEMS_INDEX_PREFIX}*"],
34+
"template": {"settings": ES_ITEMS_SETTINGS, "mappings": ES_ITEMS_MAPPINGS},
35+
},
36+
)
37+
await client.close()

stac_fastapi/tests/database/test_database.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1-
import os
21
import uuid
32

43
import pytest
54
from stac_pydantic import api
65

7-
from ..conftest import MockRequest, database
6+
from stac_fastapi.sfeos_helpers.mappings import (
7+
COLLECTIONS_INDEX,
8+
ES_COLLECTIONS_MAPPINGS,
9+
ES_ITEMS_MAPPINGS,
10+
index_alias_by_collection_id,
11+
)
812

9-
if os.getenv("BACKEND", "elasticsearch").lower() == "opensearch":
10-
from stac_fastapi.opensearch.database_logic import (
11-
COLLECTIONS_INDEX,
12-
ES_COLLECTIONS_MAPPINGS,
13-
ES_ITEMS_MAPPINGS,
14-
index_alias_by_collection_id,
15-
)
16-
else:
17-
from stac_fastapi.elasticsearch.database_logic import (
18-
COLLECTIONS_INDEX,
19-
ES_COLLECTIONS_MAPPINGS,
20-
ES_ITEMS_MAPPINGS,
21-
index_alias_by_collection_id,
22-
)
13+
from ..conftest import MockRequest, database
2314

2415

2516
@pytest.mark.asyncio

0 commit comments

Comments
 (0)