簡體   English   中英

TypeORM-seeding 運行,但沒有添加新條目

[英]TypeORM-seeding runs through but no new entries are added

我正在嘗試使用 typeorm-seeding 將一些數據添加到我的數據庫中。 運行seed:run通過並說它執行我的種子,但是,數據庫中沒有添加新條目。 我的種子的路徑是正確的,有什么建議可能是錯的嗎? :)
我有以下基本的.env文件

# Database Config
IDEA_DB_HOST=localhost
IDEA_DB_PORT=5432
IDEA_DB_USER=postgres
IDEA_DB_PASS=postgres
IDEA_DB_NAME=idea_db

和以下ormconfig.js

const path = require('path'); // eslint-disable-line

module.exports = {
  host: process.env.IDEA_DB_HOST,
  username: process.env.IDEA_DB_USER,
  password: process.env.IDEA_DB_PASS,
  port: process.env.IDEA_DB_PORT,
  name: process.env.IDEA_DB_NAME,
  seeds: ['src/database/seeds/*.ts'],
  factories: ['src/database/factories/*.ts'],
  type: 'postgres',
  entities: ['src/**/entities/*.entity{.ts,.js}'],
  synchronize: true,
  logging: false,
};

我的種子很基礎

import { Factory, Seeder } from 'typeorm-seeding';
import { Connection } from 'typeorm';
import {
  Exercise,
  ExerciseType,
} from '../../exercises/entities/exercise.entity';

export default class CreateExercises implements Seeder {
  public async run(factory: Factory, connection: Connection): Promise<any> {
    await connection
      .createQueryBuilder()
      .insert()
      .into(Exercise)
      .values([
        {
          id: 1,
          createdAt: new Date(),
          updatedAt: new Date(),
          content: 'some demo content',
          solution: 'some demo content',
          labels: ['Winter'],
          exerciseType: ExerciseType.MATHE,
          skills: ['skill 1'],
          minDifficulty: 1,
          maxDifficulty: 2,
          verified: true,
          authorId: '65ca2dd2-6649-11ec-90d6-0242ac120003',
        },
      ])
      .execute();
  }
}

當我運行種子時,我得到以下 output:運行

$ ts-node ./node_modules/typeorm-seeding/dist/cli.js seed
🌱  TypeORM Seeding v1.6.1
√ ORM Config loaded
√ Factories are imported
√ Seeders are imported
√ Database connected
√ Seeder CreateExercises executed
👍  Finished Seeding
Done in 4.58s.

看來你沒有保存數據!

我的意見是這樣工作

import { Factory, Seeder } from 'typeorm-seeding';
import { Connection } from 'typeorm';
import {
  Exercise,
  ExerciseType,
} from '../../exercises/entities/exercise.entity';

export default class CreateExercises implements Seeder {
  private data = [
    {
      id: 1,
      createdAt: new Date(),
      updatedAt: new Date(),
      content: 'some demo content',
      solution: 'some demo content',
      labels: ['Winter'],
      exerciseType: ExerciseType.MATHE,
      skills: ['skill 1'],
      minDifficulty: 1,
      maxDifficulty: 2,
      verified: true,
      authorId: '65ca2dd2-6649-11ec-90d6-0242ac120003',
    },
  ];
  public async run(factory: Factory, connection: Connection): Promise<any> {
    await Promise.all(
      this.data.map((x) =>
        connection.getRepository<Exercise>('exercise').create(x).save(),
      ),
    );
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM