Skip to content

Commit d3eda00

Browse files
committed
backup
1 parent b95649c commit d3eda00

File tree

14 files changed

+804
-136
lines changed

14 files changed

+804
-136
lines changed
File renamed without changes.

dist/stack.js

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,58 @@ class Stack {
1717
constructor(stackConfig, existingDB) {
1818
this.config = lodash_1.merge(config_1.config, stackConfig);
1919
this.q = {};
20-
this.q = {};
2120
this.internal = {};
2221
this.db = existingDB;
2322
}
2423
ascending(field) {
25-
if (!(field) || typeof field !== 'string') {
26-
throw new Error('Kindly provide a valid field name for \'.ascending()\'');
27-
}
28-
else if (typeof this.q.content_type_uid !== 'string') {
24+
if (typeof this.q.content_type_uid !== 'string') {
2925
throw new Error('Kindly call \'.contentType()\' before \.ascending()\'');
3026
}
31-
if (this.internal.sort && typeof this.internal.sort === 'object') {
32-
this.internal.sort[field] = 1;
27+
if (!(field) || typeof field !== 'string') {
28+
if (this.internal.sort && typeof this.internal.sort === 'object') {
29+
this.internal.sort.published_at = 1;
30+
}
31+
else {
32+
this.internal.sort = {
33+
published_at: 1,
34+
};
35+
}
3336
}
3437
else {
35-
this.internal.sort = {
36-
[field]: 1,
37-
};
38+
if (this.internal.sort && typeof this.internal.sort === 'object') {
39+
this.internal.sort[field] = 1;
40+
}
41+
else {
42+
this.internal.sort = {
43+
[field]: 1,
44+
};
45+
}
3846
}
3947
return this;
4048
}
4149
descending(field) {
42-
if (!(field) || typeof field !== 'string') {
43-
throw new Error('Kindly provide a valid field name for \'.descending()\'');
44-
}
45-
else if (typeof this.q.content_type_uid !== 'string') {
50+
if (typeof this.q.content_type_uid !== 'string') {
4651
throw new Error('Kindly call \'.contentType()\' before \.descending()\'');
4752
}
48-
if (this.internal.sort && typeof this.internal.sort === 'object') {
49-
this.internal.sort[field] = -1;
53+
if (!(field) || typeof field !== 'string') {
54+
if (this.internal.sort && typeof this.internal.sort === 'object') {
55+
this.internal.sort[field] = -1;
56+
}
57+
else {
58+
this.internal.sort = {
59+
[field]: -1,
60+
};
61+
}
5062
}
5163
else {
52-
this.internal.sort = {
53-
[field]: -1,
54-
};
64+
if (this.internal.sort && typeof this.internal.sort === 'object') {
65+
this.internal.sort[field] = -1;
66+
}
67+
else {
68+
this.internal.sort = {
69+
[field]: -1,
70+
};
71+
}
5572
}
5673
return this;
5774
}
@@ -301,9 +318,9 @@ class Stack {
301318
asset(uid) {
302319
const stack = new Stack(this.config, this.db);
303320
if (uid && typeof uid === 'string') {
304-
stack.q.content_type_uid = '_assets';
305321
stack.q.uid = uid;
306322
}
323+
stack.q.content_type_uid = '_assets';
307324
stack.collection = stack.db.collection(stack.config.collectionName);
308325
stack.internal.limit = 1;
309326
stack.internal.single = true;
@@ -316,19 +333,21 @@ class Stack {
316333
return stack;
317334
}
318335
schema(uid) {
336+
const stack = new Stack(this.config, this.db);
319337
if (uid && typeof uid === 'string') {
320-
this.q.content_type_uid = 'contentTypes';
321-
this.q.uid = uid;
338+
stack.q.uid = uid;
322339
}
323-
this.collection = this.db.collection(this.config.collectionName);
324-
this.internal.limit = 1;
325-
this.internal.single = true;
326-
return this;
340+
stack.q.content_type_uid = 'contentTypes';
341+
stack.collection = stack.db.collection(stack.config.collectionName);
342+
stack.internal.limit = 1;
343+
stack.internal.single = true;
344+
return stack;
327345
}
328346
schemas() {
329-
this.q.content_type_uid = 'contentTypes';
330-
this.collection = this.db.collection(this.config.collectionName);
331-
return this;
347+
const stack = new Stack(this.config, this.db);
348+
stack.q.content_type_uid = 'contentTypes';
349+
stack.collection = stack.db.collection(stack.config.collectionName);
350+
return stack;
332351
}
333352
limit(no) {
334353
if (typeof no === 'number' && (no >= 0) && typeof this.q.content_type_uid === 'string') {
@@ -476,12 +495,14 @@ class Stack {
476495
.toArray()
477496
.then((result) => {
478497
let contentType;
479-
if (this.internal.includeSchema) {
498+
if (this.internal.includeSchema && this.q.content_type_uid !== 'contentTypes' && this.q.content_type_uid !==
499+
'_assets') {
480500
contentType = lodash_1.remove(result, { uid: this.q.content_type_uid });
481501
contentType = (typeof contentType === 'object' && contentType instanceof Array && contentType.length) ?
482502
contentType[0] : null;
483503
}
484-
if (this.internal.excludeReferences) {
504+
if (this.internal.excludeReferences || this.q.content_type_uid === 'contentTypes' || this.q.content_type_uid
505+
=== '_assets') {
485506
result = this.postProcess(result, contentType);
486507
return resolve(result);
487508
}
@@ -506,9 +527,12 @@ class Stack {
506527
count(query) {
507528
return new Promise((resolve, reject) => {
508529
const queryFilters = this.preProcess(query);
530+
this.collection = this.collection.find(queryFilters);
531+
if (this.internal.sort) {
532+
this.collection = this.collection.sort(this.internal.sort);
533+
}
509534
if (this.internal.queryReferences) {
510535
return this.collection
511-
.find(queryFilters)
512536
.project(this.internal.projections)
513537
.toArray()
514538
.then((result) => {
@@ -529,7 +553,6 @@ class Stack {
529553
});
530554
}
531555
return this.collection
532-
.find(queryFilters)
533556
.project(this.internal.projections)
534557
.count()
535558
.then((result) => {
@@ -621,9 +644,6 @@ class Stack {
621644
else {
622645
queryFilters = filters;
623646
}
624-
if (this.internal.sort) {
625-
this.collection = this.collection.sort(this.internal.sort);
626-
}
627647
return queryFilters;
628648
}
629649
cleanup() {
@@ -645,6 +665,8 @@ class Stack {
645665
assets: result,
646666
};
647667
}
668+
result.content_type_uid = 'assets';
669+
result.locale = this.q.locale;
648670
break;
649671
case 'contentTypes':
650672
if (this.internal.single) {
@@ -657,6 +679,7 @@ class Stack {
657679
content_types: result,
658680
};
659681
}
682+
result.content_type_uid = 'content_types';
660683
break;
661684
default:
662685
if (this.internal.single) {
@@ -669,19 +692,16 @@ class Stack {
669692
entries: result,
670693
};
671694
}
695+
result.content_type_uid = this.q.content_type_uid;
696+
result.locale = this.q.locale;
672697
break;
673698
}
674699
if (this.internal.includeCount) {
675700
result.count = count;
676701
}
677-
if (this.q.content_type_uid === '_assets') {
678-
this.q.content_type_uid = 'assets';
679-
}
680702
if (this.internal.includeSchema) {
681703
result.content_type = contentType;
682704
}
683-
result.content_type_uid = this.q.content_type_uid;
684-
result.locale = this.q.locale;
685705
this.cleanup();
686706
return result;
687707
}

example/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const Contentstack = require('../dist/contentstack').Contentstack
1+
const Contentstack = require('../dist').Contentstack
2+
// const Stack = require('../dist/contentstack').Stack
3+
// import * as Contentstack from '../dist'
24

35
const Stack = Contentstack.Stack({
46
api_key: '',

example/random.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const Contentstack = require('../dist/contentstack').Contentstack
2+
3+
const Stack = Contentstack.Stack({
4+
api_key: '',
5+
access_token: '',
6+
locales: [
7+
{
8+
code: 'en-us',
9+
relative_url_prefix: '/'
10+
},
11+
{
12+
code: 'es-es',
13+
relative_url_prefix: '/es/'
14+
}
15+
],
16+
dbName: 'sync-test'
17+
})
18+
19+
Stack.connect().then(() => {
20+
Stack.asset()
21+
// .schemas()
22+
// .entries()
23+
.find()
24+
.then(console.log)
25+
.catch(console.error)
26+
})

src/contentstack.ts renamed to src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ export class Contentstack {
2020
public static Stack(config?, db?) {
2121
return new Stack(config, db)
2222
}
23-
}
23+
}

0 commit comments

Comments
 (0)