To start with, I wish to mention that I’ve joined to a new team that develops a on-permise product in the field of RTC communication using WebRTC, which is a cool team, so I’m pretty glad on that new journey.

Upon arrival, I was given a few days to get familiar with several subjects, including mongoose. It was decided to implement an inheritance behavior on nested documents inside a normal document, instead of discriminating the document itself, which is already documented thoroughly on the mongoose website.

Although I came from a background in other programming languages, including JavaScript and how to use mongoose, I encountered difficulties in understanding how to make a TypeScript server using NestJs and make calls to create nested documents. From that experience, I aim to provide anyone who needs to make use of nested documents with a summarized and simple answer in this article.

To ensure everyone reading this is on the same page, let’s go over some basic ideas:

  • JavaScript: a language interpreted on each browser with its own engine to execute the logic of a website. It can also run on the PC itself, also known as Node.js.
  • TypeScript: a newer version of JavaScript that offers OOP behavior over JavaScript. It means we can use JavaScript inside TypeScript.
  • MongoDB: a NoSQL database that saves its data in a file format of JSON, where each table is considered a collection, and each row is considered a document containing key-value pair properties. MongoDB does not follow the “col-row” style of SQL databases such as MySQL and PostgreSQL.
  • Mongoose: a popular open-source library for handling a connection with MongoDB. Mongoose provides a simple line of communication with a set of rules that incorporate into our MongoDB logically, such as schema limitations for fields on each table/collection schema definition.
  • Node.js: “a JavaScript runtime built on Chrome’s V8 JavaScript engine,” according to the Node.js website.
  • Express: an HTTP server written in JavaScript.
  • NestJs: a framework for developing server-side applications with ease, among other reasons.

Before we start, it should be noted that this article only discusses nested discriminators. Therefore, to fully understand what is going on, it is highly recommended to review or read about the above-mentioned subjects.

Now that we have covered the basics, we can get started! 🙂

First things first

Let’s setup a normal environment of NestJs(after downloading and installing it off course).
* The following commands on how to initialize NestJs environment below from their website(https://docs.nestjs.com/)

Linux/Mac:
$ npm i -g @nestjs/cli
$ nest new project-nameWindows:
C:/Users/your-user-name> npm i -g @nestjs/cli
C:/Users/your-user-name> nest new project-name

Right now we have a standard working environment of NestJs. Now let’s move on to the databse.

Manning the Database

Alright, let’s dive into the fun stuff! To get this party started, we need to have a MongoDB server running on our machine, container, or through a fancy-schmancy DaaS (Database as a Service). Wherever you choose to have it, the important thing is that it’s up and running, and we’re able to connect to it.

Personally, I went with the official container image of MongoDB (https://hub.docker.com/_/mongo) because it was a breeze to set up and deploy on my Linux machine. So, let’s get this show on the road!

Discriminator Time!

Well, discriminator comes from the word to discriminate between objects. Meaning that we have the same object but some types of that object needs a little of discrimination from the parent object. You all might have guessed already, but I’m talking about inheritance on a JSON object, which will be nested inside another object.

Let’s say that we want to have a collection of our clients cars.
Now, today cars split between casual day car, sports car, premium car, construction types car and etc…
Each car has properties that not necessarily other car has them. For example, we can categorize sports
Lets say we hold extra properties for each car type, like the following example:

  • Sports Car — hasNitro, hasTurbo.
  • Casual Day Car — maxRPM.

What we can do in terms of code and mongoose is the following:

The part of call the path(), is to access a property inside the ClientSchema, which is the base schema of CarSchema that we created.
The CarSchema is used like a base parent class like in OOP.

What probably most people are also wondering right now, is how to discriminate between the cars and how we tell mongoose when creating objects through HTTP requests, to discriminate between their car schema type.

Graph explanation for the discrimination between the car types

Well, notice the “discriminatorKey: ‘carType’”.
What it actually does, is telling the mongoose, when receiving an object with the field carType with corresponding value in the type(‘CasualDayCar’ or ‘SportsCar’), that we gave in the discriminator keys declaration, mongoose will discriminate the object to the right type.

Well, that’s the way of creating a discrimination of objects inside MongoDb using NestJs and mongoose.
I didn’t had any code examples of NestJs/Node.js/JavaScript of using them, because there are plenty of them to find.

I hope you had a great time reading this article and I had love to hear your thoughts if you have any.

Thanks ☺

Author

I simply love learning and improving by what I do, and by doing so strive to achieve my goals which is also help others to achieve theirs.

Write A Comment