Skip to content

Commit

Permalink
refactor: Use parameter destructuring for replicateAllDocs
Browse files Browse the repository at this point in the history
This replies to #1483 (comment)
  • Loading branch information
Ldoppea committed Sep 13, 2024
1 parent e9dc8c4 commit 9c83810
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
18 changes: 12 additions & 6 deletions packages/cozy-pouch-link/src/startReplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<Array>} 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
Expand Down
21 changes: 18 additions & 3 deletions packages/cozy-pouch-link/src/startReplication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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, {
Expand Down

0 comments on commit 9c83810

Please sign in to comment.