Browse Source

fix: asynchronous read/write for NcMeta and SQL managers

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/5031/head
mertmit 2 years ago
parent
commit
23ac1adeb7
  1. 10
      packages/nocodb/src/lib/db/sql-mgr/SqlMgr.ts
  2. 13
      packages/nocodb/src/lib/meta/NcMetaMgr.ts

10
packages/nocodb/src/lib/db/sql-mgr/SqlMgr.ts

@ -1,6 +1,7 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import url from 'url'; import url from 'url';
import { promisify } from 'util';
import fsExtra from 'fs-extra'; import fsExtra from 'fs-extra';
import importFresh from 'import-fresh'; import importFresh from 'import-fresh';
@ -19,6 +20,9 @@ import Result from '../util/Result';
const randomID = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz_', 20); const randomID = customAlphabet('1234567890abcdefghijklmnopqrstuvwxyz_', 20);
const log = new Debug('SqlMgr'); const log = new Debug('SqlMgr');
const writeFileAsync = promisify(fs.writeFile);
const readFileAsync = promisify(fs.readFile);
const ToolOps = { const ToolOps = {
DB_TABLE_LIST: 'tableList', DB_TABLE_LIST: 'tableList',
DB_VIEW_LIST: 'viewList', DB_VIEW_LIST: 'viewList',
@ -1098,13 +1102,13 @@ export default class SqlMgr {
public async projectChangeEnv(args) { public async projectChangeEnv(args) {
try { try {
const xcConfig = JSON.parse( const xcConfig = JSON.parse(
fs.readFileSync( await readFileAsync(
path.join(this.currentProjectFolder, 'config.xc.json'), path.join(this.currentProjectFolder, 'config.xc.json'),
'utf8' 'utf8'
) )
); );
xcConfig.workingEnv = args.env; xcConfig.workingEnv = args.env;
fs.writeFileSync( await writeFileAsync(
path.join(this.currentProjectFolder, 'config.xc.json'), path.join(this.currentProjectFolder, 'config.xc.json'),
JSON.stringify(xcConfig, null, 2) JSON.stringify(xcConfig, null, 2)
); );
@ -1357,7 +1361,7 @@ export default class SqlMgr {
break; break;
case ToolOps.WRITE_FILE: case ToolOps.WRITE_FILE:
console.log('Within WRITE_FILE handler', args); 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; break;
case ToolOps.REST_API_CALL: case ToolOps.REST_API_CALL:

13
packages/nocodb/src/lib/meta/NcMetaMgr.ts

@ -47,6 +47,9 @@ const XC_PLUGIN_DET = 'XC_PLUGIN_DET';
const NOCO_RELEASE = 'NOCO_RELEASE'; const NOCO_RELEASE = 'NOCO_RELEASE';
const writeFileAsync = promisify(fs.writeFile);
const readFileAsync = promisify(fs.readFile);
export default class NcMetaMgr { export default class NcMetaMgr {
public projectConfigs = {}; public projectConfigs = {};
public readonly pluginMgr: NcPluginMgr; public readonly pluginMgr: NcPluginMgr;
@ -439,7 +442,7 @@ export default class NcMetaMgr {
for (const tn of META_TABLES[this.config.projectType.toLowerCase()]) { for (const tn of META_TABLES[this.config.projectType.toLowerCase()]) {
if (fs.existsSync(path.join(metaFolder, `${tn}.json`))) { if (fs.existsSync(path.join(metaFolder, `${tn}.json`))) {
const data = JSON.parse( 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) { for (const row of data) {
delete row.id; delete row.id;
@ -495,7 +498,7 @@ export default class NcMetaMgr {
throw new Error('Missing project config file'); throw new Error('Missing project config file');
} }
const projectDetailsJSON: any = fs.readFileSync( const projectDetailsJSON: any = await readFileAsync(
path.join(this.config.toolDir, 'uploads', projectConfigPath), path.join(this.config.toolDir, 'uploads', projectConfigPath),
'utf8' 'utf8'
); );
@ -618,7 +621,7 @@ export default class NcMetaMgr {
if (projectConfigPath) { if (projectConfigPath) {
// read project config and extract project id // read project config and extract project id
let projectConfig: any = fs.readFileSync( let projectConfig: any = await readFileAsync(
path.join(this.config?.toolDir, projectConfigPath), path.join(this.config?.toolDir, projectConfigPath),
'utf8' 'utf8'
); );
@ -709,7 +712,7 @@ export default class NcMetaMgr {
for (const tn of META_TABLES[this.config.projectType.toLowerCase()]) { for (const tn of META_TABLES[this.config.projectType.toLowerCase()]) {
// const metaData = await client.knex(tn).select(); // const metaData = await client.knex(tn).select();
const metaData = await this.xcMeta.metaList(projectId, dbAlias, tn); const metaData = await this.xcMeta.metaList(projectId, dbAlias, tn);
fs.writeFileSync( await writeFileAsync(
path.join(metaFolder, `${tn}.json`), path.join(metaFolder, `${tn}.json`),
JSON.stringify(metaData, null, 2) JSON.stringify(metaData, null, 2)
); );
@ -720,7 +723,7 @@ export default class NcMetaMgr {
true true
); );
projectMetaData.key = this.config?.auth?.jwt?.secret; projectMetaData.key = this.config?.auth?.jwt?.secret;
fs.writeFileSync( await writeFileAsync(
path.join(metaFolder, `nc_project.json`), path.join(metaFolder, `nc_project.json`),
JSON.stringify(projectMetaData, null, 2) JSON.stringify(projectMetaData, null, 2)
); );

Loading…
Cancel
Save