diff --git a/packages/worker/postgres/schema-factory.ts b/packages/worker/postgres/schema-factory.ts new file mode 100644 index 0000000..9aa0103 --- /dev/null +++ b/packages/worker/postgres/schema-factory.ts @@ -0,0 +1,91 @@ +import { + boolean, + jsonb, + pgTable, + primaryKey, + text, + timestamp, +} from 'drizzle-orm/pg-core' + +// NOTE: Introduce schema name also? + +/** e.g. crm_accounts */ +export function getCommonObjectTable(tableName: TName) { + return pgTable( + tableName, + { + supaglueApplicationId: text('_supaglue_application_id').notNull(), + supaglueProviderName: text('_supaglue_provider_name').notNull(), + supaglueCustomerId: text('_supaglue_customer_id').notNull(), + id: text('id').notNull(), + supaglueEmittedAt: timestamp('_supaglue_emitted_at', { + precision: 3, + mode: 'string', + }).notNull(), + createdAt: timestamp('created_at', {precision: 3, mode: 'string'}), + updatedAt: timestamp('updated_at', {precision: 3, mode: 'string'}), + isDeleted: boolean('is_deleted').default(false).notNull(), + lastModifiedAt: timestamp('last_modified_at', { + precision: 3, + mode: 'string', + }).notNull(), + rawData: jsonb('raw_data'), + supaglueUnifiedData: jsonb('_supaglue_unified_data'), + }, + (table) => ({ + primaryKey: primaryKey({ + columns: [ + table.supaglueApplicationId, + table.supaglueProviderName, + table.supaglueCustomerId, + table.id, + ], + name: `${tableName}_pkey`, + }), + }), + ) +} + +/** e.g. salesforce_contact */ +export function getProviderObjectTable( + tableName: TName, + opts?: {custom?: boolean}, +) { + return pgTable( + tableName, + { + supaglueApplicationId: text('_supaglue_application_id').notNull(), + supaglueProviderName: text('_supaglue_provider_name').notNull(), + supaglueCustomerId: text('_supaglue_customer_id').notNull(), + id: text('id').notNull(), + supaglueEmittedAt: timestamp('_supaglue_emitted_at', { + precision: 3, + mode: 'string', + }).notNull(), + supaglueLastModifiedAt: timestamp('_supaglue_last_modified_at', { + precision: 3, + mode: 'string', + }).notNull(), + supaglueIsDeleted: boolean('_supaglue_is_deleted') + .default(false) + .notNull(), + supaglueRawData: jsonb('_supaglue_raw_data'), + supaglueMappedData: jsonb('_supaglue_mapped_data'), + // e.g. salesforce_product_gaps_c , or hubspot_productgaps + ...(opts?.custom && { + supaglueObjectName: text('_supaglue_object_name').notNull(), + }), + }, + (table) => ({ + primaryKey: primaryKey({ + columns: [ + table.supaglueApplicationId, + table.supaglueProviderName, + table.supaglueCustomerId, + table.id, + ], + name: `${tableName}_pkey`, + }), + }), + ) +} diff --git a/packages/worker/postgres/schema.ts b/packages/worker/postgres/schema.ts index 846de7d..fe120eb 100644 --- a/packages/worker/postgres/schema.ts +++ b/packages/worker/postgres/schema.ts @@ -1,12 +1,5 @@ -import { - boolean, - integer, - jsonb, - pgTable, - primaryKey, - text, - timestamp, -} from 'drizzle-orm/pg-core' +import {pgTable, text, timestamp} from 'drizzle-orm/pg-core' +import {getCommonObjectTable} from './schema-factory' export const syncLog = pgTable('sync_log', { connectionId: text('connectionId').notNull(), @@ -21,119 +14,6 @@ export const syncLog = pgTable('sync_log', { }).defaultNow(), }) -export const engagementUsers = pgTable( - 'engagement_users', - { - supaglueApplicationId: text('_supaglue_application_id').notNull(), - supaglueProviderName: text('_supaglue_provider_name').notNull(), - supaglueCustomerId: text('_supaglue_customer_id').notNull(), - supaglueEmittedAt: timestamp('_supaglue_emitted_at', { - precision: 3, - mode: 'string', - }).notNull(), - id: text('id').notNull(), - createdAt: timestamp('created_at', {precision: 3, mode: 'string'}), - updatedAt: timestamp('updated_at', {precision: 3, mode: 'string'}), - isDeleted: boolean('is_deleted').default(false).notNull(), - lastModifiedAt: timestamp('last_modified_at', { - precision: 3, - mode: 'string', - }).notNull(), - firstName: text('first_name'), - lastName: text('last_name'), - email: text('email'), - rawData: jsonb('raw_data'), - supaglueUnifiedData: jsonb('_supaglue_unified_data'), - }, - (table) => ({ - engagementUsersPkey: primaryKey({ - columns: [ - table.supaglueApplicationId, - table.supaglueProviderName, - table.supaglueCustomerId, - table.id, - ], - name: 'engagement_users_pkey', - }), - }), -) - -export const engagementSequences = pgTable( - 'engagement_sequences', - { - supaglueApplicationId: text('_supaglue_application_id').notNull(), - supaglueProviderName: text('_supaglue_provider_name').notNull(), - supaglueCustomerId: text('_supaglue_customer_id').notNull(), - supaglueEmittedAt: timestamp('_supaglue_emitted_at', { - precision: 3, - mode: 'string', - }).notNull(), - id: text('id').notNull(), - createdAt: timestamp('created_at', {precision: 3, mode: 'string'}), - updatedAt: timestamp('updated_at', {precision: 3, mode: 'string'}), - isDeleted: boolean('is_deleted').default(false).notNull(), - lastModifiedAt: timestamp('last_modified_at', { - precision: 3, - mode: 'string', - }).notNull(), - rawData: jsonb('raw_data'), - supaglueUnifiedData: jsonb('_supaglue_unified_data'), - }, - (table) => ({ - engagementSequencesPkey: primaryKey({ - columns: [ - table.supaglueApplicationId, - table.supaglueProviderName, - table.supaglueCustomerId, - table.id, - ], - name: 'engagement_sequences_pkey', - }), - }), -) - -export const engagementContacts = pgTable( - 'engagement_contacts', - { - supaglueApplicationId: text('_supaglue_application_id').notNull(), - supaglueProviderName: text('_supaglue_provider_name').notNull(), - supaglueCustomerId: text('_supaglue_customer_id').notNull(), - supaglueEmittedAt: timestamp('_supaglue_emitted_at', { - precision: 3, - mode: 'string', - }).notNull(), - id: text('id').notNull(), - createdAt: timestamp('created_at', {precision: 3, mode: 'string'}), - updatedAt: timestamp('updated_at', {precision: 3, mode: 'string'}), - isDeleted: boolean('is_deleted').notNull(), - lastModifiedAt: timestamp('last_modified_at', { - precision: 3, - mode: 'string', - }).notNull(), - firstName: text('first_name'), - lastName: text('last_name'), - jobTitle: text('job_title'), - address: jsonb('address'), - emailAddresses: jsonb('email_addresses').notNull(), - phoneNumbers: jsonb('phone_numbers').notNull(), - ownerId: text('owner_id'), - accountId: text('account_id'), - openCount: integer('open_count').notNull(), - clickCount: integer('click_count').notNull(), - replyCount: integer('reply_count').notNull(), - bouncedCount: integer('bounced_count').notNull(), - rawData: jsonb('raw_data'), - supaglueUnifiedData: jsonb('_supaglue_unified_data'), - }, - (table) => ({ - engagementContactsPkey: primaryKey({ - columns: [ - table.supaglueApplicationId, - table.supaglueProviderName, - table.supaglueCustomerId, - table.id, - ], - name: 'engagement_contacts_pkey', - }), - }), -) +export const engagementUsers = getCommonObjectTable('engagement_users') +export const engagementSequences = getCommonObjectTable('engagement_sequences') +export const engagementContacts = getCommonObjectTable('engagement_contacts')