1- import { MongoClient } from ' mongodb' ;
2- import { ObjectId } from ' mongodb' ;
3- import dotenv from ' dotenv' ;
1+ import { MongoClient } from " mongodb" ;
2+ import { ObjectId } from " mongodb" ;
3+ import dotenv from " dotenv" ;
44dotenv . config ( ) ;
55
6- import LOGGER from ' ../utils/logger.js' ;
7- import Constants from ' ../constants/dbConstants.js' ;
6+ import LOGGER from " ../utils/logger.js" ;
7+ import Constants from " ../constants/dbConstants.js" ;
88
9-
10- const { CORE_DB , JSR_DB , JSRF_DB } = Constants ;
9+ const { CORE_DB , JSR_DB , JSRF_DB , BRC_DB } = Constants ;
1110
1211const buildMongoUri = ( ) => {
1312 const user = process . env . MONGO_USER ;
@@ -18,7 +17,9 @@ const buildMongoUri = () => {
1817 return LOGGER . error ( `Invalid admin user found while building mongo uri` ) ;
1918 }
2019 if ( ! password ) {
21- return LOGGER . error ( `Invalid admin password found while building mongo uri` ) ;
20+ return LOGGER . error (
21+ `Invalid admin password found while building mongo uri`
22+ ) ;
2223 }
2324 if ( ! clusterName ) {
2425 return LOGGER . error ( `Invalid cluster name found while building mongo uri` ) ;
@@ -27,97 +28,153 @@ const buildMongoUri = () => {
2728 return LOGGER . error ( `Invalid domain name found while building mongo uri` ) ;
2829 }
2930 return `mongodb+srv://${ user } :${ password } @${ clusterName } .${ domainName } ?retryWrites=true&w=majority` ;
30- }
31+ } ;
3132
3233const client = new MongoClient ( buildMongoUri ( ) ) ;
3334
3435/* Database Connections */
3536export const performCoreAdminAction = async ( action , username ) => {
3637 try {
3738 await client . connect ( ) ;
38- return await action ( client , CORE_DB , ' Admin' , username ) ;
39- } catch ( err ) {
39+ return await action ( client , CORE_DB , " Admin" , username ) ;
40+ } catch ( err ) {
4041 console . error ( err ) ;
4142 return err ;
4243 } finally {
4344 await client . close ( ) ;
4445 }
45- }
46+ } ;
4647
4748export const performCoreAction = async ( action , collection , id , qps ) => {
4849 try {
4950 await client . connect ( ) ;
5051 const queryActions = [ getSortQuery ( qps ) , getLimitSize ( qps ) ] ;
51- return await action ( client , CORE_DB , collection , id , getQueryObject ( qps ) , queryActions ) ;
52- } catch ( err ) {
52+ return await action (
53+ client ,
54+ CORE_DB ,
55+ collection ,
56+ id ,
57+ getQueryObject ( qps ) ,
58+ queryActions
59+ ) ;
60+ } catch ( err ) {
5361 console . error ( err ) ;
5462 return err ;
5563 } finally {
5664 await client . close ( ) ;
5765 }
58- }
66+ } ;
5967
6068export const performJSRAction = async ( action , collection , id , qps ) => {
6169 try {
6270 await client . connect ( ) ;
6371 const queryActions = [ getSortQuery ( qps ) , getLimitSize ( qps ) ] ;
64- return await action ( client , JSR_DB , collection , id , getQueryObject ( qps ) , queryActions ) ;
65- } catch ( err ) {
72+ return await action (
73+ client ,
74+ JSR_DB ,
75+ collection ,
76+ id ,
77+ getQueryObject ( qps ) ,
78+ queryActions
79+ ) ;
80+ } catch ( err ) {
6681 console . error ( err ) ;
6782 return err ;
6883 } finally {
6984 await client . close ( ) ;
7085 }
71- }
86+ } ;
7287
7388export const performJSRFAction = async ( action , collection , id , qps ) => {
7489 try {
7590 await client . connect ( ) ;
7691 const queryActions = [ getSortQuery ( qps ) , getLimitSize ( qps ) ] ;
77- return await action ( client , JSRF_DB , collection , id , getQueryObject ( qps ) , queryActions ) ;
78- } catch ( err ) {
92+ return await action (
93+ client ,
94+ JSRF_DB ,
95+ collection ,
96+ id ,
97+ getQueryObject ( qps ) ,
98+ queryActions
99+ ) ;
100+ } catch ( err ) {
101+ console . error ( err ) ;
102+ return err ;
103+ } finally {
104+ await client . close ( ) ;
105+ }
106+ } ;
107+
108+ export const performBRCAction = async ( action , collection , id , qps ) => {
109+ try {
110+ await client . connect ( ) ;
111+ const queryActions = [ getSortQuery ( qps ) , getLimitSize ( qps ) ] ;
112+ return await action (
113+ client ,
114+ BRC_DB ,
115+ collection ,
116+ id ,
117+ getQueryObject ( qps ) ,
118+ queryActions
119+ ) ;
120+ } catch ( err ) {
79121 console . error ( err ) ;
80122 return err ;
81123 } finally {
82124 await client . close ( ) ;
83125 }
84- }
126+ } ;
85127
86128export const listCollections = async ( dbName ) => {
87129 try {
88130 await client . connect ( ) ;
89131 return await client . db ( dbName ) . listCollections ( ) . toArray ( ) ;
90- } catch ( err ) {
132+ } catch ( err ) {
91133 console . error ( err ) ;
92134 return err ;
93135 } finally {
94136 await client . close ( ) ;
95137 }
96- }
97-
98- const excludedKeys = [ 'sortBy' , 'orderBy' , 'filter' , 'page' , 'limit' ] ;
99- const objectIdKeys = [ 'gameId' , 'locationId' , 'levelId' , 'graffitiTagId' , 'characterId' , 'artistId' , 'songId' ,
100- 'game.id' , 'location.id' , 'adjacentLocations.id' , 'level.id' , 'graffitiTag.id' , 'character.id' , 'artist.id' , 'song.id' ] ;
138+ } ;
139+
140+ const excludedKeys = [ "sortBy" , "orderBy" , "filter" , "page" , "limit" ] ;
141+ const objectIdKeys = [
142+ "gameId" ,
143+ "locationId" ,
144+ "levelId" ,
145+ "graffitiTagId" ,
146+ "characterId" ,
147+ "artistId" ,
148+ "songId" ,
149+ "game.id" ,
150+ "location.id" ,
151+ "adjacentLocations.id" ,
152+ "level.id" ,
153+ "graffitiTag.id" ,
154+ "character.id" ,
155+ "artist.id" ,
156+ "song.id" ,
157+ ] ;
101158
102159// Prepare the queryParameters as one single object for mongoDB query
103160const getQueryObject = ( qps ) => {
104161 if ( qps ) {
105162 const queryMap = { } ;
106163
107164 /* Remove special keys that will be used for query suffix */
108- excludedKeys . forEach ( key => delete qps [ key ] ) ;
165+ excludedKeys . forEach ( ( key ) => delete qps [ key ] ) ;
109166
110167 /* Transform the user query object into a format MongoDB can understand */
111168 for ( let [ key , value ] of Object . entries ( qps ) ) {
112- if ( typeof value === String && value . includes ( ',' ) ) {
113- value = value . split ( ',' )
169+ if ( typeof value === String && value . includes ( "," ) ) {
170+ value = value . split ( "," ) ;
114171 queryMap [ key ] = { $all : value } ; // Uses AND currently...
115- } else if ( value === ' true' || value === ' false' ) {
116- queryMap [ key ] = value === ' true' ? true : false ;
117- } else if ( typeof value === String && value . toLowerCase ( ) === ' female' ) {
118- queryMap [ key ] = ' Female' ;
119- } else if ( typeof value === String && value . toLowerCase ( ) === ' male' ) {
120- queryMap [ key ] = ' Male' ;
172+ } else if ( value === " true" || value === " false" ) {
173+ queryMap [ key ] = value === " true" ? true : false ;
174+ } else if ( typeof value === String && value . toLowerCase ( ) === " female" ) {
175+ queryMap [ key ] = " Female" ;
176+ } else if ( typeof value === String && value . toLowerCase ( ) === " male" ) {
177+ queryMap [ key ] = " Male" ;
121178 } else if ( objectIdKeys . includes ( key ) && ObjectId . isValid ( value ) ) {
122179 queryMap [ key ] = new ObjectId ( value ) ;
123180 } else {
@@ -127,17 +184,16 @@ const getQueryObject = (qps) => {
127184 return queryMap ;
128185 }
129186 return { } ;
130- }
187+ } ;
131188
132189const getSortQuery = ( query ) => {
133190 const field = query ?. sortBy ;
134- const order = query ?. orderBy || ' asc' ;
135- const orderMap = { asc : 1 , desc : - 1 } ;
191+ const order = query ?. orderBy || " asc" ;
192+ const orderMap = { asc : 1 , desc : - 1 } ;
136193 return field ? { [ field ] : orderMap [ order ] } : { } ;
137- }
194+ } ;
138195
139196const getLimitSize = ( query ) => {
140- const limit = query ?. limit || '0' ;
141- return ! isNaN ( Number ( limit ) ) && limit !== '0' ? Number ( limit ) : 0 ;
142- }
143-
197+ const limit = query ?. limit || "0" ;
198+ return ! isNaN ( Number ( limit ) ) && limit !== "0" ? Number ( limit ) : 0 ;
199+ } ;
0 commit comments