TypeORM with JavaScript



The default language supported by TypeORM is TypeScript. Since, TypeScript supports static typing, classes and decorators, it is easy to define the entity and its attributes. At the same time, JavaScript is necessary as well in certain project where the preferred language is JavaScript. TypeORM provides full support for JavaScript language as well. TypeORM support both es5 and es6 flavors of JavaScript.

In this chapter, let us learn how to write TypeORM application in JavaScript ES5 (ECMAScript 5).

Open a command prompt and go to your workspace.

cd /path/to/workspace/

Run below command to create a TypeORM project.

typeorm init --name typeorm-javascript-student-app --database mysql

Open package.json file to remove the typescipt references.

original

{ 
   "name": "typeorm-javascript-student-app", "version": "0.0.1", 
   "description": "Awesome project developed with TypeORM.", "devDependencies": { 
      "ts-node": "3.3.0", "@types/node": "^8.0.29", "typescript": "3.3.3333" 
   }, 
   "dependencies": { 
      "typeorm": "0.2.24", "reflect-metadata": "^0.1.10", "mysql": "^2.14.1" 
   }, 
   "scripts": { 
      "start": "ts-node src/index.ts" } 
}

updated

{ 
   "name": "typeorm-javascript-student-app", "version": "0.0.1", 
   "description": "Awesome project developed with TypeORM.", "dependencies": { 
      "typeorm": "0.2.24",
      "mysql": "^2.14.1" 
   }, 
   "scripts": { 
   "start": "node src/index.js" 
   } 
}

Here,

  • Removed the devDependencies section and typescript related package in dependences section.
  • Changed the start script to point javascript code instead of typescript code.

Run below command to install necessary packages.

npm install

Remove tsconfig.json and index.ts file.

Remove User.ts file inside entity folder and then create student entity in json format, student.json as specified below −

{ 
   "name": "Student", 
   "columns": { 
      "id": { 
         "primary": true, 
         "type": "int", 
         "generated": true 
      }, 
      "name": { 
         "type": "varchar" 
      }, 
      "age": { 
         "type": "integer" 
      } 
   } 
}

Create a new file, src/index.js and put the below code −

var typeorm = require("typeorm"); var EntitySchema = typeorm.EntitySchema; 

typeorm.createConnection({ 
   "type": "mysql", 
   "host": "localhost", 
   "port": 3306, 
   "username": "root", 
   "password": "123456", 
   "database": "typeorm_test_db",
   "synchronize": true, 
   "logging": false, 
   entities: [ new EntitySchema(require("./entity/student.json")) 
   ] 
}) 
.then(function(connection) { 
   return connection.getRepository("Student"); }) .then(function(studentRepository) { 
   var student = { 
      name: "Student1", 
      age: 18 
   }; 
   return studentRepository.save(student) .then(function(savedStudent) { console.log("Student has been successfully saved: ", savedStudent); 
   return studentRepository.find(); }) 
   .then(function(students) { console.log("All students: ", students); 
   return; 
   }) 
   .catch(function(error) { console.log("Error: ", error); return; 
   }) 
}) 
.catch(function(error) { console.log("Error: ", error) 
   return; });

Here,

We have used the same typeORM methods except the below mentioned changes,

  • Used EntitySchema to configure the student entity.
  • Used JavaScript Promise concept (then / catch / finally) blocks.

Now, run the application using below command −

npm start

The application inserts the student information into the database and then fetch all student in the database and show it in the console as shown below −

> typeorm-javascript-student-app@0.0.1 start /path/to/workspace/typeorm-javascript-student-app

> node src/index.js 

Student has been successfully saved: { name: 'Student1', age: 18, id: 1 } All students: [ { id: 1, name: 'Student1', age: 18 } ]
Advertisements