Skip to content

Commit 658a441

Browse files
committed
backup
1 parent c32d75d commit 658a441

File tree

14 files changed

+273
-116
lines changed

14 files changed

+273
-116
lines changed

dist/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ exports.config = {
2222
projections: {
2323
_id: 0,
2424
_version: 0,
25+
content_type_uid: 0,
2526
created_at: 0,
2627
sys_keys: 0,
2728
updated_at: 0,

dist/contentstack.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
Object.defineProperty(exports, "__esModule", { value: true });
88
const stack_1 = require("./stack");
99
class Contentstack {
10-
Stack(config, db) {
10+
static Stack(config, db) {
1111
return new stack_1.Stack(config, db);
1212
}
1313
}
14-
module.exports = new Contentstack();
14+
exports.Contentstack = Contentstack;

dist/stack.js

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -484,14 +484,20 @@ class Stack {
484484
.skip(this.internal.skip)
485485
.toArray()
486486
.then((result) => {
487+
let contentType;
488+
if (this.internal.includeSchema) {
489+
contentType = lodash_1.remove(result, { uid: this.q.content_type_uid });
490+
contentType = (typeof contentType === 'object' && contentType instanceof Array && contentType.length) ?
491+
contentType[0] : null;
492+
}
487493
if (this.internal.excludeReferences) {
488-
result = this.postProcess(result);
494+
result = this.postProcess(result, contentType);
489495
return resolve(result);
490496
}
491497
else {
492498
return this.includeReferencesI(result, this.q.locale, {}, undefined)
493499
.then(() => {
494-
result = this.postProcess(result);
500+
result = this.postProcess(result, contentType);
495501
return resolve(result);
496502
})
497503
.catch((refError) => {
@@ -509,40 +515,9 @@ class Stack {
509515
findOne(query = {}) {
510516
return new Promise((resolve, reject) => {
511517
this.internal.single = true;
512-
const queryFilters = this.preProcess(query);
513-
console.log('Query formed: ' + JSON.stringify(queryFilters, null, 1));
514-
if (this.internal.queryReferences) {
515-
return this.queryOnReferences(queryFilters)
516-
.then(resolve)
517-
.catch(reject);
518-
}
519-
return this.collection
520-
.find(queryFilters)
521-
.project(this.internal.projections)
522-
.limit(this.internal.limit)
523-
.skip(this.internal.skip)
524-
.toArray()
525-
.then((result) => {
526-
if (this.internal.includeReferences) {
527-
result = this.postProcess(result);
528-
return resolve(result);
529-
}
530-
else {
531-
return this.includeReferencesI(result, this.q.locale, {})
532-
.then(() => {
533-
result = this.postProcess(result);
534-
return resolve(result);
535-
})
536-
.catch((refError) => {
537-
this.cleanup();
538-
return reject(refError);
539-
});
540-
}
541-
})
542-
.catch((error) => {
543-
this.cleanup();
544-
return reject(error);
545-
});
518+
return this.find(query)
519+
.then(resolve)
520+
.catch(reject);
546521
});
547522
}
548523
queryOnReferences(query) {
@@ -627,13 +602,7 @@ class Stack {
627602
this.internal = {};
628603
this.q = {};
629604
}
630-
postProcess(result) {
631-
let contentType;
632-
if (this.internal.includeSchema) {
633-
contentType = lodash_1.remove(result, { uid: this.q.content_type_uid });
634-
contentType = (typeof contentType === 'object' && contentType instanceof Array && contentType.length) ?
635-
contentType[0] : null;
636-
}
605+
postProcess(result, contentType) {
637606
const count = (result === null) ? 0 : result.length;
638607
switch (this.q.content_type_uid) {
639608
case '_assets':
@@ -699,7 +668,8 @@ class Stack {
699668
const referencesFound = [];
700669
for (const prop in entry) {
701670
if (entry[prop] !== null && typeof entry[prop] === 'object') {
702-
if (entry[prop] && entry[prop].reference_to) {
671+
if (entry[prop] && entry[prop].reference_to && ((!(this.internal.includeReferences)
672+
&& entry[prop].reference_to === '_assets') || this.internal.includeReferences)) {
703673
if (entry[prop].values.length === 0) {
704674
entry[prop] = [];
705675
}

example/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ function find (contentType = 'blog') {
3232
Stack.contentType(contentType)
3333
.entries()
3434
.includeCount()
35-
// .includeReferences()
36-
.includeSchema()
35+
.includeReferences()
36+
// .includeSchema()
3737
.language('es-es')
38-
.notEqualTo('title', 'Blog One')
39-
// .limit(1)
38+
// .notEqualTo('title', 'Blog One')
39+
// .query({"title": "Blog Two"})
40+
.limit(1)
4041
// .skip(1)
4142
// .query({
4243
// "data.uid": 'blt17559b99fee73d6f'

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module.exports = {
9898
// Options that will be passed to the testEnvironment
9999
// testEnvironmentOptions: {},
100100
testPathIgnorePatterns: [
101-
'/test/dummy/*',
101+
'/test/data/*',
102102
],
103103
// The glob patterns Jest uses to detect test files
104104
testMatch: [

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const config = {
2323
projections: {
2424
_id: 0,
2525
_version: 0,
26+
content_type_uid: 0,
2627
created_at: 0,
2728
sys_keys: 0,
2829
updated_at: 0,

src/contentstack.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ import { Stack } from './stack'
1111
* @description Creates an instance of `Contentstack`.
1212
* @api public
1313
*/
14-
class Contentstack {
15-
/**
16-
* @summary
17-
* Initialize Stack instance
18-
* @param {Object} stack_arguments - Stack config
19-
*/
20-
public Stack(config, db) {
14+
export class Contentstack {
15+
/**
16+
* @summary
17+
* Initialize Stack instance
18+
* @param {Object} stack_arguments - Stack config
19+
*/
20+
public static Stack(config?, db?) {
2121
return new Stack(config, db)
2222
}
2323
}
24-
25-
module.exports = new Contentstack()

src/stack.ts

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -766,9 +766,10 @@ export class Stack {
766766
public queryReferences(query) {
767767
if (query && typeof query === 'object') {
768768
this.internal.queryReferences = query
769+
769770
return this
770771
}
771-
772+
772773
throw new Error('Kindly pass a query object for \'.queryReferences()\'')
773774
}
774775

@@ -809,14 +810,22 @@ export class Stack {
809810
.skip(this.internal.skip)
810811
.toArray()
811812
.then((result) => {
813+
let contentType
814+
if (this.internal.includeSchema) {
815+
contentType = remove(result, {uid: this.q.content_type_uid})
816+
contentType = (typeof contentType === 'object' && contentType instanceof Array && contentType.length) ?
817+
contentType[0] : null
818+
}
819+
812820
if (this.internal.excludeReferences) {
813-
result = this.postProcess(result)
821+
result = this.postProcess(result, contentType)
814822

815823
return resolve(result)
816824
} else {
825+
817826
return this.includeReferencesI(result, this.q.locale, {}, undefined)
818827
.then(() => {
819-
result = this.postProcess(result)
828+
result = this.postProcess(result, contentType)
820829

821830
return resolve(result)
822831
})
@@ -846,45 +855,10 @@ export class Stack {
846855
public findOne(query = {}) {
847856
return new Promise((resolve, reject) => {
848857
this.internal.single = true
849-
const queryFilters = this.preProcess(query)
850-
console.log('Query formed: ' + JSON.stringify(queryFilters, null, 1))
851-
// process it in a different manner
852-
if (this.internal.queryReferences) {
853-
return this.queryOnReferences(queryFilters)
854-
.then(resolve)
855-
.catch(reject)
856-
}
857858

858-
return this.collection
859-
.find(queryFilters)
860-
.project(this.internal.projections)
861-
.limit(this.internal.limit)
862-
.skip(this.internal.skip)
863-
.toArray()
864-
.then((result) => {
865-
if (this.internal.includeReferences) {
866-
result = this.postProcess(result)
867-
868-
return resolve(result)
869-
} else {
870-
return this.includeReferencesI(result, this.q.locale, {})
871-
.then(() => {
872-
result = this.postProcess(result)
873-
874-
return resolve(result)
875-
})
876-
.catch((refError) => {
877-
this.cleanup()
878-
879-
return reject(refError)
880-
})
881-
}
882-
})
883-
.catch((error) => {
884-
this.cleanup()
885-
886-
return reject(error)
887-
})
859+
return this.find(query)
860+
.then(resolve)
861+
.catch(reject)
888862
})
889863
}
890864

@@ -1005,13 +979,7 @@ export class Stack {
1005979
* @param {Object} result - Result, which's to be manipulated
1006980
* @returns {Object} - Returns the formatted version of the `result` object
1007981
*/
1008-
private postProcess(result) {
1009-
let contentType
1010-
if (this.internal.includeSchema) {
1011-
contentType = remove(result, {uid: this.q.content_type_uid})
1012-
contentType = (typeof contentType === 'object' && contentType instanceof Array && contentType.length) ?
1013-
contentType[0] : null
1014-
}
982+
private postProcess(result, contentType?) {
1015983
const count = (result === null) ? 0 : result.length
1016984
switch (this.q.content_type_uid) {
1017985
case '_assets':
@@ -1096,7 +1064,8 @@ export class Stack {
10961064
// iterate over each key in the object
10971065
for (const prop in entry) {
10981066
if (entry[prop] !== null && typeof entry[prop] === 'object') {
1099-
if (entry[prop] && entry[prop].reference_to) {
1067+
if (entry[prop] && entry[prop].reference_to && ((!(this.internal.includeReferences)
1068+
&& entry[prop].reference_to === '_assets') || this.internal.includeReferences)) {
11001069
if (entry[prop].values.length === 0) {
11011070
entry[prop] = []
11021071
} else {

test/data/author.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
export const entries = [
2+
{
3+
age: 1,
4+
blogs: {
5+
reference_to: 'blog',
6+
values: ['b1', 'b2', 'b3']
7+
},
8+
content_type_uid: 'author',
9+
locale: 'en-us',
10+
self_reference: {
11+
reference_to: 'author',
12+
values: [
13+
'a1',
14+
'a2',
15+
'a3',
16+
],
17+
},
18+
title: 'author One',
19+
uid: 'a1',
20+
},
21+
{
22+
content_type_uid: 'author',
23+
uid: 'a2',
24+
locale: 'en-us',
25+
alive: true,
26+
title: 'author Two',
27+
age: 2,
28+
self_reference: {
29+
reference_to: 'author',
30+
values: [
31+
'a1',
32+
'a2',
33+
'a3',
34+
'a4'
35+
]
36+
},
37+
tags: ['1'],
38+
blogs: {
39+
reference_to: 'blog',
40+
values: ['b1', 'b2', 'b3']
41+
}
42+
},
43+
{
44+
content_type_uid: 'author',
45+
uid: 'a3',
46+
age: 3,
47+
locale: 'en-us',
48+
title: 'author Three',
49+
self_reference: {
50+
reference_to: 'author',
51+
values: [
52+
'a1',
53+
'a2',
54+
'a3',
55+
'a5'
56+
]
57+
},
58+
tags: ['2', '3'],
59+
blogs: {
60+
reference_to: 'blog',
61+
values: ['b1', 'b2', 'b3']
62+
}
63+
},
64+
{
65+
content_type_uid: 'author',
66+
uid: 'a9',
67+
age: 9,
68+
locale: 'en-us',
69+
title: 'author nine',
70+
tags: ['8'],
71+
category: {
72+
reference_to: 'category',
73+
values: ['c1']
74+
}
75+
},
76+
{
77+
content_type_uid: 'author',
78+
uid: 'a10',
79+
age: 10,
80+
locale: 'en-us',
81+
title: 'author ten',
82+
tags: ['10'],
83+
category: {
84+
reference_to: 'category',
85+
values: ['c2']
86+
}
87+
}
88+
]

0 commit comments

Comments
 (0)