Typeorm 与 postgres 的连接
我正在尝试将 typeorm 连接到无服务器框架上的 postgresql,但出现错误。
“message”:“找不到“博客”的存储库。看起来该实体未在当前“默认”连接中注册?” ormconfig
。
export class Database {
private static connection: Connection
public getConnection = async () => {
if (!Database.connection) {
Database.connection = await createConnection({
name: 'default',
type: 'postgres',
host: process.env.DB_HOST,
port: 5432,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
entities: [
'src/entities/*.ts',
],
migrations: [
"src/migrations/*.ts"
],
cli: {
entitiesDir: "src/entities",
migrationsDir: "src/migrations",
},
synchronize: true,
logging: false
})
}
}
}
博客实体
import {BaseEntity, Entity, PrimaryGeneratedColumn, Column} from "typeorm";
@Entity()
export class Blog extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'varchar', nullable: true })
public name: string;
@Column({ type: 'varchar', nullable: true })
public description: string;
@Column({ type: 'varchar', nullable: true })
public author: string;
@Column({ type: 'varchar', nullable: true })
public image: string;
@Column({ type: 'timestamp', nullable: true })
public created_at: Date;
}
lambda
export const main = middyfy(async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
const db = new Database();
db.getConnection();
try {
const blog = new Blog();
blog.name = "Timber";
blog.description = "Testing";
await blog.save();
return formatJSONResponse({
blog
})
}catch (e) {
return formatJSONResponse({
status: 500,
message: e
});
}
})
我是新手,只是想连接它,我创建了迁移和模型博客,现在我想将数据保存到其中 我做错了什么有人可以帮助我吗)
I'am trying to connect typeorm to postgresql on serverless framework but i'm getting error.
"message": "No repository for "Blog" was found. Looks like this entity is not registered in current "default" connection?"
}
ormconfig
export class Database {
private static connection: Connection
public getConnection = async () => {
if (!Database.connection) {
Database.connection = await createConnection({
name: 'default',
type: 'postgres',
host: process.env.DB_HOST,
port: 5432,
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
entities: [
'src/entities/*.ts',
],
migrations: [
"src/migrations/*.ts"
],
cli: {
entitiesDir: "src/entities",
migrationsDir: "src/migrations",
},
synchronize: true,
logging: false
})
}
}
}
blog entity
import {BaseEntity, Entity, PrimaryGeneratedColumn, Column} from "typeorm";
@Entity()
export class Blog extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'varchar', nullable: true })
public name: string;
@Column({ type: 'varchar', nullable: true })
public description: string;
@Column({ type: 'varchar', nullable: true })
public author: string;
@Column({ type: 'varchar', nullable: true })
public image: string;
@Column({ type: 'timestamp', nullable: true })
public created_at: Date;
}
lambda
export const main = middyfy(async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
const db = new Database();
db.getConnection();
try {
const blog = new Blog();
blog.name = "Timber";
blog.description = "Testing";
await blog.save();
return formatJSONResponse({
blog
})
}catch (e) {
return formatJSONResponse({
status: 500,
message: e
});
}
})
I'm new in this and just trying to connect it i created migration and model blog now i want to save data to it.
whats i'm doing wrong can someone please help me )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论