I want to show you how to use TypeORM with Loopback3 project.
Let's start! Create a loobpack project, then we will install typeORM:
Install the npm package:
- npm install typeorm --save
- You need to install reflect-metadata shim:
- 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"
- Create a "ormconfig.js" and "ormconfig.json" in the root folder of project:
Then in this file especify the folder for entyties, i create in this route:
- In the file that we want to use typeORM, change the extesion ".js" to ".ts" and import:
import "reflect-metadata";
- Example of entity:
- 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! :)