Build an API to access the included pokedex dataset, and practise writing schemas with validation and subdocuments
For this assignment you will have to;
- Upload the
pokedex
dataset to your MongoDB database - Create a mongoose schema for the
pokedex
database - Add validation to the schema
- Create an API for accessing the
pokedex
database
Some code has already been written for you
Before starting these tasks install the included npm packages, by running the command npm install
or npm i
-
In your MongoDB server, create a new database
db-validation
-
Inside this database, create a new collection called
pokedex
-
Upload the
pokedex.json
dataset into yourpokedex
collection
-
Using the
.env.example
file as a template, create a.env
file -
Add your database connection details to your
.env
file, filling in the details as provided to you by MongoDBHint: The key
DB_NAME
points to the name of the database you want to connect to. Use the namedb-validation
. This will ensure that Mongoose will try and use the existing sample dataset you previously set up.Hint: The key
DB_HOST
is the domain of your MongoDB connection string
Inside server.js
;
-
Using the
mongoose.connect()
method, use the variabledbConnectionString
to connect to your database -
mongoose.connect()
returns a promise- use the
then()
method to display a message saying the connection was successful - use the
catch()
method to display a message saying the connection failed
- use the
-
Check that your database can connect
-
Analyse the data in your
pokedex
collection -
Complete the schema in
models/Pokedex.js
Hint: Some values maybe held inside an array, for example a string of arrays would be represented as
[String]
Hint: For some data you may need to use subdocuments
For this task you will be adding validation to your schema
Add validation to your schema based on the following criteria:
name
>english
should be requiredname
>all languages
should have a minimum character count of 3name
>all languages
should have a maximum character count of 24type
should be requiredbase
>HP
can not be less than 10 and should default to 20base
>Attack
can not be less than 1 and should default to 5base
>Defense
can not be less than 0 and should default to 1base
>Speed
can not be less than 20 and should default to 5
For this task you will be adding enum validation to your schema
- For
type
, use anenum
with the following strings;
'grass', 'poison',
'fire', 'flying',
'water', 'bug',
'normal', 'electric',
'ground', 'fairy',
'fighting', 'psychic',
'rock', 'steel',
'ice', 'ghost',
'dragon', 'dark'
Our database may end up having more than one collection, or our business logic may become quite complex. We can imagine that if we were to fully develop our application, it could get quite big.
Let's try and keep things organised from the start.
Create a route pokedex
with the path /pokedex
-
Create the file
pokedex.js
in the folderroutes
- import
express
- create the
router
instance fromexpress.Router()
- export your
router
instance
- import
-
Import your route into
server.js
- Use
app.use()
to use the pokedex router you imported - Use the path
/pokedex
- Use
We will create an endpoint to load all pokemon
-
Create an endpoint with the path
/all
. This will be aGET
endpoint. -
Use the endpoint to interact with the Pokedex model to find all the pokemons in the collection
-
Return the results to the client
-
Test your endpoint
We will create an endpoint to load a specific pokemon, based on the name
-
Create an endpoint with the path
/name
. This will be aGET
endpoint. The endpoint should expect a request parameter from the client, the pokemonname
. -
Use the endpoint to interact with the Pokedex model to find the pokemon by
name
. For now, default to the english version of the name, then:- If found, return a status of
200
and the result - If not found, return a status of
404
and an appropriate message
- If found, return a status of
Hint: You might want to use a request parameter
Hint: You can access parameters from the request object with the
params
property
We will create an endpoint to load all pokemons of a specific type
-
Create an endpoint with the path
/type
. This will be aGET
endpoint. The endpoint should expect a request parameter from the client, the pokemontype
. -
Use the endpoint to interact with the Pokedex model
-
Search for the pokemon based on the type supplied by the client
-
Return the result to the client
Hint: You might want to normalize the input (make it lowercase) so that it matches the data in the collection
-
Modify the endpoint you created in Task 9
-
Using a
query
parameter, allow the user to change the language of the search
Example: If the user attaches the query
?language=japanese
then search for the japanese version of the name
Hint: You can access query parameters from the request object with the
query
property