From 7b7bcf600ddf913b80f55e3a8ad529ce00d42fa3 Mon Sep 17 00:00:00 2001 From: whyour Date: Fri, 12 Nov 2021 14:22:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=87=E4=BB=B6=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E5=BC=95=E8=B5=B7=E6=9C=8D=E5=8A=A1=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/loaders/db.ts | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/back/loaders/db.ts b/back/loaders/db.ts index 13796437..e89de4f9 100644 --- a/back/loaders/db.ts +++ b/back/loaders/db.ts @@ -13,8 +13,19 @@ interface Dbs { const db: Dbs = {} as any; -async function truncateDb() { +async function fileExist(file: any) { return new Promise((resolve) => { + try { + fs.accessSync(file); + resolve(true); + } catch (error) { + resolve(false); + } + }); +} + +async function truncateDb() { + return new Promise(async (resolve) => { const files = [ config.cronDbFile, config.dependenceDbFile, @@ -24,7 +35,8 @@ async function truncateDb() { ]; for (const file of files) { - if (fs.statSync(file).size >= 1024 * 1024 * 500) { + const _fileExist = await fileExist(file); + if (_fileExist && fs.statSync(file).size >= 1024 * 1024 * 500) { fs.truncateSync(file, 1024 * 1024 * 500); } } @@ -35,11 +47,14 @@ export default async () => { try { await truncateDb(); - db.cronDb = new DataStore({ filename: config.cronDbFile }); - db.dependenceDb = new DataStore({ filename: config.dependenceDbFile }); - db.envDb = new DataStore({ filename: config.envDbFile }); - db.appDb = new DataStore({ filename: config.appDbFile }); - db.authDb = new DataStore({ filename: config.authDbFile }); + db.cronDb = new DataStore({ filename: config.cronDbFile, autoload: true }); + db.dependenceDb = new DataStore({ + filename: config.dependenceDbFile, + autoload: true, + }); + db.envDb = new DataStore({ filename: config.envDbFile, autoload: true }); + db.appDb = new DataStore({ filename: config.appDbFile, autoload: true }); + db.authDb = new DataStore({ filename: config.authDbFile, autoload: true }); // compaction data file db.cronDb.persistence.compactDatafile(); @@ -48,22 +63,6 @@ export default async () => { db.appDb.persistence.compactDatafile(); db.authDb.persistence.compactDatafile(); - db.cronDb.loadDatabase((err) => { - if (err) throw err; - }); - db.envDb.loadDatabase((err) => { - if (err) throw err; - }); - db.dependenceDb.loadDatabase((err) => { - if (err) throw err; - }); - db.appDb.loadDatabase((err) => { - if (err) throw err; - }); - db.authDb.loadDatabase((err) => { - if (err) throw err; - }); - Logger.info('✌️ DB loaded'); } catch (error) { Logger.info('✌️ DB load failed');