Javascript TypeORM连接失败,没有任何错误消息
我正在尝试开始使用TypeORM,但无法让createConnection正常工作。我运行默认的Javascript TypeORM连接失败,没有任何错误消息,javascript,node.js,postgresql,typeorm,Javascript,Node.js,Postgresql,Typeorm,我正在尝试开始使用TypeORM,但无法让createConnection正常工作。我运行默认的tpyeorminit服务器文件,但是没有显示错误或日志记录,postgres数据库也没有更新 索引 import "reflect-metadata"; import {createConnection, ConnectionOptions} from "typeorm"; import {User} from "./entity/User"
tpyeorminit
服务器文件,但是没有显示错误或日志记录,postgres数据库也没有更新
索引
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”)
运行start会产生以下输出:
> 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.
更新
我遵循并从下载了代码,它神奇地工作了。但不幸的是,我的老项目仍然失败了
最后,我试着弄乱我的模型,发现失败是因为TypeORM不喜欢我设置一些模型的方式。我有一个模型,a
,它可以有两个模型的数组B
(B存在于事件a之前,B存在于事件a之后)
在TypeForm中,我将其设置为A类上的2个一对一关系,B类上的2个多对一关系。不过,关于这个崩溃的打字机。正确的方法是什么
A:
B:
在更新中,我说由于实体设置问题,它无法启动。实体
A
可以在事件前后有许多B
s。我最终通过如下方式设置实体使其工作:
//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[];
}
我还必须更新我的打字包
编辑:在创建解析程序、种子程序和在前端查询时,复制实体会导致工作加倍。更简单的方法是在实体上添加“Type”枚举字段/列
...
@OneToMany(type => B, preB => preB.preA)
preBs: B[];
@OneToMany(type => B, postB => postB.postA)
postEmotions: B[];
...
@ManyToOne(type => A, preA => preA.preBs)
preA: A;
@ManyToOne(type => A, postA => postA.postBs)
postA: A;
//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[];
}