File tree Expand file tree Collapse file tree 5 files changed +20
-21
lines changed
generators/with-mongo/templates Expand file tree Collapse file tree 5 files changed +20
-21
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,6 @@ PORT=9000
55
66MONGO_HOST = localhost
77MONGO_PORT = 27017
8+ MONGO_DB_NAME = <%= elementName %>
89MONGO_USER =
910MONGO_PASSWORD =
10- MONGO_DB_NAME = <%= elementName %>
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ const path = '/';
99const routes = ( _ : RouteOptions ) : Router => {
1010 const router = Router ( ) ;
1111
12- // TODO: register components to router
12+ // TODO: plug components to router
1313 // router.use(things.path, things.routes(_));
1414
1515 return router ;
Original file line number Diff line number Diff line change 11const host = process . env . MONGO_HOST ?? 'localhost' ;
22const port = + ( process . env . MONGO_PORT ?? '27017' ) ;
3+ const dbName = process . env . MONGO_DB_NAME ?? '<%= elementName %>' ;
34const user = process . env . MONGO_USER ;
45const password = process . env . MONGO_PASSWORD ;
5- const dbName = process . env . MONGO_DB_NAME ?? '<%= elementName %>' ;
66
77const config = {
8- uri : `mongodb://${ host } :${ port } ` ,
8+ uri : `mongodb://${ host } :${ port } / ${ dbName } ` ,
99 user,
1010 password,
11- dbName,
1211} ;
1312
1413export default config ;
Original file line number Diff line number Diff line change @@ -3,26 +3,26 @@ import logger from '@boringcodes/utils/logger';
33
44import config from '../config/mongo' ;
55
6- mongoose . set ( 'bufferCommands' , false ) ;
7-
8- const connect = ( ) : void => {
9- mongoose
10- . connect ( config . uri , {
6+ // connect mongo
7+ const connect = async ( ) : Promise < void > => {
8+ try {
9+ await mongoose . connect ( config . uri , {
1110 user : config . user ,
1211 pass : config . password ,
13- dbName : config . dbName ,
1412 useUnifiedTopology : true ,
1513 useNewUrlParser : true ,
1614 promiseLibrary : Promise ,
17- } )
18- . then ( ( ) => {
19- logger . info ( '> Mongo connected' ) ;
20- } )
21- . catch ( ( err ) => {
22- logger . error ( '> Mongo failed to connect' ) ;
23-
24- throw err ;
2515 } ) ;
16+
17+ logger . info ( '> Mongo connected' ) ;
18+ } catch ( err ) {
19+ logger . error ( '> Mongo failed to connect' ) ;
20+
21+ throw err ;
22+ }
2623} ;
2724
28- export default { connect } ;
25+ // create mongo model
26+ const createModel = mongoose . model ;
27+
28+ export default { connect, createModel } ;
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ import app from './app';
88import config from './config' ;
99
1010// connect mongo
11- mongo . connect ( ) ;
11+ mongo . connect ( ) ; // eslint-disable-line @typescript-eslint/no-floating-promises
1212
1313// create server
1414const server = http . createServer ( app ) ;
You can’t perform that action at this time.
0 commit comments