Skip to content

Commit

Permalink
Update logging levels in router and data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
MXPOL committed Jan 29, 2024
1 parent cabcc27 commit b1444da
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
14 changes: 7 additions & 7 deletions libs/external-db-mysql/src/mysql_data_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class DataProvider implements IDataProvider {
const projectionExpr = this.filterParser.selectFieldsFor(projection)
const sql = `SELECT ${projectionExpr} FROM ${escapeTable(collectionName)} ${filterExpr} ${sortExpr} LIMIT ?, ?`

this.logger?.info('find', { sql, parameters })
this.logger?.debug('mysql-find', { sql, parameters })

const resultset = await this.query(sql, [...parameters, skip, limit])
.catch( err => translateErrorCodes(err, collectionName) )
Expand All @@ -39,7 +39,7 @@ export default class DataProvider implements IDataProvider {
const { filterExpr, parameters } = this.filterParser.transform(filter)
const sql = `SELECT COUNT(*) AS num FROM ${escapeTable(collectionName)} ${filterExpr}`

this.logger?.info('count', { sql, parameters })
this.logger?.debug('mysql-count', { sql, parameters })
const resultset = await this.query(sql, parameters)
.catch( err => translateErrorCodes(err, collectionName) )
return resultset[0]['num']
Expand All @@ -52,7 +52,7 @@ export default class DataProvider implements IDataProvider {

const data = items.map((item: Item) => asParamArrays( patchItem(item) ) )

this.logger?.info('insert', { sql, parameters: data })
this.logger?.debug('mysql-insert', { sql, parameters: data })

const resultset = await this.query(sql, [data])
.catch( err => translateErrorCodes(err, collectionName) )
Expand All @@ -66,7 +66,7 @@ export default class DataProvider implements IDataProvider {
const updatables: Item[] = items.map((i: Item) => [...updateFields, '_id'].reduce((obj, key) => ({ ...obj, [key]: i[key] }), {}) )
.map((u: Item) => asParamArrays( patchItem(u) ))

this.logger?.info('update', { sql: queries, parameters: updatables })
this.logger?.debug('mysql-update', { sql: queries, parameters: updatables })

// @ts-ignore
const resultset = await this.query(queries, [].concat(...updatables))
Expand All @@ -78,7 +78,7 @@ export default class DataProvider implements IDataProvider {
async delete(collectionName: string, itemIds: string[]): Promise<number> {
const sql = `DELETE FROM ${escapeTable(collectionName)} WHERE _id IN (${wildCardWith(itemIds.length, '?')})`

this.logger?.info('delete', { sql, parameters: itemIds })
this.logger?.debug('mysql-delete', { sql, parameters: itemIds })

const rs = await this.query(sql, itemIds)
.catch( err => translateErrorCodes(err, collectionName) )
Expand All @@ -87,7 +87,7 @@ export default class DataProvider implements IDataProvider {

async truncate(collectionName: string): Promise<void> {
const sql = `TRUNCATE ${escapeTable(collectionName)}`
this.logger?.info('truncate', { sql })
this.logger?.debug('mysql-truncate', { sql })
await this.query(`TRUNCATE ${escapeTable(collectionName)}`).catch( err => translateErrorCodes(err, collectionName) )
}

Expand All @@ -98,7 +98,7 @@ export default class DataProvider implements IDataProvider {

const sql = `SELECT ${fieldsStatement} FROM ${escapeTable(collectionName)} ${whereFilterExpr} GROUP BY ${groupByColumns.map( escapeId ).join(', ')} ${havingFilter} ${sortExpr} LIMIT ?, ?`

this.logger?.info('aggregate', { sql, parameters: [...whereParameters, ...parameters, skip, limit] })
this.logger?.debug('mysql-aggregate', { sql, parameters: [...whereParameters, ...parameters, skip, limit] })

const resultset = await this.query(sql, [...whereParameters, ...parameters, skip, limit])
.catch( err => translateErrorCodes(err, collectionName) )
Expand Down
16 changes: 9 additions & 7 deletions libs/external-db-mysql/src/mysql_schema_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class SchemaProvider implements ISchemaProvider {
const currentDb = this.pool.config.connectionConfig.database
const sql = 'SELECT TABLE_NAME as table_name, COLUMN_NAME as field, DATA_TYPE as type FROM information_schema.columns WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME, ORDINAL_POSITION'

this.logger?.info('list', { sql, parameters: currentDb })
this.logger?.debug('mysql-list', { sql, parameters: currentDb })

const data = await this.query(sql, currentDb)
const tables: {[x:string]: { field: string, type: string}[]} = parseTableData( data )
Expand All @@ -44,7 +44,7 @@ export default class SchemaProvider implements ISchemaProvider {
const currentDb = this.pool.config.connectionConfig.database
const sql = 'SELECT TABLE_NAME as table_name FROM information_schema.tables WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME'

this.logger?.info('listHeaders', { sql, parameters: currentDb })
this.logger?.debug('mysql-listHeaders', { sql, parameters: currentDb })
const data = await this.query(sql, currentDb)
return data.map( (rs: { table_name: any }) => rs.table_name )
}
Expand All @@ -59,37 +59,39 @@ export default class SchemaProvider implements ISchemaProvider {
const sql = `CREATE TABLE IF NOT EXISTS ${escapeTable(collectionName)} (${dbColumnsSql}, PRIMARY KEY (${primaryKeySql}))`
const parameters = columns.map( c => c.name )

this.logger?.info('create table', { sql, parameters })
this.logger?.debug('mysql-create table', { sql, parameters })
await this.query(sql, parameters)
.catch( err => translateErrorCodes(err, collectionName) )
}

async drop(collectionName: string): Promise<void> {
const sql = `DROP TABLE IF EXISTS ${escapeTable(collectionName)}`

this.logger?.info('drop table', { sql })
this.logger?.debug('mysql-drop table', { sql })
await this.query(`DROP TABLE IF EXISTS ${escapeTable(collectionName)}`)
.catch( err => translateErrorCodes(err, collectionName) )
}

async addColumn(collectionName: string, column: InputField): Promise<void> {
await validateSystemFields(column.name)
const sql = `ALTER TABLE ${escapeTable(collectionName)} ADD ${escapeId(column.name)} ${this.sqlSchemaTranslator.dbTypeFor(column)}`
this.logger?.info('add column', { sql })
this.logger?.debug('mysql-add column', { sql })
await this.query(`ALTER TABLE ${escapeTable(collectionName)} ADD ${escapeId(column.name)} ${this.sqlSchemaTranslator.dbTypeFor(column)}`)
.catch( err => translateErrorCodes(err, collectionName) )
}

async changeColumnType(collectionName: string, column: InputField): Promise<void> {
await validateSystemFields(column.name)
const sql = `ALTER TABLE ${escapeTable(collectionName)} MODIFY ${escapeId(column.name)} ${this.sqlSchemaTranslator.dbTypeFor(column)}`
this.logger?.info('change column type', { sql })
this.logger?.debug('mysql-change column type', { sql })
await this.query(sql)
.catch( err => translateErrorCodes(err, collectionName) )
}

async removeColumn(collectionName: string, columnName: string): Promise<void> {
await validateSystemFields(columnName)
const sql = `ALTER TABLE ${escapeTable(collectionName)} DROP COLUMN ${escapeId(columnName)}`
this.logger?.debug('mysql-remove column', { sql })
return await this.query(`ALTER TABLE ${escapeTable(collectionName)} DROP COLUMN ${escapeId(columnName)}`)
.catch( err => translateErrorCodes(err, collectionName) )
}
Expand All @@ -100,7 +102,7 @@ export default class SchemaProvider implements ISchemaProvider {
Type: string,
}
const sql = `DESCRIBE ${escapeTable(collectionName)}`
this.logger?.info('describe table', { sql })
this.logger?.debug('mysql-describe table', { sql })
const res: describeTableResponse[] = await this.query(`DESCRIBE ${escapeTable(collectionName)}`)
.catch( err => translateErrorCodes(err, collectionName) )
const fields = res.map(r => ({ field: r.Field, type: r.Type })).map(this.appendAdditionalRowDetails.bind(this))
Expand Down
10 changes: 7 additions & 3 deletions libs/velo-external-db-core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,23 @@ const serviceContext = (): ServiceContext => ({


const executeDataHooksFor = async<T>(action: string, payload: T, requestContext: RequestContext, customContext: any): Promise<T> => {
logger?.info(`data params before ${action} hook`, payload as any)
logger?.debug(`Data params before ${action} hook`, payload as any)
return BPromise.reduce(DataHooksForAction[action], async(lastHookResult: AnyFixMe, hookName: string) => {
return await executeHook(dataHooks, hookName, lastHookResult, requestContext, customContext)
}, payload).then( res => {
logger?.info(`data params after ${action} hook`, res as any)
logger?.debug(`data params after ${action} hook`, res as any)
return res
})
}

const executeSchemaHooksFor = async<T>(action: string, payload: T, requestContext: RequestContext, customContext: any): Promise<T> => {
logger?.debug(`Schema params before ${action} hook`, payload as any)
return BPromise.reduce(SchemaHooksForAction[action], async(lastHookResult: any, hookName: string) => {
return await executeHook(schemaHooks, hookName, lastHookResult, requestContext, customContext)
}, payload)
}, payload).then( res => {
logger?.debug(`Schema params after ${action} hook`, res as any)
return res
})
}

const executeHook = async(hooks: DataHooks | SchemaHooks, _actionName: string, payload: AnyFixMe, requestContext: RequestContext, customContext: any) => {
Expand Down

0 comments on commit b1444da

Please sign in to comment.