Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CloudComputing_Act1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jesus Galaz Reyes
CloudComputing_Act1
Commits
b814678f
Commit
b814678f
authored
8 months ago
by
Rokas Stankunas
Browse files
Options
Downloads
Patches
Plain Diff
refactored function into connectDB()
parent
aaf3b483
No related branches found
No related tags found
1 merge request
!4
Resolve "Configure MongoDB Connection"
Pipeline
#15540
passed
8 months ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mongodb.js
+20
-17
20 additions, 17 deletions
mongodb.js
with
20 additions
and
17 deletions
mongodb.js
+
20
−
17
View file @
b814678f
const
{
MongoClient
,
Db
}
=
require
(
'
mongodb
'
);
const
mongoose
=
require
(
'
mongoose
'
);
const
url
=
'
mongodb://localhost:27017/todo
'
;
async
function
createCollections
()
{
// Function to connect to MongoDB using Mongoose and create collections
try
{
async
function
connectDB
()
{
const
client
=
new
MongoClient
(
url
);
try
{
await
client
.
connect
();
// Connect using the MongoDB URI from environment variables
const
conn
=
await
mongoose
.
connect
(
"
mongodb://127.0.0.1:27017
"
);
console
.
log
(
`MongoDB connected:
${
conn
.
connection
.
host
}
`
);
// Check if the "todo" database exists and create it if not
// Get the database object from the connection
const
db
=
client
.
db
(
'
todo
'
);
const
db
=
conn
.
connection
.
db
;
await
db
.
createCollection
(
'
users
'
);
// Check if collection "users" exists and create it if it doesn't exist
await
db
.
createCollection
(
'
todos
'
);
// Check if collection "todos" exists and create it if it doesn't exist
await
db
.
createCollection
(
'
global
'
);
// Check if collection "global" exists and create it if it doesn't exist
console
.
log
(
'
Collections created successfully!
'
);
// Check if the collections exist and create them if not
await
client
.
close
();
await
db
.
createCollection
(
'
users
'
);
}
catch
(
error
)
{
await
db
.
createCollection
(
'
todos
'
);
console
.
error
(
'
Error creating collections:
'
,
error
);
await
db
.
createCollection
(
'
global
'
);
}
console
.
log
(
'
Collections created successfully!
'
);
}
catch
(
err
)
{
console
.
error
(
`Error:
${
err
.
message
}
`
);
process
.
exit
(
1
);
// Exit the process with failure if the connection or collection creation fails
}
}
}
createCollections
();
module
.
exports
=
connectDB
;
\ No newline at end of file
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment