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

Commit

Permalink
feat: pluraize table name and ensure custom objects table match
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyxiao committed Feb 29, 2024
1 parent a5122f4 commit 3b0c236
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/vdk/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function proxyCallProvider({
const instance = ctx.provider.__init__({
getCredentials: async () => {
if (featureFlags.mode === 'nango') {
throw new Error('Not implemented')
throw new Error('Nango getCredentials not implemented')
}
const supaglueApiKey = ctx.headers.get('x-api-key') ?? ctx.supaglueApiKey
if (!supaglueApiKey) {
Expand Down
44 changes: 38 additions & 6 deletions packages/worker/postgres/schema-dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,39 @@ import {

export const mySchema = pgSchema('my_schema')

/**
* Supaglue inconsistently pluralized table names... Only did it for common objects too not standard objects...
* @see https://github.com/supaglue-labs/supaglue/blob/d482ca9468b8047c0fa874b77a38ab765dc5e9c3/packages/core/destination_writers/postgres.ts#L373-L393
*/
function pluralizeCommonObjectName(name: string) {
switch (name) {
case 'crm_opportunity':
return 'crm_opportunities'
case 'engagement_mailbox':
return 'engagement_mailboxes'
case 'crm_account':
case 'crm_contact':
case 'crm_lead':
case 'crm_user':
case 'engagement_account':
case 'engagement_contact':
case 'engagement_sequence_state':
case 'engagement_sequence_step':
case 'engagement_user':
case 'engagement_sequence':
return `${name}s`
default:
return name
}
}

/** e.g. crm_accounts */
export function getCommonObjectTable<TName extends string>(
tableName: TName,
_tableName: TName,
opts: {schema?: string} = {},
) {
const schema = opts.schema ? pgSchema(opts.schema) : undefined
const tableName = pluralizeCommonObjectName(_tableName)
const table = (schema ? schema.table : pgTable)(
tableName,
{
Expand Down Expand Up @@ -77,23 +104,25 @@ export function getCommonObjectTable<TName extends string>(
return table as typeof table & typeof extension
}

/** e.g. salesforce_contact */
/** e.g. salesforce_contact, also `custom_objects` too... */
export function getProviderObjectTable<TName extends string>(
tableName: TName,
_tableName: TName,
opts: {custom?: boolean; schema?: string} = {},
) {
const schema = opts.schema ? pgSchema(opts.schema) : undefined
// Supaglue put all custom objects into a single table... so we need to handle that too...
const tableName = opts.custom ? 'custom_objects' : _tableName
const table = (schema ? schema.table : pgTable)(
tableName,
{
_supaglue_application_id: text('_supaglue_application_id').notNull(),
_supaglue_provider_name: text('_supaglue_provider_name').notNull(),
_supaglue_customer_id: text('_supaglue_customer_id').notNull(),
_supaglue_id: text('_supaglue_id').notNull(),
_supaglue_emitted_at: timestamp('_supaglue_emitted_at', {
precision: 3,
mode: 'string',
}).notNull(),
id: text('id').notNull(),
_supaglue_last_modified_at: timestamp('_supaglue_last_modified_at', {
precision: 3,
mode: 'string',
Expand All @@ -114,7 +143,7 @@ export function getProviderObjectTable<TName extends string>(
table._supaglue_application_id,
table._supaglue_provider_name,
table._supaglue_customer_id,
table.id,
table._supaglue_id,
],
name: `${tableName}_pkey`,
}),
Expand All @@ -133,5 +162,8 @@ export function getProviderObjectTable<TName extends string>(
// beyond initial creation anyways...

export const crm_account = getCommonObjectTable('crm_account')
export const engagement_sequences = getCommonObjectTable('engagement_sequences')
export const engagement_sequence = getCommonObjectTable('engagement_sequence')
export const salesforce_account = getProviderObjectTable('salesforce_account')
export const custom_objects = getProviderObjectTable('hubspot_productgaps', {
custom: true,
})
6 changes: 3 additions & 3 deletions packages/worker/postgres/upsert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {sql} from 'drizzle-orm'
import prettier from 'prettier'
import prettierSql from 'prettier-plugin-sql'
import {db} from '.'
import {engagement_sequences} from './schema-dynamic'
import {engagement_sequence} from './schema-dynamic'
import {dbUpsert} from './upsert'

async function formatSql(sqlString: string) {
Expand All @@ -14,12 +14,12 @@ async function formatSql(sqlString: string) {
})
}

console.log(engagement_sequences._)
console.log(engagement_sequence._)

test('upsert query', async () => {
const query = dbUpsert(
db,
engagement_sequences,
engagement_sequence,
[
{
_supaglue_application_id: '$YOUR_APPLICATION_ID',
Expand Down

0 comments on commit 3b0c236

Please sign in to comment.