Browse Source

fix: asynchronous ssl config read

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/5031/head
mertmit 2 years ago
parent
commit
2e5ec1ed18
  1. 2
      packages/nocodb/src/lib/db/sql-mgr/SqlMgr.ts
  2. 8
      packages/nocodb/src/lib/utils/common/BaseApiBuilder.ts
  3. 25
      packages/nocodb/src/lib/utils/common/NcConnectionMgr.ts
  4. 14
      packages/nocodb/src/lib/utils/common/XcProcedure.ts
  5. 2
      packages/nocodb/src/lib/v1-legacy/gql/GqlApiBuilder.ts
  6. 2
      packages/nocodb/src/lib/v1-legacy/rest/RestApiBuilder.ts

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

@ -299,7 +299,7 @@ export default class SqlMgr {
this.currentProjectConnections[connectionKey] =
SqlClientFactory.create({
...connectionConfig,
knex: NcConnectionMgr.get({
knex: await NcConnectionMgr.get({
dbAlias: this.currentProjectJson.envs[env].db[i].meta.dbAlias,
env: env,
config: args,

8
packages/nocodb/src/lib/utils/common/BaseApiBuilder.ts

@ -1291,8 +1291,8 @@ export default abstract class BaseApiBuilder<T extends Noco>
this.models[viewName] = this.getBaseModel(newMeta);
}
public getDbDriver(): XKnex {
this.initDbDriver();
public async getDbDriver(): Promise<XKnex> {
await this.initDbDriver();
return this.dbDriver;
}
@ -1669,8 +1669,8 @@ export default abstract class BaseApiBuilder<T extends Noco>
await this.cronJob.init();
}
protected initDbDriver(): void {
this.dbDriver = NcConnectionMgr.get({
protected async initDbDriver(): Promise<void> {
this.dbDriver = await NcConnectionMgr.get({
dbAlias: this.dbAlias,
env: this.config.env,
config: this.config,

25
packages/nocodb/src/lib/utils/common/NcConnectionMgr.ts

@ -2,11 +2,14 @@ import SqlClientFactory from '../../db/sql-client/lib/SqlClientFactory';
import { XKnex } from '../../db/sql-data-mapper';
import { NcConfig } from '../../../interface/config';
import fs from 'fs';
import { promisify } from 'util';
import { Knex } from 'knex';
import NcMetaIO from '../../meta/NcMetaIO';
import { defaultConnectionConfig } from '../NcConfigFactory';
const readFileAsync = promisify(fs.readFile);
export default class NcConnectionMgr {
private static connectionRefs: {
[projectId: string]: {
@ -43,7 +46,7 @@ export default class NcConnectionMgr {
}
}
public static get({
public static async get({
dbAlias = 'db',
env = '_noco',
config,
@ -53,7 +56,7 @@ export default class NcConnectionMgr {
env: string;
config: NcConfig;
projectId: string;
}): XKnex {
}): Promise<XKnex> {
if (this.connectionRefs?.[projectId]?.[env]?.[dbAlias]) {
return this.connectionRefs?.[projectId]?.[env]?.[dbAlias];
}
@ -73,25 +76,25 @@ export default class NcConnectionMgr {
connectionConfig.connection.ssl.caFilePath &&
!connectionConfig.connection.ssl.ca
) {
connectionConfig.connection.ssl.ca = fs
.readFileSync(connectionConfig.connection.ssl.caFilePath)
.toString();
connectionConfig.connection.ssl.ca = await readFileAsync(
connectionConfig.connection.ssl.caFilePath
).toString();
}
if (
connectionConfig.connection.ssl.keyFilePath &&
!connectionConfig.connection.ssl.key
) {
connectionConfig.connection.ssl.key = fs
.readFileSync(connectionConfig.connection.ssl.keyFilePath)
.toString();
connectionConfig.connection.ssl.key = await readFileAsync(
connectionConfig.connection.ssl.keyFilePath
).toString();
}
if (
connectionConfig.connection.ssl.certFilePath &&
!connectionConfig.connection.ssl.cert
) {
connectionConfig.connection.ssl.cert = fs
.readFileSync(connectionConfig.connection.ssl.certFilePath)
.toString();
connectionConfig.connection.ssl.cert = await readFileAsync(
connectionConfig.connection.ssl.certFilePath
).toString();
}
}

14
packages/nocodb/src/lib/utils/common/XcProcedure.ts

@ -10,8 +10,8 @@ export default class XcProcedure {
public async callFunction(name: string, args: any[]) {
try {
if (this.builder.getDbType() === 'mssql') {
const result = await this.builder
.getDbDriver()
const result = await (await this.builder
.getDbDriver())
.raw(
`select dbo.??(${new Array(args.length)
.fill('?')
@ -20,8 +20,8 @@ export default class XcProcedure {
);
return result[0];
} else {
const result = await this.builder
.getDbDriver()
const result = await (await this.builder
.getDbDriver())
.raw(
`select ??(${new Array(args.length).fill('?').join(',')}) as ??`,
[name, ...args, name]
@ -65,7 +65,7 @@ export default class XcProcedure {
) {
const knexRef = args.reduce(
(knex, val, i) => knex.raw(`SET @var${i}=?`, [val]),
this.builder.getDbDriver().schema
(await this.builder.getDbDriver()).schema
);
const count = args.length;
const result = await knexRef.raw(
@ -76,8 +76,8 @@ export default class XcProcedure {
);
return [result[count][0][0]];
} else if (this.builder.getDbType() === 'pg') {
const result = await this.builder
.getDbDriver()
const result = await (await this.builder
.getDbDriver())
.raw(`Call ??(${new Array(args.length).fill('?').join(',')})`, [
name,
...args,

2
packages/nocodb/src/lib/v1-legacy/gql/GqlApiBuilder.ts

@ -249,7 +249,7 @@ export class GqlApiBuilder extends BaseApiBuilder<Noco> implements XcMetaMgr {
const t = process.hrtime();
this.initDbDriver();
await this.initDbDriver();
// todo: change condition
if (this.connectionConfig.meta.reset) {

2
packages/nocodb/src/lib/v1-legacy/rest/RestApiBuilder.ts

@ -86,7 +86,7 @@ export class RestApiBuilder extends BaseApiBuilder<Noco> {
this.log('loadRoutes');
const t = process.hrtime();
this.initDbDriver();
await this.initDbDriver();
// todo: change condition
if (this.connectionConfig.meta.reset) {

Loading…
Cancel
Save