如何从sql文件运行typeorm mysql命令?
我想在从sql文件迁移的过程中运行mysql命令。谁能帮帮我,怎么解决如何从sql文件运行typeorm mysql命令?,mysql,nestjs,typeorm,Mysql,Nestjs,Typeorm,我想在从sql文件迁移的过程中运行mysql命令。谁能帮帮我,怎么解决 import {MigrationInterface, QueryRunner} from "typeorm"; import * as fs from "fs"; var queries = fs.readFileSync(__dirname + '/geoinfo.sql').toString() .replace(/(\r\n|\n|\r)/gm," ") // remove newlines .re
import {MigrationInterface, QueryRunner} from "typeorm";
import * as fs from "fs";
var queries = fs.readFileSync(__dirname + '/geoinfo.sql').toString()
.replace(/(\r\n|\n|\r)/gm," ") // remove newlines
.replace(/\s+/g, ' '); // excess white space
export class HungarianStatesAndCities1564393283405 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query(queries.toString() );
}
public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query("");
}
}
从“typeorm”导入{MigrationInterface,QueryRunner};
从“fs”导入*作为fs;
var querys=fs.readFileSync(_dirname+'/geoinfo.sql').toString()
.replace(/(\r\n |\n |\r)/gm,“”//删除换行符
.替换(/\s+/g',);//多余空白
导出类Hungarianstates和Cities1564393283405实现迁移接口{
公共异步启动(queryRunner:queryRunner):承诺{
等待queryRunner.query(querys.toString());
}
公共异步关闭(queryRunner:queryRunner):承诺{
等待queryRunner.query(“”);
}
}
您可以按“;”使用拆分查询并运行单个查询
const readSqlFile = (filepath: string): string[] => {
return fs
.readFileSync(path.join(__dirname, filepath))
.toString()
.replace(/\r?\n|\r/g, '')
.split(';')
.filter((query) => query?.length);
};
public async up(queryRunner: QueryRunner): Promise<void> {
const queries = readSqlFile('/geoinfo.sql');
for (let i = 0; i < queries.length; i++) {
await queryRunner.query(queries[i]);
}
}
const readSqlFile=(文件路径:string):string[]=>{
返回fs
.readFileSync(path.join(\uu dirname,filepath))
.toString()
.替换(/\r?\n |\r/g“”)
.split(“;”)
.filter((查询)=>query?.length);
};
公共异步启动(queryRunner:queryRunner):承诺{
const querys=readSqlFile('/geoinfo.sql');
for(设i=0;i
包装到npm的库中。现在,您可以轻松地将其包含在项目中。
这似乎是一个可行的解决方案。怎么了?它不起作用了。有人有可行的解决办法吗?失败时出现语法错误,但文件中的代码没有问题。错误是什么?正如我所说,代码看起来足够正确,但是如果不知道在您尝试运行脚本时具体发生了什么,就无法提供帮助。