@@ -1471,7 +1471,7 @@ class Stack {
1471
1471
// Ignore references include, for empty list, exclude call, content type & assets
1472
1472
if ( result . length === 0 || this . internal . excludeReferences || this . q . content_type_uid === this
1473
1473
. types . content_types || this . q . content_type_uid
1474
- === this . types . assets ) {
1474
+ === this . types . assets || this . internal . onlyCount ) {
1475
1475
// Do nothing
1476
1476
}
1477
1477
else if ( this . internal . includeSpecificReferences ) {
@@ -1485,7 +1485,7 @@ class Stack {
1485
1485
yield this . includeAssetsOnly ( result , this . q . content_type_uid , this . q . locale ) ;
1486
1486
}
1487
1487
if ( this . internal . queryReferences ) {
1488
- result = sift_1 . default ( this . internal . queryReferences , result ) ;
1488
+ result = result . filter ( sift_1 . default ( this . internal . queryReferences ) ) ;
1489
1489
if ( this . internal . skip ) {
1490
1490
result = result . splice ( this . internal . skip , this . internal . limit ) ;
1491
1491
}
@@ -1522,21 +1522,9 @@ class Stack {
1522
1522
* @returns {object } Returns count of the entries/asset's matched
1523
1523
*/
1524
1524
count ( query ) {
1525
- return new Promise ( ( resolve , reject ) => {
1526
- const queryFilters = this . preProcess ( query ) ;
1527
- this . collection = this . collection . find ( queryFilters ) ;
1528
- return this . collection
1529
- . count ( )
1530
- . then ( ( result ) => {
1531
- this . cleanup ( ) ;
1532
- return resolve ( {
1533
- count : result ,
1534
- } ) ;
1535
- } )
1536
- . catch ( ( error ) => {
1537
- this . cleanup ( ) ;
1538
- return reject ( error ) ;
1539
- } ) ;
1525
+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
1526
+ this . internal . onlyCount = true ;
1527
+ return this . find ( query ) ;
1540
1528
} ) ;
1541
1529
}
1542
1530
/**
@@ -1635,7 +1623,13 @@ class Stack {
1635
1623
postProcess ( result ) {
1636
1624
return __awaiter ( this , void 0 , void 0 , function * ( ) {
1637
1625
const count = ( result === null ) ? 0 : result . length ;
1638
- const output = { } ;
1626
+ const output = {
1627
+ locale : this . q . locale ,
1628
+ } ;
1629
+ if ( this . internal . onlyCount ) {
1630
+ output . count = count ;
1631
+ return output ;
1632
+ }
1639
1633
switch ( this . q . content_type_uid ) {
1640
1634
case this . types . assets :
1641
1635
if ( this . internal . single ) {
@@ -1644,8 +1638,7 @@ class Stack {
1644
1638
else {
1645
1639
output . assets = result ;
1646
1640
}
1647
- result . content_type_uid = 'assets' ;
1648
- result . locale = this . q . locale ;
1641
+ output . content_type_uid = 'assets' ;
1649
1642
break ;
1650
1643
case this . types . content_types :
1651
1644
if ( this . internal . single ) {
@@ -1654,7 +1647,7 @@ class Stack {
1654
1647
else {
1655
1648
output . content_types = result ;
1656
1649
}
1657
- result . content_type_uid = 'content_types' ;
1650
+ output . content_type_uid = 'content_types' ;
1658
1651
break ;
1659
1652
default :
1660
1653
if ( this . internal . single ) {
@@ -1663,18 +1656,18 @@ class Stack {
1663
1656
else {
1664
1657
output . entries = result ;
1665
1658
}
1666
- result . content_type_uid = this . q . content_type_uid ;
1667
- result . locale = this . q . locale ;
1659
+ output . content_type_uid = this . q . content_type_uid ;
1668
1660
break ;
1669
1661
}
1670
1662
if ( this . internal . includeCount ) {
1671
- result . count = count ;
1663
+ output . count = count ;
1672
1664
}
1665
+ // console.log('this.internal.includeSchema', this.internal.includeSchema)
1673
1666
if ( this . internal . includeSchema ) {
1674
- result . content_type = yield this . db . collection ( {
1667
+ output . content_type = yield this . db . collection ( util_1 . getCollectionName ( {
1675
1668
content_type_uid : '_content_types' ,
1676
1669
locale : this . q . locale ,
1677
- } , this . collectionNames )
1670
+ } , this . collectionNames ) )
1678
1671
. findOne ( {
1679
1672
uid : this . q . content_type_uid ,
1680
1673
} , {
@@ -1685,7 +1678,7 @@ class Stack {
1685
1678
} ) ;
1686
1679
}
1687
1680
this . cleanup ( ) ;
1688
- return result ;
1681
+ return output ;
1689
1682
} ) ;
1690
1683
}
1691
1684
includeAssetsOnly ( entries , contentTypeUid , locale ) {
@@ -1772,9 +1765,6 @@ class Stack {
1772
1765
const { paths, // ref. fields in the current content types
1773
1766
pendingPath, // left over of *paths*
1774
1767
schemaList, } = yield this . getReferencePath ( ctQuery , locale , include ) ;
1775
- // console.log('paths: ' + paths)
1776
- // console.log('pending paths: ' + pendingPath)
1777
- // console.log('schema list: ' + JSON.stringify(schemaList))
1778
1768
const queries = {
1779
1769
$or : [ ] ,
1780
1770
} ; // reference field paths
@@ -1784,13 +1774,10 @@ class Stack {
1784
1774
for ( let i = 0 , j = paths . length ; i < j ; i ++ ) {
1785
1775
this . fetchPathDetails ( entries , locale , paths [ i ] . split ( '.' ) , queries , shelf , true , entries , 0 ) ;
1786
1776
}
1787
- // console.log('@sub queries', queries)
1788
- // console.log('@shelf', JSON.stringify(shelf))
1789
1777
// even after traversing, if no references were found, simply return the entries found thusfar
1790
1778
if ( shelf . length === 0 ) {
1791
1779
return entries ;
1792
1780
}
1793
- // console.log('@shelf-1', JSON.stringify(shelf))
1794
1781
// else, self-recursively iterate and fetch references
1795
1782
// Note: Shelf is the one holding `pointers` to the actual entry
1796
1783
// Once the pointer has been used, for GC, point the object to null
@@ -1799,17 +1786,13 @@ class Stack {
1799
1786
}
1800
1787
fetchPathDetails ( data , locale , pathArr , queryBucket , shelf , assetsOnly = false , parent , pos , counter = 0 ) {
1801
1788
if ( counter === ( pathArr . length ) ) {
1802
- // console.log('data', data)
1803
- // console.log('parent', parent)
1804
1789
if ( data && typeof data === 'object' ) {
1805
- // console.log('data is object')
1806
1790
if ( data instanceof Array && data . length ) {
1807
- // console.log('data is array')
1808
1791
data . forEach ( ( elem , idx ) => {
1809
- // console.log('elem', elem)
1810
1792
if ( typeof elem === 'string' ) {
1811
1793
queryBucket . $or . push ( {
1812
1794
_content_type_uid : '_assets' ,
1795
+ _version : { $exists : true } ,
1813
1796
locale,
1814
1797
uid : elem ,
1815
1798
} ) ;
@@ -1834,7 +1817,6 @@ class Stack {
1834
1817
} ) ;
1835
1818
}
1836
1819
else if ( typeof data === 'object' ) {
1837
- // console.log('data is plain object')
1838
1820
if ( data . hasOwnProperty ( '_content_type_uid' ) ) {
1839
1821
queryBucket . $or . push ( {
1840
1822
_content_type_uid : data . _content_type_uid ,
@@ -1850,13 +1832,12 @@ class Stack {
1850
1832
}
1851
1833
}
1852
1834
else if ( typeof data === 'string' ) {
1853
- // console.log('data is string')
1854
1835
queryBucket . $or . push ( {
1855
1836
_content_type_uid : '_assets' ,
1837
+ _version : { $exists : true } ,
1856
1838
locale,
1857
1839
uid : data ,
1858
1840
} ) ;
1859
- // console.log('shelf for assets: ' + JSON.stringify(parent), pos)
1860
1841
shelf . push ( {
1861
1842
path : parent ,
1862
1843
position : pos ,
@@ -1866,20 +1847,17 @@ class Stack {
1866
1847
}
1867
1848
else {
1868
1849
const currentField = pathArr [ counter ] ;
1869
- // console.log('path not over. current field is', currentField)
1870
1850
counter ++ ;
1871
1851
if ( data instanceof Array ) {
1872
1852
// tslint:disable-next-line: prefer-for-of
1873
1853
for ( let i = 0 ; i < data . length ; i ++ ) {
1874
1854
if ( data [ i ] [ currentField ] ) {
1875
- // console.log('data was array, lookin for field in data.current field')
1876
1855
this . fetchPathDetails ( data [ i ] [ currentField ] , locale , pathArr , queryBucket , shelf , assetsOnly , data [ i ] , currentField , counter ) ;
1877
1856
}
1878
1857
}
1879
1858
}
1880
1859
else {
1881
1860
if ( data [ currentField ] ) {
1882
- // console.log('data was object, lookin for field in data.current field')
1883
1861
this . fetchPathDetails ( data [ currentField ] , locale , pathArr , queryBucket , shelf , assetsOnly , data , currentField , counter ) ;
1884
1862
}
1885
1863
}
@@ -1893,15 +1871,9 @@ class Stack {
1893
1871
return ;
1894
1872
}
1895
1873
const { paths, pendingPath, schemaList, } = yield this . getReferencePath ( ctQuery , locale , include ) ;
1896
- // console.log('@paths 2', JSON.stringify(paths))
1897
- // console.log('@pending path', pendingPath)
1898
- // console.log('@schema list 2', JSON.stringify(schemaList))
1899
1874
const { result, queries, shelf, } = yield this . fetchEntries ( eQuery , locale , paths , include ) ;
1900
1875
// GC to avoid mem leaks!
1901
- // eQuery = null
1902
- // console.log('@entries 2', JSON.stringify(result))
1903
- // console.log('@queries 2', queries)
1904
- // console.log('@shelf-2', JSON.stringify(shelf))
1876
+ eQuery = null ;
1905
1877
for ( let i = 0 , j = oldShelf . length ; i < j ; i ++ ) {
1906
1878
const element = oldShelf [ i ] ;
1907
1879
for ( let k = 0 , l = result . length ; k < l ; k ++ ) {
@@ -1958,9 +1930,9 @@ class Stack {
1958
1930
// tslint:disable-next-line: forin
1959
1931
for ( const path in entryReferences ) {
1960
1932
const idx = includePath . indexOf ( path ) ;
1933
+ // tslint:disable-next-line: no-bitwise
1961
1934
if ( ~ idx ) {
1962
1935
let subPath ;
1963
- // console.log('path vs includePath', path, includePath)
1964
1936
// Its the complete path!! Hurrah!
1965
1937
if ( path . length !== includePath . length ) {
1966
1938
subPath = includePath . slice ( 0 , path . length ) ;
@@ -2001,7 +1973,6 @@ class Stack {
2001
1973
}
2002
1974
fetchEntries ( query , locale , paths , include , includeAll = false ) {
2003
1975
return __awaiter ( this , void 0 , void 0 , function * ( ) {
2004
- // console.log('@fetch entries query', JSON.stringify(query))
2005
1976
const result = yield this . db . collection ( util_1 . getCollectionName ( {
2006
1977
content_type_uid : 'entries' ,
2007
1978
locale,
@@ -2053,8 +2024,6 @@ class Stack {
2053
2024
} ;
2054
2025
const { paths, // ref. fields in the current content types
2055
2026
ctQueries, } = yield this . getAllReferencePaths ( ctQuery , locale ) ;
2056
- // console.log('paths: ' + paths)
2057
- // console.log('schema list: ' + JSON.stringify(schemaList))
2058
2027
const queries = {
2059
2028
$or : [ ] ,
2060
2029
} ; // reference field paths
@@ -2064,13 +2033,10 @@ class Stack {
2064
2033
for ( let i = 0 , j = paths . length ; i < j ; i ++ ) {
2065
2034
this . fetchPathDetails ( entries , locale , paths [ i ] . split ( '.' ) , queries , objectPointerList , true , entries , 0 ) ;
2066
2035
}
2067
- // console.log('@sub queries', queries)
2068
- // console.log('@objectPointerList', JSON.stringify(objectPointerList))
2069
2036
// even after traversing, if no references were found, simply return the entries found thusfar
2070
2037
if ( objectPointerList . length === 0 ) {
2071
2038
return entries ;
2072
2039
}
2073
- // console.log('@shelf-1', JSON.stringify(shelf))
2074
2040
// else, self-recursively iterate and fetch references
2075
2041
// Note: Shelf is the one holding `pointers` to the actual entry
2076
2042
// Once the pointer has been used, for GC, point the object to null
@@ -2083,7 +2049,6 @@ class Stack {
2083
2049
return ;
2084
2050
}
2085
2051
const { ctQueries, paths, } = yield this . getAllReferencePaths ( oldCtQueries , locale ) ;
2086
- // console.log('@paths', paths)
2087
2052
// GC to aviod mem leaks
2088
2053
oldCtQueries = null ;
2089
2054
const { result, queries, shelf, } = yield this . fetchEntries ( oldEntryQueries , locale , paths , [ ] , true ) ;
@@ -2124,8 +2089,6 @@ class Stack {
2124
2089
} ;
2125
2090
let paths = [ ] ;
2126
2091
for ( let i = 0 , j = contents . length ; i < j ; i ++ ) {
2127
- // console.log('_assets', contents[i]._assets)
2128
- // console.log('references', contents[i]._references)
2129
2092
let assetFieldPaths ;
2130
2093
let entryReferencePaths ;
2131
2094
if ( contents [ i ] . hasOwnProperty ( '_assets' ) ) {
@@ -2157,8 +2120,6 @@ class Stack {
2157
2120
}
2158
2121
}
2159
2122
}
2160
- // console.log('@paths later..', JSON.stringify(paths))
2161
- // console.log('@ctQueries later..', JSON.stringify(ctQueries))
2162
2123
return {
2163
2124
ctQueries,
2164
2125
paths,
0 commit comments