@@ -66,8 +66,9 @@ EXIT /B 0
6666 echo [3] Drop Database
6767 echo [4] List Databases
6868 echo [5] List Local Databases
69- echo [6] Update Connection Credentials
70- echo [7] Reset
69+ echo [6] Create Local Database and User
70+ echo [7] Update Connection Credentials
71+ echo [8] Reset
7172 echo [x] Exit
7273 set " choice = -1"
7374 echo .
@@ -84,8 +85,9 @@ EXIT /B 0
8485 set /A NextScreen = _ViewDatabaseCredentials
8586 Goto ShowDatabases
8687 )
87- if %choice% EQU 6 Goto SetDatabaseCredentials
88- if %choice% EQU 7 Goto ResetData
88+ if %choice% EQU 6 Goto CreateDatabaseAndUser
89+ if %choice% EQU 7 Goto SetDatabaseCredentials
90+ if %choice% EQU 8 Goto ResetData
8991 if %choice% == x EXIT /B 0
9092
9193 Goto ViewDatabaseCredentials
@@ -344,6 +346,76 @@ EXIT /B 0
344346 GoTo SetDatabaseCredentials
345347EXIT /B 0
346348
349+ :: Creates a local database and local database user
350+ :CreateDatabaseAndUser
351+ setlocal enabledelayedexpansion
352+
353+ cls
354+ echo ----------------------------------------------------------
355+ echo CREATE DATABASE AND USER
356+ echo ----------------------------------------------------------
357+
358+ set " databaseName = "
359+ set " databaseUser = "
360+ set " userPassword = "
361+
362+ set /p databaseName = " Enter the database name:"
363+ set /p databaseUser = " Enter the database user:"
364+ set /p userPassword = " Enter the database user password:"
365+ echo .
366+
367+ echo Do you want to create the database and user
368+ echo on host [%MONGO_HOST% ]?
369+ echo - Database: %databaseName%
370+ echo - User: %databaseUser%
371+ echo - Passsword: %userPassword%
372+ echo .
373+
374+ set " continue = Y"
375+ echo Press enter to continue
376+ set /p continue = Type " n" and press enter to cancel:
377+
378+ if %continue% EQU n (
379+ set " retry = Y"
380+ set /p retry = " Retry? [Y/n]:"
381+
382+ if /i " !retry! " == " n" (
383+ GoTo ViewDatabaseCredentials
384+ ) else (
385+ GoTo CreateDatabaseAndUser
386+ )
387+ ) else (
388+ (if %MONGO_HOST% EQU localhost (
389+ echo Creating local database and user...
390+
391+ %MONGO_SHELL% !databaseName! --eval " db.createCollection('_!databaseName! ')"
392+ %MONGO_SHELL% !databaseName! --eval " db.createUser({user: '!databaseUser! ' , pwd: '!userPassword! ', roles: [{ role: 'readWrite', db: '!databaseName! ' }]})"
393+
394+ echo Success!
395+ set /A NextScreen = _ViewDatabaseCredentials
396+ GoTo ShowDatabases
397+ ) else (
398+ echo Creating remote database...
399+
400+ %MONGO_SHELL% mongodb+srv://%MONGO_USER% :%MONGO_PASSWORD% @ %MONGO_HOST% /!databaseName! --eval " db.createCollection('_!databaseName! '); db.adminCommand({ listDatabases: 1, nameOnly:true })" > .dbs
401+
402+ echo .
403+ echo Success!
404+ echo [NOTE]: Creating database users on remote hosts are currently not supported.
405+ echo [NOTE]: Skipping creating remote database user...
406+
407+ echo .
408+ echo ----------------------------------------------------------
409+ echo Host: %MONGO_HOST%
410+ echo Available databases:
411+ findstr /C:name .dbs
412+
413+ set /p go = Press enter to continue...
414+ GoTo ViewDatabaseCredentials
415+ ))
416+ )
417+ EXIT /B 0
418+
347419
348420:: ----------------------------------------------------------
349421:: Utility helper scripts
0 commit comments