Skip to content

Datastore/feat add per model sync page size #11179

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
49 changes: 49 additions & 0 deletions packages/datastore/__tests__/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,55 @@ describe('Sync', () => {
});
});

/*
* This is where new tests would be added for per-model syncPageSize
* Proposed test cases are added below with // comments
*/

// describe('syncPageSize', () => {
// test('should use default syncPageSize if not specified (null, undefined or zero)', async () => {
// const resolveResponse = {
// data: {
// syncPosts: {
// items: [
// {
// id: '1',
// title: 'Item 1',
// },
// {
// id: '2',
// title: 'Item 2',
// },
// ],
// },
// },
// };

// const SyncProcessor = jitteredRetrySyncProcessorSetup({
// resolveResponse,
// });

// await SyncProcessor.jitteredRetry({
// query: defaultQuery,
// variables: defaultVariables,
// opName: defaultOpName,
// modelDefinition: defaultModelDefinition,
// });

// expect(mockGraphQl).toHaveBeenCalledWith(
// expect.objectContaining({
// variables: {
// syncPageSize: DEFAULT_SYNC_PAGE_SIZE,
// },
// })
// );
// });
// test('the limit for the sync query should equal syncPageSize if it is defined as a natural number', async () => {});
// test('a meaningful error should be thrown if syncPageSize is not defined as a natural number', async () => {});
// test('the limit for the sync query should equal perModelSyncPageSize[`modelName`] if it is defined as a natural number', async () => {});
// test('a meaningful error should be thrown if perModelSyncPageSize[`modelName`] is not a natural number', async () => {});
// }

describe('error handler', () => {
const errorHandler = jest.fn();
const data = {
Expand Down
4 changes: 4 additions & 0 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,7 @@ class DataStore {
private storage?: Storage;
private sync?: SyncEngine;
private syncPageSize!: number;
private perModelSyncPageSize?: { [modelName: string]: number };
private syncExpressions!: SyncExpression[];
private syncPredicates: WeakMap<SchemaModel, ModelPredicate<any> | null> =
new WeakMap<SchemaModel, ModelPredicate<any>>();
Expand Down Expand Up @@ -2460,6 +2461,9 @@ class DataStore {
// store on config object, so that Sync, Subscription, and Mutation processors can have access
this.amplifyConfig.syncPageSize = this.syncPageSize;

// also store perModelSyncPageSize on config object, so that Sync, Subscription, and Mutation processors can have access
this.amplifyConfig.perModelSyncPageSize = this.perModelSyncPageSize;

this.fullSyncInterval =
(configDataStore && configDataStore.fullSyncInterval) ||
configFullSyncInterval ||
Expand Down
16 changes: 11 additions & 5 deletions packages/datastore/src/sync/processors/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ class SyncProcessor {
start(
typesLastSync: Map<SchemaModel, [string, number]>
): Observable<SyncModelPage> {
const { maxRecordsToSync, syncPageSize } = this.amplifyConfig;
const { maxRecordsToSync, syncPageSize, perModelSyncPageSize } =
this.amplifyConfig;
const parentPromises = new Map<string, Promise<void>>();
const observable = new Observable<SyncModelPage>(observer => {
const sortedTypesLastSyncs = Object.values(this.schema.namespaces).reduce(
Expand Down Expand Up @@ -360,10 +361,15 @@ class SyncProcessor {
return;
}

const limit = Math.min(
maxRecordsToSync - recordsReceived,
syncPageSize
);
const limit = perModelSyncPageSize?.[modelDefinition.name]
? Math.min(
maxRecordsToSync - recordsReceived,
perModelSyncPageSize[modelDefinition.name]
)
: Math.min(
maxRecordsToSync - recordsReceived,
syncPageSize
);

({ items, nextToken, startedAt } = await this.retrievePage(
modelDefinition,
Expand Down
2 changes: 2 additions & 0 deletions packages/datastore/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ export type DataStoreConfig = {
errorHandler?: (error: SyncError<PersistentModel>) => void; // default : logger.warn
maxRecordsToSync?: number; // merge
syncPageSize?: number;
perModelSyncPageSize?: { [modelName: string]: number }; // over-ride syncPageSize for specific models
fullSyncInterval?: number;
syncExpressions?: SyncExpression[];
authProviders?: AuthProviders;
Expand All @@ -944,6 +945,7 @@ export type DataStoreConfig = {
errorHandler?: (error: SyncError<PersistentModel>) => void; // default : logger.warn
maxRecordsToSync?: number; // merge
syncPageSize?: number;
perModelSyncPageSize?: { [modelName: string]: number }; // over-ride syncPageSize for specific models
fullSyncInterval?: number;
syncExpressions?: SyncExpression[];
authProviders?: AuthProviders;
Expand Down