typeorm如何生成默认数据的migration?

export class Init1684122178932 implements MigrationInterface {
    name = 'Init1684122178932'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`CREATE TABLE \`role\` (\`id\` int NOT NULL AUTO_INCREMENT, \`roleName\` varchar(255) NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);

        // 手写插入语句
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`DROP TABLE \`role\``);
    }
}

如果要手写,有没有sql编辑器,能够生成像上面sql一样的带转义符号的语句,而不是自己全部手写

阅读 2.3k
1 个回答
import {MigrationInterface, QueryRunner} from "typeorm";

export class Init1684122178932 implements MigrationInterface {
    name = 'Init1684122178932'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`CREATE TABLE \`role\` (\`id\` int NOT NULL AUTO_INCREMENT, \`roleName\` varchar(255) NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);

        // 插入默认数据
        await queryRunner.query(`INSERT INTO \`role\` (\`roleName\`) VALUES ('Admin')`);
        await queryRunner.query(`INSERT INTO \`role\` (\`roleName\`) VALUES ('User')`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`DROP TABLE \`role\``);
    }
}
推荐问题