【发布时间】:2022-06-21 14:51:50
【问题描述】:
有没有一种简单的方法可以使用 DataSource 在 typeORM v.0.3.6 中播种数据? typeorm-seeding 似乎使用了已弃用的 Connection。
【问题讨论】:
-
您找到解决方案了吗?有同样的问题和@2Mighty 解决方案我认为不考虑数据源
标签: typeorm
有没有一种简单的方法可以使用 DataSource 在 typeORM v.0.3.6 中播种数据? typeorm-seeding 似乎使用了已弃用的 Connection。
【问题讨论】:
标签: typeorm
我使用了“typeorm-seeding”:“^1.6.1”,以下是我采取的步骤。
require('dotenv').config();
module.exports = {
type: "postgres",
host: process.env.DB_HOST,
port: 5432,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
entities: ["src/db/entities/**/*.ts"],
seeds: ['src/db/seeds/**/*{.ts,.js}'],
factories: ['src/db/factories/**/*{.ts,.js}'],
}
@Entity()
export class User {
@PrimaryGeneratedColumn("uuid")
id: string
@Column()
firstName: string
@Column()
lastName: string
@IsEmail()
@Column({
unique: true,
})
email: string
@Column()
password: string
}
import { Factory, Seeder } from 'typeorm-seeding'
import { DataSource } from 'typeorm'
import { User } from '../entities/User'
import * as bcrypt from "bcrypt"
export default class CreateUsers implements Seeder {
public async run(factory: Factory, datasource: DataSource): Promise<any> {
const salt = 10
const password = await bcrypt.hash("password", salt)
await datasource
.createQueryBuilder()
.insert()
.into(User)
.values([
{ firstName: 'Timber', lastName: 'Saw', email: "example@gmail.com", password },
{ firstName: 'Phantom', lastName: 'Lancer', email: "example@gmail.com", password},
])
.execute()
}
}
npm run seed:run 然后你的播种机就完成了您可以查看Typeorm Seeding Link了解更多信息。
【讨论】:
我刚刚解决了同样的问题。因此,您需要通过 DataSource 在配置文件中再创建一个连接(如果您已经通过 DataSource 连接,则不需要),我的 DataSource 连接如下所示:
export const MigrationAppDataSource = new DataSource({
type: "postgres",
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: ["../**/*.entity.ts"],
migrations: ["dist/migrations/*{.ts,.js}"],
synchronize: false,
});
还必须提到:您必须在两个连接中都将 synchronize 设置为 false(如果您有 2+ ofc)。
下一步是尝试创建简单的迁移。我的 package.json sn-p 用于创建一个简单的迁移:
"typeorm": "typeorm-ts-node-commonjs",
"db:createMigration": "typeorm migration:create",
npm run db:createMigration src/migrations/MigrationName
如果一切正常,那么您必须更改此迁移的时间戳,并使用我的 SQL 代码插入您需要的种子数据:
export class Seed2617378125500 implements MigrationInterface {
name = "Seed2617378125500";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`INSERT INTO package_entity (id, created_at, name, description, price) VALUES(1, '20062022', 'Creative Direction', '', '450')`,
);
await queryRunner.query(
`INSERT INTO project_type (id, created_at, name) VALUES(1, '20062022', 'Animation')`,
);
await queryRunner.query(
`INSERT INTO project_type_packages_package_entity ("projectTypeId", "packageEntityId") VALUES(1, 3)`,
);
await queryRunner.query(
`INSERT INTO project_type_packages_package_entity ("projectTypeId", "packageEntityId") VALUES(1, 4)`,
);
await queryRunner.query(
`INSERT INTO project_type_packages_package_entity ("projectTypeId", "packageEntityId") VALUES(1, 5)`,
);
await queryRunner.query(
`INSERT INTO project_type_packages_package_entity ("projectTypeId", "packageEntityId") VALUES(1, 6)`,
);
await queryRunner.query(
`INSERT INTO project_type_packages_package_entity ("projectTypeId", "packageEntityId") VALUES(1, 11)`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DELETE * FROM project_type`);
await queryRunner.query(`DELETE * FROM package_entity`);
}
}
提示:我的种子包括多对多连接,我一直在努力理解如何在多对多连接列中传递值。因此,您需要在中间表中传递值,该中间表是在连接列的初始化期间创建的。
下一步就是运行您的迁移。在我的示例中,我有 2 次迁移。生成第一个来初始化 DB itslef:
"db:migrate": "npm run build && node --require ts-node/register ./node_modules/typeorm/cli.js -d src/config/configuration.ts migration:generate",
还必须提一下:你必须在生成迁移的命令和运行迁移的命令中提供-d path/to/configWithDataSource。
当我的初始生成生成并且种子也完成时,我只需运行一个命令来运行迁移(你不需要在那里输入路径,因为它在你的 DataSource 文件中使用路径),我的:
"db:migrationRun": "npm run build && npx typeorm-ts-node-commonjs migration:run -d src/config/configuration.ts"
享受吧! 如果您有任何问题,请随时问我:)
【讨论】: