Skip to content

Commit a289399

Browse files
committed
backup
1 parent 1c3cef6 commit a289399

File tree

6 files changed

+52
-42
lines changed

6 files changed

+52
-42
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ package-lock.json
66
coverage
77

88
# Dev files
9-
includeReferences.js
9+
includeReferences.js
10+
example/parallel-testing.js
11+
example/parallel-testing-2.js

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(...args) {
11-
return new stack_1.Stack(...args);
10+
Stack(config, db) {
11+
return new stack_1.Stack(config, db);
1212
}
1313
}
1414
module.exports = new Contentstack();

dist/stack.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ const config_1 = require("./config");
1515
const util_1 = require("./util");
1616
const debug = debug_1.default('stack');
1717
class Stack {
18-
constructor(...stackInfo) {
19-
this.config = lodash_1.merge(config_1.config, ...stackInfo);
18+
constructor(stackConfig, existingDB) {
19+
this.config = lodash_1.merge(config_1.config, stackConfig);
2020
this.q = {};
2121
this.q = {};
2222
this.internal = {};
23+
this.db = existingDB;
2324
}
2425
ascending(field) {
2526
if (!(field) || typeof field !== 'string') {
@@ -279,10 +280,11 @@ class Stack {
279280
return this;
280281
}
281282
contentType(uid) {
283+
const stack = new Stack(this.config, this.db);
282284
if (uid && typeof uid === 'string') {
283-
this.q.content_type_uid = uid;
284-
this.collection = this.db.collection(this.config.collectionName);
285-
return this;
285+
stack.q.content_type_uid = uid;
286+
stack.collection = stack.db.collection(stack.config.collectionName);
287+
return stack;
286288
}
287289
throw new Error('Kindly pass the content type\'s uid');
288290
}
@@ -304,19 +306,21 @@ class Stack {
304306
throw new Error('Kindly call \'contentType()\' before \'entries()\'!');
305307
}
306308
asset(uid) {
309+
const stack = new Stack(this.config, this.db);
307310
if (uid && typeof uid === 'string') {
308-
this.q.content_type_uid = '_assets';
309-
this.q.uid = uid;
311+
stack.q.content_type_uid = '_assets';
312+
stack.q.uid = uid;
310313
}
311-
this.collection = this.db.collection(this.config.collectionName);
312-
this.internal.limit = 1;
313-
this.internal.single = true;
314-
return this;
314+
stack.collection = stack.db.collection(stack.config.collectionName);
315+
stack.internal.limit = 1;
316+
stack.internal.single = true;
317+
return stack;
315318
}
316319
assets() {
317-
this.q.content_type_uid = '_assets';
318-
this.collection = this.db.collection(this.config.collectionName);
319-
return this;
320+
const stack = new Stack(this.config, this.db);
321+
stack.q.content_type_uid = '_assets';
322+
stack.collection = stack.db.collection(stack.config.collectionName);
323+
return stack;
320324
}
321325
schema(uid) {
322326
if (uid && typeof uid === 'string') {
@@ -469,7 +473,7 @@ class Stack {
469473
find(query = {}) {
470474
return new Promise((resolve, reject) => {
471475
const queryFilters = this.preProcess(query);
472-
console.log('find() query: ' + JSON.stringify(queryFilters, null, 1));
476+
console.log('Query formed: ' + JSON.stringify(queryFilters, null, 1));
473477
return this.collection
474478
.find(queryFilters)
475479
.project(this.internal.projections)
@@ -505,7 +509,7 @@ class Stack {
505509
return new Promise((resolve, reject) => {
506510
this.internal.single = true;
507511
const queryFilters = this.preProcess(query);
508-
console.log('findOne query: ' + JSON.stringify(queryFilters, null, 1));
512+
console.log('Query formed: ' + JSON.stringify(queryFilters, null, 1));
509513
return this.collection
510514
.find(queryFilters)
511515
.project(this.internal.projections)

src/contentstack.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class Contentstack {
1717
* Initialize Stack instance
1818
* @param {Object} stack_arguments - Stack config
1919
*/
20-
public Stack(...args) {
21-
return new Stack(...args)
20+
public Stack(config, db) {
21+
return new Stack(config, db)
2222
}
2323
}
2424

src/stack.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ export class Stack {
2828
private internal: any
2929
private db: Db
3030

31-
constructor(...stackInfo) {
32-
this.config = merge(config, ...stackInfo)
31+
constructor(stackConfig, existingDB?) {
32+
this.config = merge(config, stackConfig)
3333
this.q = {}
3434
this.q = {}
3535
this.internal = {}
36+
this.db = existingDB
3637
}
3738

3839
/**
@@ -420,11 +421,13 @@ export class Stack {
420421
* @returns {this} - Returns `stack's` instance
421422
*/
422423
public contentType(uid) {
424+
// create new instances, instead of re-using the old one
425+
const stack = new Stack(this.config, this.db)
423426
if (uid && typeof uid === 'string') {
424-
this.q.content_type_uid = uid
425-
this.collection = this.db.collection(this.config.collectionName)
427+
stack.q.content_type_uid = uid
428+
stack.collection = stack.db.collection(stack.config.collectionName)
426429

427-
return this
430+
return stack
428431
}
429432
throw new Error('Kindly pass the content type\'s uid')
430433
}
@@ -472,16 +475,16 @@ export class Stack {
472475
* @returns {this} - Returns `stack's` instance
473476
*/
474477
public asset(uid ? ) {
478+
const stack = new Stack(this.config, this.db)
475479
if (uid && typeof uid === 'string') {
476-
this.q.content_type_uid = '_assets'
477-
this.q.uid = uid
480+
stack.q.content_type_uid = '_assets'
481+
stack.q.uid = uid
478482
}
479-
this.collection = this.db.collection(this.config.collectionName)
480-
// this.collection = this.collection.limit(1)
481-
this.internal.limit = 1
482-
this.internal.single = true
483+
stack.collection = stack.db.collection(stack.config.collectionName)
484+
stack.internal.limit = 1
485+
stack.internal.single = true
483486

484-
return this
487+
return stack
485488
}
486489

487490
/**
@@ -490,10 +493,11 @@ export class Stack {
490493
* @returns {this} - Returns `stack's` instance
491494
*/
492495
public assets() {
493-
this.q.content_type_uid = '_assets'
494-
this.collection = this.db.collection(this.config.collectionName)
496+
const stack = new Stack(this.config, this.db)
497+
stack.q.content_type_uid = '_assets'
498+
stack.collection = stack.db.collection(stack.config.collectionName)
495499

496-
return this
500+
return stack
497501
}
498502

499503
/**
@@ -779,7 +783,7 @@ export class Stack {
779783
public find(query = {}) {
780784
return new Promise((resolve, reject) => {
781785
const queryFilters = this.preProcess(query)
782-
console.log('find() query: ' + JSON.stringify(queryFilters, null, 1))
786+
console.log('Query formed: ' + JSON.stringify(queryFilters, null, 1))
783787

784788
return this.collection
785789
.find(queryFilters)
@@ -829,7 +833,7 @@ export class Stack {
829833
return new Promise((resolve, reject) => {
830834
this.internal.single = true
831835
const queryFilters = this.preProcess(query)
832-
console.log('findOne query: ' + JSON.stringify(queryFilters, null, 1))
836+
console.log('Query formed: ' + JSON.stringify(queryFilters, null, 1))
833837

834838
return this.collection
835839
.find(queryFilters)

typings/stack.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export declare class Stack {
1010
private collection;
1111
private internal;
1212
private db;
13-
constructor(...stackInfo: any[]);
13+
constructor(stackConfig: any, existingDB?: any);
1414
ascending(field: any): this;
1515
descending(field: any): this;
1616
connect(overrides?: {}): Promise<{}>;
@@ -27,11 +27,11 @@ export declare class Stack {
2727
notContainedIn(key: any, value: any): this;
2828
exists(key: any): this;
2929
notExists(key: any): this;
30-
contentType(uid: any): this;
30+
contentType(uid: any): Stack;
3131
entry(uid?: any): this;
3232
entries(): this;
33-
asset(uid?: any): this;
34-
assets(): this;
33+
asset(uid?: any): Stack;
34+
assets(): Stack;
3535
schema(uid?: any): this;
3636
schemas(): this;
3737
limit(no: any): this;

0 commit comments

Comments
 (0)