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.

7.9 KB
Raw Blame
import { MakeLogicType, connect, kea, path, props } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'

import api from 'lib/api'
import { integrationsLogic } from 'lib/integrations/integrationsLogic'
import { lemonToast } from 'lib/lemon-ui/LemonToast'

import type { IntegrationType } from '~/types'

export interface PostgreSQLSetupModalLogicProps {
    isOpen: boolean
    integration?: IntegrationType | null
    onComplete: (integrationId?: number) => void
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface postgreSQLSetupModalLogicValues {
    isPostgreSQLIntegrationSubmitting: boolean
    isPostgreSQLIntegrationValid: boolean
    postgreSQLIntegration: {
        host: string | null
        name: string
        password: string | null
        port: number
        ssl_mode: string
        ssl_root_cert: string | null
        use_system_ca: false
        user: string | null
    }
    postgreSQLIntegrationAllErrors: Record<string, any>
    postgreSQLIntegrationChanged: boolean
    postgreSQLIntegrationErrors: DeepPartialMap<
        {
            host: string | null
            name: string
            password: string | null
            port: number
            ssl_mode: string
            ssl_root_cert: string | null
            use_system_ca: false
            user: string | null
        },
        ValidationErrorType
    >
    postgreSQLIntegrationHasErrors: boolean
    postgreSQLIntegrationManualErrors: Record<string, any>
    postgreSQLIntegrationTouched: boolean
    postgreSQLIntegrationTouches: Record<string, boolean>
    postgreSQLIntegrationValidationErrors: DeepPartialMap<
        {
            host: string | null
            name: string
            password: string | null
            port: number
            ssl_mode: string
            ssl_root_cert: string | null
            use_system_ca: false
            user: string | null
        },
        ValidationErrorType
    >
    showPostgreSQLIntegrationErrors: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface postgreSQLSetupModalLogicActions {
    loadIntegrations: (_?: any) => any // integrationsLogic
    resetPostgreSQLIntegration: (values?: {
        host: string | null
        name: string
        password: string | null
        port: number
        ssl_mode: string
        ssl_root_cert: string | null
        use_system_ca: false
        user: string | null
    }) => {
        values?: {
            host: string | null
            name: string
            password: string | null
            port: number
            ssl_mode: string
            ssl_root_cert: string | null
            use_system_ca: false
            user: string | null
        }
    }
    setPostgreSQLIntegrationManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setPostgreSQLIntegrationValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setPostgreSQLIntegrationValues: (
        values: DeepPartial<{
            host: string | null
            name: string
            password: string | null
            port: number
            ssl_mode: string
            ssl_root_cert: string | null
            use_system_ca: false
            user: string | null
        }>
    ) => {
        values: DeepPartial<{
            host: string | null
            name: string
            password: string | null
            port: number
            ssl_mode: string
            ssl_root_cert: string | null
            use_system_ca: false
            user: string | null
        }>
    }
    submitPostgreSQLIntegration: () => {
        value: boolean
    }
    submitPostgreSQLIntegrationFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitPostgreSQLIntegrationRequest: (postgreSQLIntegration: {
        host: string | null
        name: string
        password: string | null
        port: number
        ssl_mode: string
        ssl_root_cert: string | null
        use_system_ca: false
        user: string | null
    }) => {
        postgreSQLIntegration: {
            host: string | null
            name: string
            password: string | null
            port: number
            ssl_mode: string
            ssl_root_cert: string | null
            use_system_ca: false
            user: string | null
        }
    }
    submitPostgreSQLIntegrationSuccess: (postgreSQLIntegration: {
        host: string | null
        name: string
        password: string | null
        port: number
        ssl_mode: string
        ssl_root_cert: string | null
        use_system_ca: false
        user: string | null
    }) => {
        postgreSQLIntegration: {
            host: string | null
            name: string
            password: string | null
            port: number
            ssl_mode: string
            ssl_root_cert: string | null
            use_system_ca: false
            user: string | null
        }
    }
    touchPostgreSQLIntegrationField: (key: string) => {
        key: string
    }
}

export type postgreSQLSetupModalLogicType = MakeLogicType<
    postgreSQLSetupModalLogicValues,
    postgreSQLSetupModalLogicActions,
    PostgreSQLSetupModalLogicProps
>

export const postgreSQLSetupModalLogic = kea<postgreSQLSetupModalLogicType>([
    path(['integrations', 'postgreSQL', 'postgreSQLSetupModalLogic']),
    props({} as PostgreSQLSetupModalLogicProps),
    connect(() => ({
        actions: [integrationsLogic, ['loadIntegrations']],
    })),
    forms(({ props, actions, values }) => ({
        postgreSQLIntegration: {
            defaults: {
                name: '' as string,
                host: null as string | null,
                port: 5432,
                user: null as string | null,
                password: null as string | null,
                ssl_mode: 'no',
                use_system_ca: false,
                ssl_root_cert: null as string | null,
            },
            errors: ({ host, port, user, password, ssl_mode, use_system_ca, ssl_root_cert }) => ({
                host: host?.trim() ? undefined : 'Host is required',
                port: port ? undefined : 'Port is required',
                user: user?.trim() ? undefined : 'User is required',
                password: password?.trim() ? undefined : 'Password is required',
                ssl_root_cert:
                    ssl_mode === 'no' || use_system_ca || ssl_root_cert?.trim()
                        ? undefined
                        : 'Upload a root certificate, or use the system certificate authorities',
            }),
            submit: async () => {
                try {
                    const { name, host, port, user, password, ssl_mode, use_system_ca, ssl_root_cert } =
                        values.postgreSQLIntegration
                    const integration = await api.integrations.create({
                        kind: 'postgresql',
                        config: {
                            ...(name?.trim() ? { name: name.trim() } : {}),
                            host: host,
                            port: port,
                            user: user,
                            password: password,
                            ...(ssl_mode !== 'no' && {
                                ssl_mode: ssl_mode,
                                ssl_root_cert: use_system_ca ? 'system' : ssl_root_cert,
                            }),
                        },
                    })
                    actions.loadIntegrations()
                    lemonToast.success('PostgreSQL integration created successfully!')
                    props.onComplete(integration.id)
                } catch (error: any) {
                    lemonToast.error(error.detail || 'Failed to create PostgreSQL integration')
                    throw error
                }
            },
        },
    })),
])