简体   繁体   English

带 JS 的 TypeORM

[英]TypeORM with JS

I wanted to use TypeORM with my express app which is in JS and not TS.我想将 TypeORM 与我的 express 应用程序一起使用,该应用程序在 JS 而不是 TS 中。

Firstly is this possible?首先这可能吗?

If yes, then when I try using TypeORM I run into the following error:如果是,那么当我尝试使用 TypeORM 时会遇到以下错误:

@Entity()
^

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)

My entity file - Users.js我的实体文件 - Users.js

const typeorm = require("typeorm");
const Entity = typeorm.Entity;
const Column = typeorm.Column;
const PrimaryGeneratedColumn = typeorm.PrimaryGeneratedColumn;

@Entity()
module.exports = class Users {
    @PrimaryGeneratedColumn
    id: Number;

    @Column()
    name: String;
}

What can be done to fix this?可以做些什么来解决这个问题?

Decorators like @Entity are TS feature, for js you need to use EntitySchema class instead. @Entity之类的装饰器是 TS 功能,对于 js,您需要使用EntitySchema class 代替。

More examples you can find here: https://github.com/typeorm/javascript-example .您可以在此处找到更多示例: https://github.com/typeorm/javascript-example But please keep in mind that it's not well tested in js.但请记住,它在 js 中没有经过很好的测试。

const EntitySchema = require("typeorm").EntitySchema; // import {EntitySchema} from "typeorm";
const Category = require("../model/Category").Category; // import {Category} from "../model/Category";

module.exports = new EntitySchema({
    name: "Category",
    target: Category,
    columns: {
        id: {
            primary: true,
            type: "int",
            generated: true
        },
        name: {
            type: "varchar"
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM