TypeORM连接失败,没有任何错误消息

时间:2020-09-06 19:17:38

标签: javascript node.js postgresql typeorm

我正在尝试使用TypeORM,但是无法使createConnection正常工作。我运行默认的tpyeorm init服务器文件,但未显示错误或日志记录,也未更新postgres数据库。

index.ts

import "reflect-metadata";
import {createConnection, ConnectionOptions} from "typeorm";
import {User} from "./entity/User";
import * as ormconfig from '../ormconfig.json';

console.log("is the file called correctly?");

createConnection(ormconfig as ConnectionOptions).then(async connection => {//ive also tried removing this 'async' keyword, but that doesnt help

    console.log("Inserting a new user into the database...");
    const user = new User();
    user.firstName = "Timber";
    user.lastName = "Saw";
    user.age = 25;
    await connection.manager.save(user);
    console.log("Saved a new user with id: " + user.id);

    console.log("Loading users from the database...");
    const users = await connection.manager.find(User);
    console.log("Loaded users: ", users);

    console.log("Here you can setup and run express/koa/any other framework.");

}).catch(error => console.log(error));

console.log('is this code reached?')

ormconfig.json(注意:我将postgres密码更改为“ root”)

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

运行开始会给出以下输出:

> mhfit-server@0.0.1 start /home/james/Documents/mhfit/mhfit-server
> ts-node src/index.ts

is the file called correctly?
is this code reached?

请注意,其他任何日志记录语句均未命中。这是为什么?都不会显示错误。

我的本​​地计算机上的postgres数据库:

User$ sudo -i -u postgres
'postgres@user:~$ psql
postgres=# \list
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 mhfit     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

尝试启动服务器后未创建默认的User

postgres-# \c mhfit
You are now connected to database "mhfit" as user "postgres".
mhfit-# \dt
Did not find any relations.

Update

我遵循并从this tutorial下载了代码,并且神奇地起作用了。但是不幸的是,我的旧项目仍然失败了。

最终,我尝试弄乱我的模型,发现失败归因于TypeORM不喜欢我如何设置某些模型。我有一个模型A,它可以有两个模型B数组(B在事件A之前存在,而B在事件A之后存在)。

在TypeORM中,我在A上将其设置为2个OneToMany关系,在B类上将其设置为2个ManyToOne关系。虽然如此,但使TypeORM崩溃了。正确的做法是什么?

A:

...
@OneToMany(type => B, preB => preB.preA)
preBs: B[];
@OneToMany(type => B, postB => postB.postA)
postEmotions: B[];

B:

...
@ManyToOne(type => A, preA => preA.preBs)
preA: A;
@ManyToOne(type => A, postA => postA.postBs)
postA: A;

1 个答案:

答案 0 :(得分:0)

在更新中,我说由于实体设置问题而无法启动。实体A在事件之前和之后可以有多个B。我最终通过这样设置实体来使其正常工作:

//added this class just for fun
@InterfaceType()//TypeGraphQL stuff
export abstract class ParentEntity extends BaseEntity {
    @Field(type => ID)//TypeGraphQL stuff
    @PrimaryGeneratedColumn() public id: number;

    @Field()
    @CreateDateColumn() public createdAt: Date;

    @Field()
    @UpdateDateColumn() public updatedAt: Date;
}


@InterfaceType({ implements: ParentEntity }) //TypeGraphQL stuff
export abstract class B extends ParentEntity { //Notice this is an abstract class now
    @Field()//TypeGraphQL stuff
    @Column({
        type: "varchar",
        length: 100,
        nullable: true
    })
    myField: string;
}

@Entity()
@ObjectType({ implements: B })//TypeGraphQL stuff
export class PostB extends B {
    @ManyToOne(type => A, postB => postB.A)
    myA: A;
}


@Entity()
@ObjectType({ implements: B })
export class PreB extends B {
    @ManyToOne(type => A, PreB => PreB.A)
    myA: A;
}


@Entity()
@ObjectType({ implements: ParentEntity })
export class A extends ParentEntity {
    @OneToMany(type => PreB, bPre => bPre.myA)
    preBs: PreB[];
    @OneToMany(type => PostB, bPost => bPost.myA)
    postBs: PostB[];
}

我还必须更新TypeORM软件包...

编辑:复制实体会导致创建解析器,播种器以及在前端查询时使工作加倍。一种更简单的方法是在实体上添加“类型”枚举字段/列。