问题描述
曾经在打字机方面苦苦挣扎,也许我的后脚有些不适。
编写数据库启动脚本(然后开始使用数据)。其中一些文件具有多达一百万条记录,其中有一些外键,并且将在后端加载大约五个文件以对基进行排序。
我的问题是,如何使typeorm告诉我保存成功的时间,就像我刚刚等待保存一样
await profileRepository.save(EUprofile)
我明白了
“错误:(37,17)TS1308:'await'表达式仅在 异步功能和模块的顶层。”
但是,如果我不使用它,则在发出保存指令后,它将继续执行代码。
我可以通过为每次写入打开一个新的连接来解决此问题,等待工作,但是随后我很快就用尽了连接,但是等待工作了,所以我将整个文件包装在其中:
createConnection().then(async connection => {
有效,但是不能使用await,所以我不知道什么时候完成加载。
完整脚本:
import "reflect-metadata";
import {createConnection} from "typeorm";
import * as csvParser from "csv-parser";
import * as fs from "fs";
// Lets load the profiles
import {EUProfile} from "./config/EUProfile.entity";
import {EUUser} from "./config/EUUsers.entity";
// Lets open up the database
createConnection().then(async connection => {
// Lets set the standard input files
const profileFS:string = "./dbinitdata/ProfileIdName.csv";
const usersFS:string = "./dbinitdata/UserProfiles.csv";
var loadsRunning:number = 1;
let readCount:number = 0;
let saveCount:number = 0;
let readStartDate:number = new Date().getTime();
console.log("PROFILE: Starting load:" + profileFS + " at " + new Date(readStartDate));
fs.createReadStream(profileFS)
.pipe(csvParser())
.on('data',(data) => {
// Lets map it
const EUprofile = new EUProfile();
EUprofile.id = data.ID;
EUprofile.profileName = data.NAME
readCount++;
const profileRepository = connection.getRepository(EUProfile);
await profileRepository.save(EUprofile)
saveCount++;
if (saveCount == readCount ){
var commitTime:number = new Date().getTime()
console.log("PROFILE: Records commited to database: " + saveCount +" in " + new Date(commitTime - readStartDate).getMilliseconds() + " MS");
loadsRunning--;
if (loadsRunning == 0){
//console.log("Completed database init at " + new Date());
//process.exit();
}
}
})
.on('end',() => {
var readTime:number = new Date().getTime()
console.log("PROFILE: Records Read from file: "+ readCount + " in " + new Date(readTime - readStartDate).getMilliseconds() + " MS");
});
}).catch(error => console.log(error));
数据库配置(
{
"name": "default","type": "mysql","host": "localhost","port": 3306,"username": "node","password": "NOTGONNATELLYOU","database": "eu_stats","synchronize": true,"logging": false,"entities": [
"./config/*.entity.js"
],"migrations": [
"config/migration/**/*.ts"
],"subscribers": [
"config/subscriber/**/*.ts"
]
}
P.S。应该承认,这是我第一次尝试用Typescript编写节点应用程序,如果我缺少明显的东西,对不起:)
解决方法
您需要使用async
使箭头功能异步。
更改此行:
.on('data',(data) => {
对此:
.on('data',async (data) => {
,
安德鲁·亚瑟斯(Andrew Arthurs)对数据进行异步处理的响应似乎很好,并且两者都给出了相同的结果。
询问(您知道,第十五个Google)后找到了第二个选择
您可以使用connection.manager保存功能和.then
// await profileRepository.save(EUprofile)
connection.manager.save(EUprofile).then(EUprofile => {
saveCount++;
if (saveCount == readCount ){
var commitTime:number = new Date().getTime()
console.log("PROFILE: Records commited to database: " + saveCount +" in " + new Date(commitTime - readStartDate).getMilliseconds() + " MS");
loadsRunning--;
if (loadsRunning == 0){
console.log("Completed database init at " + new Date());
process.exit();
}
}
})