From 7cfeeb8febc9fa8a85720d5efddc6548a80cd93e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daphn=C3=A9=20Popin?= <daphne.popin@gmail.com> Date: Fri, 17 Nov 2023 15:09:11 +0100 Subject: [PATCH] Connector: Move models in file per connector --- .../20230505_add_notion_connector_state.ts | 3 +- .../20230725_slack_channel_permissions.ts | 3 +- .../20230828_notion_block_parents.ts | 3 +- .../20230906_2_slack_fill_parents_field.ts | 3 +- .../20230906_3_github_fill_parents_field.ts | 7 +- .../20230906_notion_fill_parents_field.ts | 3 +- .../20231109_2_create_gdrive_config.ts | 3 +- ...31109_incident_gdrive_non_deleted_files.ts | 3 +- connectors/src/admin/cli.ts | 9 +- connectors/src/admin/db.ts | 19 +- connectors/src/api/get_connector.ts | 9 +- .../api/slack_channels_linked_with_agent.ts | 3 +- connectors/src/api/webhooks/webhook_github.ts | 3 +- .../src/api/webhooks/webhook_google_drive.ts | 2 +- connectors/src/api/webhooks/webhook_slack.ts | 7 +- connectors/src/connectors/github/index.ts | 6 +- .../connectors/github/temporal/activities.ts | 7 +- .../src/connectors/google_drive/index.ts | 10 +- connectors/src/connectors/google_drive/lib.ts | 3 +- .../google_drive/temporal/activities.ts | 6 +- connectors/src/connectors/notion/index.ts | 6 +- .../notion/lib/connectors_db_helpers.ts | 8 +- .../src/connectors/notion/lib/parents.ts | 8 +- .../connectors/notion/temporal/activities.ts | 7 +- connectors/src/connectors/slack/bot.ts | 5 +- connectors/src/connectors/slack/index.ts | 6 +- .../src/connectors/slack/lib/channels.ts | 8 +- .../connectors/slack/temporal/activities.ts | 8 +- connectors/src/lib/models.ts | 1338 ----------------- connectors/src/lib/models/github.ts | 154 ++ connectors/src/lib/models/google_drive.ts | 296 ++++ connectors/src/lib/models/index.ts | 134 ++ connectors/src/lib/models/notion.ts | 479 ++++++ connectors/src/lib/models/slack.ts | 286 ++++ connectors/src/lib/temporal.ts | 2 +- 35 files changed, 1424 insertions(+), 1433 deletions(-) delete mode 100644 connectors/src/lib/models.ts create mode 100644 connectors/src/lib/models/github.ts create mode 100644 connectors/src/lib/models/google_drive.ts create mode 100644 connectors/src/lib/models/index.ts create mode 100644 connectors/src/lib/models/notion.ts create mode 100644 connectors/src/lib/models/slack.ts diff --git a/connectors/migrations/20230505_add_notion_connector_state.ts b/connectors/migrations/20230505_add_notion_connector_state.ts index 481cf2825abce..7d48270e9984c 100644 --- a/connectors/migrations/20230505_add_notion_connector_state.ts +++ b/connectors/migrations/20230505_add_notion_connector_state.ts @@ -1,4 +1,5 @@ -import { Connector, NotionConnectorState } from "@connectors/lib/models"; +import { Connector } from "@connectors/lib/models"; +import { NotionConnectorState } from "@connectors/lib/models/notion"; async function main() { const connectors = await Connector.findAll(); diff --git a/connectors/migrations/20230725_slack_channel_permissions.ts b/connectors/migrations/20230725_slack_channel_permissions.ts index b0bd0f0e8fc5d..0ef6238e07dbd 100644 --- a/connectors/migrations/20230725_slack_channel_permissions.ts +++ b/connectors/migrations/20230725_slack_channel_permissions.ts @@ -1,5 +1,6 @@ import { getChannels } from "@connectors/connectors/slack/temporal/activities"; -import { Connector, SlackChannel } from "@connectors/lib/models"; +import { Connector } from "@connectors/lib/models"; +import { SlackChannel } from "@connectors/lib/models/slack"; async function main() { const slackConnectors = await Connector.findAll({ diff --git a/connectors/migrations/20230828_notion_block_parents.ts b/connectors/migrations/20230828_notion_block_parents.ts index 7f456d7759fa3..38c74ffd1ad71 100644 --- a/connectors/migrations/20230828_notion_block_parents.ts +++ b/connectors/migrations/20230828_notion_block_parents.ts @@ -7,7 +7,8 @@ import PQueue from "p-queue"; import { Op } from "sequelize"; import { getBlockParentMemoized } from "@connectors/connectors/notion/lib/notion_api"; -import { Connector, NotionDatabase, NotionPage } from "@connectors/lib/models"; +import { Connector } from "@connectors/lib/models"; +import { NotionDatabase, NotionPage } from "@connectors/lib/models/notion"; import { nango_client } from "@connectors/lib/nango_client"; import mainLogger from "@connectors/logger/logger"; diff --git a/connectors/migrations/20230906_2_slack_fill_parents_field.ts b/connectors/migrations/20230906_2_slack_fill_parents_field.ts index 20ba934d3602d..403b875d59349 100644 --- a/connectors/migrations/20230906_2_slack_fill_parents_field.ts +++ b/connectors/migrations/20230906_2_slack_fill_parents_field.ts @@ -2,7 +2,8 @@ import { existsSync, readFileSync, writeFileSync } from "fs"; import { Op } from "sequelize"; import { updateDocumentParentsField } from "@connectors/lib/data_sources"; -import { Connector, SlackMessages } from "@connectors/lib/models"; +import { Connector } from "@connectors/lib/models"; +import { SlackMessages } from "@connectors/lib/models/slack"; async function main() { if (!process.argv[2]) { diff --git a/connectors/migrations/20230906_3_github_fill_parents_field.ts b/connectors/migrations/20230906_3_github_fill_parents_field.ts index 5eec1d5e56212..94b4ee2f75bf4 100644 --- a/connectors/migrations/20230906_3_github_fill_parents_field.ts +++ b/connectors/migrations/20230906_3_github_fill_parents_field.ts @@ -6,11 +6,8 @@ import { getIssueDocumentId, } from "@connectors/connectors/github/temporal/activities"; import { updateDocumentParentsField } from "@connectors/lib/data_sources"; -import { - Connector, - GithubDiscussion, - GithubIssue, -} from "@connectors/lib/models"; +import { Connector } from "@connectors/lib/models"; +import { GithubDiscussion, GithubIssue } from "@connectors/lib/models/github"; async function main() { if (!process.argv[2]) { diff --git a/connectors/migrations/20230906_notion_fill_parents_field.ts b/connectors/migrations/20230906_notion_fill_parents_field.ts index aa77465c82e91..8f78ba476e45a 100644 --- a/connectors/migrations/20230906_notion_fill_parents_field.ts +++ b/connectors/migrations/20230906_notion_fill_parents_field.ts @@ -2,7 +2,8 @@ import { existsSync, readFileSync, writeFileSync } from "fs"; import { Op } from "sequelize"; import { updateAllParentsFields } from "@connectors/connectors/notion/lib/parents"; -import { Connector, NotionDatabase, NotionPage } from "@connectors/lib/models"; +import { Connector } from "@connectors/lib/models"; +import { NotionDatabase, NotionPage } from "@connectors/lib/models/notion"; async function main() { if (!process.argv[2]) { diff --git a/connectors/migrations/20231109_2_create_gdrive_config.ts b/connectors/migrations/20231109_2_create_gdrive_config.ts index 8186aee6d7c02..25c061ccaa978 100644 --- a/connectors/migrations/20231109_2_create_gdrive_config.ts +++ b/connectors/migrations/20231109_2_create_gdrive_config.ts @@ -1,4 +1,5 @@ -import { Connector, GoogleDriveConfig } from "@connectors/lib/models"; +import { Connector } from "@connectors/lib/models"; +import { GoogleDriveConfig } from "@connectors/lib/models/google_drive"; async function main() { const gDriveConnectors = await Connector.findAll({ diff --git a/connectors/migrations/20231109_incident_gdrive_non_deleted_files.ts b/connectors/migrations/20231109_incident_gdrive_non_deleted_files.ts index d274609477665..86a169dae852a 100644 --- a/connectors/migrations/20231109_incident_gdrive_non_deleted_files.ts +++ b/connectors/migrations/20231109_incident_gdrive_non_deleted_files.ts @@ -2,7 +2,8 @@ import { Sequelize } from "sequelize"; import { dataSourceConfigFromConnector } from "@connectors/lib/api/data_source_config"; import { deleteFromDataSource } from "@connectors/lib/data_sources"; -import { Connector, GoogleDriveFiles } from "@connectors/lib/models"; +import { Connector } from "@connectors/lib/models"; +import { GoogleDriveFiles } from "@connectors/lib/models/google_drive"; // To be run from connectors with `CORE_DATABASE_URI` and `FRONT_DATABASE_URI` set. const { CORE_DATABASE_URI, FRONT_DATABASE_URI, LIVE = false } = process.env; diff --git a/connectors/src/admin/cli.ts b/connectors/src/admin/cli.ts index b430e3d7e3104..d9b4be3c0c325 100644 --- a/connectors/src/admin/cli.ts +++ b/connectors/src/admin/cli.ts @@ -14,12 +14,9 @@ import { } from "@connectors/connectors/google_drive/temporal/client"; import { toggleSlackbot } from "@connectors/connectors/slack/bot"; import { launchSlackSyncOneThreadWorkflow } from "@connectors/connectors/slack/temporal/client"; -import { - Connector, - GoogleDriveFiles, - NotionDatabase, - NotionPage, -} from "@connectors/lib/models"; +import { Connector } from "@connectors/lib/models"; +import { GoogleDriveFiles } from "@connectors/lib/models/google_drive"; +import { NotionDatabase, NotionPage } from "@connectors/lib/models/notion"; import { Result } from "@connectors/lib/result"; const connectors = async (command: string, args: parseArgs.ParsedArgs) => { diff --git a/connectors/src/admin/db.ts b/connectors/src/admin/db.ts index 787918f8cb511..0e251f58c45ad 100644 --- a/connectors/src/admin/db.ts +++ b/connectors/src/admin/db.ts @@ -1,31 +1,36 @@ import { Sequelize } from "sequelize"; +import { Connector, sequelize_conn } from "@connectors/lib/models"; import { - Connector, GithubConnectorState, GithubDiscussion, GithubIssue, +} from "@connectors/lib/models/github"; +import { GoogleDriveConfig, GoogleDriveFiles, GoogleDriveFolders, GoogleDriveSyncToken, GoogleDriveWebhook, +} from "@connectors/lib/models/google_drive"; +import { + IntercomArticle, + IntercomCollection, +} from "@connectors/lib/models/intercom"; +import { NotionConnectorBlockCacheEntry, NotionConnectorPageCacheEntry, NotionConnectorResourcesToCheckCacheEntry, NotionConnectorState, NotionDatabase, NotionPage, - sequelize_conn, +} from "@connectors/lib/models/notion"; +import { SlackChannel, SlackChatBotMessage, SlackConfiguration, SlackMessages, -} from "@connectors/lib/models"; -import { - IntercomArticle, - IntercomCollection, -} from "@connectors/lib/models/intercom"; +} from "@connectors/lib/models/slack"; import logger from "@connectors/logger/logger"; async function main(): Promise<void> { diff --git a/connectors/src/api/get_connector.ts b/connectors/src/api/get_connector.ts index 8fca7218cdf90..8c02a645bd6b5 100644 --- a/connectors/src/api/get_connector.ts +++ b/connectors/src/api/get_connector.ts @@ -1,11 +1,8 @@ import { Request, Response } from "express"; -import { - Connector, - GithubDiscussion, - GithubIssue, - NotionPage, -} from "@connectors/lib/models"; +import { Connector } from "@connectors/lib/models"; +import { GithubDiscussion, GithubIssue } from "@connectors/lib/models/github"; +import { NotionPage } from "@connectors/lib/models/notion"; import { apiError, withLogging } from "@connectors/logger/withlogging"; import { ConnectorType } from "@connectors/types/connector"; import { ConnectorsAPIErrorResponse } from "@connectors/types/errors"; diff --git a/connectors/src/api/slack_channels_linked_with_agent.ts b/connectors/src/api/slack_channels_linked_with_agent.ts index 6c5c844327cce..e9b90cd6528e3 100644 --- a/connectors/src/api/slack_channels_linked_with_agent.ts +++ b/connectors/src/api/slack_channels_linked_with_agent.ts @@ -7,7 +7,8 @@ import { Op } from "sequelize"; import { joinChannel } from "@connectors/connectors/slack/lib/channels"; import { getChannels } from "@connectors/connectors/slack/temporal/activities"; import { APIErrorWithStatusCode } from "@connectors/lib/error"; -import { sequelize_conn, SlackChannel } from "@connectors/lib/models"; +import { sequelize_conn } from "@connectors/lib/models"; +import { SlackChannel } from "@connectors/lib/models/slack"; import { apiError, withLogging } from "@connectors/logger/withlogging"; const PatchSlackChannelsLinkedWithAgentReqBodySchema = t.type({ diff --git a/connectors/src/api/webhooks/webhook_github.ts b/connectors/src/api/webhooks/webhook_github.ts index 0723d62c322db..fd9798bd67be2 100644 --- a/connectors/src/api/webhooks/webhook_github.ts +++ b/connectors/src/api/webhooks/webhook_github.ts @@ -21,7 +21,8 @@ import { launchGithubReposSyncWorkflow, } from "@connectors/connectors/github/temporal/client"; import { assertNever } from "@connectors/lib/assert_never"; -import { Connector, GithubConnectorState } from "@connectors/lib/models"; +import { Connector } from "@connectors/lib/models"; +import { GithubConnectorState } from "@connectors/lib/models/github"; import mainLogger from "@connectors/logger/logger"; import { withLogging } from "@connectors/logger/withlogging"; import { ConnectorsAPIErrorResponse } from "@connectors/types/errors"; diff --git a/connectors/src/api/webhooks/webhook_google_drive.ts b/connectors/src/api/webhooks/webhook_google_drive.ts index 86bc779b22927..6c426602d7e8c 100644 --- a/connectors/src/api/webhooks/webhook_google_drive.ts +++ b/connectors/src/api/webhooks/webhook_google_drive.ts @@ -2,7 +2,7 @@ import { Request, Response } from "express"; import { launchGoogleDriveIncrementalSyncWorkflow } from "@connectors/connectors/google_drive/temporal/client"; import { APIErrorWithStatusCode } from "@connectors/lib/error"; -import { GoogleDriveWebhook } from "@connectors/lib/models"; +import { GoogleDriveWebhook } from "@connectors/lib/models/google_drive"; import { apiError, withLogging } from "@connectors/logger/withlogging"; type GoogleDriveWebhookResBody = null | APIErrorWithStatusCode; diff --git a/connectors/src/api/webhooks/webhook_slack.ts b/connectors/src/api/webhooks/webhook_slack.ts index 19552a36c5fde..f02cfa771ef80 100644 --- a/connectors/src/api/webhooks/webhook_slack.ts +++ b/connectors/src/api/webhooks/webhook_slack.ts @@ -8,11 +8,8 @@ import { } from "@connectors/connectors/slack/temporal/client"; import { launchSlackGarbageCollectWorkflow } from "@connectors/connectors/slack/temporal/client"; import { APIErrorWithStatusCode } from "@connectors/lib/error"; -import { - Connector, - SlackChannel, - SlackConfiguration, -} from "@connectors/lib/models"; +import { Connector } from "@connectors/lib/models"; +import { SlackChannel, SlackConfiguration } from "@connectors/lib/models/slack"; import { Ok } from "@connectors/lib/result"; import logger from "@connectors/logger/logger"; import { apiError, withLogging } from "@connectors/logger/withlogging"; diff --git a/connectors/src/connectors/github/index.ts b/connectors/src/connectors/github/index.ts index 0a9c6362d214f..04e12b4f611a2 100644 --- a/connectors/src/connectors/github/index.ts +++ b/connectors/src/connectors/github/index.ts @@ -3,13 +3,11 @@ import { validateInstallationId, } from "@connectors/connectors/github/lib/github_api"; import { launchGithubFullSyncWorkflow } from "@connectors/connectors/github/temporal/client"; +import { Connector, ModelId, sequelize_conn } from "@connectors/lib/models"; import { - Connector, GithubConnectorState, GithubIssue, - ModelId, - sequelize_conn, -} from "@connectors/lib/models"; +} from "@connectors/lib/models/github"; import { Err, Ok, Result } from "@connectors/lib/result"; import mainLogger from "@connectors/logger/logger"; import { DataSourceConfig } from "@connectors/types/data_source_config"; diff --git a/connectors/src/connectors/github/temporal/activities.ts b/connectors/src/connectors/github/temporal/activities.ts index eb98eb1e461fb..f2a5f680e9429 100644 --- a/connectors/src/connectors/github/temporal/activities.ts +++ b/connectors/src/connectors/github/temporal/activities.ts @@ -15,11 +15,8 @@ import { deleteFromDataSource, upsertToDatasource, } from "@connectors/lib/data_sources"; -import { - Connector, - GithubDiscussion, - GithubIssue, -} from "@connectors/lib/models"; +import { Connector } from "@connectors/lib/models"; +import { GithubDiscussion, GithubIssue } from "@connectors/lib/models/github"; import { syncStarted, syncSucceeded } from "@connectors/lib/sync_status"; import mainLogger from "@connectors/logger/logger"; import { DataSourceConfig } from "@connectors/types/data_source_config"; diff --git a/connectors/src/connectors/google_drive/index.ts b/connectors/src/connectors/google_drive/index.ts index 5a67e3cb47d8a..e2c190c03cef7 100644 --- a/connectors/src/connectors/google_drive/index.ts +++ b/connectors/src/connectors/google_drive/index.ts @@ -7,16 +7,18 @@ import { registerWebhook, } from "@connectors/connectors/google_drive/lib"; import { ConnectorPermissionRetriever } from "@connectors/connectors/interface"; -import { +import {@connectors/lib/models/connector.js Connector, + ModelId, + sequelize_conn, +} from "@connectors/lib/connector.js"; +import { GoogleDriveConfig, GoogleDriveFiles, GoogleDriveFolders, GoogleDriveSyncToken, GoogleDriveWebhook, - ModelId, - sequelize_conn, -} from "@connectors/lib/models.js"; +} from "@connectors/lib/models/google_drive"; import { nangoDeleteConnection } from "@connectors/lib/nango_client"; import { Err, Ok, type Result } from "@connectors/lib/result.js"; import logger from "@connectors/logger/logger"; diff --git a/connectors/src/connectors/google_drive/lib.ts b/connectors/src/connectors/google_drive/lib.ts index 40c927ab15665..7c76bd90bc922 100644 --- a/connectors/src/connectors/google_drive/lib.ts +++ b/connectors/src/connectors/google_drive/lib.ts @@ -2,7 +2,8 @@ import memoize from "lodash.memoize"; import { v4 as uuidv4 } from "uuid"; import { HTTPError } from "@connectors/lib/error"; -import { Connector, GoogleDriveFiles, ModelId } from "@connectors/lib/models"; +import { Connector, ModelId } from "@connectors/lib/models"; +import { GoogleDriveFiles } from "@connectors/lib/models/google_drive"; import { Err, Ok, type Result } from "@connectors/lib/result.js"; import { getAuthObject } from "./temporal/activities"; diff --git a/connectors/src/connectors/google_drive/temporal/activities.ts b/connectors/src/connectors/google_drive/temporal/activities.ts index 9085f717d0134..265bd02a89de5 100644 --- a/connectors/src/connectors/google_drive/temporal/activities.ts +++ b/connectors/src/connectors/google_drive/temporal/activities.ts @@ -24,16 +24,14 @@ import { CreationAttributes, literal, Op } from "sequelize"; import { dataSourceConfigFromConnector } from "@connectors/lib/api/data_source_config"; import { dpdf2text } from "@connectors/lib/dpdf2text"; import { ExternalOauthTokenError } from "@connectors/lib/error"; +import { Connector, ModelId, sequelize_conn } from "@connectors/lib/models"; import { - Connector, GoogleDriveConfig, GoogleDriveFiles, GoogleDriveFolders, GoogleDriveSyncToken, GoogleDriveWebhook, - ModelId, - sequelize_conn, -} from "@connectors/lib/models"; +} from "@connectors/lib/models/google_drive"; import logger from "@connectors/logger/logger"; import { registerWebhook } from "../lib"; diff --git a/connectors/src/connectors/notion/index.ts b/connectors/src/connectors/notion/index.ts index 710f81c9561d7..798dbb0976311 100644 --- a/connectors/src/connectors/notion/index.ts +++ b/connectors/src/connectors/notion/index.ts @@ -6,14 +6,12 @@ import { stopNotionSyncWorkflow, } from "@connectors/connectors/notion/temporal/client"; import { dataSourceConfigFromConnector } from "@connectors/lib/api/data_source_config"; +import { Connector, ModelId, sequelize_conn } from "@connectors/lib/models"; import { - Connector, - ModelId, NotionConnectorState, NotionDatabase, NotionPage, - sequelize_conn, -} from "@connectors/lib/models"; +} from "@connectors/lib/models/notion"; import { nango_client, nangoDeleteConnection, diff --git a/connectors/src/connectors/notion/lib/connectors_db_helpers.ts b/connectors/src/connectors/notion/lib/connectors_db_helpers.ts index 08d5cdf71f0a5..3b145cbcecd82 100644 --- a/connectors/src/connectors/notion/lib/connectors_db_helpers.ts +++ b/connectors/src/connectors/notion/lib/connectors_db_helpers.ts @@ -1,9 +1,5 @@ -import { - Connector, - ModelId, - NotionDatabase, - NotionPage, -} from "@connectors/lib/models"; +import { Connector, ModelId } from "@connectors/lib/models"; +import { NotionDatabase, NotionPage } from "@connectors/lib/models/notion"; import { DataSourceInfo } from "@connectors/types/data_source_config"; // Note: this function does not let you "remove" a skipReason. diff --git a/connectors/src/connectors/notion/lib/parents.ts b/connectors/src/connectors/notion/lib/parents.ts index 61635f6e69393..a216657f67921 100644 --- a/connectors/src/connectors/notion/lib/parents.ts +++ b/connectors/src/connectors/notion/lib/parents.ts @@ -8,12 +8,8 @@ import { getPageChildrenOf, } from "@connectors/connectors/notion/lib/connectors_db_helpers"; import { updateDocumentParentsField } from "@connectors/lib/data_sources"; -import { - Connector, - ModelId, - NotionDatabase, - NotionPage, -} from "@connectors/lib/models"; +import { Connector, ModelId } from "@connectors/lib/models"; +import { NotionDatabase, NotionPage } from "@connectors/lib/models/notion"; /** Compute the parents field for a notion pageOrDb See the [Design * Doc](https://www.notion.so/dust-tt/Engineering-e0f834b5be5a43569baaf76e9c41adf2?p=3d26536a4e0a464eae0c3f8f27a7af97&pm=s) diff --git a/connectors/src/connectors/notion/temporal/activities.ts b/connectors/src/connectors/notion/temporal/activities.ts index a0badc1c4db51..3a22cf374a699 100644 --- a/connectors/src/connectors/notion/temporal/activities.ts +++ b/connectors/src/connectors/notion/temporal/activities.ts @@ -36,21 +36,20 @@ import { PageObjectProperties, ParsedNotionBlock, } from "@connectors/connectors/notion/lib/types"; +import { Connector, ModelId } from "@connectors/lib/connector"; import { deleteFromDataSource, MAX_DOCUMENT_TXT_LEN, upsertToDatasource, -} from "@connectors/lib/data_sources"; +} from "@connectors/lib/data_sources@connectors/lib/models/connector import { - Connector, - ModelId, NotionConnectorBlockCacheEntry, NotionConnectorPageCacheEntry, NotionConnectorResourcesToCheckCacheEntry, NotionConnectorState, NotionDatabase, NotionPage, -} from "@connectors/lib/models"; +} from "@connectors/lib/models/notion"; import { getAccessTokenFromNango } from "@connectors/lib/nango_helpers"; import { syncStarted, syncSucceeded } from "@connectors/lib/sync_status"; import mainLogger from "@connectors/logger/logger"; diff --git a/connectors/src/connectors/slack/bot.ts b/connectors/src/connectors/slack/bot.ts index 39f1e69e9b380..8a847900ef91d 100644 --- a/connectors/src/connectors/slack/bot.ts +++ b/connectors/src/connectors/slack/bot.ts @@ -15,13 +15,12 @@ import { RetrievalDocumentType, UserMessageType, } from "@connectors/lib/dust_api"; +import { Connector, ModelId } from "@connectors/lib/models"; import { - Connector, - ModelId, SlackChannel, SlackChatBotMessage, SlackConfiguration, -} from "@connectors/lib/models"; +} from "@connectors/lib/models/slack"; import { Err, Ok, Result } from "@connectors/lib/result"; import logger from "@connectors/logger/logger"; diff --git a/connectors/src/connectors/slack/index.ts b/connectors/src/connectors/slack/index.ts index 15901ced75ba4..02a952131be39 100644 --- a/connectors/src/connectors/slack/index.ts +++ b/connectors/src/connectors/slack/index.ts @@ -11,14 +11,16 @@ import { getChannels } from "@connectors/connectors/slack//temporal/activities"; import { joinChannel } from "@connectors/connectors/slack/lib/channels"; import { getSlackClient } from "@connectors/connectors/slack/lib/slack_client"; import { launchSlackSyncWorkflow } from "@connectors/connectors/slack/temporal/client.js"; -import { +import {@connectors/lib/models/connector.js Connector, ModelId, sequelize_conn, +} from "@connectors/lib/connector.js"; +import { SlackChannel, SlackConfiguration, SlackMessages, -} from "@connectors/lib/models.js"; +} from "@connectors/lib/models/slack"; import { nango_client, nangoDeleteConnection, diff --git a/connectors/src/connectors/slack/lib/channels.ts b/connectors/src/connectors/slack/lib/channels.ts index 72d1f976d611c..daf1e28526970 100644 --- a/connectors/src/connectors/slack/lib/channels.ts +++ b/connectors/src/connectors/slack/lib/channels.ts @@ -1,12 +1,8 @@ import { CodedError, ErrorCode, WebAPIPlatformError } from "@slack/web-api"; import { Channel } from "@slack/web-api/dist/response/ConversationsListResponse"; -import { - Connector, - ModelId, - sequelize_conn, - SlackChannel, -} from "@connectors/lib/models"; +import { Connector, ModelId, sequelize_conn } from "@connectors/lib/models"; +import { SlackChannel } from "@connectors/lib/models/slack"; import { Err, Ok, Result } from "@connectors/lib/result"; import logger from "@connectors/logger/logger"; import { ConnectorPermission } from "@connectors/types/resources"; diff --git a/connectors/src/connectors/slack/temporal/activities.ts b/connectors/src/connectors/slack/temporal/activities.ts index f9e530588f65a..d8a7b6f0ce15b 100644 --- a/connectors/src/connectors/slack/temporal/activities.ts +++ b/connectors/src/connectors/slack/temporal/activities.ts @@ -29,12 +29,8 @@ import { upsertToDatasource, } from "@connectors/lib/data_sources"; import { WorkflowError } from "@connectors/lib/error"; -import { - Connector, - ModelId, - SlackChannel, - SlackMessages, -} from "@connectors/lib/models"; +import { Connector, ModelId } from "@connectors/lib/models"; +import { SlackChannel, SlackMessages } from "@connectors/lib/models/slack"; import { reportInitialSyncProgress, syncSucceeded, diff --git a/connectors/src/lib/models.ts b/connectors/src/lib/models.ts deleted file mode 100644 index 1a9c7f30de2bf..0000000000000 --- a/connectors/src/lib/models.ts +++ /dev/null @@ -1,1338 +0,0 @@ -import { - type CreationOptional, - DataTypes, - type ForeignKey, - type InferAttributes, - type InferCreationAttributes, - Model, - Sequelize, -} from "sequelize"; - -import { - NotionBlockType, - PageObjectProperties, -} from "@connectors/connectors/notion/lib/types"; -import { - ConnectorErrorType, - type ConnectorProvider, - ConnectorSyncStatus, -} from "@connectors/types/connector"; -import { ConnectorPermission } from "@connectors/types/resources"; - -const { CONNECTORS_DATABASE_URI } = process.env; -if (!CONNECTORS_DATABASE_URI) { - throw new Error("CONNECTORS_DATABASE_URI is not defined"); -} - -export const sequelize_conn = new Sequelize(CONNECTORS_DATABASE_URI as string, { - logging: false, -}); - -export type ModelId = number; - -export class Connector extends Model< - InferAttributes<Connector>, - InferCreationAttributes<Connector> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - declare type: ConnectorProvider; - declare connectionId: string; - - declare workspaceAPIKey: string; - declare workspaceId: string; - declare dataSourceName: string; - - declare lastSyncStatus?: ConnectorSyncStatus; - declare errorType: ConnectorErrorType | null; - declare lastSyncStartTime?: Date; - declare lastSyncFinishTime?: Date; - declare lastSyncSuccessfulTime?: Date | null; - declare firstSuccessfulSyncTime?: Date; - declare firstSyncProgress?: string; - declare lastGCTime: Date | null; - - declare defaultNewResourcePermission: ConnectorPermission; -} - -Connector.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - type: { - type: DataTypes.STRING, - allowNull: false, - }, - connectionId: { - type: DataTypes.STRING, - allowNull: false, - }, - workspaceAPIKey: { - type: DataTypes.STRING, - allowNull: false, - }, - workspaceId: { - type: DataTypes.STRING, - allowNull: false, - }, - dataSourceName: { - type: DataTypes.STRING, - allowNull: false, - }, - lastSyncStatus: { - type: DataTypes.STRING, - allowNull: true, - }, - errorType: { - type: DataTypes.STRING, - allowNull: true, - }, - lastSyncStartTime: { - type: DataTypes.DATE, - allowNull: true, - }, - lastSyncFinishTime: { - type: DataTypes.DATE, - allowNull: true, - }, - lastSyncSuccessfulTime: { - type: DataTypes.DATE, - allowNull: true, - }, - firstSuccessfulSyncTime: { - type: DataTypes.DATE, - allowNull: true, - }, - firstSyncProgress: { - type: DataTypes.STRING, - allowNull: true, - }, - lastGCTime: { - type: DataTypes.DATE, - allowNull: true, - }, - defaultNewResourcePermission: { - type: DataTypes.STRING, - allowNull: false, - defaultValue: "read_write", - }, - }, - { - sequelize: sequelize_conn, - modelName: "connectors", - indexes: [{ fields: ["workspaceId", "dataSourceName"], unique: true }], - } -); - -export class SlackConfiguration extends Model< - InferAttributes<SlackConfiguration>, - InferCreationAttributes<SlackConfiguration> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - declare slackTeamId: string; - declare botEnabled: boolean; - declare connectorId: ForeignKey<Connector["id"]>; - // declare defaultChannelPermission: ConnectorPermission; -} - -SlackConfiguration.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - slackTeamId: { - type: DataTypes.STRING, - allowNull: false, - }, - botEnabled: { - type: DataTypes.BOOLEAN, - allowNull: false, - defaultValue: false, - }, - }, - { - sequelize: sequelize_conn, - indexes: [ - { fields: ["slackTeamId"] }, - { fields: ["connectorId"], unique: true }, - { - fields: ["slackTeamId", "botEnabled"], - where: { botEnabled: true }, - unique: true, - }, - ], - modelName: "slack_configurations", - } -); -Connector.hasOne(SlackConfiguration); - -export class SlackMessages extends Model< - InferAttributes<SlackMessages>, - InferCreationAttributes<SlackMessages> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - declare connectorId: ForeignKey<Connector["id"]>; - declare channelId: string; - declare messageTs?: string; - declare documentId: string; -} - -SlackMessages.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, - channelId: { - type: DataTypes.STRING, - allowNull: false, - }, - messageTs: { - type: DataTypes.STRING, - allowNull: true, - }, - documentId: { - type: DataTypes.STRING, - allowNull: false, - }, - }, - { - sequelize: sequelize_conn, - modelName: "slack_messages", - indexes: [ - { fields: ["connectorId", "channelId", "messageTs"], unique: true }, - ], - } -); - -Connector.hasOne(SlackMessages); - -export class SlackChannel extends Model< - InferAttributes<SlackChannel>, - InferCreationAttributes<SlackChannel> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - - declare connectorId: ForeignKey<Connector["id"]>; - declare slackChannelId: string; - declare slackChannelName: string; - - declare permission: ConnectorPermission; - declare agentConfigurationId: CreationOptional<string | null>; -} - -SlackChannel.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, - slackChannelId: { - type: DataTypes.STRING, - allowNull: false, - }, - slackChannelName: { - type: DataTypes.STRING, - allowNull: false, - }, - permission: { - type: DataTypes.STRING, - allowNull: false, - defaultValue: "read_write", - }, - agentConfigurationId: { - type: DataTypes.STRING, - allowNull: true, - }, - }, - { - sequelize: sequelize_conn, - modelName: "slack_channels", - indexes: [ - { fields: ["connectorId", "slackChannelId"], unique: true }, - { fields: ["connectorId"] }, - ], - } -); - -Connector.hasMany(SlackChannel); - -export class SlackChatBotMessage extends Model< - InferAttributes<SlackChatBotMessage>, - InferCreationAttributes<SlackChatBotMessage> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - declare connectorId: ForeignKey<Connector["id"]>; - declare channelId: string; - declare message: string; - declare slackUserId: string; - declare slackEmail: string; - declare slackUserName: string; - declare slackFullName: string | null; - declare slackAvatar: string | null; - declare slackTimezone: string | null; - declare messageTs: string | null; - declare threadTs: string | null; - declare chatSessionSid: string | null; - declare completedAt: Date | null; - declare conversationId: string | null; // conversationId is set only for V2 conversations -} - -SlackChatBotMessage.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - }, - updatedAt: { - type: DataTypes.DATE, - }, - connectorId: { - type: DataTypes.INTEGER, - - allowNull: false, - }, - channelId: { - type: DataTypes.STRING, - allowNull: false, - }, - messageTs: { - type: DataTypes.STRING, - allowNull: true, - }, - threadTs: { - type: DataTypes.STRING, - allowNull: true, - }, - chatSessionSid: { - type: DataTypes.STRING, - allowNull: true, - }, - message: { - type: DataTypes.TEXT, - allowNull: false, - }, - slackUserId: { - type: DataTypes.STRING, - allowNull: false, - }, - slackEmail: { - type: DataTypes.STRING, - allowNull: false, - }, - slackUserName: { - type: DataTypes.STRING, - allowNull: false, - }, - slackTimezone: { - type: DataTypes.STRING, - allowNull: true, - }, - completedAt: { - type: DataTypes.DATE, - allowNull: true, - }, - conversationId: { - type: DataTypes.STRING, - allowNull: true, - }, - slackFullName: { - type: DataTypes.STRING, - allowNull: true, - }, - slackAvatar: { - type: DataTypes.STRING, - allowNull: true, - }, - }, - { - sequelize: sequelize_conn, - modelName: "slack_chat_bot_messages", - } -); - -Connector.hasOne(SlackChatBotMessage); - -export class NotionConnectorState extends Model< - InferAttributes<NotionConnectorState>, - InferCreationAttributes<NotionConnectorState> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - - declare lastGarbageCollectionFinishTime?: Date; - - declare connectorId: ForeignKey<Connector["id"]>; -} - -NotionConnectorState.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - lastGarbageCollectionFinishTime: { - type: DataTypes.DATE, - allowNull: true, - }, - }, - { - sequelize: sequelize_conn, - modelName: "notion_connector_states", - indexes: [{ fields: ["connectorId"], unique: true }], - } -); - -Connector.hasOne(NotionConnectorState); - -export class NotionPage extends Model< - InferAttributes<NotionPage>, - InferCreationAttributes<NotionPage> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - - declare notionPageId: string; - declare lastSeenTs: Date; - declare lastUpsertedTs?: Date; - declare lastCreatedOrMovedRunTs: CreationOptional<Date | null>; - - declare skipReason?: string | null; - - declare parentType?: string | null; - declare parentId?: string | null; - declare title?: string | null; - declare titleSearchVector: unknown; - declare notionUrl?: string | null; - - declare connectorId: ForeignKey<Connector["id"]> | null; -} - -NotionPage.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - notionPageId: { - type: DataTypes.STRING, - allowNull: false, - }, - lastSeenTs: { - type: DataTypes.DATE, - allowNull: false, - }, - lastUpsertedTs: { - type: DataTypes.DATE, - allowNull: true, - }, - lastCreatedOrMovedRunTs: { - type: DataTypes.DATE, - allowNull: true, - }, - skipReason: { - type: DataTypes.STRING, - allowNull: true, - }, - parentType: { - type: DataTypes.STRING, - allowNull: true, - }, - parentId: { - type: DataTypes.STRING, - allowNull: true, - }, - title: { - type: DataTypes.TEXT, - allowNull: true, - }, - titleSearchVector: { - type: DataTypes.TSVECTOR, - allowNull: true, - }, - notionUrl: { - type: DataTypes.STRING, - allowNull: true, - }, - }, - { - sequelize: sequelize_conn, - indexes: [ - { fields: ["notionPageId", "connectorId"], unique: true }, - { fields: ["connectorId"] }, - { fields: ["lastSeenTs"] }, - { fields: ["parentId"] }, - { fields: ["lastCreatedOrMovedRunTs"] }, - { - fields: ["titleSearchVector"], - using: "gist", - name: "notion_pages_title_search_vector_gist_idx", - }, - ], - modelName: "notion_pages", - } -); -Connector.hasMany(NotionPage); - -export class NotionDatabase extends Model< - InferAttributes<NotionDatabase>, - InferCreationAttributes<NotionDatabase> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - - declare notionDatabaseId: string; - declare lastSeenTs: Date; - declare lastCreatedOrMovedRunTs: CreationOptional<Date | null>; - - declare skipReason?: string | null; - - declare parentType?: string | null; - declare parentId?: string | null; - declare title?: string | null; - declare titleSearchVector: unknown; - declare notionUrl?: string | null; - - declare connectorId: ForeignKey<Connector["id"]> | null; -} - -NotionDatabase.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - notionDatabaseId: { - type: DataTypes.STRING, - allowNull: false, - }, - lastSeenTs: { - type: DataTypes.DATE, - allowNull: false, - }, - lastCreatedOrMovedRunTs: { - type: DataTypes.DATE, - allowNull: true, - }, - skipReason: { - type: DataTypes.STRING, - allowNull: true, - }, - parentType: { - type: DataTypes.STRING, - allowNull: true, - }, - parentId: { - type: DataTypes.STRING, - allowNull: true, - }, - title: { - type: DataTypes.TEXT, - allowNull: true, - }, - titleSearchVector: { - type: DataTypes.TSVECTOR, - allowNull: true, - }, - notionUrl: { - type: DataTypes.STRING, - allowNull: true, - }, - }, - { - sequelize: sequelize_conn, - indexes: [ - { fields: ["notionDatabaseId", "connectorId"], unique: true }, - { fields: ["connectorId", "skipReason"] }, - { fields: ["lastSeenTs"] }, - { fields: ["lastCreatedOrMovedRunTs"] }, - { fields: ["parentId"] }, - { - fields: ["titleSearchVector"], - using: "gist", - name: "notion_databases_title_search_vector_gist_idx", - }, - ], - modelName: "notion_databases", - } -); - -Connector.hasMany(NotionDatabase); - -export class NotionConnectorPageCacheEntry extends Model< - InferAttributes<NotionConnectorPageCacheEntry>, - InferCreationAttributes<NotionConnectorPageCacheEntry> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - - declare notionPageId: string; - declare pageProperties: PageObjectProperties; // JSON -- typed but not guaranteed - declare pagePropertiesText: string; - declare parentId: string; - declare parentType: "database" | "page" | "workspace" | "block" | "unknown"; - declare lastEditedById: string; - declare createdById: string; - declare createdTime: string; - declare lastEditedTime: string; - declare url: string; - - declare connectorId: ForeignKey<Connector["id"]>; -} - -NotionConnectorPageCacheEntry.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - notionPageId: { - type: DataTypes.STRING, - allowNull: false, - }, - pageProperties: { - type: DataTypes.JSONB, - allowNull: true, - }, - pagePropertiesText: { - type: DataTypes.TEXT, - allowNull: false, - defaultValue: "{}", - }, - parentId: { - type: DataTypes.STRING, - allowNull: true, - }, - parentType: { - type: DataTypes.STRING, - allowNull: false, - }, - lastEditedById: { - type: DataTypes.STRING, - allowNull: false, - }, - createdById: { - type: DataTypes.STRING, - allowNull: false, - }, - createdTime: { - type: DataTypes.STRING, - allowNull: false, - }, - lastEditedTime: { - type: DataTypes.STRING, - allowNull: false, - }, - url: { - type: DataTypes.STRING, - allowNull: false, - }, - }, - { - sequelize: sequelize_conn, - modelName: "notion_connector_page_cache_entries", - indexes: [ - { fields: ["notionPageId", "connectorId"], unique: true }, - { fields: ["connectorId"] }, - { fields: ["parentId"] }, - ], - } -); - -Connector.hasMany(NotionConnectorPageCacheEntry); - -export class NotionConnectorBlockCacheEntry extends Model< - InferAttributes<NotionConnectorBlockCacheEntry>, - InferCreationAttributes<NotionConnectorBlockCacheEntry> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - - declare notionPageId: string; - declare notionBlockId: string; - declare blockText: string | null; - declare blockType: NotionBlockType; - declare parentBlockId: string | null; - declare indexInParent: number; - - // special case for child DBs - declare childDatabaseTitle?: string | null; - - declare connectorId: ForeignKey<Connector["id"]>; -} - -NotionConnectorBlockCacheEntry.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - notionPageId: { - type: DataTypes.STRING, - allowNull: false, - }, - notionBlockId: { - type: DataTypes.STRING, - allowNull: false, - }, - blockText: { - type: DataTypes.TEXT, - allowNull: true, - }, - blockType: { - type: DataTypes.STRING, - allowNull: false, - }, - parentBlockId: { - type: DataTypes.STRING, - allowNull: true, - }, - indexInParent: { - type: DataTypes.INTEGER, - allowNull: false, - }, - childDatabaseTitle: { - type: DataTypes.STRING, - allowNull: true, - }, - }, - { - sequelize: sequelize_conn, - modelName: "notion_connector_block_cache_entries", - indexes: [ - { - fields: ["notionBlockId", "connectorId", "notionPageId"], - unique: true, - name: "uq_notion_block_id_conn_id_page_id", - }, - { fields: ["connectorId"] }, - { fields: ["parentBlockId"] }, - { fields: ["notionPageId"] }, - ], - } -); - -Connector.hasMany(NotionConnectorBlockCacheEntry); - -export class NotionConnectorResourcesToCheckCacheEntry extends Model< - InferAttributes<NotionConnectorResourcesToCheckCacheEntry>, - InferCreationAttributes<NotionConnectorResourcesToCheckCacheEntry> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - - declare notionId: string; - declare resourceType: "page" | "database"; - declare connectorId: ForeignKey<Connector["id"]>; -} - -NotionConnectorResourcesToCheckCacheEntry.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - notionId: { - type: DataTypes.STRING, - allowNull: false, - }, - resourceType: { - type: DataTypes.STRING, - allowNull: false, - }, - }, - { - sequelize: sequelize_conn, - modelName: "notion_connector_resources_to_check_cache_entries", - indexes: [ - { - fields: ["notionId", "connectorId"], - unique: true, - name: "uq_notion_to_check_notion_id_conn_id", - }, - { fields: ["connectorId"] }, - ], - } -); - -Connector.hasMany(NotionConnectorResourcesToCheckCacheEntry); - -export class GithubConnectorState extends Model< - InferAttributes<GithubConnectorState>, - InferCreationAttributes<GithubConnectorState> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - - declare webhooksEnabledAt?: Date | null; - - declare connectorId: ForeignKey<Connector["id"]>; -} - -GithubConnectorState.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - webhooksEnabledAt: { - type: DataTypes.DATE, - allowNull: true, - }, - }, - { - sequelize: sequelize_conn, - modelName: "github_connector_states", - indexes: [{ fields: ["connectorId"], unique: true }], - } -); - -Connector.hasOne(GithubConnectorState); - -export class GithubIssue extends Model< - InferAttributes<GithubIssue>, - InferCreationAttributes<GithubIssue> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - - declare repoId: string; - declare issueNumber: number; - - declare connectorId: ForeignKey<Connector["id"]>; -} - -GithubIssue.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - repoId: { - type: DataTypes.STRING, - allowNull: false, - }, - issueNumber: { - type: DataTypes.INTEGER, - allowNull: false, - }, - }, - { - sequelize: sequelize_conn, - indexes: [ - { fields: ["repoId", "issueNumber", "connectorId"], unique: true }, - { fields: ["connectorId"] }, - { fields: ["repoId"] }, - ], - modelName: "github_issues", - } -); -Connector.hasMany(GithubIssue); - -export class GithubDiscussion extends Model< - InferAttributes<GithubDiscussion>, - InferCreationAttributes<GithubDiscussion> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - - declare repoId: string; - declare discussionNumber: number; - - declare connectorId: ForeignKey<Connector["id"]>; -} - -GithubDiscussion.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - repoId: { - type: DataTypes.STRING, - allowNull: false, - }, - discussionNumber: { - type: DataTypes.INTEGER, - allowNull: false, - }, - }, - { - sequelize: sequelize_conn, - indexes: [ - { fields: ["repoId", "discussionNumber", "connectorId"], unique: true }, - { fields: ["connectorId"] }, - { fields: ["repoId"] }, - ], - modelName: "github_discussions", - } -); -Connector.hasMany(GithubDiscussion); - -export class GoogleDriveConfig extends Model< - InferAttributes<GoogleDriveConfig>, - InferCreationAttributes<GoogleDriveConfig> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - declare connectorId: ForeignKey<Connector["id"]>; - declare pdfEnabled: boolean; -} - -GoogleDriveConfig.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, - pdfEnabled: { - type: DataTypes.BOOLEAN, - allowNull: false, - defaultValue: false, - }, - }, - { - sequelize: sequelize_conn, - modelName: "google_drive_configs", - indexes: [{ fields: ["connectorId"], unique: true }], - } -); - -// GoogleDriveFolders stores the folders selected by the user to sync. -export class GoogleDriveFolders extends Model< - InferAttributes<GoogleDriveFolders>, - InferCreationAttributes<GoogleDriveFolders> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - declare connectorId: ForeignKey<Connector["id"]>; - declare folderId: string; -} - -GoogleDriveFolders.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, - folderId: { - type: DataTypes.STRING, - allowNull: false, - }, - }, - { - sequelize: sequelize_conn, - modelName: "google_drive_folders", - indexes: [{ fields: ["connectorId", "folderId"], unique: true }], - } -); - -Connector.hasOne(GoogleDriveFolders); - -// GoogleDriveFiles stores files and folders synced from Google Drive. -export class GoogleDriveFiles extends Model< - InferAttributes<GoogleDriveFiles>, - InferCreationAttributes<GoogleDriveFiles> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - declare lastSeenTs: Date | null; - declare lastUpsertedTs: Date | null; - declare skipReason: string | null; - declare connectorId: ForeignKey<Connector["id"]>; - declare dustFileId: string; - declare driveFileId: string; - declare name: string; - declare mimeType: string; - declare parentId: string | null; -} - -GoogleDriveFiles.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - lastSeenTs: { - type: DataTypes.DATE, - allowNull: true, - }, - lastUpsertedTs: { - type: DataTypes.DATE, - allowNull: true, - }, - skipReason: { - type: DataTypes.STRING, - allowNull: true, - }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, - dustFileId: { - type: DataTypes.STRING, - allowNull: false, - }, - driveFileId: { - type: DataTypes.STRING, - allowNull: false, - }, - name: { - type: DataTypes.TEXT, - allowNull: false, - defaultValue: "", - }, - mimeType: { - type: DataTypes.STRING, - allowNull: false, - defaultValue: "", - }, - parentId: { - type: DataTypes.STRING, - allowNull: true, - }, - }, - { - sequelize: sequelize_conn, - modelName: "google_drive_files", - indexes: [{ fields: ["connectorId", "driveFileId"], unique: true }], - } -); -Connector.hasOne(GoogleDriveFiles); - -// Sync Token are the equivalent of a timestamp for syncing the delta -// between the last sync and the current sync. -export class GoogleDriveSyncToken extends Model< - InferAttributes<GoogleDriveSyncToken>, - InferCreationAttributes<GoogleDriveSyncToken> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - declare driveId: string; - declare syncToken: string; - declare connectorId: ForeignKey<Connector["id"]>; -} - -GoogleDriveSyncToken.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, - driveId: { - type: DataTypes.STRING, - allowNull: false, - }, - syncToken: { - type: DataTypes.STRING, - allowNull: false, - }, - }, - { - sequelize: sequelize_conn, - modelName: "google_drive_sync_tokens", - indexes: [{ fields: ["connectorId", "driveId"], unique: true }], - } -); -Connector.hasOne(GoogleDriveSyncToken); - -export class GoogleDriveWebhook extends Model< - InferAttributes<GoogleDriveWebhook>, - InferCreationAttributes<GoogleDriveWebhook> -> { - declare id: CreationOptional<number>; - declare createdAt: CreationOptional<Date>; - declare updatedAt: CreationOptional<Date>; - declare webhookId: string; - declare renewedByWebhookId: string | null; - declare expiresAt: Date; - declare renewAt: Date | null; - declare connectorId: ForeignKey<Connector["id"]>; -} - -GoogleDriveWebhook.init( - { - id: { - type: DataTypes.INTEGER, - autoIncrement: true, - primaryKey: true, - }, - createdAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - updatedAt: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, - }, - connectorId: { - type: DataTypes.INTEGER, - allowNull: false, - }, - webhookId: { - type: DataTypes.STRING, - allowNull: false, - }, - renewedByWebhookId: { - type: DataTypes.STRING, - allowNull: true, - }, - expiresAt: { - type: DataTypes.DATE, - allowNull: false, - }, - renewAt: { - type: DataTypes.DATE, - allowNull: true, - defaultValue: DataTypes.NOW, - }, - }, - { - sequelize: sequelize_conn, - modelName: "google_drive_webhooks", - indexes: [ - { fields: ["webhookId"], unique: true }, - { fields: ["renewAt"] }, - { fields: ["connectorId"] }, - ], - } -); -Connector.hasOne(GoogleDriveWebhook); diff --git a/connectors/src/lib/models/github.ts b/connectors/src/lib/models/github.ts new file mode 100644 index 0000000000000..05d9b0e72e3b0 --- /dev/null +++ b/connectors/src/lib/models/github.ts @@ -0,0 +1,154 @@ +import { + type CreationOptional, + DataTypes, + type ForeignKey, + type InferAttributes, + type InferCreationAttributes, + Model, +} from "sequelize"; + +import { Connector, sequelize_conn } from "@connectors/lib/models"; + +export class GithubConnectorState extends Model< + InferAttributes<GithubConnectorState>, + InferCreationAttributes<GithubConnectorState> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + + declare webhooksEnabledAt?: Date | null; + + declare connectorId: ForeignKey<Connector["id"]>; +} +GithubConnectorState.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + webhooksEnabledAt: { + type: DataTypes.DATE, + allowNull: true, + }, + }, + { + sequelize: sequelize_conn, + modelName: "github_connector_states", + indexes: [{ fields: ["connectorId"], unique: true }], + } +); +Connector.hasOne(GithubConnectorState); + +export class GithubIssue extends Model< + InferAttributes<GithubIssue>, + InferCreationAttributes<GithubIssue> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + + declare repoId: string; + declare issueNumber: number; + + declare connectorId: ForeignKey<Connector["id"]>; +} +GithubIssue.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + repoId: { + type: DataTypes.STRING, + allowNull: false, + }, + issueNumber: { + type: DataTypes.INTEGER, + allowNull: false, + }, + }, + { + sequelize: sequelize_conn, + indexes: [ + { fields: ["repoId", "issueNumber", "connectorId"], unique: true }, + { fields: ["connectorId"] }, + { fields: ["repoId"] }, + ], + modelName: "github_issues", + } +); +Connector.hasMany(GithubIssue); + +export class GithubDiscussion extends Model< + InferAttributes<GithubDiscussion>, + InferCreationAttributes<GithubDiscussion> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + + declare repoId: string; + declare discussionNumber: number; + + declare connectorId: ForeignKey<Connector["id"]>; +} +GithubDiscussion.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + repoId: { + type: DataTypes.STRING, + allowNull: false, + }, + discussionNumber: { + type: DataTypes.INTEGER, + allowNull: false, + }, + }, + { + sequelize: sequelize_conn, + indexes: [ + { fields: ["repoId", "discussionNumber", "connectorId"], unique: true }, + { fields: ["connectorId"] }, + { fields: ["repoId"] }, + ], + modelName: "github_discussions", + } +); +Connector.hasMany(GithubDiscussion); diff --git a/connectors/src/lib/models/google_drive.ts b/connectors/src/lib/models/google_drive.ts new file mode 100644 index 0000000000000..26a03fba7ee77 --- /dev/null +++ b/connectors/src/lib/models/google_drive.ts @@ -0,0 +1,296 @@ +import { + type CreationOptional, + DataTypes, + type ForeignKey, + type InferAttributes, + type InferCreationAttributes, + Model, +} from "sequelize"; + +import { Connector, sequelize_conn } from "@connectors/lib/models"; + +export class GoogleDriveConfig extends Model< + InferAttributes<GoogleDriveConfig>, + InferCreationAttributes<GoogleDriveConfig> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + declare connectorId: ForeignKey<Connector["id"]>; + declare pdfEnabled: boolean; +} +GoogleDriveConfig.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + connectorId: { + type: DataTypes.INTEGER, + allowNull: false, + }, + pdfEnabled: { + type: DataTypes.BOOLEAN, + allowNull: false, + defaultValue: false, + }, + }, + { + sequelize: sequelize_conn, + modelName: "google_drive_configs", + indexes: [{ fields: ["connectorId"], unique: true }], + } +); +// GoogleDriveFolders stores the folders selected by the user to sync. + +export class GoogleDriveFolders extends Model< + InferAttributes<GoogleDriveFolders>, + InferCreationAttributes<GoogleDriveFolders> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + declare connectorId: ForeignKey<Connector["id"]>; + declare folderId: string; +} +GoogleDriveFolders.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + connectorId: { + type: DataTypes.INTEGER, + allowNull: false, + }, + folderId: { + type: DataTypes.STRING, + allowNull: false, + }, + }, + { + sequelize: sequelize_conn, + modelName: "google_drive_folders", + indexes: [{ fields: ["connectorId", "folderId"], unique: true }], + } +); +Connector.hasOne(GoogleDriveFolders); +// GoogleDriveFiles stores files and folders synced from Google Drive. + +export class GoogleDriveFiles extends Model< + InferAttributes<GoogleDriveFiles>, + InferCreationAttributes<GoogleDriveFiles> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + declare lastSeenTs: Date | null; + declare lastUpsertedTs: Date | null; + declare skipReason: string | null; + declare connectorId: ForeignKey<Connector["id"]>; + declare dustFileId: string; + declare driveFileId: string; + declare name: string; + declare mimeType: string; + declare parentId: string | null; +} +GoogleDriveFiles.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + lastSeenTs: { + type: DataTypes.DATE, + allowNull: true, + }, + lastUpsertedTs: { + type: DataTypes.DATE, + allowNull: true, + }, + skipReason: { + type: DataTypes.STRING, + allowNull: true, + }, + connectorId: { + type: DataTypes.INTEGER, + allowNull: false, + }, + dustFileId: { + type: DataTypes.STRING, + allowNull: false, + }, + driveFileId: { + type: DataTypes.STRING, + allowNull: false, + }, + name: { + type: DataTypes.TEXT, + allowNull: false, + defaultValue: "", + }, + mimeType: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: "", + }, + parentId: { + type: DataTypes.STRING, + allowNull: true, + }, + }, + { + sequelize: sequelize_conn, + modelName: "google_drive_files", + indexes: [{ fields: ["connectorId", "driveFileId"], unique: true }], + } +); +Connector.hasOne(GoogleDriveFiles); +// Sync Token are the equivalent of a timestamp for syncing the delta +// between the last sync and the current sync. + +export class GoogleDriveSyncToken extends Model< + InferAttributes<GoogleDriveSyncToken>, + InferCreationAttributes<GoogleDriveSyncToken> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + declare driveId: string; + declare syncToken: string; + declare connectorId: ForeignKey<Connector["id"]>; +} +GoogleDriveSyncToken.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + connectorId: { + type: DataTypes.INTEGER, + allowNull: false, + }, + driveId: { + type: DataTypes.STRING, + allowNull: false, + }, + syncToken: { + type: DataTypes.STRING, + allowNull: false, + }, + }, + { + sequelize: sequelize_conn, + modelName: "google_drive_sync_tokens", + indexes: [{ fields: ["connectorId", "driveId"], unique: true }], + } +); +Connector.hasOne(GoogleDriveSyncToken); + +export class GoogleDriveWebhook extends Model< + InferAttributes<GoogleDriveWebhook>, + InferCreationAttributes<GoogleDriveWebhook> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + declare webhookId: string; + declare renewedByWebhookId: string | null; + declare expiresAt: Date; + declare renewAt: Date | null; + declare connectorId: ForeignKey<Connector["id"]>; +} +GoogleDriveWebhook.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + connectorId: { + type: DataTypes.INTEGER, + allowNull: false, + }, + webhookId: { + type: DataTypes.STRING, + allowNull: false, + }, + renewedByWebhookId: { + type: DataTypes.STRING, + allowNull: true, + }, + expiresAt: { + type: DataTypes.DATE, + allowNull: false, + }, + renewAt: { + type: DataTypes.DATE, + allowNull: true, + defaultValue: DataTypes.NOW, + }, + }, + { + sequelize: sequelize_conn, + modelName: "google_drive_webhooks", + indexes: [ + { fields: ["webhookId"], unique: true }, + { fields: ["renewAt"] }, + { fields: ["connectorId"] }, + ], + } +); +Connector.hasOne(GoogleDriveWebhook); diff --git a/connectors/src/lib/models/index.ts b/connectors/src/lib/models/index.ts new file mode 100644 index 0000000000000..e3ae3002abc80 --- /dev/null +++ b/connectors/src/lib/models/index.ts @@ -0,0 +1,134 @@ +import { + type CreationOptional, + DataTypes, + type InferAttributes, + type InferCreationAttributes, + Model, + Sequelize, +} from "sequelize"; + +import { + ConnectorErrorType, + type ConnectorProvider, + ConnectorSyncStatus, +} from "@connectors/types/connector"; +import { ConnectorPermission } from "@connectors/types/resources"; + +const { CONNECTORS_DATABASE_URI } = process.env; +if (!CONNECTORS_DATABASE_URI) { + throw new Error("CONNECTORS_DATABASE_URI is not defined"); +} + +export const sequelize_conn = new Sequelize(CONNECTORS_DATABASE_URI as string, { + logging: false, +}); + +export type ModelId = number; + +export class Connector extends Model< + InferAttributes<Connector>, + InferCreationAttributes<Connector> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + declare type: ConnectorProvider; + declare connectionId: string; + + declare workspaceAPIKey: string; + declare workspaceId: string; + declare dataSourceName: string; + + declare lastSyncStatus?: ConnectorSyncStatus; + declare errorType: ConnectorErrorType | null; + declare lastSyncStartTime?: Date; + declare lastSyncFinishTime?: Date; + declare lastSyncSuccessfulTime?: Date | null; + declare firstSuccessfulSyncTime?: Date; + declare firstSyncProgress?: string; + declare lastGCTime: Date | null; + + declare defaultNewResourcePermission: ConnectorPermission; +} + +Connector.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + type: { + type: DataTypes.STRING, + allowNull: false, + }, + connectionId: { + type: DataTypes.STRING, + allowNull: false, + }, + workspaceAPIKey: { + type: DataTypes.STRING, + allowNull: false, + }, + workspaceId: { + type: DataTypes.STRING, + allowNull: false, + }, + dataSourceName: { + type: DataTypes.STRING, + allowNull: false, + }, + lastSyncStatus: { + type: DataTypes.STRING, + allowNull: true, + }, + errorType: { + type: DataTypes.STRING, + allowNull: true, + }, + lastSyncStartTime: { + type: DataTypes.DATE, + allowNull: true, + }, + lastSyncFinishTime: { + type: DataTypes.DATE, + allowNull: true, + }, + lastSyncSuccessfulTime: { + type: DataTypes.DATE, + allowNull: true, + }, + firstSuccessfulSyncTime: { + type: DataTypes.DATE, + allowNull: true, + }, + firstSyncProgress: { + type: DataTypes.STRING, + allowNull: true, + }, + lastGCTime: { + type: DataTypes.DATE, + allowNull: true, + }, + defaultNewResourcePermission: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: "read_write", + }, + }, + { + sequelize: sequelize_conn, + modelName: "connectors", + indexes: [{ fields: ["workspaceId", "dataSourceName"], unique: true }], + } +); diff --git a/connectors/src/lib/models/notion.ts b/connectors/src/lib/models/notion.ts new file mode 100644 index 0000000000000..a458f7cdfaca8 --- /dev/null +++ b/connectors/src/lib/models/notion.ts @@ -0,0 +1,479 @@ +import { + type CreationOptional, + DataTypes, + type ForeignKey, + type InferAttributes, + type InferCreationAttributes, + Model, +} from "sequelize"; + +import { + NotionBlockType, + PageObjectProperties, +} from "@connectors/connectors/notion/lib/types"; +import { Connector, sequelize_conn } from "@connectors/lib/models"; + +export class NotionConnectorState extends Model< + InferAttributes<NotionConnectorState>, + InferCreationAttributes<NotionConnectorState> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + + declare lastGarbageCollectionFinishTime?: Date; + + declare connectorId: ForeignKey<Connector["id"]>; +} +NotionConnectorState.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + lastGarbageCollectionFinishTime: { + type: DataTypes.DATE, + allowNull: true, + }, + }, + { + sequelize: sequelize_conn, + modelName: "notion_connector_states", + indexes: [{ fields: ["connectorId"], unique: true }], + } +); +Connector.hasOne(NotionConnectorState); + +export class NotionPage extends Model< + InferAttributes<NotionPage>, + InferCreationAttributes<NotionPage> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + + declare notionPageId: string; + declare lastSeenTs: Date; + declare lastUpsertedTs?: Date; + declare lastCreatedOrMovedRunTs: CreationOptional<Date | null>; + + declare skipReason?: string | null; + + declare parentType?: string | null; + declare parentId?: string | null; + declare title?: string | null; + declare titleSearchVector: unknown; + declare notionUrl?: string | null; + + declare connectorId: ForeignKey<Connector["id"]> | null; +} +NotionPage.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + notionPageId: { + type: DataTypes.STRING, + allowNull: false, + }, + lastSeenTs: { + type: DataTypes.DATE, + allowNull: false, + }, + lastUpsertedTs: { + type: DataTypes.DATE, + allowNull: true, + }, + lastCreatedOrMovedRunTs: { + type: DataTypes.DATE, + allowNull: true, + }, + skipReason: { + type: DataTypes.STRING, + allowNull: true, + }, + parentType: { + type: DataTypes.STRING, + allowNull: true, + }, + parentId: { + type: DataTypes.STRING, + allowNull: true, + }, + title: { + type: DataTypes.TEXT, + allowNull: true, + }, + titleSearchVector: { + type: DataTypes.TSVECTOR, + allowNull: true, + }, + notionUrl: { + type: DataTypes.STRING, + allowNull: true, + }, + }, + { + sequelize: sequelize_conn, + indexes: [ + { fields: ["notionPageId", "connectorId"], unique: true }, + { fields: ["connectorId"] }, + { fields: ["lastSeenTs"] }, + { fields: ["parentId"] }, + { fields: ["lastCreatedOrMovedRunTs"] }, + { + fields: ["titleSearchVector"], + using: "gist", + name: "notion_pages_title_search_vector_gist_idx", + }, + ], + modelName: "notion_pages", + } +); +Connector.hasMany(NotionPage); + +export class NotionDatabase extends Model< + InferAttributes<NotionDatabase>, + InferCreationAttributes<NotionDatabase> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + + declare notionDatabaseId: string; + declare lastSeenTs: Date; + declare lastCreatedOrMovedRunTs: CreationOptional<Date | null>; + + declare skipReason?: string | null; + + declare parentType?: string | null; + declare parentId?: string | null; + declare title?: string | null; + declare titleSearchVector: unknown; + declare notionUrl?: string | null; + + declare connectorId: ForeignKey<Connector["id"]> | null; +} +NotionDatabase.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + notionDatabaseId: { + type: DataTypes.STRING, + allowNull: false, + }, + lastSeenTs: { + type: DataTypes.DATE, + allowNull: false, + }, + lastCreatedOrMovedRunTs: { + type: DataTypes.DATE, + allowNull: true, + }, + skipReason: { + type: DataTypes.STRING, + allowNull: true, + }, + parentType: { + type: DataTypes.STRING, + allowNull: true, + }, + parentId: { + type: DataTypes.STRING, + allowNull: true, + }, + title: { + type: DataTypes.TEXT, + allowNull: true, + }, + titleSearchVector: { + type: DataTypes.TSVECTOR, + allowNull: true, + }, + notionUrl: { + type: DataTypes.STRING, + allowNull: true, + }, + }, + { + sequelize: sequelize_conn, + indexes: [ + { fields: ["notionDatabaseId", "connectorId"], unique: true }, + { fields: ["connectorId", "skipReason"] }, + { fields: ["lastSeenTs"] }, + { fields: ["lastCreatedOrMovedRunTs"] }, + { fields: ["parentId"] }, + { + fields: ["titleSearchVector"], + using: "gist", + name: "notion_databases_title_search_vector_gist_idx", + }, + ], + modelName: "notion_databases", + } +); +Connector.hasMany(NotionDatabase); + +export class NotionConnectorPageCacheEntry extends Model< + InferAttributes<NotionConnectorPageCacheEntry>, + InferCreationAttributes<NotionConnectorPageCacheEntry> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + + declare notionPageId: string; + declare pageProperties: PageObjectProperties; // JSON -- typed but not guaranteed + declare pagePropertiesText: string; + declare parentId: string; + declare parentType: "database" | "page" | "workspace" | "block" | "unknown"; + declare lastEditedById: string; + declare createdById: string; + declare createdTime: string; + declare lastEditedTime: string; + declare url: string; + + declare connectorId: ForeignKey<Connector["id"]>; +} +NotionConnectorPageCacheEntry.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + notionPageId: { + type: DataTypes.STRING, + allowNull: false, + }, + pageProperties: { + type: DataTypes.JSONB, + allowNull: true, + }, + pagePropertiesText: { + type: DataTypes.TEXT, + allowNull: false, + defaultValue: "{}", + }, + parentId: { + type: DataTypes.STRING, + allowNull: true, + }, + parentType: { + type: DataTypes.STRING, + allowNull: false, + }, + lastEditedById: { + type: DataTypes.STRING, + allowNull: false, + }, + createdById: { + type: DataTypes.STRING, + allowNull: false, + }, + createdTime: { + type: DataTypes.STRING, + allowNull: false, + }, + lastEditedTime: { + type: DataTypes.STRING, + allowNull: false, + }, + url: { + type: DataTypes.STRING, + allowNull: false, + }, + }, + { + sequelize: sequelize_conn, + modelName: "notion_connector_page_cache_entries", + indexes: [ + { fields: ["notionPageId", "connectorId"], unique: true }, + { fields: ["connectorId"] }, + { fields: ["parentId"] }, + ], + } +); +Connector.hasMany(NotionConnectorPageCacheEntry); + +export class NotionConnectorBlockCacheEntry extends Model< + InferAttributes<NotionConnectorBlockCacheEntry>, + InferCreationAttributes<NotionConnectorBlockCacheEntry> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + + declare notionPageId: string; + declare notionBlockId: string; + declare blockText: string | null; + declare blockType: NotionBlockType; + declare parentBlockId: string | null; + declare indexInParent: number; + + // special case for child DBs + declare childDatabaseTitle?: string | null; + + declare connectorId: ForeignKey<Connector["id"]>; +} +NotionConnectorBlockCacheEntry.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + notionPageId: { + type: DataTypes.STRING, + allowNull: false, + }, + notionBlockId: { + type: DataTypes.STRING, + allowNull: false, + }, + blockText: { + type: DataTypes.TEXT, + allowNull: true, + }, + blockType: { + type: DataTypes.STRING, + allowNull: false, + }, + parentBlockId: { + type: DataTypes.STRING, + allowNull: true, + }, + indexInParent: { + type: DataTypes.INTEGER, + allowNull: false, + }, + childDatabaseTitle: { + type: DataTypes.STRING, + allowNull: true, + }, + }, + { + sequelize: sequelize_conn, + modelName: "notion_connector_block_cache_entries", + indexes: [ + { + fields: ["notionBlockId", "connectorId", "notionPageId"], + unique: true, + name: "uq_notion_block_id_conn_id_page_id", + }, + { fields: ["connectorId"] }, + { fields: ["parentBlockId"] }, + { fields: ["notionPageId"] }, + ], + } +); +Connector.hasMany(NotionConnectorBlockCacheEntry); + +export class NotionConnectorResourcesToCheckCacheEntry extends Model< + InferAttributes<NotionConnectorResourcesToCheckCacheEntry>, + InferCreationAttributes<NotionConnectorResourcesToCheckCacheEntry> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + + declare notionId: string; + declare resourceType: "page" | "database"; + declare connectorId: ForeignKey<Connector["id"]>; +} +NotionConnectorResourcesToCheckCacheEntry.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + notionId: { + type: DataTypes.STRING, + allowNull: false, + }, + resourceType: { + type: DataTypes.STRING, + allowNull: false, + }, + }, + { + sequelize: sequelize_conn, + modelName: "notion_connector_resources_to_check_cache_entries", + indexes: [ + { + fields: ["notionId", "connectorId"], + unique: true, + name: "uq_notion_to_check_notion_id_conn_id", + }, + { fields: ["connectorId"] }, + ], + } +); +Connector.hasMany(NotionConnectorResourcesToCheckCacheEntry); diff --git a/connectors/src/lib/models/slack.ts b/connectors/src/lib/models/slack.ts new file mode 100644 index 0000000000000..1f3d8c647beae --- /dev/null +++ b/connectors/src/lib/models/slack.ts @@ -0,0 +1,286 @@ +import { + type CreationOptional, + DataTypes, + type ForeignKey, + type InferAttributes, + type InferCreationAttributes, + Model, +} from "sequelize"; + +import { Connector, sequelize_conn } from "@connectors/lib/models"; +import { ConnectorPermission } from "@connectors/types/resources"; + +export class SlackConfiguration extends Model< + InferAttributes<SlackConfiguration>, + InferCreationAttributes<SlackConfiguration> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + declare slackTeamId: string; + declare botEnabled: boolean; + declare connectorId: ForeignKey<Connector["id"]>; +} +SlackConfiguration.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + slackTeamId: { + type: DataTypes.STRING, + allowNull: false, + }, + botEnabled: { + type: DataTypes.BOOLEAN, + allowNull: false, + defaultValue: false, + }, + }, + { + sequelize: sequelize_conn, + indexes: [ + { fields: ["slackTeamId"] }, + { fields: ["connectorId"], unique: true }, + { + fields: ["slackTeamId", "botEnabled"], + where: { botEnabled: true }, + unique: true, + }, + ], + modelName: "slack_configurations", + } +); +Connector.hasOne(SlackConfiguration); + +export class SlackMessages extends Model< + InferAttributes<SlackMessages>, + InferCreationAttributes<SlackMessages> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + declare connectorId: ForeignKey<Connector["id"]>; + declare channelId: string; + declare messageTs?: string; + declare documentId: string; +} +SlackMessages.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + connectorId: { + type: DataTypes.INTEGER, + allowNull: false, + }, + channelId: { + type: DataTypes.STRING, + allowNull: false, + }, + messageTs: { + type: DataTypes.STRING, + allowNull: true, + }, + documentId: { + type: DataTypes.STRING, + allowNull: false, + }, + }, + { + sequelize: sequelize_conn, + modelName: "slack_messages", + indexes: [ + { fields: ["connectorId", "channelId", "messageTs"], unique: true }, + ], + } +); +Connector.hasOne(SlackMessages); + +export class SlackChannel extends Model< + InferAttributes<SlackChannel>, + InferCreationAttributes<SlackChannel> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + + declare connectorId: ForeignKey<Connector["id"]>; + declare slackChannelId: string; + declare slackChannelName: string; + + declare permission: ConnectorPermission; + declare agentConfigurationId: CreationOptional<string | null>; +} +SlackChannel.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + updatedAt: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + }, + connectorId: { + type: DataTypes.INTEGER, + allowNull: false, + }, + slackChannelId: { + type: DataTypes.STRING, + allowNull: false, + }, + slackChannelName: { + type: DataTypes.STRING, + allowNull: false, + }, + permission: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: "read_write", + }, + agentConfigurationId: { + type: DataTypes.STRING, + allowNull: true, + }, + }, + { + sequelize: sequelize_conn, + modelName: "slack_channels", + indexes: [ + { fields: ["connectorId", "slackChannelId"], unique: true }, + { fields: ["connectorId"] }, + ], + } +); +Connector.hasMany(SlackChannel); + +export class SlackChatBotMessage extends Model< + InferAttributes<SlackChatBotMessage>, + InferCreationAttributes<SlackChatBotMessage> +> { + declare id: CreationOptional<number>; + declare createdAt: CreationOptional<Date>; + declare updatedAt: CreationOptional<Date>; + declare connectorId: ForeignKey<Connector["id"]>; + declare channelId: string; + declare message: string; + declare slackUserId: string; + declare slackEmail: string; + declare slackUserName: string; + declare slackFullName: string | null; + declare slackAvatar: string | null; + declare slackTimezone: string | null; + declare messageTs: string | null; + declare threadTs: string | null; + declare chatSessionSid: string | null; + declare completedAt: Date | null; + declare conversationId: string | null; // conversationId is set only for V2 conversations +} +SlackChatBotMessage.init( + { + id: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + }, + createdAt: { + type: DataTypes.DATE, + }, + updatedAt: { + type: DataTypes.DATE, + }, + connectorId: { + type: DataTypes.INTEGER, + + allowNull: false, + }, + channelId: { + type: DataTypes.STRING, + allowNull: false, + }, + messageTs: { + type: DataTypes.STRING, + allowNull: true, + }, + threadTs: { + type: DataTypes.STRING, + allowNull: true, + }, + chatSessionSid: { + type: DataTypes.STRING, + allowNull: true, + }, + message: { + type: DataTypes.TEXT, + allowNull: false, + }, + slackUserId: { + type: DataTypes.STRING, + allowNull: false, + }, + slackEmail: { + type: DataTypes.STRING, + allowNull: false, + }, + slackUserName: { + type: DataTypes.STRING, + allowNull: false, + }, + slackTimezone: { + type: DataTypes.STRING, + allowNull: true, + }, + completedAt: { + type: DataTypes.DATE, + allowNull: true, + }, + conversationId: { + type: DataTypes.STRING, + allowNull: true, + }, + slackFullName: { + type: DataTypes.STRING, + allowNull: true, + }, + slackAvatar: { + type: DataTypes.STRING, + allowNull: true, + }, + }, + { + sequelize: sequelize_conn, + modelName: "slack_chat_bot_messages", + } +); +Connector.hasOne(SlackChatBotMessage); diff --git a/connectors/src/lib/temporal.ts b/connectors/src/lib/temporal.ts index d623fa544d9b5..997162c9ee2f9 100644 --- a/connectors/src/lib/temporal.ts +++ b/connectors/src/lib/temporal.ts @@ -7,7 +7,7 @@ import { import { NativeConnection } from "@temporalio/worker"; import fs from "fs-extra"; -import { ModelId } from "./models"; +import { ModelId } from "@connectors/lib/models"; // This is a singleton connection to the Temporal server. let TEMPORAL_CLIENT: Client | undefined;