@@ -1090,10 +1090,10 @@ export class Stack {
1090
1090
} else if ( this . q . includeSpecificReferences ) {
1091
1091
await this
1092
1092
. includeSpecificReferences ( data , this . q . content_type_uid , locale , this . q
1093
- . includeSpecificReferences )
1093
+ . includeSpecificReferences , true )
1094
1094
} else if ( this . q . includeAllReferences ) {
1095
1095
// 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 )
1097
1097
} else {
1098
1098
1099
1099
await this . includeAssetsOnly ( data , locale , this . q . content_type_uid )
@@ -1292,7 +1292,7 @@ export class Stack {
1292
1292
return { output }
1293
1293
}
1294
1294
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 ) {
1296
1296
const ctQuery = {
1297
1297
_content_type_uid : this . types . content_types ,
1298
1298
uid : contentTypeUid ,
@@ -1324,13 +1324,12 @@ export class Stack {
1324
1324
// else, self-recursively iterate and fetch references
1325
1325
// Note: Shelf is the one holding `pointers` to the actual entry
1326
1326
// 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 )
1328
1328
1329
1329
return
1330
1330
}
1331
1331
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 ) {
1334
1333
if ( oldShelf . length === 0 ) {
1335
1334
return
1336
1335
} else if ( ctQuery . $or . length === 0 && eQuery . $or . length > 0 ) {
@@ -1370,12 +1369,18 @@ export class Stack {
1370
1369
1371
1370
if ( flag ) {
1372
1371
for ( let e = 0 , f = oldShelf [ i ] . path . length ; e < f ; e ++ ) {
1373
- // tslint:disable-next-line: max-line-length
1374
1372
if (
1375
1373
oldShelf [ i ] . path [ e ] ?. hasOwnProperty ( "_content_type_uid" ) &&
1376
1374
Object . keys ( oldShelf [ i ] . path [ e ] ) . length === 2
1377
1375
) {
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
+ }
1379
1384
break ;
1380
1385
}
1381
1386
}
@@ -1387,7 +1392,7 @@ export class Stack {
1387
1392
result = null
1388
1393
1389
1394
// 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 )
1391
1396
1392
1397
return
1393
1398
}
@@ -1667,7 +1672,7 @@ export class Stack {
1667
1672
return
1668
1673
}
1669
1674
1670
- private async bindReferences ( entries : any [ ] , contentTypeUid : string , locale : string ) {
1675
+ private async bindReferences ( entries : any [ ] , contentTypeUid : string , locale : string , preserveUnpublished = false ) {
1671
1676
const ctQuery : IQuery = {
1672
1677
$or : [ {
1673
1678
_content_type_uid : this . types . content_types ,
@@ -1699,7 +1704,7 @@ export class Stack {
1699
1704
// Note: Shelf is the one holding `pointers` to the actual entry
1700
1705
// Once the pointer has been used, for GC, point the object to null
1701
1706
1702
- return this . includeAllReferencesIteration ( queries , ctQueries , locale , objectPointerList )
1707
+ return this . includeAllReferencesIteration ( queries , ctQueries , locale , objectPointerList , 0 , preserveUnpublished )
1703
1708
}
1704
1709
1705
1710
private async bindLeftoverAssets ( queries : IQuery , locale : string , pointerList : IShelf [ ] ) {
@@ -1727,7 +1732,7 @@ export class Stack {
1727
1732
}
1728
1733
1729
1734
// 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 ) {
1731
1736
if ( depth > this . q . referenceDepth || oldObjectPointerList . length === 0 ) {
1732
1737
return
1733
1738
} else if ( oldCtQueries . $or . length === 0 && oldObjectPointerList . length > 0 && oldEntryQueries . $or . length > 0 ) {
@@ -1767,10 +1772,16 @@ export class Stack {
1767
1772
1768
1773
if ( flag ) {
1769
1774
for ( let e = 0 , f = oldObjectPointerList [ i ] . path . length ; e < f ; e ++ ) {
1770
- // tslint:disable-next-line: max-line-length
1771
1775
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 ;
1774
1785
}
1775
1786
}
1776
1787
}
@@ -1781,7 +1792,7 @@ export class Stack {
1781
1792
1782
1793
++ depth
1783
1794
// 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 )
1785
1796
1786
1797
return
1787
1798
}
0 commit comments