Browse Source

refactor(nocodb): make it return promise

pull/5262/head
Wing-Kam Wong 2 years ago
parent
commit
32e97a417c
  1. 5
      packages/nocodb/src/lib/models/Base.ts
  2. 103
      packages/nocodb/src/lib/utils/common/NcConnectionMgrv2.ts

5
packages/nocodb/src/lib/models/Base.ts

@ -234,14 +234,15 @@ export default class Base implements BaseType {
} }
} }
public getConnectionConfig(): any { // NC_DATA_DB is not available in community version
// make it return Promise to avoid conflicts
public getConnectionConfig(): Promise<any> {
if (this.is_meta) { if (this.is_meta) {
const metaConfig = Noco.getConfig()?.meta?.db; const metaConfig = Noco.getConfig()?.meta?.db;
const config = { ...metaConfig }; const config = { ...metaConfig };
if (config.client === 'sqlite3') { if (config.client === 'sqlite3') {
config.connection = metaConfig; config.connection = metaConfig;
} }
return config; return config;
} }

103
packages/nocodb/src/lib/utils/common/NcConnectionMgrv2.ts

@ -1,10 +1,5 @@
import SqlClientFactory from '../../db/sql-client/lib/SqlClientFactory'; import SqlClientFactory from '../../db/sql-client/lib/SqlClientFactory';
import { XKnex } from '../../db/sql-data-mapper'; import { XKnex } from '../../db/sql-data-mapper';
// import { NcConfig } from '../../../interface/config';
// import fs from 'fs';
// import Knex from 'knex';
// import NcMetaIO from '../meta/NcMetaIO';
import { import {
defaultConnectionConfig, defaultConnectionConfig,
defaultConnectionOptions, defaultConnectionOptions,
@ -19,12 +14,6 @@ export default class NcConnectionMgrv2 {
}; };
} = {}; } = {};
// private static metaKnex: NcMetaIO;
//
// public static setXcMeta(ncMeta: NcMetaIO) {
// this.metaKnex = ncMeta;
// }
public static async destroyAll() { public static async destroyAll() {
for (const projectId in this.connectionRefs) { for (const projectId in this.connectionRefs) {
for (const baseId in this.connectionRefs[projectId]) { for (const baseId in this.connectionRefs[projectId]) {
@ -60,7 +49,9 @@ export default class NcConnectionMgrv2 {
} }
} }
public static get(base: Base): XKnex { // NC_DATA_DB is not available in community version
// make it return Promise<XKnex> to avoid conflicts
public static async get(base: Base): Promise<XKnex> {
if (base.is_meta) return Noco.ncMeta.knex; if (base.is_meta) return Noco.ncMeta.knex;
if (this.connectionRefs?.[base.project_id]?.[base.id]) { if (this.connectionRefs?.[base.project_id]?.[base.id]) {
@ -68,76 +59,26 @@ export default class NcConnectionMgrv2 {
} }
this.connectionRefs[base.project_id] = this.connectionRefs[base.project_id] =
this.connectionRefs?.[base.project_id] || {}; this.connectionRefs?.[base.project_id] || {};
// if (?.prefix && this.metaKnex) {
// this.connectionRefs[projectId][env][dbAlias] = this.metaKnex?.knex;
// } else {
// const connectionConfig = this.getConnectionConfig(config, env, dbAlias);
const connectionConfig = base.getConnectionConfig();
//
// if (
// connectionConfig?.connection?.ssl &&
// typeof connectionConfig?.connection?.ssl === 'object'
// ) {
// if (
// connectionConfig.connection.ssl.caFilePath &&
// !connectionConfig.connection.ssl.ca
// ) {
// connectionConfig.connection.ssl.ca = fs
// .readFileSync(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();
// }
// if (
// connectionConfig.connection.ssl.certFilePath &&
// !connectionConfig.connection.ssl.cert
// ) {
// connectionConfig.connection.ssl.cert = fs
// .readFileSync(connectionConfig.connection.ssl.certFilePath)
// .toString();
// }
// }
//
// const isSqlite = connectionConfig?.client === 'sqlite3';
//
// if (connectionConfig?.connection?.port) {
// connectionConfig.connection.port = +connectionConfig.connection.port;
// }
this.connectionRefs[base.project_id][base.id] = XKnex( const connectionConfig = await base.getConnectionConfig();
// isSqlite
// ? (connectionConfig.connection as Knex.Config)
// :
{
...defaultConnectionOptions,
...connectionConfig,
connection: {
...defaultConnectionConfig,
...connectionConfig.connection,
typeCast(_field, next) {
const res = next();
if (res instanceof Buffer) {
return [...res]
.map((v) => ('00' + v.toString(16)).slice(-2))
.join('');
}
return res;
},
},
} as any
);
// if (isSqlite) {
// this.connectionRefs[projectId][env][dbAlias]
// .raw(`PRAGMA journal_mode=WAL;`)
// .then(() => {});
// }
this.connectionRefs[base.project_id][base.id] = XKnex({
...defaultConnectionOptions,
...connectionConfig,
connection: {
...defaultConnectionConfig,
...connectionConfig.connection,
typeCast(_field, next) {
const res = next();
if (res instanceof Buffer) {
return [...res]
.map((v) => ('00' + v.toString(16)).slice(-2))
.join('');
}
return res;
},
},
} as any);
return this.connectionRefs[base.project_id][base.id]; return this.connectionRefs[base.project_id][base.id];
} }
@ -153,7 +94,7 @@ export default class NcConnectionMgrv2 {
const knex = _knex || this.get(base); const knex = _knex || this.get(base);
return SqlClientFactory.create({ return SqlClientFactory.create({
knex, knex,
...base.getConnectionConfig(), ...(await base.getConnectionConfig()),
}); });
} }
} }

Loading…
Cancel
Save