Typeorm 与 postgres 的连接

发布于 2025-01-11 06:50:49 字数 2031 浏览 0 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文