TypeORM with Loopback 3

I want to show you how to use TypeORM with Loopback3 project.

Let's start! Create a loobpack project, then we will install typeORM:

typeorm.io/#

Install the npm package:

  1. npm install typeorm --save
  2. You need to install reflect-metadata shim:
  3. npm install reflect-metadata --save

Then, in the controlles what we want to use typeORM, we need to make typescript loopback project, because typeORM use typescript.

To make this is just simple:

In the package.json replace start comand with that:

"scripts": { … "start": "ts-node server/server.ts" … }

and add:

"main": "server/server.js"

1_5qwF3w0GoHEJ-7s-CqtQIQ.png

  1. Create a "ormconfig.js" and "ormconfig.json" in the root folder of project:

2_TdmThKSGILWY8vEppf0E3w.png

Then in this file especify the folder for entyties, i create in this route:

3_KUxsNMoyvB_QcQm3_8TH7A.png

  1. In the file that we want to use typeORM, change the extesion ".js" to ".ts" and import:

import "reflect-metadata";

  • Example of entity:

4_m3KMjurX_67LSg0cqts32g.png

  • Example of controller with dinamic sql:

IMPORTANT, add entyties for dinamic use, even if it says: "ObrasEntity" is declared, but its value is never read.ts (6133)..

That all folks!

Any problem? Feel free to contact! :)