From 23ac1adeb74d66db50a2927d6ec7efed4eadc5f2 Mon Sep 17 00:00:00 2001 From: mertmit Date: Sat, 4 Feb 2023 15:08:40 +0300 Subject: [PATCH] fix: asynchronous read/write for NcMeta and SQL managers Signed-off-by: mertmit --- packages/nocodb/src/lib/db/sql-mgr/SqlMgr.ts | 10 +++++++--- packages/nocodb/src/lib/meta/NcMetaMgr.ts | 13 ++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/nocodb/src/lib/db/sql-mgr/SqlMgr.ts b/packages/nocodb/src/lib/db/sql-mgr/SqlMgr.ts index eb51c6d135..6fd4a3990f 100644 --- a/packages/nocodb/src/lib/db/sql-mgr/SqlMgr.ts +++ b/packages/nocodb/src/lib/db/sql-mgr/SqlMgr.ts @@ -1,6 +1,7 @@ import fs from 'fs'; import path from 'path'; import url from 'url'; +import { promisify } from 'util'; import fsExtra from 'fs-extra'; import importFresh from 'import-fresh'; @@ -19,6 +20,9 @@ import Result from '../util/Result'; const randomID = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz_', 20); const log = new Debug('SqlMgr'); +const writeFileAsync = promisify(fs.writeFile); +const readFileAsync = promisify(fs.readFile); + const ToolOps = { DB_TABLE_LIST: 'tableList', DB_VIEW_LIST: 'viewList', @@ -1098,13 +1102,13 @@ export default class SqlMgr { public async projectChangeEnv(args) { try { const xcConfig = JSON.parse( - fs.readFileSync( + await readFileAsync( path.join(this.currentProjectFolder, 'config.xc.json'), 'utf8' ) ); xcConfig.workingEnv = args.env; - fs.writeFileSync( + await writeFileAsync( path.join(this.currentProjectFolder, 'config.xc.json'), JSON.stringify(xcConfig, null, 2) ); @@ -1357,7 +1361,7 @@ export default class SqlMgr { break; case ToolOps.WRITE_FILE: console.log('Within WRITE_FILE handler', args); - result = fs.writeFileSync(args.args.path, args.args.data); + result = await writeFileAsync(args.args.path, args.args.data); break; case ToolOps.REST_API_CALL: diff --git a/packages/nocodb/src/lib/meta/NcMetaMgr.ts b/packages/nocodb/src/lib/meta/NcMetaMgr.ts index 1380d2ab6d..524a591fed 100644 --- a/packages/nocodb/src/lib/meta/NcMetaMgr.ts +++ b/packages/nocodb/src/lib/meta/NcMetaMgr.ts @@ -47,6 +47,9 @@ const XC_PLUGIN_DET = 'XC_PLUGIN_DET'; const NOCO_RELEASE = 'NOCO_RELEASE'; +const writeFileAsync = promisify(fs.writeFile); +const readFileAsync = promisify(fs.readFile); + export default class NcMetaMgr { public projectConfigs = {}; public readonly pluginMgr: NcPluginMgr; @@ -439,7 +442,7 @@ export default class NcMetaMgr { for (const tn of META_TABLES[this.config.projectType.toLowerCase()]) { if (fs.existsSync(path.join(metaFolder, `${tn}.json`))) { const data = JSON.parse( - fs.readFileSync(path.join(metaFolder, `${tn}.json`), 'utf8') + await readFileAsync(path.join(metaFolder, `${tn}.json`), 'utf8') ); for (const row of data) { delete row.id; @@ -495,7 +498,7 @@ export default class NcMetaMgr { throw new Error('Missing project config file'); } - const projectDetailsJSON: any = fs.readFileSync( + const projectDetailsJSON: any = await readFileAsync( path.join(this.config.toolDir, 'uploads', projectConfigPath), 'utf8' ); @@ -618,7 +621,7 @@ export default class NcMetaMgr { if (projectConfigPath) { // read project config and extract project id - let projectConfig: any = fs.readFileSync( + let projectConfig: any = await readFileAsync( path.join(this.config?.toolDir, projectConfigPath), 'utf8' ); @@ -709,7 +712,7 @@ export default class NcMetaMgr { for (const tn of META_TABLES[this.config.projectType.toLowerCase()]) { // const metaData = await client.knex(tn).select(); const metaData = await this.xcMeta.metaList(projectId, dbAlias, tn); - fs.writeFileSync( + await writeFileAsync( path.join(metaFolder, `${tn}.json`), JSON.stringify(metaData, null, 2) ); @@ -720,7 +723,7 @@ export default class NcMetaMgr { true ); projectMetaData.key = this.config?.auth?.jwt?.secret; - fs.writeFileSync( + await writeFileAsync( path.join(metaFolder, `nc_project.json`), JSON.stringify(projectMetaData, null, 2) );