Setup TypeORM

Setup TypeORM

Here we'll set up TypeORM on our server and get it running.

The Object Relational Mapper - TypeORM - is a framework built on top of Node.js and TypeScript. High-level programming structures, such as classes, decorators, and functions, can be utilized in place of traditional SQL commands to create and query databases.

Getting back to your terminal. If you're already inside the backend's project, you can begin by running the following commands to get things going:

npm install typeorm reflect-metadata mysql 

The TypeScript decorators needed by TypeORM can only be supported with the help of the reflect-metadata package.

Then, apply the following modifications to the tsconfig.json file:

{ 
  "compilerOptions": { 
    "target": "es6", 
    "module": "commonjs", 
    "rootDir": "./src", 
    "outDir": "./dist", 
    "esModuleInterop": true, 
    "strict": true, 
    "experimentalDecorators": true, 
    "emitDecoratorMetadata": true, 
    "strictPropertyInitialization": false 
  } 
} 

The emitDecoratorMetadata and experimentalDecorators properties were added, and they were both set to true. strictPropertyInitialization was also added, and its value was set to false.

The experimentalDecorators attribute offers experimental support for decorators, which are presently an experimental feature. The compiler is instructed to emit the metadata required for decorated declarations by the emitDecoratorMetadata property. We must import the reflect-metadata package into our TypeScript code in order to generate decorator metadata.

The strictPropertyInitialization attribute tells the compiler whether to enable or disable strict property initialization checks in classes.

Then, after opening the src/index.ts file, start editing the imports as follows:

import express, { Application } from 'express'; 
import { ApolloServer } from 'apollo-server-express'; 
import schema from './graphql/schema'; 
import 'reflect-metadata'; 
import { createConnection, Connection } from 'typeorm'; 

async function startApolloServer() { 

  const PORT = 8080; 
  const app: Application = express(); 
  const server : ApolloServer = new ApolloServer({schema}); 
  await server.start(); 

  server.applyMiddleware({ 
    app, 
    path: '/graphql'
  }); 

  app.listen(PORT, () => { 
    console.log(`Server is running at http://localhost:${PORT}`); 
  }); 
} 

const connection: Promise<Connection> = createConnection(); 
connection.then(() => { 
  startApolloServer(); 
}).catch(error => console.log("Database connection error: ", error)); 

We imported the reflect-metadata library to support the decorators. Following that, we eliminated a number of symbols, including casual, which was no longer needed in this phase before importing the createConnection method from the typeorm package.

A useful library that expands the functionality of the TypeScript decorators is the reflect-metadata package. This is necessary for TypeORM to operate properly. After that, the mocks object was deleted, and the mocks option was eliminated from the line that creates the Apollo Server. We won't be generating mock resolvers in this section, therefore we don't need them. Invoking the then() method of the returned promise after creating a TypeORM connection using the createConnection function. We then called the code to create the Express and Apollo Server in the then() method's body. The server will only launch as a result of a successful connection to the MySQL database thanks to this security measure. A database connection error message with the specifics of the error will be printed to the console if a problem arises.

Next, run the following command to create TypeORM configuration file:

touch ormconfig.json 

In the ormconfig.json file add the following contents:

{ 

   "type": "mysql", 
   "host": "localhost", 
   "port": 3306, 
   "username": "dbuser", 
   "password": "p4ssw0rd", 
   "database": "communitydb", 
   "synchronize": true, 
   "logging": false, 
   "entities": [ 
      "src/models/**/*.ts" 
   ], 
   "migrations": [ 
      "src/migration/**/*.ts" 
   ], 
   "subscribers": [ 
    "src/subscriber/**/*.ts" 
   ] 
} 

The database connection setup settings, including the database type, the database host and port, the database name, the username, and the password, are added to the TypeORM configuration file.

After that, assuming everything is configured correctly, use the following command to start the development server:

npm start

The creation of the database entities for our community application will next be covered.


  • Date: