Skip to content

Commit e6999ea

Browse files
authored
Merge pull request #87 from contentstack/development
DX | 21-07-2025 | Release
2 parents e3d1723 + 735f44b commit e6999ea

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ Contentstack is a headless CMS with an API-first approach. It is a CMS that deve
2020
|referenceDepth|number|2|**Optional** The default nested-reference-field depth that'd be considered when calling .includeReferences(). This can be overridden by passing a numerical argument to .includeReferences(4)|
2121
|projections|object|{_content_type_uid: 0}|**Optional** Keys that by default would be removed from results. Pass `key: 0` to remove, `key: 1` to override the existing..|
2222

23+
### Environment Variables
24+
25+
The SDK supports the following environment variables for advanced configuration:
26+
27+
| Variable | Description |
28+
|-------------|-------------|
29+
| `APP_ROOT` | (Optional) Sets the root directory for content storage. If not set, defaults to the current working directory. |
30+
2331
### Config Overview
2432

2533
Here's an overview of the SDK's configurable properties

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/datasync-filesystem-sdk",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "JavaScript filesystem SDK to query data synced via @contentstack/datasync-content-store-filesystem",
55
"main": "dist/index.js",
66
"scripts": {

src/stack.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,10 +1090,10 @@ export class Stack {
10901090
} else if (this.q.includeSpecificReferences) {
10911091
await this
10921092
.includeSpecificReferences(data, this.q.content_type_uid, locale, this.q
1093-
.includeSpecificReferences)
1093+
.includeSpecificReferences, true)
10941094
} else if (this.q.includeAllReferences) {
10951095
// need re-writes
1096-
await this.bindReferences(data, this.q.content_type_uid, locale)
1096+
await this.bindReferences(data, this.q.content_type_uid, locale, false)
10971097
} else {
10981098

10991099
await this.includeAssetsOnly(data, locale, this.q.content_type_uid)
@@ -1292,7 +1292,7 @@ export class Stack {
12921292
return { output }
12931293
}
12941294

1295-
private async includeSpecificReferences(entries: any[], contentTypeUid: string, locale: string, include: string[]) {
1295+
private async includeSpecificReferences(entries: any[], contentTypeUid: string, locale: string, include: string[], preserveUnpublished = false) {
12961296
const ctQuery = {
12971297
_content_type_uid: this.types.content_types,
12981298
uid: contentTypeUid,
@@ -1324,13 +1324,12 @@ export class Stack {
13241324
// else, self-recursively iterate and fetch references
13251325
// Note: Shelf is the one holding `pointers` to the actual entry
13261326
// Once the pointer has been used, for GC, point the object to null
1327-
await this.includeReferenceIteration(queries, schemaList, locale, pendingPath, shelf)
1327+
await this.includeReferenceIteration(queries, schemaList, locale, pendingPath, shelf, preserveUnpublished)
13281328

13291329
return
13301330
}
13311331

1332-
private async includeReferenceIteration(eQuery: any, ctQuery: any, locale: string, include: string[], oldShelf:
1333-
IShelf[]) {
1332+
private async includeReferenceIteration(eQuery: any, ctQuery: any, locale: string, include: string[], oldShelf: IShelf[], preserveUnpublished = false) {
13341333
if (oldShelf.length === 0) {
13351334
return
13361335
} else if (ctQuery.$or.length === 0 && eQuery.$or.length > 0) {
@@ -1370,12 +1369,18 @@ export class Stack {
13701369

13711370
if (flag) {
13721371
for (let e = 0, f = oldShelf[i].path.length; e < f; e++) {
1373-
// tslint:disable-next-line: max-line-length
13741372
if (
13751373
oldShelf[i].path[e]?.hasOwnProperty("_content_type_uid") &&
13761374
Object.keys(oldShelf[i].path[e]).length === 2
13771375
) {
1378-
(oldShelf[i].path as any).splice(e, 1);
1376+
const ref = oldShelf[i].path[e];
1377+
if (preserveUnpublished) {
1378+
((oldShelf[i].path as any) as any[])[e] = typeof ref === "string"
1379+
? { uid: ref, _content_type_uid: null }
1380+
: { uid: (ref as any).uid, _content_type_uid: (ref as any)._content_type_uid };
1381+
} else {
1382+
((oldShelf[i].path as any) as any[]).splice(e, 1);
1383+
}
13791384
break;
13801385
}
13811386
}
@@ -1387,7 +1392,7 @@ export class Stack {
13871392
result = null
13881393

13891394
// Iterative loops, that traverses paths and binds them onto entries
1390-
await this.includeReferenceIteration(queries, schemaList, locale, pendingPath, shelf)
1395+
await this.includeReferenceIteration(queries, schemaList, locale, pendingPath, shelf, preserveUnpublished)
13911396

13921397
return
13931398
}
@@ -1667,7 +1672,7 @@ export class Stack {
16671672
return
16681673
}
16691674

1670-
private async bindReferences(entries: any[], contentTypeUid: string, locale: string) {
1675+
private async bindReferences(entries: any[], contentTypeUid: string, locale: string, preserveUnpublished = false) {
16711676
const ctQuery: IQuery = {
16721677
$or: [{
16731678
_content_type_uid: this.types.content_types,
@@ -1699,7 +1704,7 @@ export class Stack {
16991704
// Note: Shelf is the one holding `pointers` to the actual entry
17001705
// Once the pointer has been used, for GC, point the object to null
17011706

1702-
return this.includeAllReferencesIteration(queries, ctQueries, locale, objectPointerList)
1707+
return this.includeAllReferencesIteration(queries, ctQueries, locale, objectPointerList, 0, preserveUnpublished)
17031708
}
17041709

17051710
private async bindLeftoverAssets(queries: IQuery, locale: string, pointerList: IShelf[]) {
@@ -1727,7 +1732,7 @@ export class Stack {
17271732
}
17281733

17291734
// tslint:disable-next-line: max-line-length
1730-
private async includeAllReferencesIteration(oldEntryQueries: IQuery, oldCtQueries: IQuery, locale: string, oldObjectPointerList: IShelf[], depth = 0) {
1735+
private async includeAllReferencesIteration(oldEntryQueries: IQuery, oldCtQueries: IQuery, locale: string, oldObjectPointerList: IShelf[], depth = 0, preserveUnpublished = false) {
17311736
if (depth > this.q.referenceDepth || oldObjectPointerList.length === 0) {
17321737
return
17331738
} else if (oldCtQueries.$or.length === 0 && oldObjectPointerList.length > 0 && oldEntryQueries.$or.length > 0) {
@@ -1767,10 +1772,16 @@ export class Stack {
17671772

17681773
if (flag) {
17691774
for (let e = 0, f = oldObjectPointerList[i].path.length; e < f; e++) {
1770-
// tslint:disable-next-line: max-line-length
17711775
if (oldObjectPointerList[i].path[e]?.hasOwnProperty('_content_type_uid') && Object.keys(oldObjectPointerList[i].path[e]).length === 2) {
1772-
(oldObjectPointerList[i].path as any).splice(e, 1)
1773-
break
1776+
const ref = oldObjectPointerList[i].path[e];
1777+
if (preserveUnpublished) {
1778+
((oldObjectPointerList[i].path as any) as any[])[e] = typeof ref === "string"
1779+
? { uid: ref, _content_type_uid: null }
1780+
: { uid: (ref as any).uid, _content_type_uid: (ref as any)._content_type_uid };
1781+
} else {
1782+
((oldObjectPointerList[i].path as any) as any[]).splice(e, 1);
1783+
}
1784+
break;
17741785
}
17751786
}
17761787
}
@@ -1781,7 +1792,7 @@ export class Stack {
17811792

17821793
++depth
17831794
// Iterative loops, that traverses paths and binds them onto entries
1784-
await this.includeAllReferencesIteration(queries, ctQueries, locale, shelf, depth)
1795+
await this.includeAllReferencesIteration(queries, ctQueries, locale, shelf, depth, preserveUnpublished)
17851796

17861797
return
17871798
}

0 commit comments

Comments
 (0)