Skip to content

Commit ab9ce44

Browse files
committed
index provision added
1 parent 41b98f5 commit ab9ce44

File tree

6 files changed

+77
-4
lines changed

6 files changed

+77
-4
lines changed

dist/config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ exports.config = {
99
contentStore: {
1010
collectionName: 'contents',
1111
dbName: 'contentstack-persistent-db',
12+
indexes: {
13+
content_type_uid: 1,
14+
locale: 1,
15+
uid: 1
16+
},
1217
limit: 100,
1318
locales: [],
1419
options: {

dist/stack.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,40 @@ class Stack {
8484
this.client = client;
8585
return client.connect().then(() => {
8686
this.db = client.db(dbName);
87-
return resolve(this.db);
87+
resolve(this.db);
88+
const bucket = [];
89+
const indexes = this.config.contentStore.indexes;
90+
const collectionName = this.config.contentStore.collectionName;
91+
for (let index in indexes) {
92+
if (indexes[index] === 1 || indexes[index] === -1) {
93+
bucket.push(this.createIndexes(this.config.contentStore.collectionName, index, indexes[index]));
94+
}
95+
}
96+
Promise.all(bucket)
97+
.then(() => {
98+
console.info(`Indexes created successfully in '${collectionName}' collection`);
99+
})
100+
.catch((error) => {
101+
console.error(`Failed while creating indexes in '${collectionName}' collection`);
102+
console.error(error);
103+
});
88104
}).catch(reject);
89105
}
90106
catch (error) {
91107
return reject(error);
92108
}
93109
});
94110
}
111+
createIndexes(collectionId, index, type) {
112+
return this.db.collection(collectionId)
113+
.createIndex({
114+
[index]: type
115+
})
116+
.then(() => {
117+
console.info(`Index '${index}' created successfully!`);
118+
return;
119+
});
120+
}
95121
close() {
96122
this.client.close();
97123
}
@@ -761,7 +787,7 @@ class Stack {
761787
referencesFound.push(new Promise((rs, rj) => {
762788
return self.db.collection(this.contentStore.collectionName)
763789
.find(query)
764-
.project(self.config.projections)
790+
.project(self.config.contentStore.projections)
765791
.toArray()
766792
.then((result) => {
767793
if (result.length === 0) {

src/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export const config = {
88
contentStore: {
99
collectionName: 'contents',
1010
dbName: 'contentstack-persistent-db',
11+
indexes: {
12+
// published_at: -1,
13+
content_type_uid: 1,
14+
locale: 1,
15+
uid: 1
16+
},
1117
limit: 100,
1218
locales: [
1319
],

src/stack.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,26 @@ export class Stack {
127127
return client.connect().then(() => {
128128
this.db = client.db(dbName)
129129

130-
return resolve(this.db)
130+
resolve(this.db)
131+
132+
// Create indexes in the background
133+
const bucket: any = []
134+
const indexes = this.config.contentStore.indexes
135+
const collectionName = this.config.contentStore.collectionName
136+
for (let index in indexes) {
137+
if (indexes[index] === 1 || indexes[index] === -1) {
138+
bucket.push(this.createIndexes(this.config.contentStore.collectionName, index, indexes[index]))
139+
}
140+
}
141+
142+
Promise.all(bucket)
143+
.then(() => {
144+
console.info(`Indexes created successfully in '${collectionName}' collection`)
145+
})
146+
.catch((error) => {
147+
console.error(`Failed while creating indexes in '${collectionName}' collection`)
148+
console.error(error)
149+
})
131150
}).catch(reject)
132151
} catch (error) {
133152

@@ -136,6 +155,17 @@ export class Stack {
136155
})
137156
}
138157

158+
private createIndexes (collectionId, index, type) {
159+
return this.db.collection(collectionId)
160+
.createIndex({
161+
[index]: type
162+
})
163+
.then(() => {
164+
console.info(`Index '${index}' created successfully!`)
165+
return
166+
})
167+
}
168+
139169
/**
140170
* @summary
141171
* Closes connection with mongodb
@@ -1176,7 +1206,7 @@ export class Stack {
11761206
referencesFound.push(new Promise((rs, rj) => {
11771207
return self.db.collection(this.contentStore.collectionName)
11781208
.find(query)
1179-
.project(self.config.projections)
1209+
.project(self.config.contentStore.projections)
11801210
.toArray()
11811211
.then((result) => {
11821212
if (result.length === 0) {

typings/config.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export declare const config: {
77
contentStore: {
88
collectionName: string;
99
dbName: string;
10+
indexes: {
11+
content_type_uid: number;
12+
locale: number;
13+
uid: number;
14+
};
1015
limit: number;
1116
locales: any[];
1217
options: {

typings/stack.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export declare class Stack {
1515
ascending(field?: any): this;
1616
descending(field?: any): this;
1717
connect(overrides?: {}): Promise<{}>;
18+
private createIndexes;
1819
close(): void;
1920
language(code: any): this;
2021
and(queries: any): this;

0 commit comments

Comments
 (0)