Loopback 4: Modern ways to Build APIs in Typescript & NodeJs
- 5 hours on-demand video
- 1 article
- 34 downloadable resources
- Full lifetime access
- Access on mobile and TV
- Certificate of Completion
Get your team access to 4,000+ top Udemy courses anytime, anywhere.Try Udemy for Business
- Students will learn how to build REST APIs with NodeJs, Typescript, MysQL and MongoDB
- Students will be able to do authentication and authorization in Loopback 4
- Students will be able to test the REST APIs with Loopback 4
LoopBack is a highly extensible, open-source Node.js framework based on Express that enables you to quickly create APIs and microservices composed from backend systems such as databases and SOAP or REST services.
The diagram below demonstrates how LoopBack serves as a composition bridge between incoming requests and outgoing integrations. It also shows the different personas who are interested in various capabilities provided by LoopBack.
A major part of software engineering is building components that not only have well-defined and consistent APIs, but are also reusable. Components that are capable of working on the data of today as well as the data of tomorrow will give you the most flexible capabilities for building up large software systems.
In languages like C# and Java, one of the main tools in the toolbox for creating reusable components is generics, that is, being able to create a component that can work over a variety of types rather than a single one. This allows users to consume these components and use their own types.
In this lecture, I will teach you how to install and setup typescript with Node.js
There are two main ways to get the TypeScript tools:
Via npm (the Node.js package manager)
By installing TypeScript’s Visual Studio plugins
For NPM users:
> npm install -g typescript
You have to create the .ts file and run the tsc and typescript file
The primary reason you create a generic function is because you have some code that meets two criteria:
It's a function or class that will work with a variety of data types
The function or class uses that data type in several places
You have other options: When you have code that works with a variety of data types, you could write one version of the function for each data type, but that's a lot of work and a maintenance nightmare. You could also write one function and have it use the any keyword for its data types, but that would mean abandoning type safety.
Provided that the data types involved are used in several places in your code, a generic function or class is a better solution than your other options: generic functions let you write one version of your code and ensure that the code consistently uses data types (and then, of course, let the developer specify the data type when calling the function/instantiating the class).
In the above example, the type variable T is specified with the function in the angle brackets getArray<T>. The type variable T is also used to specify the type of the arguments and the return value. This means that the data type which will be specified at the time of a function call, will also be the data type of the arguments and of the return value.
Decorators are simply functions that modify a class, property, method, or method parameter. The syntax is an “@” symbol followed by a function. The @readonlydecorator is a class decorator; it is passed a reference to the constructor function and returns a new constructor function that extends its behavior.
You can make a decorator configurable by converting your decorator function into a decorator factory. A decorator factory is a function that returns a decorator function. The factory can have any number of parameters that can be used in the creation of the decorator.
Datasources are LoopBack’s way of connecting to various sources of data, such as databases, APIs, message queues and more. A DataSource in LoopBack 4 is a named configuration for a Connector instance that represents data in an external system. The Connector is used by legacy-juggler-bridge to power LoopBack 4 Repositories for Data operations.
The repository pattern is one of the more fundamental differences between LoopBack 3 and 4. In LoopBack 3, you would use the model class definitions themselves to perform CRUD operations. In LoopBack 4, the layer responsible for this has been separated from the definition of the model itself, into the repository layer.
A Repository represents a specialized Service interface that provides strong-typed data access (for example, CRUD) operations of a domain model against the underlying database or service.
In LoopBack 4, controllers handle the request-response lifecycle for your API. Each function on a controller can be addressed individually to handle an incoming request (like a POST request to /todos), to perform business logic, and to return a response.
Controller is a class that implements operations defined by application’s API. It implements an application’s business logic and acts as a bridge between the HTTP/REST API and domain/database models
A hasMany relation denotes a one-to-many connection of a model to another model through referential integrity. The referential integrity is enforced by a foreign key constraint on the target model which usually references a primary key on the source mode
A hasOne relation denotes a one-to-one connection of a model to another model through referential integrity. The referential integrity is enforced by a foreign key constraint on the target model which usually references a primary key on the source model and a unique constraint on the same column/key to ensure one-to-one mapping. This relation indicates that each instance of the declaring or source model has exactly one instance of the target model.
In the JSON Web Token (JWT) authentication approach, when the user provides the correct credentials to a login endpoint, the server creates a JWT token and returns it in the response. The token is of type string and consists of 3 parts: the header, the payload, and the signature. Each part is encrypted using a secret, and the parts are separated by a period.
Dependency injection is a technique whereby one object supplies the dependencies of another object. A "dependency" is an object that can be used, for example as a service. Before we can use methods of other classes, we first need to create the object of that class (i.e. class A needs to create an instance of class B). Dependency Injection is a set of software design principles and patterns that enables you to develop loosely coupled code.
In this lecture, You will learn how to implement dependency injection in Loopback 4. You will learn how to bind value to Loopback 4 context object. Binding represents the items in context object. Binding represents items within a Context instance. A binding connects its value to a unique key as the address to access the entry in a context.
Binding represents items within a Context instance. A binding connects its value to a unique key as the address to access the entry in a context.
Attributes of a binding
A binding typically has the following attributes:
key: Each binding has a key to uniquely identify itself within the context
scope: The scope controls how the binding value is created and cached within the context
tags: Tags are names or name/value pairs to describe or annotate a binding
value: Each binding must be configured with a type of value provider so that it be resolved to a constant or calculated value
- Basic Knowledge of Typescript is requried
- Knowledge of NodeJs is required
LoopBack is a highly-extensible, open-source Node.js framework that enables you to create dynamic end-to-end REST APIs with little or no coding. LoopBack 4 is the next step in the evolution of LoopBack. You can build Amazing APIs with Modern NodeJs, Typescript, Mysql and MongoDB.
Why Loopback 4:
A brand new LoopBack core to deliver great extensibility and flexibility written in TypeScript/ES2017.
You can Create powerful APIs easily with a new creation experience for defining REST API's and handling API requests/responses.
A new, improved programming model with Dependency Injection and new concepts such as Components, Mixins, Repositories, etc. make this the most extensible version yet.
Using OpenAPI-to-GraphQL, create a GraphQL interface for any REST API
LoopBack is a highly-extensible, open-source Node.js framework that enables you to:
Create dynamic end-to-end REST APIs with little or no coding.
Access data from major relational databases, MongoDB, SOAP and REST APIs.
Incorporate model relationships and access controls for complex APIs.
Separable components for file storage, third-party login, and OAuth 2.0.
What you will learn in this course:
Getting started with Typescript
Getting started with Loopback 4
Basic/Fundamentals of Typescript
CRUD(Create, Read, Update, Delete) with Loopback 4
Build RESTFUL API and OpenAPI documentation
Exploring multiple datasources with Loopback 4 like Mysql, MongoDB.
Working with Relationship with Models like One to One, One to Many, Many to One.
Exploring Repositories, Controllers, Dependency Injections, Design Patterns in more depth
Authentication/Authorization in Loopback 4
Using External Component in Loopback 4
Exposing GraphQL API with Loopback 4
- NodeJs developers who want to learn how to build REST APIS with Loopback 4
- Existing Loopback 3 developers who want to explore the modern way to build RESTFUL APIS with Typescript
- Typescript developers who want to learn how to build REST APIS with Typescript, NodeJs, Mysql and MongoDB