From dc44365a7eeb4c6c5db0eb9491a461cbe84be133 Mon Sep 17 00:00:00 2001 From: Ldoppea Date: Tue, 27 Aug 2024 17:59:18 +0200 Subject: [PATCH] refactor: Use parameter destructuring for `replicateAllDocs` This replies to https://github.com/cozy/cozy-client/pull/1483#discussion_r1626156446 --- .../cozy-pouch-link/src/startReplication.js | 18 ++++++++++------ .../src/startReplication.spec.js | 21 ++++++++++++++++--- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/packages/cozy-pouch-link/src/startReplication.js b/packages/cozy-pouch-link/src/startReplication.js index 647961759..88731b8a4 100644 --- a/packages/cozy-pouch-link/src/startReplication.js +++ b/packages/cozy-pouch-link/src/startReplication.js @@ -75,7 +75,12 @@ export const startReplication = ( // For the first remote->local replication, we manually replicate all docs // as it avoids to replicate all revs history, which can lead to // performances issues - docs = await replicateAllDocs(pouch, url, doctype, storage) + docs = await replicateAllDocs({ + db: pouch, + baseUrl: url, + doctype, + storage + }) const end = new Date() if (process.env.NODE_ENV !== 'production') { logger.info( @@ -146,13 +151,14 @@ const filterDocs = docs => { * Note it saves the last replicated _id for each run and * starts from there in case the process stops before the end. * - * @param {object} db - Pouch instance - * @param {string} baseUrl - The remote instance - * @param {string} doctype - The doctype to replicate - * @param {import('./localStorage').PouchLocalStorage} storage - Methods to access local storage + * @param {object} params - The replications parameters + * @param {object} params.db - Pouch instance + * @param {string} params.baseUrl - The remote instance + * @param {string} params.doctype - The doctype to replicate + * @param {import('./localStorage').PouchLocalStorage} params.storage - Methods to access local storage * @returns {Promise} The retrieved documents */ -export const replicateAllDocs = async (db, baseUrl, doctype, storage) => { +export const replicateAllDocs = async ({ db, baseUrl, doctype, storage }) => { const remoteUrlAllDocs = new URL(`${baseUrl}/_all_docs`) const batchSize = BATCH_SIZE let hasMore = true diff --git a/packages/cozy-pouch-link/src/startReplication.spec.js b/packages/cozy-pouch-link/src/startReplication.spec.js index d3ea709c7..e6bc72a7d 100644 --- a/packages/cozy-pouch-link/src/startReplication.spec.js +++ b/packages/cozy-pouch-link/src/startReplication.spec.js @@ -48,7 +48,12 @@ describe('startReplication', () => { const dummyDocs = generateDocs(2) fetchRemoteInstance.mockResolvedValue({ rows: dummyDocs }) - const rep = await replicateAllDocs(null, url, undefined, storage) + const rep = await replicateAllDocs({ + db: null, + baseUrl: url, + doctype: undefined, + storage + }) const expectedDocs = dummyDocs.map(doc => doc.doc) expect(rep).toEqual(expectedDocs) expect(fetchRemoteInstance).toHaveBeenCalledTimes(1) @@ -65,7 +70,12 @@ describe('startReplication', () => { rows: dummyDocs.slice(1000, 1002) }) - const rep = await replicateAllDocs(null, url, undefined, storage) + const rep = await replicateAllDocs({ + db: null, + baseUrl: url, + doctype: undefined, + storage + }) const expectedDocs = dummyDocs.map(doc => doc.doc) expect(rep).toEqual(expectedDocs) expect(fetchRemoteInstance).toHaveBeenCalledTimes(2) @@ -77,7 +87,12 @@ describe('startReplication', () => { const dummyDocs = generateDocs(10) fetchRemoteInstance.mockResolvedValue({ rows: dummyDocs.slice(5, 11) }) - const rep = await replicateAllDocs(null, url, undefined, storage) + const rep = await replicateAllDocs({ + db: null, + baseUrl: url, + doctype: undefined, + storage + }) const calledUrl = new URL(`${url}/_all_docs`) expect(fetchRemoteInstance).toHaveBeenCalledWith(calledUrl, {