Adding SQL logging capabilities.

This commit is contained in:
mike12345567 2024-01-18 18:13:11 +00:00
parent 643a46a3b7
commit 3ce00c42a2
7 changed files with 55 additions and 625 deletions

View File

@ -67,6 +67,7 @@ const environment = {
DISABLE_RATE_LIMITING: process.env.DISABLE_RATE_LIMITING, DISABLE_RATE_LIMITING: process.env.DISABLE_RATE_LIMITING,
MULTI_TENANCY: process.env.MULTI_TENANCY, MULTI_TENANCY: process.env.MULTI_TENANCY,
ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS, ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS,
ENABLE_SQL_LOGGING: process.env.ENABLE_SQL_LOGGING,
SELF_HOSTED: process.env.SELF_HOSTED, SELF_HOSTED: process.env.SELF_HOSTED,
HTTP_MB_LIMIT: process.env.HTTP_MB_LIMIT, HTTP_MB_LIMIT: process.env.HTTP_MB_LIMIT,
FORKED_PROCESS_NAME: process.env.FORKED_PROCESS_NAME || "main", FORKED_PROCESS_NAME: process.env.FORKED_PROCESS_NAME || "main",

View File

@ -671,6 +671,18 @@ class SqlQueryBuilder extends SqlTableQueryBuilder {
} }
return results.length ? results : [{ [operation.toLowerCase()]: true }] return results.length ? results : [{ [operation.toLowerCase()]: true }]
} }
log(query: string, values?: any[]) {
if (!environment.ENABLE_SQL_LOGGING) {
return
}
const sqlClient = this.getSqlClient()
let string = `[SQL] [${sqlClient.toUpperCase()}] query="${query}"`
if (values) {
string += ` values="${values.join(", ")}"`
}
console.log(string)
}
} }
export default SqlQueryBuilder export default SqlQueryBuilder

View File

@ -329,6 +329,7 @@ class SqlServerIntegration extends Sql implements DatasourcePlus {
operation === Operation.CREATE operation === Operation.CREATE
? `${query.sql}; SELECT SCOPE_IDENTITY() AS id;` ? `${query.sql}; SELECT SCOPE_IDENTITY() AS id;`
: query.sql : query.sql
this.log(sql, query.bindings)
return await request.query(sql) return await request.query(sql)
} catch (err: any) { } catch (err: any) {
let readableMessage = getReadableErrorMessage( let readableMessage = getReadableErrorMessage(

View File

@ -261,6 +261,7 @@ class MySQLIntegration extends Sql implements DatasourcePlus {
const bindings = opts?.disableCoercion const bindings = opts?.disableCoercion
? baseBindings ? baseBindings
: bindingTypeCoerce(baseBindings) : bindingTypeCoerce(baseBindings)
this.log(query.sql, bindings)
// Node MySQL is callback based, so we must wrap our call in a promise // Node MySQL is callback based, so we must wrap our call in a promise
const response = await this.client!.query(query.sql, bindings) const response = await this.client!.query(query.sql, bindings)
return response[0] return response[0]

View File

@ -368,6 +368,7 @@ class OracleIntegration extends Sql implements DatasourcePlus {
const options: ExecuteOptions = { autoCommit: true } const options: ExecuteOptions = { autoCommit: true }
const bindings: BindParameters = query.bindings || [] const bindings: BindParameters = query.bindings || []
this.log(query.sql, bindings)
return await connection.execute<T>(query.sql, bindings, options) return await connection.execute<T>(query.sql, bindings, options)
} finally { } finally {
if (connection) { if (connection) {

View File

@ -262,7 +262,9 @@ class PostgresIntegration extends Sql implements DatasourcePlus {
} }
} }
try { try {
return await client.query(query.sql, query.bindings || []) const bindings = query.bindings || []
this.log(query.sql, bindings)
return await client.query(query.sql, bindings)
} catch (err: any) { } catch (err: any) {
await this.closeConnection() await this.closeConnection()
let readableMessage = getReadableErrorMessage( let readableMessage = getReadableErrorMessage(

660
yarn.lock

File diff suppressed because it is too large Load Diff