【问题标题】:Is it possible to seed data only not exists with typeorm-seeding?是否可以仅使用 typeorm-seeding 播种不存在的数据?
【发布时间】:2022-10-18 20:58:45
【问题描述】:

在 ormconfig.ts 中,它可以设置seeds 指向所有文件的路径。

  {
    ...
    seeds: ['db/seed/*.ts'],
  },

是否可以只运行一个带有类似选项的文件

$ npm run seed:run -f db/seed/file1.ts

换句话说,如果数据已经播种到数据库中,再次运行它将创建重复数据。如何避免?

【问题讨论】:

    标签: typeorm seeding


    【解决方案1】:

    我刚遇到这个。我所做的是首先检查数据是否存在,如果不存在,则将updates 推送到数组中。然后使用updates 调用插入命令。

    await connection
          .createQueryBuilder()
          .insert()
          .into(TableName)
          .values(updates)
          .execute()
    

    【讨论】:

      猜你喜欢
      • 2017-02-28
      • 2018-01-29
      • 1970-01-01
      • 2018-12-14
      • 2022-06-21
      • 2020-10-22
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      相关资源
      最近更新 更多