Another JS Framework, Another Day!

Nest framework

Number of JavaScript frameworks built by the community is getting the consuming developers feel not an another one; not again. Though, not at the frequency where you see a new one every day. Probably it serves an indication of popularity of the JavaScript language!

These were my first reactions when I heard about Nest framework couple of months back (Work on this framework started early quarter of 2017[1]). It is claimed to be first of the kind for Node.js runtime built on Chrome’s V8 JavaScript engine. Nest framework is introduced to the developer community as progressive framework built for Node.js runtime.

The novelty; and truly a differentiator for this framework is – It is the first TypeScript based (and yet fully compatible with vanilla JavaScript) framework for Node.js.

The novelty; and truly a differentiator for this framework is – It attempts to solve a ubiquitous problem of application development in Node.js runtime; namely architecture of application!

Getting familiar

Best place to get familiar with a framework is the documentation provided by the framework developer themselves. There might be few frameworks which missed big time on such docs but then you have their githuburl’s :). Fortunately for Nest we have a good documentation and I would rather say amazing documentation[2].

For a developer who is consuming a new framework a major hurdle is to find the right purpose and place to put the framework in use. Documentation from the authoring developer; though a good place to start is sometimes is loaded with adjectives which are difficult to translate to purpose or scenario to put it to use. In the case of Nest these adjectives are(ii)

  1. Efficient
  2. Scalable
  3. Progressive JavaScript

If you care reading further in the philosophy section of the documentation you would notice these additional adjectives all in the same sentence(ii) –

  1. Highly testable
  2. Scalable (again)
  3. Loosely coupled
  4. Easily maintainable

These adjectives though seem to delude us from the purpose but help set expectations the quality attributes (scalable solution, developer efficiency, ease of maintenance, test-ability) of the solution we build.

Let me cease the literary analysis of the framework and quickly introduce the packages available for Nest framework; and I will return back to the literary analysis just the one interesting word “Progressive JavaScript”.

These are the official packages available by the framework collaborator / author at the time of writing this article[3]

  • @nestjs/common[4] – Most used package of Nest. It offers fundamental blocks of Nest which are further extended in the core package. Refer the package.json[5] in core to understand their relationship.
  • @nestjs/core[6] – Offers much of the strength to the framework. You will mostly consume the types here and if you intend to indulge; you can extend Nest from the types defined here.
  • @nestjs/websockets[7] – Offers the functionality of websocket and observablesocket.
  • @nestjs/microservices[8] – Offers the basic infrastructure with plumbing required to implement microservices in Nest.
  • @nestjs/passport[9] – Offered as a recipe which uses the Passport strategy to authenticate in Node.js runtime.
  • @nestjs/cqrs[10],[11] – Offered as a recipe that implements the Command Query Responsibility Segregation design pattern in Nest framework
  • @nestjs/swagger[12] – Offered as a recipe which exposes OpenAPI (swagger) features for use within Nest framework.
  • @nestjs/typeorm[13] – Offered as technique to connect with databases along with the ORM capabilities.
  • @nestjs/graphql[14] – A wrapper module to Apollo[15]graphql server. This module helps Nest classes consume graphql.
  • @nestjs/mongoose[16] – A wrapper module to Mongoose[17]
  • @nestjs/elasticsearch[18] – A wrapper module toelasticsearch[19]npm package.
  • @nestjs/ng-universal[20] – A wrapper module for the angular’s[21] universal module.

Progressive JavaScript

An interesting and perplexing term “Progressive JavaScript” kept me engaged thinking and reading about the term. Not to be confused with “Progressive Web Application” which is about adaptive features of a web application based on the capability of the browser.

[1]https://www.npmjs.com/~nestjscore

[1]https://github.com/nestjs/nest/tree/master/packages/common

[1]https://github.com/nestjs/nest/blob/master/packages/core/package.json

[1]https://github.com/nestjs/nest/tree/master/packages/core

[1]https://github.com/nestjs/nest/tree/master/packages/websockets

[1]https://github.com/nestjs/nest/tree/master/packages/microservices

[1]https://docs.nestjs.com/techniques/authentication

[1]https://docs.nestjs.com/recipes/cqrs

[1]https://github.com/kamilmysliwiec/nest-cqrs-example

[1]https://docs.nestjs.com/recipes/swagger

[1]https://docs.nestjs.com/techniques/database

[1]https://docs.nestjs.com/graphql/quick-start

[1]https://www.apollographql.com/

[1]https://docs.nestjs.com/techniques/mongodb

[1]http://mongoosejs.com/

[1]https://www.npmjs.com/package/@nestjs/elasticsearch

[1]https://www.npmjs.com/package/elasticsearch

[1]https://github.com/kamilmysliwiec/universal-nest

[1]https://github.com/angular/universal

Progressive JavaScript on the other hand could be looked upon in crude sense composability (much talked about in MicroServices) or in other words (complimentary view) as modularity.

Evan You[22] best describes the term in the context of complexity here. In the context of Nest; it is best experienced by the diverse packages available which offer bounty of a feature / architectural or design pattern. These packages are modelled as adapters and plugins (two common design patterns). Best experienced by peeking into the package.json file e.g. for @nestjs/corepackage

Programming paradigm

This is that one aspect we did not introduce ourselves while we were getting to understand the Nest framework. The framework combines

  • Object oriented programming – Being developed in TypeScript; it has to be naturally OO
  • Functional programming – Framework uses features of Closure, Promises, etc.
  • Functional reactive programming – Being built using advanced features of ES standards; framework uses Observable, async-await etc.

Knowledge of though does not contribute towards identifying the purpose but does contribute towards a well-built solution. Oh oops, I stand corrected the application of this (programming paradigm) knowledge contributes towards a well-built solution. By “application of knowledge” I mean the design of the domain / problem statement you intend to solve. Best use of the framework will be by designing the application using the same mix of OO, FP and FRP in different bounded contexts.

Getting the hands dirty

Plethora of content on the internet can help you get started. However, the critical stuff for you to get started are to have the right set of pre-requisites. Here we will do just that; take a look at the right set of pre-requisites.

There could be no better place and authority than package.json of the Nest framework to list the pre-requisite –

 

“engines”: {

 

“node”: “>= 8.9.0”

 

}

 

 

This line essentially indicates we will need Node.js version 8.9.0 and higher to use Nest framework. Depending on the operating system your box has you should download and install the Node.js version 8.9.0 or higher[23]. Choose your own style of installation whichever you feel comfortable.

Once you have completed the Node.js installation you can quickly start with Nest but with one constraint – What do you want to do? Typically, the answer will be “Hello World” program. Do not tell me that you came all this way to say “Hello Word” or “Hello Nest” or something like that.

It is highly recommended you surf the samples developed by the author of the framework itself – https://github.com/nestjs/nest/tree/master/sample

Different scenarios different sample. You should also attempt stitching two samples together to build an application of your own choice. The most elegant way to explore the existing sample is to clone the repository to your own box. Consider using git command line[24] to issue the following command

 

git clone https://github.com/nestjs/nest.git

 

 

You could then use a preferred IDE like VS Code or Atom to run and analyse the sample..

 

The Editorial Board

Teamware Solutions