Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
feat: Dynamically defined schemas for tables
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyxiao committed Jan 31, 2024
1 parent da1e7cc commit 82af8df
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 125 deletions.
91 changes: 91 additions & 0 deletions packages/worker/postgres/schema-factory.ts
Original file line number Diff line number Diff line change
@@ -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<TName extends string>(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<TName extends string>(
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`,
}),
}),
)
}
130 changes: 5 additions & 125 deletions packages/worker/postgres/schema.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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')

0 comments on commit 82af8df

Please sign in to comment.