gpt4 book ai didi

javascript - 通过 config.json 文件注册时,导入语句会破坏 typeorm 实体

转载 作者:行者123 更新时间:2023-11-30 08:18:05 28 4
gpt4 key购买 nike

根据官方文档,我创建了小型 koa/typeorm/postgres 应用程序。当我将 createConnection 与配置一起使用时,在同一文件中导入实体时,应用程序工作正常,但 typeorm cli 找不到配置文件,因此我尝试将配置移动到“ormconfig.json”。现在我得到这个错误:

语法错误:无法在模块外使用 import 语句

看起来 typeorm 无法使用 es6 特性。

我的ormconfig.json:

{
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": ****,
"password": ****,
"database": ****,
"synchronize": true,
"entities": ["src/entity/**/*.ts"],
"migrations": ["src/migration/**/*.ts"],
"subscribers": ["src/subscriber/**/*.ts"],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
}

我的tsconfig.json:

{
"compilerOptions": {
"lib": ["es5", "es6"],
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./dist",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
},
"exclude": ["node_modules"]
}

错误文件:

import {
BaseEntity,
Column,
Entity,
PrimaryGeneratedColumn,
CreateDateColumn,
ManyToOne
} from 'typeorm';
import { IsIn, IsPositive, IsNotEmpty } from 'class-validator';

import { LOAN_TYPE } from '../consts';
import { User } from './user';

@Entity('loans')
export class Loan extends BaseEntity {
@PrimaryGeneratedColumn()
public id: number;

@CreateDateColumn({ type: 'timestamp' })
public createdAt: Date;

@Column()
@IsNotEmpty()
@IsPositive()
public amount: number;

@Column({ type: 'enum', enum: LOAN_TYPE })
@IsNotEmpty()
@IsIn(Object.values(LOAN_TYPE))
public type: LOAN_TYPE;

@Column({ default: false })
public approvalStatus: boolean;

@ManyToOne(type => User, user => user.loans)
@IsNotEmpty()
public user: User;
}

export default Loan;

最佳答案

  1. 确保在 tsconfig.json
  2. "compilerOptions" 中有 "module": "commonjs"
  3. 使用 ts-node 运行 typeorm cli:ts-node ./node_modules/typeorm/cli.js

See docs

关于javascript - 通过 config.json 文件注册时,导入语句会破坏 typeorm 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58529950/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com