This GitHub repository contains the source code for a Game-library application backend built with node.js & Typescript mainly.
Before getting started with the Weather App repository, ensure that you have the following prerequisites installed:
- Node.js: Make sure you have Node.js installed on your system. You can download it from the official Node.js website: https://nodejs.org
- Clone the repository:
git clone https://github.com/Kushalmydesk/game_lib.git
- Navigate to the project directory:
cd root_folder
- Install the dependencies:
npm install
- Run on Local Server:
npm run dev
-
Nodemon is a development tool that improves the development workflow by automatically restarting the Node.js application whenever changes are made to the watched files. It eliminates the need for manually stopping and restarting the application during development, providing a faster and more efficient development experience. The nodemon.json file configures Nodemon with the directories to watch, the file extensions to consider, and the command to execute when changes are detected.
-
watch
: Specifies the directories or files that Nodemon should watch for changes. In this case, it is set to watch the"src"
directory, indicating that any changes to files within that directory will trigger a restart of the application. -
ext
: Specifies the file extensions that Nodemon should consider when watching for changes. In this case, it is set to".ts,.js"
, indicating that Nodemon will watch for changes in both TypeScript (.ts
) and JavaScript (.js
) files. -
exec
: Specifies the command that Nodemon should execute when a change is detected. In this case, it is set to"ts-node ./src/index.ts"
, indicating that Nodemon should execute the TypeScript files (index.ts
) using thets-node
command. This allows for automatic compilation and execution of TypeScript files without the need for manual compilation steps.
-
-
-
module
: Specifies the module code generation for TypeScript. In this case, it is set to"NodeNext"
. -
moduleResolution
: Specifies how module dependencies are resolved. In this case, it is set to"node"
, indicating that Node.js-style module resolution will be used. -
baseUrl
: Specifies the base directory for module resolution. In this case, it is set to"src"
, indicating that module resolution will start from the"src"
directory. -
outDir
: Specifies the output directory for the compiled TypeScript files. In this case, it is set to"dist"
, indicating that the compiled JavaScript files will be placed in the"dist"
directory. -
sourceMap
: Specifies whether to generate source map files (.map) for the compiled JavaScript files. In this case, it is set totrue
, indicating that source map files will be generated. -
noImplicitAny
: Specifies whether to raise an error on expressions and declarations with an impliedany
type. In this case, it is set totrue
, indicating that TypeScript will report an error if it cannot infer the type and no type annotation is specified. -
include
: Specifies the files or patterns to include in the TypeScript compilation. In this case, it includes all.ts
files in the"src"
directory and its subdirectories. -
exclude
: Specifies the files or patterns to exclude from the TypeScript compilation. In this case, it excludes the"node_modules"
directory.
-
-
-
scripts
: This section defines various scripts that can be run usingnpm run [script-name]
command:start
: This script runs the compiled JavaScript files in thedist
directory using thenode
command.build
: This script runs the TypeScript compiler (tsc
) to compile the TypeScript files into JavaScript files.dev
: This script usesnodemon
to monitor changes in the source files and automatically restart the server during development.test
: This script is a placeholder and currently only echoes an error message.
-
devDependencies
: This section lists the development dependencies, which are packages required during development but not during production runtime. These dependencies include various TypeScript type definitions (@types/...
packages) for improved TypeScript development, such as type definitions for Express, MongoDB, Mongoose, Multer, etc. -
dependencies
: This section lists the runtime dependencies, which are packages required for the application to run in production. These dependencies include packages likeexpress
,dotenv
,cors
,mongoose
,multer
,firebase
, etc., which are used for server-side development, database operations, file handling, and Firebase integration.
-
-
src
- controllers
- game.controller.ts
- series.controller.ts
- models
- game.model.ts
- series.model.ts
- routes
- game.route.ts
- series.route.ts
- services
- firebase.service.ts
- mongodb.service.ts
- index.ts
- controllers
-
-
Dependencies: The code imports necessary dependencies from the Firebase SDK, including
initializeApp
,getApp
,getApps
from"firebase/app"
and various storage-related functions from"firebase/storage"
. It also imports thedotenv
package for environment variable configuration. -
Environment Configuration: The code uses
dotenv.config()
to load environment variables from a.env
file. -
Firebase App Initialization: The code defines a function
getFirebaseApp()
that returns the Firebase app instance. If no app exists, it initializes a new app usinginitializeApp()
with the provided configuration from the environment variables. If an app already exists, it retrieves the app usinggetApp()
. -
Firebase Storage Initialization: The code initializes the Firebase storage instance using
getStorage()
with the Firebase app instance. -
Export: The code exports the
getFirebaseApp()
function and theupload_Img
function. -
upload_Img
Function: This function handles the upload of an image file to Firebase Storage. It takes thefile
andname
as parameters, creates a storage reference usingref()
with the provided file name and a timestamp, and prepares the metadata for the file. It then uploads the file to Firebase Storage usinguploadBytes()
with the storage reference, file buffer, and metadata. The function returns the download URL of the uploaded image usinggetDownloadURL()
.
-
-
-
Dependencies: The code imports necessary dependencies from Mongoose, including
mongoose
,ConnectOptions
, andError
. It also imports thedotenv
package for environment variable configuration. -
connectToDB
Function: This function is responsible for connecting to the MongoDB database. It first loads environment variables from a.env
file usingdotenv.config()
. -
Database Connection: The function attempts to establish a connection to the MongoDB database using
mongoose.connect()
. It uses theDB_CONN_STRING
environment variable for the connection string and theDB_NAME
environment variable for the database name. It provides additional options for the connection, includinguseNewUrlParser: true
to use the new URL parser anduseUnifiedTopology: true
to use the new server discovery and monitoring engine. -
Connection Status: If the connection is successful, a success message is logged indicating the database name. If an error occurs during the connection process, an error message is logged.
-
Error Handling: The function wraps the database connection process in a
try-catch
block to catch and log any errors that occur. -
Export: The code exports the
connectToDB
function to be used in other parts of the application.
-
-
-
Dependencies: The code imports necessary dependencies from the Mongoose library, including
Schema
,Document
,models
,model
, andModel
. -
Interface: The code declares an interface named
IGame
that extends theDocument
interface from Mongoose. It defines the structure and types of the properties that a "Game" document should have. -
Schema Definition: The code creates a new schema named
gameSchema
using theSchema
class from Mongoose. The schema defines the fields and their types for a "Game" document. These fields includetitle
,genre
,platform
,releaseYear
,developer
,publisher
,description
,image
,rating
,tagArray
,langArray
,multiplayer
,platformArray
, andseriesId
. -
Schema Options: The schema is configured with some options:
timestamps: false
- Disables the automatic generation ofcreatedAt
andupdatedAt
timestamps for the documents.versionKey: false
- Disables the versioning feature of Mongoose.
-
Export: The code exports a Mongoose model named "Games". It checks if the model already exists (
models.Games
) and returns it if it does. Otherwise, it creates a new model usingmodel<IGame>("Games", gameSchema)
and exports it.
-
-
-
Dependencies: The code imports necessary dependencies from the Mongoose library, including
Schema
,Document
,models
,model
, andModel
. It also imports theIGame
interface from thegame.model
file. -
Interface: The code declares an interface named
ISeries
that extends theDocument
interface from Mongoose. It defines the structure and types of the properties that a "Series" document should have. The properties includetitle
(string) andgames
(an array ofIGame['_id']
). -
Schema Definition: The code creates a new schema named
seriesSchema
using theSchema
class from Mongoose. The schema defines the fields and their types for a "Series" document. These fields includetitle
(required string) andgames
(an array ofSchema.Types.ObjectId
referencing the "Game" model). -
Schema Options: The schema is configured with some options:
timestamps: false
- Disables the automatic generation ofcreatedAt
andupdatedAt
timestamps for the documents.versionKey: false
- Disables the versioning feature of Mongoose.
-
Export: The code exports a Mongoose model named "Series". It checks if the model already exists (
models.Series
) and returns it if it does. Otherwise, it creates a new model usingmodel<ISeries>("Series", seriesSchema)
and exports it.
-
-
-
getGames
: This function retrieves all games from the database by callingGame.find({})
. The retrieved games are then sent as a response with a status code of 200. -
getGamesById
: This function retrieves a game by its ID. It extracts the game ID from the request parameters and usesGame.findById
to find the game. The retrieved game is then sent as a response with a status code of 200. -
createGame
: This function is responsible for creating a new game. It extracts the necessary game information from the request body, including the title, genre, platform, release year, developer, publisher, description, rating, tags, languages, multiplayer, platforms, and series name.-
It first checks if an image file is included in the request (
!req.file
). If not, it returns a response with a status code of 400 and a message indicating that the file is not found. -
It then uploads the image to Firebase storage by calling
upload_Img
function and passing the file and title as arguments. The function returns the downloadable image URL, which is stored in theimageUrl
variable. -
It retrieves the
seriesId
by querying theSeries
collection based on the providedseriesName
. -
It splits the tags, languages, and platforms into arrays by using the
split
method and mapping over the resulting strings. -
It creates a new instance of the
Game
model with all the extracted information and saves it to the database. -
It updates the
games
array of the corresponding series by callingSeries.findByIdAndUpdate
and using the$push
operator to add the newly created game's ID. -
Finally, it sends a response with a status code of 201 and the saved game object.
-
-
deleteGame
: This function deletes a game by its ID. It extracts the game ID from the request parameters and usesGame.findByIdAndDelete
to delete the game from the database. If the game is successfully deleted, it checks if the corresponding series exists and updates thegames
array of the series by usingSeries.findByIdAndUpdate
and the$pop
operator to remove the game's ID. Finally, it sends a response with a status code of 200 and a success message.
-
-
-
getSeries
: This function retrieves all series from the database by callingSeries.find({})
. The retrieved series are then sent as a response with a status code of 200. -
getGamesBySeries
: This function retrieves all games associated with a specific series. It extracts theseriesId
from the request parameters and usesSeries.findById
to find the series. If the series does not exist, it returns a response with a status code of 404 and a message indicating that no series is found.-
If the series is found, it retrieves the
games
array from the series and uses the$in
operator in a query to find all games whose IDs match the ones in thegames
array. The retrieved games are sorted by their release year in ascending order. -
It sends a response with a status code of 200 and includes the series name, a success message, and the retrieved games.
-
-
createSeries
: This function creates a new series. It extracts thetitle
from the request body and creates a new instance of theSeries
model with the provided title. The new series is then saved to the database, and a response with a status code of 201 and the saved series object is sent.
-
-
-
upload
: This is the multer configuration that specifies the storage options and limits for file uploads. It usesmulter.memoryStorage()
to store the uploaded file in memory and sets a limit of 10MB for the file size. -
router.post("/game", [upload.single("image")], createGame)
: This route is used for creating games. It uses the[upload.single("image")]
middleware to handle the image upload. Thesingle()
function specifies that only a single file with the field name "image" should be uploaded. The uploaded file can be accessed in thecreateGame
controller function viareq.file
. -
router.get("/game", getGames)
: This route is used for retrieving all games. It calls thegetGames
controller function when a GET request is made to this endpoint. -
router.get("/game/:id", getGamesById)
: This route is used for retrieving a specific game by its ID. It calls thegetGamesById
controller function when a GET request with a game ID is made to this endpoint. -
router.delete("/game/:id", deleteGame)
: This route is used for deleting a specific game by its ID. It calls thedeleteGame
controller function when a DELETE request with a game ID is made to this endpoint.
-
-
-
Dependencies: The code imports necessary dependencies from Express, including
express
andRouter
. It also imports the series-related controller functions (getSeries
,createSeries
,getGamesBySeries
) from theseries.controller
file. -
Router Initialization: The code creates a new router instance using
express.Router()
and assigns it to therouter
variable. -
Route Definitions:
GET /series
: This route is responsible for retrieving all series. It maps to thegetSeries
controller function defined in the series controller file.POST /series
: This route is responsible for creating a new series. It maps to thecreateSeries
controller function defined in the series controller file.GET /series/:seriesId
: This route is responsible for retrieving games belonging to a specific series. It expects theseriesId
parameter in the request (req.params.seriesId
). It maps to thegetGamesBySeries
controller function defined in the series controller file.
-
Export: The code exports the router instance to be used in other parts of the application.
-
-
Dependencies: The code imports necessary dependencies from various packages, including
express
,body-parser
,cookie-parser
,compression
,cors
,dotenv
, andError
from Mongoose. It also imports theconnectToDB
function from themongodb.service
file, as well as the route handlers from the respective route files (game.route
,image.route
,series.route
). -
Environment Configuration: The code loads environment variables from a
.env
file usingdotenv.config()
. -
Server Setup: The code initializes an Express application by calling
express()
and assigns it to theapp
variable. -
Middleware Setup:
cors
: The code enables Cross-Origin Resource Sharing (CORS) by using thecors
middleware. It allows requests from different origins and includes credentials.compression
: The code uses thecompression
middleware to enable response compression for better performance.cookie-parser
: The code uses thecookie-parser
middleware to parse cookie headers from incoming requests.body-parser
: The code uses thebody-parser
middleware to parse request bodies in JSON format.
-
Port Configuration: The code retrieves the port number from the
PORT
environment variable. -
Route Setup: The code sets up the API routes for game services, image services, and series services. It prefixes these routes with
/api
usingapp.use("/api", ...)
. -
Database Connection: The code calls the
connectToDB
function to establish a connection to the MongoDB database using Mongoose. If the connection is successful, it starts the server by callingapp.listen()
and logs a success message with the server URL. If there's an error during the connection, it logs an error message and exits the process.