Reader small image

You're reading from  Nest.js: A Progressive Node.js Framework

Product typeBook
Published inNov 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781800204737
Edition1st Edition
Languages
Tools
Right arrow
Authors (5):
Greg Magolan
Greg Magolan
author image
Greg Magolan

Greg Magolan is a senior architect, full-stack engineer, and Angular consultant at Rangle. He has been developing enterprise software solutions for over 15 years for companies such as Agilent Technologies, Electronic Arts, Avigilon, Energy Transfer Partners, FunnelEnvy, Yodel and ACM Facility Safety.
Read more about Greg Magolan

Patrick Housley
Patrick Housley
author image
Patrick Housley

Patrick Housley is a lead technologist at VML. He is an IT professional with over six years of experience in the technology industry. He is capable of analyzing complex issues spanning multiple technologies while providing detailed resolutions and explanations. He has strong front-end development skills with experience leading development teams in maintenance and greenfield projects.
Read more about Patrick Housley

Adrien de Peretti
Adrien de Peretti
author image
Adrien de Peretti

Adrien de Peretti is a full-stack JavaScript developer. He is passionate about new technologies and is constantly looking for new challenges. He is especially interested in artificial intelligence and robotics. W
Read more about Adrien de Peretti

Jay Bell
Jay Bell
author image
Jay Bell

Jay Bell is the CTO of Trellis. He is a senior Angular developer who uses Nest.js in production to develop industry-leading software to help non-profit organizations and charities in Canada. He is a serial entrepreneur who has developed software for a large range of purposes, from helping combat wildfires with drones to building mobile apps.
Read more about Jay Bell

David Guijarro
David Guijarro
author image
David Guijarro

David Guijarro is a front-end developer at Car2go Group GmbH. He has a wide experience working within the JavaScript ecosystem. He has successfully built and led multicultural, multifunctional teams.
Read more about David Guijarro

View More author details
Right arrow

Chapter 7. Mongoose

Mongoose is the third and last database mapping tool that we will be covering in this book. It is the best known MongoDB mapping tool in the JavaScript world.

A word about MongoDB

When MongoDB was initially released, in 2009, it took the database world by storm. At that point the vast majority of databases in use were relational, and MongoDB quickly grew to be the most popular non-relational database (also known as “NoSQL”.)

NoSQL databases difer from relational databases (such as MySQL, PostgreSQL, etc.) in that they model the data they store in ways other than tables related one to another.

MongoDB, specifically, is a “document-oriented database.” It saves data in “documents” encoded in BSON format (“Binary JSON”, a JSON extension that includes various data types specific for MongoDB). The MongoDB documents are grouped in “collections.”

Traditional relational databases separate data in tables and columns, similar to a spreadsheet. On the other hand, document-oriented databases store complete data objects in single instances of the database, similar to a text file.

While...

A word about Mongoose

Mongoose is technically not an ORM (Object Relational Mapping) though it’s commonly referred to as one. Rather, it is an ODM (Object Document Mapping) since MongoDB itself is based in documents instead of relational tables. The idea behind ODM’s and ORM’s is the same, though: providing an easy-to-use solution for data modelling.

Mongoose works with the notion of “schemas.” A schema is simply an object that defines a collection (a group of documents) and the properties and allowed types of values that the document instances will have (i.e. what we would call “their shape.”).

Mongoose and Nest.js

Just like we saw in the TypeORM and the Sequelize chapters, Nest.js provides us with a module that we can use with Mongoose.

Getting started

As a first step, we need to install the Mongoose npm package, as well as the Nest.js/Mongoose npm package.

Run npm install --save mongoose @nestjs/mongoose in your console, and npm install --save-dev @types/mongoose inmediately after.

Set up the database

Docker Compose is the easiest way to get started with MongoDB. There’s an official MongoDB image in the Docker registry we recommend that you use. The latest stable version at the moment of writing this is 3.6.4.

Let’s create a Docker Compose file to build and start both the database we will be using, as well as our Nest.js app, and link them together so that we can access the database later from our code.

version: '3'

volumes:
  mongo_data:

services:
  mongo:
    image: mongo:latest
    ports:
    - "27017:27017"
    volumes:
    - mongo_data:/data/db
  api:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        - NODE_ENV=development
    depends_on:
      ...

Modelling our data

We already mentioned before that Mongoose works with the concept of “schemas.”

Mongoose schemas play a similar role to TypeORM entities. However, unlike the latter, the former are not classes, but rather plain objects that inherit from the Schema prototype defined (and exported) by Mongoose.

In any case, schemas need to be instantiated into “models” when you are ready to use them. We like to think about schemas as “blueprints” for objects, and about “models” as object factories.

Our first schema

With that said, let’s create our first entity, which we will name Entry. We will use this entity to store entries (posts) for our blog. We will create a new file at src/entries/entry.entity.ts; that way TypeORM will be able to find this entity file since earlier in our configuration we specified that entity files will follow the src/**/*.entity.ts file naming convention.

Let’s create our first schema. We...

Using the schema

As mentioned before, we will use the schema we just defined to instantiate a new data model that we will be able to use in our code. Mongoose models are the ones that do the heavy lifting in regards to mapping objects to database documents, and also abstract common methods for operating with the data, such as .find() and .save().

If you’ve come from the TypeORM chapter, models in Mongoose are very similar to repositories in TypeORM.

When having to connect requests to data models, the typical approach in Nest.js is building dedicated services, which serve as the “touch point” with each model, and controllers. This links the services to the requests reaching the API. We follow the data model -> service -> controller approach in the following steps.

The interface

Before we create our service and controller, we need to write a small interface for our blog entries. This is because, as mentioned before, Mongoose schemas are not TypeScript classes...

The first requests

At this point, our Nest.js API is ready to listen to requests (both GET and POST) and operate on the data stored in our MongoDB instance based on those requests. In other words, we are ready to read from and write to our database from the API.

Let’s give it a try.

We will start with a GET request to the /entries endpoint. Obviously, since we haven’t created any entries yet, we should receive an empty array as a response.

> GET /entries HTTP/1.1
> Host: localhost:3000
< HTTP/1.1 200 OK

[]

Let’s create a new entry by sending a POST request to the entries endpoint and including in the request body a JSON object that matches the shape of our previously defined EntrySchema.

> GET /entries HTTP/1.1
> Host: localhost:3000
| {
|   "title": "This is our first post",
|   "body": "Bla bla bla bla bla",
|   "image": "http://lorempixel.com/400",
|   "created_at": "2018...

Relationships

While it’s true that MongoDB is not a relational database, it’s also true that it allows “join-like” operations for retrieving two (or more) related documents at once.

Fortunately for us, Mongoose includes a layer of abstraction for these operations and allows us to set up relationships between objects in a clear, concise way. This is provided by using refs in schemas’ properties, as well as the .populate() method (that triggers something known as the “population” process; more on it later.)

Modelling relationships

Let’s go back to our blog example. Remember that so far we only had a schema that defined our blog entries. We will create a second schema that will allow us to create comments for each blog entry, and save them to the database in a way that allows us later to retrieve both a blog entry as well as the comments that belong to it, all in a single database operation.

So, first, we create a CommentSchema like...

Summary

NoSQL databases are a powerful alternative to “traditional” relational ones. MongoDB is arguably the best known of the NoSQL databases in use today, and it works with documents encoded in a JSON variant. Using a document-based database such as MongoDB allows developers to use more flexible, loosely-structured data models and can improve iteration time in a fast-moving project.

The well known Mongoose library is an adaptor for MongoDB that works in Node.js and that abstracts quite a lot of complexity when it comes to querying and saving operations.

Over this chapter we’ve covered quite some aspects of working with Mongoose and Nest.js, like:

  • How to start up a local MongoDB instance with Docker Compose.
  • How to import the @nestjs/mongoose module in our root module and connect to our MongoDb instance.
  • What are schemas and how to create one for modelling our data.
  • Setting up a pipeline that allows us to write to and read from our MongoDB database as a reaction...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Nest.js: A Progressive Node.js Framework
Published in: Nov 2019Publisher: PacktISBN-13: 9781800204737
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (5)

author image
Greg Magolan

Greg Magolan is a senior architect, full-stack engineer, and Angular consultant at Rangle. He has been developing enterprise software solutions for over 15 years for companies such as Agilent Technologies, Electronic Arts, Avigilon, Energy Transfer Partners, FunnelEnvy, Yodel and ACM Facility Safety.
Read more about Greg Magolan

author image
Patrick Housley

Patrick Housley is a lead technologist at VML. He is an IT professional with over six years of experience in the technology industry. He is capable of analyzing complex issues spanning multiple technologies while providing detailed resolutions and explanations. He has strong front-end development skills with experience leading development teams in maintenance and greenfield projects.
Read more about Patrick Housley

author image
Adrien de Peretti

Adrien de Peretti is a full-stack JavaScript developer. He is passionate about new technologies and is constantly looking for new challenges. He is especially interested in artificial intelligence and robotics. W
Read more about Adrien de Peretti

author image
Jay Bell

Jay Bell is the CTO of Trellis. He is a senior Angular developer who uses Nest.js in production to develop industry-leading software to help non-profit organizations and charities in Canada. He is a serial entrepreneur who has developed software for a large range of purposes, from helping combat wildfires with drones to building mobile apps.
Read more about Jay Bell

author image
David Guijarro

David Guijarro is a front-end developer at Car2go Group GmbH. He has a wide experience working within the JavaScript ecosystem. He has successfully built and led multicultural, multifunctional teams.
Read more about David Guijarro