Search code examples
javascriptnode.jsexpresstypeorm

Express routing using TypeORM with Javascript


Looking at the TypeORM sites examples, there are some showing routing usage done with TypeScript. Since TypeORM can use javascript instead of TypeScript, would anyone be able to point me in the right direction on how to accomplish Express routing with javascript. TypeScript uses Controllers and builds routes using json route structure. Not really sure how to translate that to javascript as the javascript example doesnt even use controllers.

What would be JS alternative to

import {getConnection} from "typeorm";

I tried

var orm = require("typeorm"); 
var conn = orm.getConnection();

but it didnt work :(

Additionally, i wanted to create entities in javascript instead of typescript so i tried something like this :

module.exports = {
    name: "EventType",
    columns: {
        EventTypeId: {
            primary: true,
            type: "int",
            generated: true
        },
        EventTypeUUID: {
            type: "uniqueidentifier"
        },
        Title: {
            type: "varchar"
        },
        IconId: {
            type: "int"
        },
        BackgroundColor: {
            type: "varchar"
        }
      }
};

and tried getting the result in controller like this :

return connection().manager.find(EventType);

and getting thos error :

EntityMetadataNotFound: No metadata for "[object Object]" was found.

Thanks in advance.


Solution

  • TypeORM has typeorm init command which allows you to generate a new project with typeorm and express, example of usage:

    typeorm init --name my-project --express --database postgres
    

    Also, there is an example how to use it with express on TypeORM site http://typeorm.io/#/example-with-express.