Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: add support for merging strategy into the runner sdk #3420

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TBonnin
Copy link
Collaborator

@TBonnin TBonnin commented Jan 31, 2025

  • adding setMergingStrategy runnder-sdk function
    • if strategy is ignore-if-modified-since the function calls persist /cursor and store it in a private variable. Its value is then forwarded when calling batchSave/Update/Delete which are now sending the next cursor. Again the updated cursor is forwarded to batchSave...
    • One limitation of this approach is that it makes concurrent batchSave undefined behavior. We already recommend not to do that anyway because it doesn't play well with rate-limiting
  • refactor persist API related code in runner-sdk into its own file
  • send merging strategy to persist and handle response with next merging strategy with new cursor
  • fix a couple of bugs from previous PR
  • modify CLI compiler to disallow setting merging strategy multiple times or setting it after bachSave

Copy link

linear bot commented Jan 31, 2025

@TBonnin TBonnin force-pushed the tbonnin/nan-2415/merging-sdk branch from 905b0c8 to b1c0f9a Compare January 31, 2025 02:02
@@ -70,15 +70,18 @@ class ParserService {
'triggerAction'
];

const disallowedActionCalls = ['batchSend', 'batchSave', 'batchDelete'];
const disallowedActionCalls = ['batchSend', 'batchSave', 'batchDelete', 'batchUpdate'];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated but batchUpdate was missing

);
usedCorrectly = false;
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding support for functions like setMergingStrategy that take multiple models as argument

Success: {
cursor?: string;
};
Success: GetCursorSuccess;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moving the type to types package so it can be used by the runner-sdk

@@ -8,7 +8,7 @@ const mergingStrategySchema = z.discriminatedUnion('strategy', [
}),
z.object({
strategy: z.literal('ignore_if_modified_after_cursor'),
cursor: z.string()
cursor: z.string().optional()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgot the scenario in my previous PR where there is not records yet (first sync or after emptying the cache). In this case there is no cursor yet

@@ -31,8 +32,7 @@ export const validateRecords = <E extends Endpoint<any>>() =>
environmentId: z.coerce.number().int().positive(),
nangoConnectionId: z.coerce.number().int().positive(),
syncId: z.string(),
syncJobId: z.coerce.number().int().positive(),
merging: mergingStrategySchema.default({ strategy: 'override' })
syncJobId: z.coerce.number().int().positive()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

.orderBy([
{ column: 'updated_at', order: 'asc' },
{ column: 'id', order: 'asc' }
]);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

insertion order is different from cursor order since cursor is based on id which is a generated uuid. so we need to order based on cursor to get the latest cursor below


export type MergingStrategy = { strategy: 'override' } | { strategy: 'ignore_if_modified_after_cursor'; cursor: string };

export type CursorOffset = 'first' | 'last';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to types package

return value;
});
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessary anymore. Was used by the persistApi related code in NangoSync runner-sdk class.
PersistApi related code now lives in its own file and the secret is not logged anymore

import type { Result } from '@nangohq/utils';
import type { CursorOffset, DeleteRecordsSuccess, GetCursorSuccess, MergingStrategy, PostRecordsSuccess, PutRecordsSuccess } from '@nangohq/types';

export interface PersistApi {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

persist related code in runner-sdk NangoSync was starting to get out of control. Moving all the persist related code to its own file


export interface DeleteRecordsSuccess {
nextMerging: MergingStrategy;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could have a single one. wdyt?

@TBonnin TBonnin marked this pull request as ready for review January 31, 2025 02:31
@TBonnin TBonnin requested a review from a team January 31, 2025 02:32
@TBonnin TBonnin force-pushed the tbonnin/nan-2415/merging-sdk branch from b1c0f9a to b5c60d6 Compare January 31, 2025 03:02
const callsReferencingModelsToCheck = callsBatchingRecords.concat('setMergingStrategy');
const proxyLines: number[] = [];
const batchingRecordsLines: number[] = [];
const setMergingStrategyLines: number[] = [];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see below. addressing footguns by raising compilation errors and enforce a couple of rules:

  • setMergingStrategy must be called only once
  • setMergingStrategy must be called before any batch operation
  • setMergingStrategy must be called before any proxy/get/post/...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant