Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(types): SyncConfig -> DBSyncConfig #3459

Merged
merged 4 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs-v2/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ paths:
example: 'string'
sync_type:
type: string
example: 'INCREMENTAL'
example: 'full'
runs:
type: string
example: 'every 30 minutes'
Expand Down
9 changes: 5 additions & 4 deletions packages/jobs/lib/execution/sync.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import type { UnencryptedRecordData, ReturnedRecord } from '@nangohq/records';
import { records as recordsService, format as recordsFormatter, migrate as migrateRecords, clearDbTestsOnly as clearRecordsDb } from '@nangohq/records';
import { handleSyncSuccess, startSync } from './sync.js';
import type { TaskAction, TaskOnEvent, TaskSync, TaskSyncAbort, TaskWebhook } from '@nangohq/nango-orchestrator';
import type { Connection, Sync, SyncResult, Job as SyncJob, SyncConfig } from '@nangohq/shared';
import type { Connection, Sync, SyncResult, Job as SyncJob } from '@nangohq/shared';
import { isSyncJobRunning, seeders, getLatestSyncJob, updateSyncJobResult } from '@nangohq/shared';
import { Ok, stringifyError } from '@nangohq/utils';
import { envs } from '../env.js';
import type { DBSyncConfig } from '@nangohq/types';

const mockStartScript = vi.fn(() => Promise.resolve(Ok(undefined)));

Expand Down Expand Up @@ -173,7 +174,7 @@ const runJob = async (
rawRecords: UnencryptedRecordData[],
connection: Connection,
sync: Sync,
syncConfig: SyncConfig,
syncConfig: DBSyncConfig,
softDelete: boolean
): Promise<SyncResult> => {
const task: TaskSync = {
Expand Down Expand Up @@ -263,7 +264,7 @@ const verifySyncRun = async (
expectedResult: SyncResult,
trackDeletes: boolean,
softDelete = false
): Promise<{ connection: Connection; model: string; sync: Sync; syncConfig: SyncConfig; records: ReturnedRecord[] }> => {
): Promise<{ connection: Connection; model: string; sync: Sync; syncConfig: DBSyncConfig; records: ReturnedRecord[] }> => {
// Write initial records
const { connection, model, sync, syncConfig } = await populateRecords(initialRecords, trackDeletes);

Expand Down Expand Up @@ -291,7 +292,7 @@ async function populateRecords(
connection: Connection;
model: string;
sync: Sync;
syncConfig: SyncConfig;
syncConfig: DBSyncConfig;
syncJob: SyncJob;
}> {
const {
Expand Down
10 changes: 5 additions & 5 deletions packages/jobs/lib/execution/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
telemetry,
LogTypes,
errorNotificationService,
SyncType,
SyncJobsType,
updateSyncJobResult,
setLastSyncDate,
NangoError,
Expand Down Expand Up @@ -96,7 +96,7 @@ export async function startSync(task: TaskSync, startScriptFn = startScript): Pr

syncJob = await createSyncJob({
sync_id: task.syncId,
type: syncType === 'full' ? SyncType.FULL : SyncType.INCREMENTAL,
type: syncType === 'full' ? SyncJobsType.FULL : SyncJobsType.INCREMENTAL,
status: SyncStatus.RUNNING,
job_id: task.name,
nangoConnection: task.connection,
Expand Down Expand Up @@ -189,7 +189,7 @@ export async function startSync(task: TaskSync, startScriptFn = startScript): Pr
export async function handleSyncSuccess({ nangoProps }: { nangoProps: NangoProps }): Promise<void> {
const logCtx = await logContextGetter.get({ id: String(nangoProps.activityLogId) });
const runTime = (new Date().getTime() - nangoProps.startedAt.getTime()) / 1000;
const syncType: SyncTypeLiteral = nangoProps.syncConfig.sync_type?.toLocaleLowerCase() === 'full' ? 'full' : 'incremental';
const syncType: SyncTypeLiteral = nangoProps.syncConfig.sync_type === 'full' ? 'full' : 'incremental';

let team: DBTeam | undefined;
let environment: DBEnvironment | undefined;
Expand Down Expand Up @@ -322,7 +322,7 @@ export async function handleSyncSuccess({ nangoProps }: { nangoProps: NangoProps
updated,
deleted
},
operation: lastSyncDate ? SyncType.INCREMENTAL : SyncType.FULL
operation: lastSyncDate ? SyncJobsType.INCREMENTAL : SyncJobsType.FULL
});

if (res.isErr()) {
Expand Down Expand Up @@ -694,7 +694,7 @@ async function onFailure({
description: error.message
},
now: lastSyncDate,
operation: lastSyncDate ? SyncType.INCREMENTAL : SyncType.FULL
operation: lastSyncDate ? SyncJobsType.INCREMENTAL : SyncJobsType.FULL
});

if (res.isErr()) {
Expand Down
4 changes: 2 additions & 2 deletions packages/jobs/lib/execution/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Config, Job, NangoConnection, Sync } from '@nangohq/shared';
import {
NangoError,
SyncStatus,
SyncType,
SyncJobsType,
configService,
createSyncJob,
environmentService,
Expand Down Expand Up @@ -79,7 +79,7 @@ export async function startWebhook(task: TaskWebhook): Promise<Result<void>> {

syncJob = await createSyncJob({
sync_id: sync.id,
type: SyncType.INCREMENTAL,
type: SyncJobsType.INCREMENTAL,
status: SyncStatus.RUNNING,
job_id: task.name,
nangoConnection: task.connection,
Expand Down
2 changes: 1 addition & 1 deletion packages/jobs/lib/routes/tasks/putTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const nangoPropsSchema = z
pre_built: z.boolean(),
is_public: z.boolean(),
input: z.string().nullable(),
sync_type: z.enum(['full', 'incremental', 'FULL', 'INCREMENTAL']).nullable(),
sync_type: z.enum(['full', 'incremental']).nullable(),
metadata: z.record(z.string(), z.any())
})
.passthrough(),
Expand Down
5 changes: 0 additions & 5 deletions packages/node-client/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ export { getUserAgent } from './utils.js';

type CustomHeaders = Record<string, string | number | boolean>;

export enum SyncType {
INITIAL = 'INITIAL',
INCREMENTAL = 'INCREMENTAL'
}

const defaultHttpsAgent = new https.Agent({ keepAlive: true });

export interface AdminAxiosProps {
Expand Down
2 changes: 1 addition & 1 deletion packages/persist/lib/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export async function persistRecords({
}

const syncConfig = await getSyncConfigByJobId(syncJobId);
if (syncConfig && !syncConfig?.models.includes(model)) {
if (syncConfig && !syncConfig.models.includes(model)) {
const err = new Error(`The model '${model}' is not included in the declared sync models: ${syncConfig.models.join(', ')}.`);
await logCtx.error(`The model '${model}' is not included in the declared sync models`);

Expand Down
17 changes: 9 additions & 8 deletions packages/persist/lib/server.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { expect, describe, it, beforeAll, afterAll, vi } from 'vitest';
import { server } from './server.js';
import fetch from 'node-fetch';
import type { AuthCredentials, Sync, SyncConfig, Job as SyncJob } from '@nangohq/shared';
import type { AuthCredentials, Sync, Job as SyncJob } from '@nangohq/shared';
import db, { multipleMigrations } from '@nangohq/database';
import {
environmentService,
connectionService,
createSync,
createSyncJob,
SyncType,
SyncJobsType,
SyncStatus,
accountService,
configService,
getProvider
} from '@nangohq/shared';
import { logContextGetter, migrateLogsMapping } from '@nangohq/logs';
import { migrate as migrateRecords, records } from '@nangohq/records';
import type { DBEnvironment, DBTeam } from '@nangohq/types';
import type { DBEnvironment, DBSyncConfig, DBTeam } from '@nangohq/types';

const mockSecretKey = 'secret-key';

Expand Down Expand Up @@ -387,13 +387,13 @@ const initDb = async () => {
if (!providerConfig) throw new Error('Provider config not created');

const [syncConfig] = await db.knex
.from<SyncConfig>(`_nango_sync_configs`)
.from<DBSyncConfig>(`_nango_sync_configs`)
.insert({
environment_id: env.id,
sync_name: Math.random().toString(36).substring(7),
type: 'sync',
file_location: 'file_location',
nango_config_id: providerConfig.id,
nango_config_id: providerConfig.id!,
version: '1',
active: true,
runs: 'runs',
Expand All @@ -404,8 +404,9 @@ const initDb = async () => {
created_at: now,
updated_at: now,
models: ['model'],
model_schema: []
} as SyncConfig)
model_schema: [],
sync_type: 'full'
})
.returning('*');
if (!syncConfig) throw new Error('Sync config not created');

Expand All @@ -429,7 +430,7 @@ const initDb = async () => {

const syncJob = await createSyncJob({
sync_id: sync.id,
type: SyncType.FULL,
type: SyncJobsType.FULL,
status: SyncStatus.RUNNING,
job_id: `job-test`,
nangoConnection: connection
Expand Down
15 changes: 8 additions & 7 deletions packages/server/lib/controllers/config.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,12 @@ class ConfigController {
}

const syncConfigs = await getUniqueSyncsByProviderConfig(environmentId, providerConfigKey);
const syncs = syncConfigs.map((sync) => {
const { metadata, ...config } = sync;
const syncs: ProviderIntegration['syncs'] = syncConfigs.map((sync) => {
return {
...config,
description: metadata?.description
name: sync.sync_name,
created_at: sync.created_at,
updated_at: sync.updated_at,
description: sync.metadata?.description || null
};
});

Expand Down Expand Up @@ -266,10 +267,10 @@ class ConfigController {
connections,
docs: provider.docs,
connection_count,
has_webhook_user_defined_secret: provider.webhook_user_defined_secret,
has_webhook_user_defined_secret: provider.webhook_user_defined_secret || false,
webhook_url: webhookUrl
} as IntegrationWithCreds)
: ({ unique_key: config.unique_key, provider: config.provider, syncs, actions } as ProviderIntegration);
} satisfies IntegrationWithCreds)
: ({ unique_key: config.unique_key, provider: config.provider, syncs, actions } satisfies ProviderIntegration);

if (includeFlows && !isHosted) {
const availablePublicFlows = flowService.getAllAvailableFlowsAsStandardConfig();
Expand Down
7 changes: 6 additions & 1 deletion packages/server/lib/controllers/sync.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,12 @@ class SyncController {
res.status(400).send({ message: 'Invalid sync_name' });
return;
}
newFrequency = syncConfigs[0]!.runs;
const selected = syncConfigs[0];
if (!selected || !selected.runs) {
res.status(400).send({ message: 'failed to find sync' });
return;
}
newFrequency = selected.runs;
}

await setFrequency(syncId, frequency);
Expand Down
5 changes: 3 additions & 2 deletions packages/server/lib/webhook/internal-nango.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import get from 'lodash-es/get.js';
import { environmentService, connectionService, telemetry, getSyncConfigsByConfigIdForWebhook, LogActionEnum, LogTypes } from '@nangohq/shared';
import type { Config as ProviderConfig, SyncConfig, Connection } from '@nangohq/shared';
import type { Config as ProviderConfig, Connection } from '@nangohq/shared';
import type { LogContextGetter } from '@nangohq/logs';
import { getOrchestrator } from '../utils/utils.js';
import type { DBSyncConfig } from '@nangohq/types';

export interface InternalNango {
getWebhooks: (environment_id: number, nango_config_id: number) => Promise<SyncConfig[]>;
getWebhooks: (environment_id: number, nango_config_id: number) => Promise<DBSyncConfig[]>;
executeScriptForWebhooks(
integration: ProviderConfig,
body: Record<string, any>,
Expand Down
10 changes: 5 additions & 5 deletions packages/shared/lib/clients/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import type {
OrchestratorSchedule,
TaskType
} from '@nangohq/nango-orchestrator';
import type { NangoIntegrationData, Sync, SyncConfig } from '../models/index.js';
import type { NangoIntegrationData, Sync } from '../models/index.js';
import { SyncCommand, SyncStatus } from '../models/index.js';
import tracer from 'dd-trace';
import { clearLastSyncDate } from '../services/sync/sync.service.js';
import { isSyncJobRunning, updateSyncJobStatus } from '../services/sync/job.service.js';
import { getSyncConfigRaw, getSyncConfigBySyncId } from '../services/sync/config/config.service.js';
import environmentService from '../services/environment.service.js';
import type { DBEnvironment, DBTeam } from '@nangohq/types';
import type { DBEnvironment, DBSyncConfig, DBTeam } from '@nangohq/types';
import type { RecordCount } from '@nangohq/records';
import type { JsonValue } from 'type-fest';

Expand Down Expand Up @@ -265,7 +265,7 @@ export class Orchestrator {
integration: ProviderConfig;
connection: NangoConnection;
webhookName: string;
syncConfig: SyncConfig;
syncConfig: DBSyncConfig;
input: object;
logContextGetter: LogContextGetter;
}): Promise<Result<T, NangoError>> {
Expand All @@ -289,7 +289,7 @@ export class Orchestrator {
environment,
integration: { id: integration.id!, name: integration.unique_key, provider: integration.provider },
connection: { id: connection.id!, name: connection.connection_id },
syncConfig: { id: syncConfig.id!, name: syncConfig.sync_name }
syncConfig: { id: syncConfig.id, name: syncConfig.sync_name }
}
);

Expand Down Expand Up @@ -687,7 +687,7 @@ export class Orchestrator {
}
);

const frequencyMs = this.getFrequencyMs(syncData.runs);
const frequencyMs = this.getFrequencyMs(syncData.runs!);

if (frequencyMs.isErr()) {
const content = `The sync was not scheduled due to an error with the sync interval "${syncData.runs}": ${frequencyMs.error.message}`;
Expand Down
14 changes: 7 additions & 7 deletions packages/shared/lib/models/NangoConfig.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable @typescript-eslint/consistent-indexed-object-style */
import type { NangoConfigMetadata, NangoSyncEndpointV2, ScriptTypeLiteral } from '@nangohq/types';
import type { SyncType } from './Sync.js';
import type { NangoConfigMetadata, NangoSyncEndpointV2, ScriptTypeLiteral, SyncTypeLiteral } from '@nangohq/types';
import type { SyncJobsType } from './Sync.js';
import type { JSONSchema7 } from 'json-schema';

export interface NangoIntegrationDataV1 {
type?: ScriptTypeLiteral;
runs: string;
runs: string | null;
returns: string[];
input?: string | undefined;
input?: string | null;
track_deletes?: boolean;
auto_start?: boolean;
attributes?: object;
Expand All @@ -22,7 +22,7 @@ export interface NangoIntegrationDataV1 {
}

export interface NangoIntegrationDataV2 extends NangoIntegrationDataV1 {
sync_type?: SyncType;
sync_type?: SyncJobsType;
description?: string;
updated_at?: string;
'webhook-subscriptions'?: string[];
Expand Down Expand Up @@ -109,7 +109,7 @@ export interface NangoSyncModel {
export interface NangoSyncConfig {
name: string;
type?: ScriptTypeLiteral;
runs: string;
runs: string | null;
auto_start?: boolean;
attributes?: object;
description?: string;
Expand All @@ -127,7 +127,7 @@ export interface NangoSyncConfig {

// v2 additions
input?: NangoSyncModel | undefined;
sync_type?: SyncType;
sync_type?: SyncTypeLiteral;
webhookSubscriptions?: string[];
enabled?: boolean;
json_schema: JSONSchema7 | null;
Expand Down
17 changes: 10 additions & 7 deletions packages/shared/lib/models/Provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AuthModeType } from '@nangohq/types';
import type { AuthModeType, DBSyncConfig } from '@nangohq/types';
import type { NangoConnection } from './Connection.js';
import type { TimestampsAndDeleted } from './Generic.js';
import type { SyncConfig, Action } from './Sync.js';
import type { Action } from './Sync.js';

export interface Config extends TimestampsAndDeleted {
id?: number | undefined;
Expand All @@ -21,12 +21,15 @@ export interface Config extends TimestampsAndDeleted {
export interface IntegrationWithCreds extends Integration {
client_id: string;
client_secret: string;
scopes: string;
scopes: string | undefined;
auth_mode: AuthModeType;
app_link?: string;
custom: any;
app_link: string | null | undefined;
has_webhook: boolean;
webhook_url?: string;
syncs: SyncConfig[];
webhook_url: string | null;
webhook_secret: string | null;
has_webhook_user_defined_secret: boolean;
syncs: (Pick<DBSyncConfig, 'created_at' | 'updated_at'> & { name: string; description: string | null })[];
actions: Action[];
created_at: Date;
connections: NangoConnection[];
Expand All @@ -37,6 +40,6 @@ export interface IntegrationWithCreds extends Integration {
export interface Integration {
unique_key: string;
provider: string;
syncs: SyncConfig[];
syncs: (Pick<DBSyncConfig, 'created_at' | 'updated_at'> & { name: string; description: string | null })[];
actions: Action[];
}
Loading
Loading