我想在 Nest 启动并连接数据库后立即加载一个表
我似乎无法为 db 找到 TypeORM 的事件 - 已连接,当我尝试时
column_norm=['Type']
df[column_norm] = df[column_norm].apply(lambda x: (x - x.min()) / (x.max() - x.min()))
我收到一个警告,现在我在一个糟糕的超时时间里解决了这个问题
getConnection
导致 const f = () => {
const con = getConnection();
if (con.isConnected) {
console.log('connected');
// this is where the table load will take place
} else {
console.error('not connected');
setTimeout(() => {
f();
}, 200);
}
};
f();
所以我想知道它周围是否有任何东西?
答案 0 :(得分:1)
您应该使用 createConnection()
而不是 getConnection()
。
getConnection()
获取已使用 createConnection()
创建的连接。
createConnection()
返回一个您可以等待的承诺,或者您可以使用 createConnection().then(...)
调用您的回调。连接成功后承诺解析,连接失败则拒绝。
请参阅 TypeOrm connection API 了解更多信息。
答案 1 :(得分:0)
以防万一其他人需要 - 答案(对我来说)在 NestJS 中 - 使用 OnApplicationBootstrap
这导致此代码在一切初始化后运行
@Injectable()
export class ConfigurationService implements OnApplicationBootstrap {
async onApplicationBootstrap() {
const all = await Configuration.find();
console.log('all?', all.length);
}
}