From 7be592ccc26fa11fd0ac500c7aa5ef3f12f664f8 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Tue, 7 Oct 2025 11:33:22 +0530 Subject: [PATCH 1/2] fix: deduplicate shelf entries to prevent duplicate references in include queries --- package-lock.json | 4 ++-- package.json | 2 +- src/stack.ts | 48 +++++++++++++++++++++++++++-------------------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index d7ee16f..563c29c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/datasync-filesystem-sdk", - "version": "1.4.0", + "version": "1.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/datasync-filesystem-sdk", - "version": "1.4.0", + "version": "1.5.0", "license": "MIT", "dependencies": { "json-mask": "2.0.0", diff --git a/package.json b/package.json index e9150d8..2feb157 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/datasync-filesystem-sdk", - "version": "1.4.0", + "version": "1.5.0", "description": "JavaScript filesystem SDK to query data synced via @contentstack/datasync-content-store-filesystem", "main": "dist/index.js", "scripts": { diff --git a/src/stack.ts b/src/stack.ts index 4f063e9..aeaf119 100755 --- a/src/stack.ts +++ b/src/stack.ts @@ -1641,11 +1641,13 @@ export class Stack { uid: elem, }) - shelf.push({ - path: data, - position: idx, - uid: elem, - }) + if (!shelf.some(entry => entry.path === data && entry.position === idx && entry.uid === elem)) { + shelf.push({ + path: data, + position: idx, + uid: elem, + }) + } } else if (elem && typeof elem === 'object' && elem.hasOwnProperty('_content_type_uid')) { queryBucket.$or.push({ _content_type_uid: elem._content_type_uid, @@ -1653,11 +1655,13 @@ export class Stack { uid: elem.uid, }) - shelf.push({ - path: data, - position: idx, - uid: elem.uid, - }) + if (!shelf.some(entry => entry.path === data && entry.position === idx && entry.uid === elem.uid)) { + shelf.push({ + path: data, + position: idx, + uid: elem.uid, + }) + } } }) } else if (typeof data === 'object') { @@ -1668,11 +1672,13 @@ export class Stack { uid: data.uid, }) - shelf.push({ - path: parent, - position: pos, - uid: data.uid, - }) + if (!shelf.some(entry => entry.path === parent && entry.position === pos && entry.uid === data.uid)) { + shelf.push({ + path: parent, + position: pos, + uid: data.uid, + }) + } } } } else if (typeof data === 'string') { @@ -1683,11 +1689,13 @@ export class Stack { uid: data, }) - shelf.push({ - path: parent, - position: pos, - uid: data, - }) + if (!shelf.some(entry => entry.path === parent && entry.position === pos && entry.uid === data)) { + shelf.push({ + path: parent, + position: pos, + uid: data, + }) + } } } else { const currentField = pathArr[counter] From 11566d8047d08692b70ff8d6a5275b82416d5cb3 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Tue, 7 Oct 2025 12:43:19 +0530 Subject: [PATCH 2/2] feat: add helper method to prevent duplicate entries in shelf --- src/stack.ts | 52 +++++++++++++++++++++------------------------- typings/stack.d.ts | 10 +++++++++ 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/stack.ts b/src/stack.ts index aeaf119..2abef91 100755 --- a/src/stack.ts +++ b/src/stack.ts @@ -1627,6 +1627,26 @@ export class Stack { } } + /** + * @private + * @method addToShelfIfNotExists + * @description Helper function to add entry to shelf only if it doesn't already exist + * @param {IShelf[]} shelf - The shelf array to add to + * @param {any} path - The path reference + * @param {number} position - The position in the path + * @param {string} uid - The unique identifier + */ + private addToShelfIfNotExists(shelf: IShelf[], path, position, uid) { + const exists = shelf.some(entry => + entry.path === path && + entry.position === position && + entry.uid === uid + ); + if (!exists) { + shelf.push({ path, position, uid }); + } + } + // tslint:disable-next-line: max-line-length private fetchPathDetails(data: any, locale: string, pathArr: string[], queryBucket: IQuery, shelf, assetsOnly = false, parent, pos, counter = 0) { if (counter === (pathArr.length)) { @@ -1641,13 +1661,7 @@ export class Stack { uid: elem, }) - if (!shelf.some(entry => entry.path === data && entry.position === idx && entry.uid === elem)) { - shelf.push({ - path: data, - position: idx, - uid: elem, - }) - } + this.addToShelfIfNotExists(shelf, data, idx, elem); } else if (elem && typeof elem === 'object' && elem.hasOwnProperty('_content_type_uid')) { queryBucket.$or.push({ _content_type_uid: elem._content_type_uid, @@ -1655,13 +1669,7 @@ export class Stack { uid: elem.uid, }) - if (!shelf.some(entry => entry.path === data && entry.position === idx && entry.uid === elem.uid)) { - shelf.push({ - path: data, - position: idx, - uid: elem.uid, - }) - } + this.addToShelfIfNotExists(shelf, data, idx, elem.uid); } }) } else if (typeof data === 'object') { @@ -1672,13 +1680,7 @@ export class Stack { uid: data.uid, }) - if (!shelf.some(entry => entry.path === parent && entry.position === pos && entry.uid === data.uid)) { - shelf.push({ - path: parent, - position: pos, - uid: data.uid, - }) - } + this.addToShelfIfNotExists(shelf, parent, pos, data.uid); } } } else if (typeof data === 'string') { @@ -1689,13 +1691,7 @@ export class Stack { uid: data, }) - if (!shelf.some(entry => entry.path === parent && entry.position === pos && entry.uid === data)) { - shelf.push({ - path: parent, - position: pos, - uid: data, - }) - } + this.addToShelfIfNotExists(shelf, parent, pos, data); } } else { const currentField = pathArr[counter] diff --git a/typings/stack.d.ts b/typings/stack.d.ts index 069397f..1a35fbe 100644 --- a/typings/stack.d.ts +++ b/typings/stack.d.ts @@ -551,6 +551,16 @@ export declare class Stack { private includeReferenceIteration; private subIncludeReferenceIteration; private getReferencePath; + /** + * @private + * @method addToShelfIfNotExists + * @description Helper function to add entry to shelf only if it doesn't already exist + * @param {IShelf[]} shelf - The shelf array to add to + * @param {any} path - The path reference + * @param {number} position - The position in the path + * @param {string} uid - The unique identifier + */ + private addToShelfIfNotExists; private fetchPathDetails; private fetchDocuments; private includeAssetsOnly;