Skip to content

Commit 97ae499

Browse files
committed
test cases updated
1 parent dc2aba0 commit 97ae499

17 files changed

+252
-635
lines changed

dist/stack.js

Lines changed: 63 additions & 265 deletions
Large diffs are not rendered by default.

src/stack.ts

Lines changed: 64 additions & 257 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,41 +1479,27 @@ export class Stack {
14791479
public find(query = {}) {
14801480
return new Promise(async (resolve, reject) => {
14811481
let queryFilters = this.preProcess(query)
1482-
let formattedQuery
1483-
1484-
// if (this.internal.includeSpecificReferences) {
1485-
// const projections = await this.excludeSpecificReferences(this.internal.includeSpecificReferences, this.q.content_type_uid, this.internal.hasOwnProperty('only'))
1486-
// console.log('projections', projections)
1487-
// this.internal.projections = merge(this.internal.projections, projections)
1488-
// console.log('this.internal.projections', this.internal.projections)
1489-
// }
1490-
1491-
if (this.internal.queryReferencesBeta) {
1492-
formattedQuery = await this.queryBuilder(this.internal.queryReferencesBeta, this.q.locale, this.q.content_type_uid)
1493-
console.log('formatted query', JSON.stringify(formattedQuery))
1494-
}
14951482

1496-
queryFilters = merge(queryFilters, formattedQuery[0])
1497-
console.log('queryfilters', JSON.stringify(queryFilters))
1483+
if (this.internal.includeSpecificReferences) {
1484+
const projections = await this.excludeSpecificReferences(this.internal.includeSpecificReferences, this.q.content_type_uid, this.internal.hasOwnProperty('only'))
1485+
console.log('projections', projections)
1486+
this.internal.projections = merge(this.internal.projections, projections)
1487+
console.log('this.internal.projections', this.internal.projections)
1488+
}
14981489

14991490
if (this.internal.sort) {
15001491
this.collection = this.collection.find(queryFilters).sort(this.internal.sort)
15011492
} else {
15021493
this.collection = this.collection.find(queryFilters)
15031494
}
15041495

1505-
// process it in a different manner
15061496
if (this.internal.queryReferences) {
1507-
return this.queryOnReferences()
1508-
.then(resolve)
1509-
.catch(reject)
1497+
this.collection = this.collection.project(this.internal.projections).toArray()
1498+
} else {
1499+
this.collection = this.collection.project(this.internal.projections).limit(this.internal.limit).skip(this.internal.skip).toArray()
15101500
}
15111501

15121502
return this.collection
1513-
.project(this.internal.projections)
1514-
.limit(this.internal.limit)
1515-
.skip(this.internal.skip)
1516-
.toArray()
15171503
.then((result) => {
15181504
let contentType
15191505
if (this.internal.includeSchema && this.q.content_type_uid !== this.types.content_types && this.q.content_type_uid !==
@@ -1531,28 +1517,36 @@ export class Stack {
15311517
} else if (this.internal.includeSpecificReferences) {
15321518
return this.includeSpecificReferences(result, this.q.locale, {}, undefined, this.internal.includeSpecificReferences)
15331519
.then(() => {
1520+
if (this.internal.queryReferences) {
1521+
result = sift(this.internal.queryReferences, result)
1522+
1523+
if (this.internal.skip) {
1524+
result = result.splice(this.internal.skip, this.internal.limit)
1525+
} else if (this.internal.limit) {
1526+
result = result.splice(0, this.internal.limit)
1527+
}
1528+
}
15341529
result = this.postProcess(result, contentType)
15351530

15361531
return resolve(result)
15371532
})
1538-
.catch((refError) => {
1539-
this.cleanup()
1540-
1541-
return reject(refError)
1542-
})
15431533
} else {
15441534

15451535
return this.includeReferencesI(result, this.q.locale, {}, undefined)
15461536
.then(() => {
1537+
if (this.internal.queryReferences) {
1538+
result = sift(this.internal.queryReferences, result)
1539+
1540+
if (this.internal.skip) {
1541+
result = result.splice(this.internal.skip, this.internal.limit)
1542+
} else if (this.internal.limit) {
1543+
result = result.splice(0, this.internal.limit)
1544+
}
1545+
}
15471546
result = this.postProcess(result, contentType)
15481547

15491548
return resolve(result)
15501549
})
1551-
.catch((refError) => {
1552-
this.cleanup()
1553-
1554-
return reject(refError)
1555-
})
15561550
}
15571551
})
15581552
.catch((error) => {
@@ -1666,43 +1660,6 @@ export class Stack {
16661660
})
16671661
}
16681662

1669-
private queryOnReferences() {
1670-
return new Promise((resolve, reject) => {
1671-
return this.collection
1672-
// .find(query)
1673-
.project(this.internal.projections)
1674-
// .limit(this.internal.limit)
1675-
// .skip(this.internal.skip)
1676-
.toArray()
1677-
.then((result) => {
1678-
return this.includeReferencesI(result, this.q.locale, {}, undefined)
1679-
.then(() => {
1680-
result = sift(this.internal.queryReferences, result)
1681-
1682-
if (this.internal.skip) {
1683-
result = result.splice(this.internal.skip, this.internal.limit)
1684-
} else if (this.internal.limit) {
1685-
result = result.splice(0, this.internal.limit)
1686-
}
1687-
1688-
result = this.postProcess(result)
1689-
1690-
return resolve(result)
1691-
})
1692-
.catch((refError) => {
1693-
this.cleanup()
1694-
1695-
return reject(refError)
1696-
})
1697-
})
1698-
.catch((error) => {
1699-
this.cleanup()
1700-
1701-
return reject(error)
1702-
})
1703-
})
1704-
}
1705-
17061663
/**
17071664
* @private
17081665
* @method preProcess
@@ -1784,10 +1741,6 @@ export class Stack {
17841741
*/
17851742
private postProcess(result, contentType ? ) {
17861743
const count = (result === null) ? 0 : result.length
1787-
// if (this.internal.only) {
1788-
// result.forEach((item) => mask(item, this.internal.except))
1789-
// }
1790-
// result.forEach((item) => delete item._id)
17911744
switch (this.q.content_type_uid) {
17921745
case this.types.assets:
17931746
if (this.internal.single) {
@@ -1946,104 +1899,8 @@ export class Stack {
19461899
})
19471900
}
19481901

1949-
private includeReferencesBeta(entry, locale, references, parentUid?) {
1950-
const self = this
1951-
1952-
return new Promise((resolve, reject) => {
1953-
if (entry === null || typeof entry !== 'object') {
1954-
return resolve()
1955-
}
1956-
1957-
// current entry becomes the parent
1958-
if (entry.uid) {
1959-
parentUid = entry.uid
1960-
}
1961-
1962-
const referencesFound = []
1963-
1964-
// iterate over each key in the object
1965-
for (const prop in entry) {
1966-
if (entry[prop] !== null && typeof entry[prop] === 'object') {
1967-
if (entry[prop] && entry[prop].reference_to) {
1968-
if (entry[prop].values.length === 0) {
1969-
entry[prop] = []
1970-
} else {
1971-
let uids = entry[prop].values
1972-
if (typeof uids === 'string') {
1973-
uids = [uids]
1974-
}
1975-
if (entry[prop].reference_to !== this.types.assets) {
1976-
uids = filter(uids, (uid) => {
1977-
return !(checkCyclic(uid, references))
1978-
})
1979-
}
1980-
if (uids.length) {
1981-
const query = {
1982-
content_type_uid: entry[prop].reference_to,
1983-
locale,
1984-
uid: {
1985-
$in: uids,
1986-
},
1987-
}
1988-
1989-
referencesFound.push(new Promise((rs, rj) => {
1990-
return self.db.collection(this.contentStore.collectionName)
1991-
.find(query)
1992-
.project(self.config.contentStore.projections)
1993-
.toArray()
1994-
.then((result) => {
1995-
if (result.length === 0) {
1996-
entry[prop] = []
1997-
1998-
return rs()
1999-
} else if (parentUid) {
2000-
references[parentUid] = references[parentUid] || []
2001-
references[parentUid] = uniq(references[parentUid].concat(map(result, 'uid')))
2002-
}
2003-
2004-
if (typeof entry[prop].values === 'string') {
2005-
entry[prop] = ((result === null) || result.length === 0) ? null : result[0]
2006-
} else {
2007-
// format the references in order
2008-
const referenceBucket = []
2009-
query.uid.$in.forEach((entityUid) => {
2010-
const elem = find(result, (entity) => {
2011-
return entity.uid === entityUid
2012-
})
2013-
if (elem) {
2014-
referenceBucket.push(elem)
2015-
}
2016-
})
2017-
entry[prop] = referenceBucket
2018-
}
2019-
2020-
return self.includeReferencesBeta(entry[prop], locale, references, parentUid)
2021-
.then(rs)
2022-
.catch(rj)
2023-
})
2024-
.catch(rj)
2025-
}))
2026-
}
2027-
}
2028-
} else {
2029-
referencesFound.push(self.includeReferencesBeta(entry[prop], locale, references, parentUid))
2030-
}
2031-
}
2032-
}
2033-
2034-
return Promise.all(referencesFound)
2035-
.then(resolve)
2036-
.catch(reject)
2037-
})
2038-
}
2039-
20401902
private isPartOfInclude(pth, include) {
20411903
for (let i = 0, j = include.length; i < j; i++) {
2042-
// const regexp = new RegExp(include[i])
2043-
// if (regexp.test(pth)) {
2044-
// return true
2045-
// }
2046-
20471904
if (include[i].indexOf(pth) !== -1) {
20481905
return true
20491906
}
@@ -2163,98 +2020,48 @@ export class Stack {
21632020
})
21642021
}
21652022

2166-
private async queryBuilder(query, locale, uid) {
2167-
return new Promise((resolve, reject) => {
2168-
if (query && Object.keys(query).length && uid) {
2169-
// find content type's schema
2170-
return this.db.collection(this.contentStore.collectionName)
2171-
.find({
2172-
content_type_uid: this.types.content_types,
2173-
uid
2174-
})
2175-
.project({
2176-
references: 1
2177-
})
2178-
.limit(1)
2179-
.toArray()
2180-
.then((result) => {
2181-
console.log('result', result)
2182-
if (result === null || result.length === 0) {
2183-
return resolve(query)
2184-
}
2185-
const references = result[0].references
2186-
2187-
if (references && Object.keys(references).length > 0) {
2188-
const promises = []
2189-
2190-
for (const field in query) {
2191-
if (query[field] === null) {
2192-
delete query[field]
2193-
} else if (typeof query[field] === 'object' && query[field] instanceof Array && query[field].length) {
2194-
promises.push(this.queryBuilder(query[field], locale, uid))
2195-
} else {
2196-
let filterField: any = field
2197-
let refQuery, refContentType
2198-
let refFieldM
2199-
for (let refField in references) {
2200-
// if the query's field matches the reference field exactly
2201-
if (field.indexOf(refField) === 0) {
2202-
refFieldM = refField
2203-
let newfilterField = filterField.replace(refField, '')
2204-
if (newfilterField.charAt(0) === '.') {
2205-
newfilterField = newfilterField.substr(1)
2206-
}
2207-
2208-
refQuery = refQuery || {}
2209-
refContentType = references[refField]
2210-
// no idea
2211-
refQuery[newfilterField] = query[field]
2212-
refQuery.content_type_uid = refContentType
2213-
refQuery.locale = locale
2214-
delete query[field]
2215-
}
2216-
}
2023+
/**
2024+
* [
2025+
* 'category.authors'
2026+
* 'category'
2027+
* 'authors.types'
2028+
* ]
2029+
*/
22172030

2218-
if (refQuery && Object.keys(refQuery).length) {
2219-
promises.push(
2220-
this.db.collection(this.contentStore.collectionName)
2221-
.find(refQuery)
2222-
.project({uid: 1})
2223-
.toArray()
2224-
.then((result) => {
2225-
console.log('result', result)
2226-
console.log('refQuery', refQuery)
2227-
if (result === null || result.length === 0) {
2228-
query[`${refFieldM}.values`] = {
2229-
$in: []
2230-
}
2231-
} else {
2232-
console.log('filterfield> ', `${refFieldM}.values`)
2233-
query[`${refFieldM}.values`] = {
2234-
$in: map(result, 'uid')
2235-
}
2236-
}
2031+
private excludeSpecificReferences (includes, uid, isOnly) {
2032+
return new Promise((resolve) => {
2033+
return this.db.collection(this.contentStore.collectionName)
2034+
.find({uid})
2035+
.project({references: 1})
2036+
.limit(1)
2037+
.toArray()
2038+
.then((result) => {
2039+
if (result === null) {
2040+
return resolve({})
2041+
}
2042+
const excludeQuery = {}
2043+
const references = result[0].references
22372044

2238-
return query
2239-
})
2240-
)
2241-
}
2242-
}
2045+
for (const referenceField in references) {
2046+
let flag = false
2047+
2048+
for (let i = 0, j = includes.length; i < j; i++) {
2049+
if (includes[i].includes(referenceField)) {
2050+
flag = true
2051+
break
22432052
}
2053+
}
22442054

2245-
return Promise.all(promises)
2246-
.then(resolve)
2247-
.catch(reject)
2248-
} else {
2249-
// $and $or $nor $where
2250-
// setting this to an empty object - no references found on the content type!
2251-
return resolve({})
2055+
if (!flag) {
2056+
const referenceFieldValues = `${referenceField}.values`
2057+
const referenceFieldRefTo = `${referenceField}.reference_to`
2058+
excludeQuery[referenceFieldValues] = (isOnly) ? 1: 0
2059+
excludeQuery[referenceFieldRefTo] = (isOnly) ? 1: 0
22522060
}
2253-
})
2254-
.catch(reject)
2255-
} else {
2256-
return resolve(query);
2257-
}
2061+
}
2062+
2063+
return resolve(excludeQuery)
2064+
})
22582065
})
22592066
}
22602067
}

0 commit comments

Comments
 (0)