Typeorm 不会等待没有保存
Typeorm won't wait for no save
一直在与 typeorm 作斗争,我可能遇到了一些不利因素。
正在编写数据库启动脚本(然后开始使用数据)。有些文件有多达一百万条记录,里面有一些外键,大约有五个文件将被加载到后端以进行基本排序。
我的问题是,如何让 typeorm 在保存成功时告诉我,就好像我只是在保存时等待
await profileRepository.save(EUprofile)
我明白了
"Error:(37, 17) TS1308: 'await' expressions are only allowed within
async functions and at the top levels of modules."
但是如果我不使用它,它会在发出保存指令后立即继续执行代码。
我可以通过为每次写入打开一个新连接来解决这个问题,等待工作,但是我 运行 很快就失去了连接,但是等待工作,所以我包装了整个文件读取在:
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();
}
}
})
一直在与 typeorm 作斗争,我可能遇到了一些不利因素。
正在编写数据库启动脚本(然后开始使用数据)。有些文件有多达一百万条记录,里面有一些外键,大约有五个文件将被加载到后端以进行基本排序。
我的问题是,如何让 typeorm 在保存成功时告诉我,就好像我只是在保存时等待
await profileRepository.save(EUprofile)
我明白了
"Error:(37, 17) TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules."
但是如果我不使用它,它会在发出保存指令后立即继续执行代码。
我可以通过为每次写入打开一个新连接来解决这个问题,等待工作,但是我 运行 很快就失去了连接,但是等待工作,所以我包装了整个文件读取在:
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();
}
}
})