From 7a5c6c7371f0936d986af8b1765b2b6b51007283 Mon Sep 17 00:00:00 2001 From: mertmit Date: Wed, 24 Aug 2022 13:45:47 +0300 Subject: [PATCH] feat: new api backend for converting connection url to config Signed-off-by: mertmit --- packages/nocodb-sdk/src/lib/Api.ts | 16 ++++++++++++++++ packages/nocodb/src/lib/meta/api/utilApis.ts | 14 +++++++++++++- .../nocodb/src/lib/utils/NcConfigFactory.ts | 15 +++++++++++++-- scripts/sdk/swagger.json | 18 ++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/packages/nocodb-sdk/src/lib/Api.ts b/packages/nocodb-sdk/src/lib/Api.ts index 08c38fdef7..f4fd0461da 100644 --- a/packages/nocodb-sdk/src/lib/Api.ts +++ b/packages/nocodb-sdk/src/lib/Api.ts @@ -3210,6 +3210,22 @@ export class Api< ...params, }), + /** + * No description + * + * @tags Utils + * @name UrlToConfig + * @request POST:/api/v1/url_to_config + */ + urlToConfig: (data: any, params: RequestParams = {}) => + this.request({ + path: `/api/v1/url_to_config`, + method: 'POST', + body: data, + type: ContentType.Json, + ...params, + }), + /** * No description * diff --git a/packages/nocodb/src/lib/meta/api/utilApis.ts b/packages/nocodb/src/lib/meta/api/utilApis.ts index a25b8c243e..dfe70e7ec1 100644 --- a/packages/nocodb/src/lib/meta/api/utilApis.ts +++ b/packages/nocodb/src/lib/meta/api/utilApis.ts @@ -4,7 +4,7 @@ import { Request, Response } from 'express'; import { packageVersion } from 'nc-help'; import ncMetaAclMw from '../helpers/ncMetaAclMw'; import SqlMgrv2 from '../../db/sql-mgr/v2/SqlMgrv2'; -import { defaultConnectionConfig } from '../../utils/NcConfigFactory'; +import NcConfigFactory, { defaultConnectionConfig } from '../../utils/NcConfigFactory'; import User from '../../models/User'; import catchError from '../helpers/catchError'; import axios from 'axios'; @@ -147,6 +147,17 @@ export async function axiosRequestMake(req: Request, res: Response) { return await _axiosRequestMake(req, res); } +export async function urlToDbConfig(req: Request, res: Response) { + const { url } = req.body; + try { + let connectionConfig; + connectionConfig = NcConfigFactory.extractXcUrlFromJdbc(url, true); + return res.json(connectionConfig); + } catch (error) { + return res.sendStatus(500) + } +} + export default (router) => { router.post( '/api/v1/db/meta/connection/test', @@ -157,4 +168,5 @@ export default (router) => { router.get('/api/v1/version', catchError(versionInfo)); router.get('/api/v1/health', catchError(appHealth)); router.get('/api/v1/feedback_form', catchError(feedbackFormGet)); + router.post('/api/v1/url_to_config', catchError(urlToDbConfig)); }; diff --git a/packages/nocodb/src/lib/utils/NcConfigFactory.ts b/packages/nocodb/src/lib/utils/NcConfigFactory.ts index 25ce6d3488..0cdfb446c1 100644 --- a/packages/nocodb/src/lib/utils/NcConfigFactory.ts +++ b/packages/nocodb/src/lib/utils/NcConfigFactory.ts @@ -636,7 +636,7 @@ export default class NcConfigFactory implements NcConfig { } } - public static extractXcUrlFromJdbc(url: string) { + public static extractXcUrlFromJdbc(url: string, rtConfig: boolean = false) { // drop the jdbc prefix if (url.startsWith('jdbc:')) { url = url.substring(5); @@ -655,9 +655,20 @@ export default class NcConfigFactory implements NcConfig { } if (!parsedConfig?.port) parsedConfig.port = defaultClientPortMapping[parsedConfig.driver]; + + if (rtConfig) { + const { driver, ...connectionConfig } = parsedConfig; + + return { + client: driverClientMapping[driver] || driver, + connection: { + ...connectionConfig + } + } as any; + } const { driver, host, port, database, user, password, ...extra } = parsedConfig; - + const extraParams = []; for (const [key, value] of Object.entries(extra)) { diff --git a/scripts/sdk/swagger.json b/scripts/sdk/swagger.json index 85c16c1795..30a3308270 100644 --- a/scripts/sdk/swagger.json +++ b/scripts/sdk/swagger.json @@ -5204,6 +5204,24 @@ "description": "" } }, + "/api/v1/url_to_config": { + "parameters": [], + "post": { + "summary": "", + "operationId": "utils-url-to-config", + "tags": [ + "Utils" + ], + "requestBody": { + "content": { + "application/json": { + "schema": {} + } + } + }, + "description": "" + } + }, "/api/v1/db/meta/nocodb/info": { "parameters": [], "get": {