Skip to content

Commit ca06930

Browse files
authored
allow for multiple stream-database combinations (#4)
add `streams` config property to allow for multiple stream database combinations.
1 parent e2737e0 commit ca06930

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

target_notion/sinks.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""notion target sink class, which handles writing streams."""
22

33
from __future__ import annotations
4-
4+
import os
55
from caseconverter import snakecase
66
from notion_client import Client
77
from notion_client.errors import HTTPResponseError
@@ -18,6 +18,14 @@ def __init__(self, **kwargs) -> None: # noqa: ANN003
1818
"""Initialize the sink."""
1919
super().__init__(**kwargs)
2020
self.client = Client(auth=self.config["api_key"])
21+
database_mapping = {
22+
mapping["extractor_namespace"]: {mapping["stream_name"]: mapping["database_id"]}
23+
for mapping in self.config["streams"]
24+
}
25+
self.database_id = database_mapping.get(os.environ.get("MELTANO_EXTRACTOR_NAMESPACE"), {}).get(self.stream_name)
26+
if not self.database_id:
27+
msg = f"Database ID not found for stream {self.stream_name}."
28+
raise ValueError(msg)
2129
self.database_schema = self.get_database_schema()
2230
self.key_property = self.key_properties[0]
2331
self.snake_key_property = snakecase(self.key_property)
@@ -58,7 +66,7 @@ def get_existing_pages(self, records: list[dict]) -> list:
5866
existing_pages = {}
5967
while has_more:
6068
pages = self.client.databases.query(
61-
database_id=self.config["database_id"],
69+
database_id=self.database_id,
6270
start_cursor=start_cursor,
6371
filter_properties=[],
6472
filter=_filter,
@@ -82,7 +90,7 @@ def create_page(self, record: dict) -> None:
8290
context: Stream partition or context dictionary.
8391
"""
8492
self.client.pages.create(
85-
parent={"database_id": self.config["database_id"]},
93+
parent={"database_id": self.database_id},
8694
properties=self.create_page_properties(record),
8795
)
8896

@@ -92,7 +100,7 @@ def get_database_schema(self) -> dict:
92100
Returns:
93101
dict: The database schema.
94102
"""
95-
db = self.client.databases.retrieve(self.config["database_id"])
103+
db = self.client.databases.retrieve(self.database_id)
96104
return {
97105
snakecase(name): {"name": name, "type": _property["type"]} for name, _property in db["properties"].items()
98106
}

target_notion/target.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,32 @@ class Targetnotion(Target):
1717

1818
config_jsonschema = th.PropertiesList(
1919
th.Property(
20-
"database_id",
21-
th.StringType(nullable=False),
22-
description="ID of the parent page you want to create the database in",
20+
"streams",
21+
th.ArrayType(
22+
th.ObjectType(
23+
th.Property(
24+
"extractor_namespace",
25+
th.StringType,
26+
description="Extractor namespace which contains the stream to be moved to Notion database.",
27+
required=True,
28+
),
29+
th.Property(
30+
"stream_name",
31+
th.StringType,
32+
description="Name of the input stream to be moved to Notion database.",
33+
required=True,
34+
),
35+
th.Property(
36+
"database_id",
37+
th.StringType,
38+
description="Id of the Notion database to be used.",
39+
required=True,
40+
),
41+
nullable=False,
42+
),
43+
),
44+
required=True,
45+
description="List of streams to be moved to Notion database.",
2346
),
2447
th.Property(
2548
"api_key",

0 commit comments

Comments
 (0)