Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CaCaoCritics
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
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
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bilel El Yaagoubi
CaCaoCritics
Commits
55c286e4
Commit
55c286e4
authored
3 years ago
by
Bilel El Yaagoubi
Browse files
Options
Downloads
Patches
Plain Diff
populate
parent
fc112b68
No related branches found
No related tags found
1 merge request
!7
Populate and db structure
Pipeline
#42476
passed
3 years ago
Stage: lint
Stage: build
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend/populate.js
+85
-0
85 additions, 0 deletions
backend/populate.js
with
85 additions
and
0 deletions
backend/populate.js
0 → 100644
+
85
−
0
View file @
55c286e4
const
mongoose
=
require
(
"
mongoose
"
);
const
MovieModel
=
require
(
"
./models/movie
"
);
const
axios
=
require
(
"
axios
"
);
async
function
fetchMoviesFromTheMovieDatabase
(
n
)
{
// TODO: fetch movies from the The Movie Database API
try
{
// Do something if call succeeded
const
movieFetch
=
await
axios
.
get
(
`https://api.themoviedb.org/3/movie/popular?api_key=522d421671cf75c2cba341597d86403a&page=`
+
n
);
// console.log(movieFetch.data.results);
return
movieFetch
.
data
.
results
;
}
catch
(
error
)
{
// Do something if call failed
console
.
error
(
error
);
}
}
async
function
populateMovies
(
movies
)
{
// TODO: populate movies into the database
try
{
for
(
const
movie
of
movies
)
{
const
newMovie
=
await
new
MovieModel
({
// Movie attributes
adult
:
movie
.
adult
,
backdrop_path
:
movie
.
backdrop_path
,
genre_ids
:
movie
.
genre_ids
,
id
:
movie
.
id
,
original_language
:
movie
.
original_language
,
original_title
:
movie
.
original_title
,
overview
:
movie
.
overview
,
popularity
:
movie
.
popularity
,
poster_path
:
movie
.
poster_path
,
release_date
:
movie
.
release_date
,
title
:
movie
.
title
,
video
:
movie
.
video
,
vote_average
:
movie
.
vote_average
,
vote_count
:
movie
.
vote_count
,
});
// Create a new movie instance
const
createdMovie
=
await
newMovie
.
save
();
// What to do after movie has been saved !
console
.
log
(
"
Movie Saved !
"
);
console
.
log
(
createdMovie
.
title
);
}
}
catch
(
error
)
{
console
.
log
(
error
);
}
}
async
function
dropDataBase
()
{
// TODO: Drop the collections
try
{
await
MovieModel
.
deleteMany
({});
}
catch
(
error
)
{
console
.
log
(
error
);
}
}
async
function
populate
(
N
)
{
// Connect mongoose client
const
client
=
await
mongoose
.
connect
(
process
.
env
.
MONGO_DB_URL
);
await
dropDataBase
();
for
(
let
n
=
1
;
n
<
N
;
n
++
)
{
const
movies
=
await
fetchMoviesFromTheMovieDatabase
(
n
);
await
populateMovies
(
movies
);
console
.
log
(
n
);
}
// disconnect mongoose client
await
client
.
disconnect
();
}
populate
(
300
)
.
then
(()
=>
{
console
.
log
(
"
All done !
"
);
})
.
catch
((
error
)
=>
{
console
.
error
(
error
);
});
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