I am new to typescript + typeorm and got stuck into following issue for quite a long time now. I read lot of issues on github but couldn't find the problem.
My project structure
<projectname>
- src
- entity
- patient.ts
- migration
- app.ts
- server.ts
- package.json
- ormconfig.json
...
package.json
{
"name": "fir-server",
"version": "0.0.0",
"private": true,
"main": "app.js",
"scripts": {
"build": "tsc",
"dev": "ts-node ./src/server.ts",
"start": "nodemon ./dist/src/server.js",
"test": "mocha --recursive './test/*Test.js' --compilers js:babel-core/register"
},
"dependencies": {
"babel-core": "^6.25.0",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-register": "^6.26.0",
"express": "^4.15.4",
"mysql2": "^1.6.1",
"reflect-metadata": "^0.1.10",
"typeorm": "0.2.7",
...
},
"devDependencies": {
"@types/node": "^8.0.29",
"ts-node": "3.3.0",
"typescript": "2.5.2",
....
}
}
ormconfig.json
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "db_user",
"password": "db_password",
"database": "firdb",
"synchronize": false,
"migrationsRun": true,
"logging": false,
"entities": [
"dist/src/entities/*.js"
// tried this as well:
// src/entities/*.js
// src/entities/**/**.js, src/entities/*.ts, src/entities/**/**.ts
],
"migrations": [
"dist/src/migration/*.js"
],
"subscribers": [
"dist/src/subscriber/*.js"
],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
}
src/app.ts
import "reflect-metadata";
import * as express from "express";
import * as bodyParser from "body-parser";
import { Routes } from "../routes/index";
class App {
public app: express.Application;
public route: Routes = new Routes();
constructor() {
this.app = express();
this.config();
this.route.routes(this.app);
}
private config(): void {
// support application/json type post data
this.app.use(bodyParser.json());
//support application/x-www-form-urlencoded post data
this.app.use(bodyParser.urlencoded({ extended: false }));
}
}
export default new App().app;
src/server.ts
import app from "./app"
import { createConnection } from 'typeorm';
const port = 3000
createConnection().then(async (connection) => {
app.listen(port, () => {
console.log('Express server listening on port ' + port);
})
}).catch((error) => console.log(error));
patient.ts
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
@Entity()
export class Patient {
@PrimaryGeneratedColumn()
id: number;
@Column({ name: "first_name" })
firstName: string;
@Column({ name: "middle_name" })
middleName: string;
@Column({ name: "last_name" })
lastName: string;
@Column()
username: string;
@Column()
gender: string;
@Column()
phone_number: string;
@Column()
email: string;
@Column()
is_mother: boolean;
}
After I run the command:
tsc
A new dist directory is created on project root with following structure:
- dist
- src
- entity
- patient.js
- patient.js.map
- migration
I use it like this in my controller:
import { Patient } from 'src/entity/patient'
let patients = getConnection().manager.find(Patient);
The error I get:
UnhandledPromiseRejectionWarning: EntityMetadataNotFound: No metadata for "Patient" was found
"dist/src/entities/*.js"but it seams you have your file in the entity folder... either rename the folder or change the config to look for the"dist/src/entity/*.js"