Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

5.8 KB
Raw Blame
from typing import cast

from products.warehouse_sources.sources.neo4j._config import Neo4jSourceConfig
from products.warehouse_sources.sources.neo4j.canonical_descriptions import NODE_DESCRIPTION, RELATIONSHIP_DESCRIPTION
from products.warehouse_sources.sources.neo4j.neo4j import Neo4jClient, neo4j_source, validate_credentials
from products.warehouse_sources.sources.neo4j.settings import API_VERSION, NON_RETRYABLE_ERRORS
from products.warehouse_sources.sources.sdk import (
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    SimpleSource,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    ValidateDatabaseHostMixin,
    detect,
)


@SourceRegistry.register
class Neo4jSource(SimpleSource[Neo4jSourceConfig], ValidateDatabaseHostMixin):
    codebase_detection = detect(
        env=("^NEO4J_",),
        npm=("neo4j-driver",),
        pypi=("neo4j",),
        cargo=("neo4rs",),
        go=("github.com/neo4j/neo4j-go-driver",),
        hex=("bolt_sips",),
        maven=(
            "org.neo4j.driver:neo4j-java-driver",
            "org.neo4j.driver:neo4j-java-driver-all",
            "org.neo4j.driver:neo4j-java-driver-encryption-aws-kms",
            "org.neo4j.driver:neo4j-java-driver-encryption-azure-keyvault",
            "org.neo4j.driver:neo4j-java-driver-encryption-google-cloud-kms",
            "org.neo4j.driver:neo4j-java-driver-observation-metrics",
            "org.neo4j.driver:neo4j-java-driver-observation-micrometer",
            "org.neo4j.driver:neo4j-java-driver-slim",
            "org.neo4j.driver:neo4j-java-driver-spring-boot-autoconfigure",
            "org.neo4j.driver:neo4j-java-driver-spring-boot-starter",
        ),
        rubygems=(
            "neo4j-ruby-driver",
            "cancancan-neo4j",
            "neo4j",
            "neo4j-core",
            "neo4j-java-driver",
            "neo4j-rake_tasks",
        ),
    )

    supported_versions = (API_VERSION,)
    default_version = API_VERSION
    api_docs_url = "https://neo4j.com/docs/query-api/current/"

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.NEO4J

    @property
    def connection_host_fields(self) -> list[str]:
        return ["host"]

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return dict(NON_RETRYABLE_ERRORS)

    def validate_credentials(
        self,
        config: Neo4jSourceConfig,
        team_id: int,
        schema_name: str | None = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        return validate_credentials(config, team_id)

    def get_schemas(
        self,
        config: Neo4jSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        return [
            SourceSchema(
                name=name,
                supports_incremental=False,
                supports_append=False,
                detected_primary_keys=["element_id"],
                description=(NODE_DESCRIPTION if name.startswith("node_") else RELATIONSHIP_DESCRIPTION)["description"],
            )
            for name in Neo4jClient(config, team_id).discover_tables()
            if names is None or name in names
        ]

    def source_for_pipeline(self, config: Neo4jSourceConfig, inputs: SourceInputs) -> SourceResponse:
        return neo4j_source(config, inputs.team_id, inputs.schema_name)

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.NEO4J,
            category=DataWarehouseSourceCategory.DATABASES,
            label="Neo4j",
            caption=(
                "Use the database credentials saved when you created your Aura instance, or ask your database administrator. "
                "Requires a public HTTPS endpoint with Query API enabled (Neo4j 5.19 or later). "
                "Imports support full refresh only, with a limit of 1,000,000 rows per table."
            ),
            keywords=["graph database", "cypher", "auradb"],
            iconPath="/static/services/neo4j.png",
            releaseStatus=ReleaseStatus.BETA,
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="host",
                        label="HTTPS host",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="https://your-instance.databases.neo4j.io",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="database",
                        label="Database",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="neo4j",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="username",
                        label="Username",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="neo4j",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="password",
                        label="Password",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )