From df074c60048fdcc565194ad803ce1ad7c0838e7b Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 3 Aug 2022 18:20:07 +0100 Subject: [PATCH] Fixing test cases. --- .../server/src/api/controllers/row/utils.js | 6 ++---- .../src/api/controllers/table/external.js | 2 +- packages/server/src/integrations/base/query.ts | 17 +++++++++++++++++ packages/server/src/integrations/base/sql.ts | 1 - packages/server/src/integrations/base/utils.ts | 18 ------------------ .../src/integrations/microsoftSqlServer.ts | 5 +++-- packages/server/src/integrations/mysql.ts | 2 +- packages/server/src/integrations/postgres.ts | 3 ++- .../server/src/integrations/tests/sql.spec.js | 2 +- 9 files changed, 27 insertions(+), 29 deletions(-) create mode 100644 packages/server/src/integrations/base/query.ts diff --git a/packages/server/src/api/controllers/row/utils.js b/packages/server/src/api/controllers/row/utils.js index da14020757..4c837e7630 100644 --- a/packages/server/src/api/controllers/row/utils.js +++ b/packages/server/src/api/controllers/row/utils.js @@ -4,10 +4,8 @@ const { InternalTables } = require("../../../db/utils") const userController = require("../user") const { FieldTypes } = require("../../../constants") const { getAppDB } = require("@budibase/backend-core/context") -const { - makeExternalQuery, - removeKeyNumbering, -} = require("../../../integrations/base/utils") +const { makeExternalQuery } = require("../../../integrations/base/query") +const { removeKeyNumbering } = require("../../../integrations/base/utils") validateJs.extend(validateJs.validators.datetime, { parse: function (value) { diff --git a/packages/server/src/api/controllers/table/external.js b/packages/server/src/api/controllers/table/external.js index 34319c5bff..d919e9dad7 100644 --- a/packages/server/src/api/controllers/table/external.js +++ b/packages/server/src/api/controllers/table/external.js @@ -14,7 +14,7 @@ const { FieldTypes, RelationshipTypes, } = require("../../../constants") -const { makeExternalQuery } = require("../../../integrations/base/utils") +const { makeExternalQuery } = require("../../../integrations/base/query") const { cloneDeep } = require("lodash/fp") const csvParser = require("../../../utilities/csvParser") const { handleRequest } = require("../row/external") diff --git a/packages/server/src/integrations/base/query.ts b/packages/server/src/integrations/base/query.ts new file mode 100644 index 0000000000..59d67b2ef1 --- /dev/null +++ b/packages/server/src/integrations/base/query.ts @@ -0,0 +1,17 @@ +import { QueryJson } from "../../definitions/datasource" +import { Datasource } from "../../definitions/common" +const { integrations } = require("../index") + +export async function makeExternalQuery( + datasource: Datasource, + json: QueryJson +) { + const Integration = integrations[datasource.source] + // query is the opinionated function + if (Integration.prototype.query) { + const integration = new Integration(datasource.config) + return integration.query(json) + } else { + throw "Datasource does not support query." + } +} diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index a46ce7aea2..4d33669a03 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -584,4 +584,3 @@ class SqlQueryBuilder extends SqlTableQueryBuilder { } export default SqlQueryBuilder -module.exports = SqlQueryBuilder diff --git a/packages/server/src/integrations/base/utils.ts b/packages/server/src/integrations/base/utils.ts index 0913d59b2a..54efdb91a0 100644 --- a/packages/server/src/integrations/base/utils.ts +++ b/packages/server/src/integrations/base/utils.ts @@ -1,23 +1,5 @@ -import { QueryJson } from "../../definitions/datasource" -import { Datasource } from "../../definitions/common" -const { integrations } = require("../index") - const QUERY_START_REGEX = /\d[0-9]*:/g -export async function makeExternalQuery( - datasource: Datasource, - json: QueryJson -) { - const Integration = integrations[datasource.source] - // query is the opinionated function - if (Integration.prototype.query) { - const integration = new Integration(datasource.config) - return integration.query(json) - } else { - throw "Datasource does not support query." - } -} - export function removeKeyNumbering(key: any): string { if (typeof key === "string" && key.match(QUERY_START_REGEX) != null) { const parts = key.split(":") diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index 1e5664748d..2fe445f8fe 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -15,10 +15,10 @@ import { } from "./utils" import { DatasourcePlus } from "./base/datasourcePlus" import { Table, TableSchema } from "../definitions/common" +import Sql from "./base/sql" module MSSQLModule { const sqlServer = require("mssql") - const Sql = require("./base/sql") const DEFAULT_SCHEMA = "dbo" interface MSSQLConfig { @@ -96,7 +96,8 @@ module MSSQLModule { class SqlServerIntegration extends Sql implements DatasourcePlus { private readonly config: MSSQLConfig private index: number = 0 - static pool: any + private readonly pool: any + private client: any public tables: Record = {} public schemaErrors: Record = {} diff --git a/packages/server/src/integrations/mysql.ts b/packages/server/src/integrations/mysql.ts index 1fdab66701..61780ffcd7 100644 --- a/packages/server/src/integrations/mysql.ts +++ b/packages/server/src/integrations/mysql.ts @@ -16,10 +16,10 @@ import { import { DatasourcePlus } from "./base/datasourcePlus" import dayjs from "dayjs" const { NUMBER_REGEX } = require("../utilities") +import Sql from "./base/sql" module MySQLModule { const mysql = require("mysql2/promise") - const Sql = require("./base/sql") interface MySQLConfig { host: string diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index 7cc01bdc79..48b9544e50 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -14,10 +14,10 @@ import { SqlClients, } from "./utils" import { DatasourcePlus } from "./base/datasourcePlus" +import Sql from "./base/sql" module PostgresModule { const { Client, types } = require("pg") - const Sql = require("./base/sql") const { escapeDangerousCharacters } = require("../utilities") // Return "date" and "timestamp" types as plain strings. @@ -117,6 +117,7 @@ module PostgresModule { private readonly client: any private readonly config: PostgresConfig private index: number = 1 + private open: boolean public tables: Record = {} public schemaErrors: Record = {} diff --git a/packages/server/src/integrations/tests/sql.spec.js b/packages/server/src/integrations/tests/sql.spec.js index 55c762573a..42fe9e3b30 100644 --- a/packages/server/src/integrations/tests/sql.spec.js +++ b/packages/server/src/integrations/tests/sql.spec.js @@ -1,4 +1,4 @@ -const Sql = require("../base/sql") +const Sql = require("../base/sql").default const { SqlClients } = require("../utils") const TABLE_NAME = "test"