3

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your reply pleerock. Is it possible for the init command to generate new project in javascript instead of typescript? (I'm trying to use TypeORM and javascript based entities and controllers, but cannot find such example)
What would be JS alternatiove to import {getConnection} from "typeorm";?
it really depend on which javascript version we are talking about... For example import {getConnection} from "typeorm"; is a valid ES6 syntax. Another question if platform you are using supports it. I strongly recommend you to use typescript since it provides you all latest javascript features without pain. there is no generation functionality for vanilla javascript yet
alternative to import {getConnection} from "typeorm"; is var getConnection = require("typeorm").getConnection
@MladenOršolić in the meanwhile, this example was created for typeorm using vanilla javascript. also, if you're worried about es6 modules support, the equivalent expression in commonjs would be const {getConnection} = require('typeorm') (if you still have sufficient engine support for destructuring), or const getConnection = require('typeorm').getConnection (if you don't).
|
0

For appropriate use of TypeORM with JavaScript / node js, refer below url.

Usage with Javascript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.