Skip to content

Commit 41b98f5

Browse files
committed
include reference method tinkered with
1 parent 201407b commit 41b98f5

File tree

3 files changed

+133
-113
lines changed

3 files changed

+133
-113
lines changed

dist/stack.js

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,10 @@ class Stack {
472472
this.internal.includeSchema = true;
473473
return this;
474474
}
475+
includeContentType() {
476+
this.internal.includeSchema = true;
477+
return this;
478+
}
475479
includeReferences() {
476480
this.internal.includeReferences = true;
477481
return this;
@@ -730,64 +734,66 @@ class Stack {
730734
const referencesFound = [];
731735
for (const prop in entry) {
732736
if (entry[prop] !== null && typeof entry[prop] === 'object') {
733-
if (entry[prop] && entry[prop].reference_to && ((!(this.internal.includeReferences)
734-
&& entry[prop].reference_to === '_assets') || this.internal.includeReferences)) {
735-
if (entry[prop].values.length === 0) {
736-
entry[prop] = [];
737-
}
738-
else {
739-
let uids = entry[prop].values;
740-
if (typeof uids === 'string') {
741-
uids = [uids];
742-
}
743-
if (entry[prop].reference_to !== '_assets') {
744-
uids = lodash_1.filter(uids, (uid) => {
745-
return !(util_1.checkCyclic(uid, references));
746-
});
737+
if (entry[prop] && entry[prop].reference_to) {
738+
if ((!(this.internal.includeReferences)
739+
&& entry[prop].reference_to === '_assets') || this.internal.includeReferences) {
740+
if (entry[prop].values.length === 0) {
741+
entry[prop] = [];
747742
}
748-
if (uids.length) {
749-
const query = {
750-
content_type_uid: entry[prop].reference_to,
751-
locale,
752-
uid: {
753-
$in: uids,
754-
},
755-
};
756-
referencesFound.push(new Promise((rs, rj) => {
757-
return self.db.collection(this.contentStore.collectionName)
758-
.find(query)
759-
.project(self.config.projections)
760-
.toArray()
761-
.then((result) => {
762-
if (result.length === 0) {
763-
entry[prop] = [];
764-
return rs();
765-
}
766-
else if (parentUid) {
767-
references[parentUid] = references[parentUid] || [];
768-
references[parentUid] = lodash_1.uniq(references[parentUid].concat(lodash_1.map(result, 'uid')));
769-
}
770-
if (typeof entry[prop].values === 'string') {
771-
entry[prop] = ((result === null) || result.length === 0) ? null : result[0];
772-
}
773-
else {
774-
const referenceBucket = [];
775-
query.uid.$in.forEach((entityUid) => {
776-
const elem = lodash_1.find(result, (entity) => {
777-
return entity.uid === entityUid;
743+
else {
744+
let uids = entry[prop].values;
745+
if (typeof uids === 'string') {
746+
uids = [uids];
747+
}
748+
if (entry[prop].reference_to !== '_assets') {
749+
uids = lodash_1.filter(uids, (uid) => {
750+
return !(util_1.checkCyclic(uid, references));
751+
});
752+
}
753+
if (uids.length) {
754+
const query = {
755+
content_type_uid: entry[prop].reference_to,
756+
locale,
757+
uid: {
758+
$in: uids,
759+
},
760+
};
761+
referencesFound.push(new Promise((rs, rj) => {
762+
return self.db.collection(this.contentStore.collectionName)
763+
.find(query)
764+
.project(self.config.projections)
765+
.toArray()
766+
.then((result) => {
767+
if (result.length === 0) {
768+
entry[prop] = [];
769+
return rs();
770+
}
771+
else if (parentUid) {
772+
references[parentUid] = references[parentUid] || [];
773+
references[parentUid] = lodash_1.uniq(references[parentUid].concat(lodash_1.map(result, 'uid')));
774+
}
775+
if (typeof entry[prop].values === 'string') {
776+
entry[prop] = ((result === null) || result.length === 0) ? null : result[0];
777+
}
778+
else {
779+
const referenceBucket = [];
780+
query.uid.$in.forEach((entityUid) => {
781+
const elem = lodash_1.find(result, (entity) => {
782+
return entity.uid === entityUid;
783+
});
784+
if (elem) {
785+
referenceBucket.push(elem);
786+
}
778787
});
779-
if (elem) {
780-
referenceBucket.push(elem);
781-
}
782-
});
783-
entry[prop] = referenceBucket;
784-
}
785-
return self.includeReferencesI(entry[prop], locale, references, parentUid)
786-
.then(rs)
788+
entry[prop] = referenceBucket;
789+
}
790+
return self.includeReferencesI(entry[prop], locale, references, parentUid)
791+
.then(rs)
792+
.catch(rj);
793+
})
787794
.catch(rj);
788-
})
789-
.catch(rj);
790-
}));
795+
}));
796+
}
791797
}
792798
}
793799
}

src/stack.ts

Lines changed: 71 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,17 @@ export class Stack {
756756
return this
757757
}
758758

759+
/**
760+
* @summary
761+
* Includes 'content_type' key in response, which is the content type schema of the entries filtered/scanned
762+
* @returns {this} - Returns `stack's` instance
763+
*/
764+
public includeContentType() {
765+
this.internal.includeSchema = true
766+
767+
return this
768+
}
769+
759770
/**
760771
* @summary
761772
* Includes all references of the entries being scanned
@@ -1138,66 +1149,68 @@ export class Stack {
11381149
// iterate over each key in the object
11391150
for (const prop in entry) {
11401151
if (entry[prop] !== null && typeof entry[prop] === 'object') {
1141-
if (entry[prop] && entry[prop].reference_to && ((!(this.internal.includeReferences)
1142-
&& entry[prop].reference_to === '_assets') || this.internal.includeReferences)) {
1143-
if (entry[prop].values.length === 0) {
1144-
entry[prop] = []
1145-
} else {
1146-
let uids = entry[prop].values
1147-
if (typeof uids === 'string') {
1148-
uids = [uids]
1149-
}
1150-
if (entry[prop].reference_to !== '_assets') {
1151-
uids = filter(uids, (uid) => {
1152-
return !(checkCyclic(uid, references))
1153-
})
1154-
}
1155-
if (uids.length) {
1156-
const query = {
1157-
content_type_uid: entry[prop].reference_to,
1158-
locale,
1159-
uid: {
1160-
$in: uids,
1161-
},
1152+
if (entry[prop] && entry[prop].reference_to) {
1153+
if ((!(this.internal.includeReferences)
1154+
&& entry[prop].reference_to === '_assets') || this.internal.includeReferences) {
1155+
if (entry[prop].values.length === 0) {
1156+
entry[prop] = []
1157+
} else {
1158+
let uids = entry[prop].values
1159+
if (typeof uids === 'string') {
1160+
uids = [uids]
11621161
}
1163-
1164-
referencesFound.push(new Promise((rs, rj) => {
1165-
return self.db.collection(this.contentStore.collectionName)
1166-
.find(query)
1167-
.project(self.config.projections)
1168-
.toArray()
1169-
.then((result) => {
1170-
if (result.length === 0) {
1171-
entry[prop] = []
1172-
1173-
return rs()
1174-
} else if (parentUid) {
1175-
references[parentUid] = references[parentUid] || []
1176-
references[parentUid] = uniq(references[parentUid].concat(map(result, 'uid')))
1177-
}
1178-
1179-
if (typeof entry[prop].values === 'string') {
1180-
entry[prop] = ((result === null) || result.length === 0) ? null : result[0]
1181-
} else {
1182-
// format the references in order
1183-
const referenceBucket = []
1184-
query.uid.$in.forEach((entityUid) => {
1185-
const elem = find(result, (entity) => {
1186-
return entity.uid === entityUid
1162+
if (entry[prop].reference_to !== '_assets') {
1163+
uids = filter(uids, (uid) => {
1164+
return !(checkCyclic(uid, references))
1165+
})
1166+
}
1167+
if (uids.length) {
1168+
const query = {
1169+
content_type_uid: entry[prop].reference_to,
1170+
locale,
1171+
uid: {
1172+
$in: uids,
1173+
},
1174+
}
1175+
1176+
referencesFound.push(new Promise((rs, rj) => {
1177+
return self.db.collection(this.contentStore.collectionName)
1178+
.find(query)
1179+
.project(self.config.projections)
1180+
.toArray()
1181+
.then((result) => {
1182+
if (result.length === 0) {
1183+
entry[prop] = []
1184+
1185+
return rs()
1186+
} else if (parentUid) {
1187+
references[parentUid] = references[parentUid] || []
1188+
references[parentUid] = uniq(references[parentUid].concat(map(result, 'uid')))
1189+
}
1190+
1191+
if (typeof entry[prop].values === 'string') {
1192+
entry[prop] = ((result === null) || result.length === 0) ? null : result[0]
1193+
} else {
1194+
// format the references in order
1195+
const referenceBucket = []
1196+
query.uid.$in.forEach((entityUid) => {
1197+
const elem = find(result, (entity) => {
1198+
return entity.uid === entityUid
1199+
})
1200+
if (elem) {
1201+
referenceBucket.push(elem)
1202+
}
11871203
})
1188-
if (elem) {
1189-
referenceBucket.push(elem)
1190-
}
1191-
})
1192-
entry[prop] = referenceBucket
1193-
}
1194-
1195-
return self.includeReferencesI(entry[prop], locale, references, parentUid)
1196-
.then(rs)
1197-
.catch(rj)
1198-
})
1199-
.catch(rj)
1200-
}))
1204+
entry[prop] = referenceBucket
1205+
}
1206+
1207+
return self.includeReferencesI(entry[prop], locale, references, parentUid)
1208+
.then(rs)
1209+
.catch(rj)
1210+
})
1211+
.catch(rj)
1212+
}))
1213+
}
12011214
}
12021215
}
12031216
} else {

typings/stack.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export declare class Stack {
4545
where(expr: any): this;
4646
includeCount(): this;
4747
includeSchema(): this;
48+
includeContentType(): this;
4849
includeReferences(): this;
4950
excludeReferences(): this;
5051
queryReferences(query: any): this;

0 commit comments

Comments
 (0)