Skip to content

Commit 9c83810

Browse files
committed
refactor: Use parameter destructuring for replicateAllDocs
This replies to #1483 (comment)
1 parent e9dc8c4 commit 9c83810

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

packages/cozy-pouch-link/src/startReplication.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ export const startReplication = (
7575
// For the first remote->local replication, we manually replicate all docs
7676
// as it avoids to replicate all revs history, which can lead to
7777
// performances issues
78-
docs = await replicateAllDocs(pouch, url, doctype, storage)
78+
docs = await replicateAllDocs({
79+
db: pouch,
80+
baseUrl: url,
81+
doctype,
82+
storage
83+
})
7984
const end = new Date()
8085
if (process.env.NODE_ENV !== 'production') {
8186
logger.info(
@@ -146,13 +151,14 @@ const filterDocs = docs => {
146151
* Note it saves the last replicated _id for each run and
147152
* starts from there in case the process stops before the end.
148153
*
149-
* @param {object} db - Pouch instance
150-
* @param {string} baseUrl - The remote instance
151-
* @param {string} doctype - The doctype to replicate
152-
* @param {import('./localStorage').PouchLocalStorage} storage - Methods to access local storage
154+
* @param {object} params - The replications parameters
155+
* @param {object} params.db - Pouch instance
156+
* @param {string} params.baseUrl - The remote instance
157+
* @param {string} params.doctype - The doctype to replicate
158+
* @param {import('./localStorage').PouchLocalStorage} params.storage - Methods to access local storage
153159
* @returns {Promise<Array>} The retrieved documents
154160
*/
155-
export const replicateAllDocs = async (db, baseUrl, doctype, storage) => {
161+
export const replicateAllDocs = async ({ db, baseUrl, doctype, storage }) => {
156162
const remoteUrlAllDocs = new URL(`${baseUrl}/_all_docs`)
157163
const batchSize = BATCH_SIZE
158164
let hasMore = true

packages/cozy-pouch-link/src/startReplication.spec.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ describe('startReplication', () => {
4848
const dummyDocs = generateDocs(2)
4949
fetchRemoteInstance.mockResolvedValue({ rows: dummyDocs })
5050

51-
const rep = await replicateAllDocs(null, url, undefined, storage)
51+
const rep = await replicateAllDocs({
52+
db: null,
53+
baseUrl: url,
54+
doctype: undefined,
55+
storage
56+
})
5257
const expectedDocs = dummyDocs.map(doc => doc.doc)
5358
expect(rep).toEqual(expectedDocs)
5459
expect(fetchRemoteInstance).toHaveBeenCalledTimes(1)
@@ -65,7 +70,12 @@ describe('startReplication', () => {
6570
rows: dummyDocs.slice(1000, 1002)
6671
})
6772

68-
const rep = await replicateAllDocs(null, url, undefined, storage)
73+
const rep = await replicateAllDocs({
74+
db: null,
75+
baseUrl: url,
76+
doctype: undefined,
77+
storage
78+
})
6979
const expectedDocs = dummyDocs.map(doc => doc.doc)
7080
expect(rep).toEqual(expectedDocs)
7181
expect(fetchRemoteInstance).toHaveBeenCalledTimes(2)
@@ -77,7 +87,12 @@ describe('startReplication', () => {
7787
const dummyDocs = generateDocs(10)
7888
fetchRemoteInstance.mockResolvedValue({ rows: dummyDocs.slice(5, 11) })
7989

80-
const rep = await replicateAllDocs(null, url, undefined, storage)
90+
const rep = await replicateAllDocs({
91+
db: null,
92+
baseUrl: url,
93+
doctype: undefined,
94+
storage
95+
})
8196

8297
const calledUrl = new URL(`${url}/_all_docs`)
8398
expect(fetchRemoteInstance).toHaveBeenCalledWith(calledUrl, {

0 commit comments

Comments
 (0)