I'm writing a microservice app with node and express. I used typeorm with mysql for relational db mapping. I have followed the documentation fully and configred everything as said in it. But now the datasource is said to be undefined when I try to debug. This is my first time with typeorm library. Appreciate if anyone can help me resolve the issue. Thanks.
data-source.ts
import "reflect-metadata";
import {DataSource} from 'typeorm';
/**
* Various data source can be configured here
*/
export const MySQLDataSource = new DataSource({
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "password",
database: "ignite_tuition_node_db",
entities: ["dist/entities/*.js"],
logging: true,
synchronize: true,
entitySkipConstructor: true,
});
index.ts
import "reflect-metadata";
import express, { Application } from "express";
const expressApp = require('./express-app');
const StartServer = async() => {
// initialize the application server
const app: Application = express();
const PORT = process.env.PORT || 8000;
await expressApp(app);
// listen to the application server on PORT
app.listen(PORT, (): void => {
console.log(`Express application server running on address=> http://localhost:${PORT}`);
});
}
StartServer();
express-app.ts
/**
* Express app
*/
module.exports = async (app: Application) => {
// initialize mysql datasource
MySQLDataSource.initialize()
.then(() => console.log("MySQL Datasource has been successfully initialized"))
.catch((e) => console.error("Error initializing mysql datasource", e));
// configuring necessary properties for server
app.use(express.json());
// controller configuration
TuitionController(app);
}
Repository.ts
export function getRepository(entity: any): Repository<any> {
try {
return MySQLDataSource.getRepository(entity);
} catch (e) {
throw new TuitionServiceException(`Error occurred when finding ${entity} repository`);
}
}
when I evaluate MySQLDataSource in Repository.ts in debugger console I recive
MySQLDataSource
Uncaught ReferenceError ReferenceError: MySQLDataSource is not defined
at eval (repl:1:1)
Repository.ts
data-source