@@ -6,10 +6,21 @@ module.exports = async function setupDb() {
66 console . log ( 'Setting up database indexes...' ) ;
77 const db = await dbconnection ( ) ;
88
9- const allProductsIndexName = await db . collection ( 'products' ) . listIndexes ( ) . toArray ( ) ;
9+ // PRODUCTS
10+ let allProductsIndexName = [ ] ;
11+ try {
12+ allProductsIndexName = await db . collection ( 'products' ) . listIndexes ( ) . toArray ( ) ;
13+ } catch ( err ) {
14+ if ( err . codeName === 'NamespaceNotFound' ) {
15+ await db . collection ( 'products' ) . insertOne ( { __seed : true } ) ;
16+ await db . collection ( 'products' ) . deleteOne ( { __seed : true } ) ;
17+ allProductsIndexName = [ ] ;
18+ } else {
19+ throw err ;
20+ }
21+ }
1022
1123 let indexArr = [ ] ;
12- // create indexes only if not exist
1324 allProductsIndexName . forEach ( ( element ) => {
1425 if ( element . name === 'productTextIndex' || element . name === 'productUniqueIndex' ) {
1526 return ;
@@ -37,7 +48,19 @@ module.exports = async function setupDb() {
3748 ] ;
3849 } ) ;
3950
40- const allUsersIndexName = await db . collection ( 'users' ) . listIndexes ( ) . toArray ( ) ;
51+ // USERS
52+ let allUsersIndexName = [ ] ;
53+ try {
54+ allUsersIndexName = await db . collection ( 'users' ) . listIndexes ( ) . toArray ( ) ;
55+ } catch ( err ) {
56+ if ( err . codeName === 'NamespaceNotFound' ) {
57+ await db . collection ( 'users' ) . insertOne ( { __seed : true } ) ;
58+ await db . collection ( 'users' ) . deleteOne ( { __seed : true } ) ;
59+ allUsersIndexName = [ ] ;
60+ } else {
61+ throw err ;
62+ }
63+ }
4164 allUsersIndexName . forEach ( ( element ) => {
4265 if ( element . name === 'userUniqueIndex' ) {
4366 return ;
@@ -50,7 +73,19 @@ module.exports = async function setupDb() {
5073 ] ;
5174 } ) ;
5275
53- const allRatingsIndexName = await db . collection ( 'ratings' ) . listIndexes ( ) . toArray ( ) ;
76+ // RATINGS
77+ let allRatingsIndexName = [ ] ;
78+ try {
79+ allRatingsIndexName = await db . collection ( 'ratings' ) . listIndexes ( ) . toArray ( ) ;
80+ } catch ( err ) {
81+ if ( err . codeName === 'NamespaceNotFound' ) {
82+ await db . collection ( 'ratings' ) . insertOne ( { __seed : true } ) ;
83+ await db . collection ( 'ratings' ) . deleteOne ( { __seed : true } ) ;
84+ allRatingsIndexName = [ ] ;
85+ } else {
86+ throw err ;
87+ }
88+ }
5489 allRatingsIndexName . forEach ( ( element ) => {
5590 if ( element . name === 'ratingsUniqueIndex' ) {
5691 // db.collection('ratings').dropIndex('ratingsUniqueIndex');
0 commit comments