diff --git a/lib/DboBuilder/TableHandler/TableHandler.ts b/lib/DboBuilder/TableHandler/TableHandler.ts index 46fe886e..4d734b19 100644 --- a/lib/DboBuilder/TableHandler/TableHandler.ts +++ b/lib/DboBuilder/TableHandler/TableHandler.ts @@ -3,7 +3,7 @@ import { AnyObject, asName, DeleteParams, FieldFilter, InsertParams, Select, Upd import { DB } from "../../Prostgles"; import { SyncRule, TableRule } from "../../PublishParser/PublishParser"; import TableConfigurator from "../../TableConfig/TableConfig"; -import { DboBuilder, Filter, LocalParams, parseError, TableHandlers } from "../DboBuilder"; +import { DboBuilder, Filter, getErrorAsObject, LocalParams, parseError, TableHandlers } from "../DboBuilder"; import type { TableSchema } from "../DboBuilderTypes"; import { parseUpdateRules } from "../parseUpdateRules"; import { COMPUTED_FIELDS, FUNCTIONS } from "../QueryBuilder/Functions"; @@ -99,9 +99,8 @@ export class TableHandler extends ViewHandler { /* External request. Cannot sync from server */ async sync(filter: Filter, params: { select?: FieldFilter }, param3_unused: undefined, table_rules: TableRule, localParams: LocalParams) { - + const start = Date.now(); try { - await this._log({ command: "sync", localParams, data: { filter, params } }); if (!localParams) throw "Sync not allowed within the server code"; const { socket } = localParams; @@ -145,7 +144,7 @@ export class TableHandler extends ViewHandler { }); /* Step 1: parse command and params */ - return this.find(filter, { select, limit: 0 }, undefined, table_rules, localParams) + const result = await this.find(filter, { select, limit: 0 }, undefined, table_rules, localParams) .then(async _isValid => { const { filterFields, forcedFilter } = table_rules?.select || {}; @@ -163,8 +162,10 @@ export class TableHandler extends ViewHandler { params: { select } }).then(channelName => ({ channelName, id_fields, synced_field })); }); - + await this._log({ command: "sync", localParams, data: { filter, params }, duration: Date.now() - start }); + return result; } catch (e) { + await this._log({ command: "sync", localParams, data: { filter, params }, duration: Date.now() - start, error: getErrorAsObject(e) }); if (localParams && localParams.testRule) throw e; throw parseError(e, `dbo.${this.name}.sync()`); } diff --git a/lib/DboBuilder/TableHandler/delete.ts b/lib/DboBuilder/TableHandler/delete.ts index 79c0e07f..9b448399 100644 --- a/lib/DboBuilder/TableHandler/delete.ts +++ b/lib/DboBuilder/TableHandler/delete.ts @@ -1,14 +1,14 @@ import pgPromise from "pg-promise"; import { AnyObject, DeleteParams, FieldFilter } from "prostgles-types"; -import { Filter, LocalParams, parseError, withUserRLS } from "../DboBuilder"; +import { Filter, LocalParams, getErrorAsObject, parseError, withUserRLS } from "../DboBuilder"; import { DeleteRule, TableRule } from "../../PublishParser/PublishParser"; import { runQueryReturnType } from "../ViewHandler/find"; import { TableHandler } from "./TableHandler"; import { onDeleteFromFileTable } from "./onDeleteFromFileTable"; export async function _delete(this: TableHandler, filter?: Filter, params?: DeleteParams, param3_unused?: undefined, tableRules?: TableRule, localParams?: LocalParams): Promise { + const start = Date.now(); try { - await this._log({ command: "delete", localParams, data: { filter, params } }); const { returning } = params || {}; filter = filter || {}; this.checkFilter(filter); @@ -86,15 +86,17 @@ export async function _delete(this: TableHandler, filter?: Filter, params?: Dele * Delete file */ if (this.is_media) { - return onDeleteFromFileTable.bind(this)({ + const result = await onDeleteFromFileTable.bind(this)({ localParams, queryType, returningQuery: returnQuery? returnQuery : undefined, filterOpts, }); + await this._log({ command: "delete", localParams, data: { filter, params }, duration: Date.now() - start }); + return result; } - return runQueryReturnType({ + const result = await runQueryReturnType({ queryWithoutRLS, queryWithRLS, newQuery: undefined, @@ -102,8 +104,11 @@ export async function _delete(this: TableHandler, filter?: Filter, params?: Dele handler: this, localParams }); + await this._log({ command: "delete", localParams, data: { filter, params }, duration: Date.now() - start }); + return result; } catch (e) { + await this._log({ command: "delete", localParams, data: { filter, params }, duration: Date.now() - start, error: getErrorAsObject(e) }); if (localParams && localParams.testRule) throw e; throw parseError(e, `dbo.${this.name}.delete(${JSON.stringify(filter || {}, null, 2)}, ${JSON.stringify(params || {}, null, 2)})`); } diff --git a/lib/DboBuilder/TableHandler/insert.ts b/lib/DboBuilder/TableHandler/insert.ts index 164c0f81..4ecc7b4b 100644 --- a/lib/DboBuilder/TableHandler/insert.ts +++ b/lib/DboBuilder/TableHandler/insert.ts @@ -1,18 +1,17 @@ import { AnyObject, InsertParams, asName, isObject } from "prostgles-types"; -import { LocalParams, parseError, withUserRLS } from "../DboBuilder"; import { TableRule, ValidateRowBasic } from "../../PublishParser/PublishParser"; +import { LocalParams, getErrorAsObject, parseError, withUserRLS } from "../DboBuilder"; import { insertNestedRecords } from "../insertNestedRecords"; -import { insertTest } from "./insertTest"; +import { prepareNewData } from "./DataValidator"; import { TableHandler } from "./TableHandler"; +import { insertTest } from "./insertTest"; import { runInsertUpdateQuery } from "./runInsertUpdateQuery"; -import { prepareNewData } from "./DataValidator"; -import { DBOFullyTyped } from "../../DBSchemaBuilder"; export async function insert(this: TableHandler, rowOrRows: AnyObject | AnyObject[] = {}, insertParams?: InsertParams, param3_unused?: undefined, tableRules?: TableRule, localParams?: LocalParams): Promise { const ACTION = "insert"; + const start = Date.now(); try { - await this._log({ command: "insert", localParams, data: { rowOrRows, param2: insertParams } }); const { fixIssues = false } = insertParams || {}; const { returnQuery = false, nestedInsert } = localParams || {}; @@ -129,7 +128,7 @@ export async function insert(this: TableHandler, rowOrRows: AnyObject | AnyObjec console.log(this.tx?.t.ctx?.start, "insert in " + this.name, data); } - return await runInsertUpdateQuery({ + const result = await runInsertUpdateQuery({ rule, localParams, queryWithoutUserRLS, @@ -141,8 +140,10 @@ export async function insert(this: TableHandler, rowOrRows: AnyObject | AnyObjec type: "insert", isMultiInsert, }); - + await this._log({ command: "insert", localParams, data: { rowOrRows, param2: insertParams }, duration: Date.now() - start }); + return result; } catch (e) { + await this._log({ command: "insert", localParams, data: { rowOrRows, param2: insertParams }, duration: Date.now() - start, error: getErrorAsObject(e) }); if (localParams?.testRule) throw e; throw parseError(e, `dbo.${this.name}.${ACTION}()`) } diff --git a/lib/DboBuilder/TableHandler/update.ts b/lib/DboBuilder/TableHandler/update.ts index 058c9edf..cf66a1e0 100644 --- a/lib/DboBuilder/TableHandler/update.ts +++ b/lib/DboBuilder/TableHandler/update.ts @@ -1,6 +1,6 @@ import { AnyObject, UpdateParams } from "prostgles-types"; import { TableRule } from "../../PublishParser/PublishParser"; -import { Filter, LocalParams, parseError, withUserRLS } from "../DboBuilder"; +import { Filter, LocalParams, getErrorAsObject, parseError, withUserRLS } from "../DboBuilder"; import { getInsertTableRules, getReferenceColumnInserts } from "../insertNestedRecords"; import { runInsertUpdateQuery } from "./runInsertUpdateQuery"; import { TableHandler } from "./TableHandler"; @@ -9,8 +9,8 @@ import { prepareNewData } from "./DataValidator"; export async function update(this: TableHandler, filter: Filter, _newData: AnyObject, params?: UpdateParams, tableRules?: TableRule, localParams?: LocalParams): Promise { const ACTION = "update"; + const start = Date.now(); try { - await this._log({ command: "update", localParams, data: { filter, _newData, params } }); /** postValidate */ const finalDBtx = this.getFinalDBtx(localParams); const wrapInTx = () => this.dboBuilder.getTX(_dbtx => _dbtx[this.name]?.[ACTION]?.(filter, _newData, params, tableRules, localParams)) @@ -105,7 +105,7 @@ export async function update(this: TableHandler, filter: Filter, _newData: AnyOb if (returnQuery) return query as unknown as void; - return runInsertUpdateQuery({ + const result = await runInsertUpdateQuery({ tableHandler: this, data: undefined, fields, @@ -117,8 +117,10 @@ export async function update(this: TableHandler, filter: Filter, _newData: AnyOb type: "update", nestedInsertsResultsObj }); - + await this._log({ command: "update", localParams, data: { filter, _newData, params }, duration: Date.now() - start }); + return result; } catch (e) { + await this._log({ command: "update", localParams, data: { filter, _newData, params }, duration: Date.now() - start, error: getErrorAsObject(e) }); if (localParams && localParams.testRule) throw e; throw parseError(e, `dbo.${this.name}.${ACTION}(${JSON.stringify(filter || {}, null, 2)}, ${Array.isArray(_newData)? "[{...}]": "{...}"}, ${JSON.stringify(params || {}, null, 2)})`) } diff --git a/lib/DboBuilder/TableHandler/updateBatch.ts b/lib/DboBuilder/TableHandler/updateBatch.ts index 227589b8..36d5cce3 100644 --- a/lib/DboBuilder/TableHandler/updateBatch.ts +++ b/lib/DboBuilder/TableHandler/updateBatch.ts @@ -1,12 +1,12 @@ import { AnyObject, UpdateParams } from "prostgles-types"; -import { Filter, LocalParams, getClientErrorFromPGError, parseError, withUserRLS } from "../DboBuilder"; +import { Filter, LocalParams, getClientErrorFromPGError, getErrorAsObject, parseError, withUserRLS } from "../DboBuilder"; import { TableRule } from "../../PublishParser/PublishParser"; import { TableHandler } from "./TableHandler"; export async function updateBatch(this: TableHandler, updates: [Filter, AnyObject][], params?: UpdateParams, tableRules?: TableRule, localParams?: LocalParams): Promise { + const start = Date.now(); try { - await this._log({ command: "updateBatch", localParams, data: { data: updates, params } }); const { checkFilter, postValidate } = tableRules?.update ?? {}; if(checkFilter || postValidate){ throw `updateBatch not allowed for tables with checkFilter or postValidate rules` @@ -31,12 +31,19 @@ export async function updateBatch(this: TableHandler, updates: [Filter, AnyObjec const t = localParams?.tx?.t ?? this.tx?.t; if(t){ - return t.none(queries.join(";\n")) + const result = await t.none(queries.join(";\n")); + await this._log({ command: "updateBatch", localParams, data: { data: updates, params }, duration: Date.now() - start }); + return result; } - return this.db.tx(t => { - return t.none(queries.join(";\n")); - }).catch(err => getClientErrorFromPGError(err, { type: "tableMethod", localParams, view: this, allowedKeys: []})); + const result = await this.db.tx(t => { + return t.none(queries.join(";\n")); + }) + .catch(err => getClientErrorFromPGError(err, { type: "tableMethod", localParams, view: this, allowedKeys: []})); + + await this._log({ command: "updateBatch", localParams, data: { data: updates, params }, duration: Date.now() - start }); + return result; } catch (e) { + await this._log({ command: "updateBatch", localParams, data: { data: updates, params }, duration: Date.now() - start, error: getErrorAsObject(e) }); if (localParams && localParams.testRule) throw e; throw parseError(e, `dbo.${this.name}.update()`); } diff --git a/lib/DboBuilder/TableHandler/upsert.ts b/lib/DboBuilder/TableHandler/upsert.ts index 3b5b1a2e..5aba56d6 100644 --- a/lib/DboBuilder/TableHandler/upsert.ts +++ b/lib/DboBuilder/TableHandler/upsert.ts @@ -2,11 +2,11 @@ import { AnyObject, UpdateParams } from "prostgles-types"; import { TableHandler } from "./TableHandler"; import { Filter, LocalParams } from "../DboBuilderTypes"; import { TableRule } from "../../PublishParser/publishTypesAndUtils"; -import { parseError } from "../dboBuilderUtils"; +import { getErrorAsObject, parseError } from "../dboBuilderUtils"; export const upsert = async function(this: TableHandler, filter: Filter, newData: AnyObject, params?: UpdateParams, table_rules?: TableRule, localParams?: LocalParams): Promise { + const start = Date.now(); try { - await this._log({ command: "upsert", localParams, data: { filter, newData, params } }); const _upsert = async function (tblH: TableHandler) { return tblH.find(filter, { select: "", limit: 1 }, undefined, table_rules, localParams) .then(exists => { @@ -22,10 +22,13 @@ export const upsert = async function(this: TableHandler, filter: Filter, newData if (!this.tx) { return this.dboBuilder.getTX(dbTX => _upsert(dbTX[this.name] as TableHandler)) } else { - return _upsert(this); + const result = await _upsert(this); + await this._log({ command: "upsert", localParams, data: { filter, newData, params }, duration: Date.now() - start }); + return result; } } catch (e) { + await this._log({ command: "upsert", localParams, data: { filter, newData, params }, duration: Date.now() - start, error: getErrorAsObject(e) }); if (localParams && localParams.testRule) throw e; throw parseError(e, `dbo.${this.name}.upsert()`); } diff --git a/lib/DboBuilder/ViewHandler/ViewHandler.ts b/lib/DboBuilder/ViewHandler/ViewHandler.ts index dc0f5e30..16a00d09 100644 --- a/lib/DboBuilder/ViewHandler/ViewHandler.ts +++ b/lib/DboBuilder/ViewHandler/ViewHandler.ts @@ -85,8 +85,8 @@ export class ViewHandler { }); } - _log = ({ command, data, localParams }: Pick) => { - return this.dboBuilder.prostgles.opts.onLog?.({ type: "table", tableName: this.name, command, data, localParams }) + _log = ({ command, data, localParams, duration, error }: Pick & { duration: number; error?: any; }) => { + return this.dboBuilder.prostgles.opts.onLog?.({ type: "table", tableName: this.name, command, data, localParams, duration, error }); } getRowHashSelect(allowedFields: FieldFilter, alias?: string, tableAlias?: string): string { @@ -271,12 +271,14 @@ export class ViewHandler { async findOne(filter?: Filter, selectParams?: SelectParams, _param3_unused?: undefined, table_rules?: TableRule, localParams?: LocalParams): Promise { try { - await this._log({ command: "find", localParams, data: { filter, selectParams } }); const { limit, ...params } = selectParams ?? {}; if (limit) { throw "limit not allowed in findOne()"; } - return this.find(filter, { ...params, limit: 1, returnType: "row" }, undefined, table_rules, localParams); + const start = Date.now(); + const result = await this.find(filter, { ...params, limit: 1, returnType: "row" }, undefined, table_rules, localParams); + await this._log({ command: "find", localParams, data: { filter, selectParams }, duration: Date.now() - start }); + return result; } catch (e) { if (localParams && localParams.testRule) throw e; throw parseError(e, `Issue with dbo.${this.name}.findOne()`); diff --git a/lib/DboBuilder/ViewHandler/count.ts b/lib/DboBuilder/ViewHandler/count.ts index ded250ab..6fceac5d 100644 --- a/lib/DboBuilder/ViewHandler/count.ts +++ b/lib/DboBuilder/ViewHandler/count.ts @@ -1,5 +1,5 @@ import { SelectParams } from "prostgles-types"; -import { parseError, withUserRLS } from "../dboBuilderUtils"; +import { getErrorAsObject, parseError, withUserRLS } from "../dboBuilderUtils"; import { ViewHandler } from "./ViewHandler"; import { Filter, LocalParams } from "../DboBuilder"; import { TableRule } from "../../PublishParser/publishTypesAndUtils"; @@ -7,9 +7,9 @@ import { TableRule } from "../../PublishParser/publishTypesAndUtils"; export async function count(this: ViewHandler, _filter?: Filter, selectParams?: SelectParams, _param3_unused?: undefined, table_rules?: TableRule, localParams?: LocalParams): Promise { const filter = _filter || {}; const { limit: _limit, ...selectParamsWithoutLimit } = selectParams ?? {}; + const start = Date.now(); try { - await this._log({ command: "count", localParams, data: { filter } }); - return await this.find(filter, { select: selectParamsWithoutLimit?.select ?? "", limit: 0 }, undefined, table_rules, localParams) + const result = await this.find(filter, { select: selectParamsWithoutLimit?.select ?? "", limit: 0 }, undefined, table_rules, localParams) .then(async _allowed => { const findQuery = await this.find( filter, @@ -28,7 +28,11 @@ export async function count(this: ViewHandler, _filter?: Filter, selectParams?: const handler = this.tx?.t ?? this.db; return handler.one(query).then(({ count }) => +count); }); + + await this._log({ command: "count", localParams, data: { filter }, duration: Date.now() - start }); + return result; } catch (e) { + await this._log({ command: "count", localParams, data: { filter }, duration: Date.now() - start, error: getErrorAsObject(e) }); if (localParams && localParams.testRule) throw e; throw parseError(e, `dbo.${this.name}.count()`) } diff --git a/lib/DboBuilder/ViewHandler/find.ts b/lib/DboBuilder/ViewHandler/find.ts index 32d0f924..6237d798 100644 --- a/lib/DboBuilder/ViewHandler/find.ts +++ b/lib/DboBuilder/ViewHandler/find.ts @@ -1,6 +1,6 @@ import { SelectParams, isObject } from "prostgles-types"; -import { Filter, LocalParams, getClientErrorFromPGError, parseError, withUserRLS } from "../DboBuilder"; +import { Filter, LocalParams, getClientErrorFromPGError, getErrorAsObject, parseError, withUserRLS } from "../DboBuilder"; import { canRunSQL } from "../runSQL"; import { TableRule } from "../../PublishParser/PublishParser"; import { getNewQuery } from "../QueryBuilder/getNewQuery"; @@ -10,8 +10,9 @@ import { ViewHandler } from "./ViewHandler"; import { NewQuery } from "../QueryBuilder/QueryBuilder"; export const find = async function(this: ViewHandler, filter?: Filter, selectParams?: SelectParams, _?: undefined, tableRules?: TableRule, localParams?: LocalParams): Promise { + const start = Date.now(); + const command = selectParams?.limit === 1 && selectParams?.returnType === "row"? "findOne" : "find"; try { - await this._log({ command: "find", localParams, data: { filter, selectParams } }); filter = filter || {}; const allowedReturnTypes = Object.keys({ row: 1, statement: 1, value: 1, values: 1, @@ -92,7 +93,7 @@ export const find = async function(this: ViewHandler, filter?: Filter, selectPar return ((localParams?.returnQuery === "noRLS"? queryWithoutRLS : queryWithRLS) as unknown as any[]); } - return runQueryReturnType({ + const result = await runQueryReturnType({ queryWithoutRLS, queryWithRLS, returnType, @@ -101,7 +102,10 @@ export const find = async function(this: ViewHandler, filter?: Filter, selectPar newQuery, }); + await this._log({ command, localParams, data: { filter, selectParams }, duration: Date.now() - start }); + return result; } catch (e) { + this._log({ command, localParams, data: { filter, selectParams }, duration: Date.now() - start, error: getErrorAsObject(e) }); if (localParams && localParams.testRule) throw e; throw parseError(e, `dbo.${this.name}.find()`); } diff --git a/lib/DboBuilder/ViewHandler/getInfo.ts b/lib/DboBuilder/ViewHandler/getInfo.ts index 8145d234..7b8df716 100644 --- a/lib/DboBuilder/ViewHandler/getInfo.ts +++ b/lib/DboBuilder/ViewHandler/getInfo.ts @@ -1,17 +1,20 @@ -import { - TableInfo as TInfo, pickKeys, - } from "prostgles-types/dist"; -import { LocalParams } from "../DboBuilder"; +import { + TableInfo as TInfo +} from "prostgles-types/dist"; import { TableRule } from "../../PublishParser/PublishParser"; +import { LocalParams } from "../DboBuilder"; import { ViewHandler } from "./ViewHandler"; export async function getInfo(this: ViewHandler, lang?: string, param2?: any, param3?: any, tableRules?: TableRule, localParams?: LocalParams): Promise { - await this._log({ command: "getInfo", localParams, data: { lang } }); const p = this.getValidatedRules(tableRules, localParams); - if (!p.getInfo) throw "Not allowed"; + if (!p.getInfo) { + await this._log({ command: "getInfo", localParams, data: { lang }, duration: 0, error: "Not allowed" }); + throw "Not allowed"; + } const fileTableName = this.dboBuilder.prostgles?.opts?.fileTable?.tableName; + await this._log({ command: "getInfo", localParams, data: { lang }, duration: 0 }); return { oid: this.tableOrViewInfo.oid, comment: this.tableOrViewInfo.comment, diff --git a/lib/DboBuilder/ViewHandler/size.ts b/lib/DboBuilder/ViewHandler/size.ts index 8021722c..1e236ced 100644 --- a/lib/DboBuilder/ViewHandler/size.ts +++ b/lib/DboBuilder/ViewHandler/size.ts @@ -2,12 +2,12 @@ import { SelectParams } from "prostgles-types"; import { Filter, LocalParams } from "../DboBuilderTypes"; import { ViewHandler } from "./ViewHandler" import { TableRule } from "../../PublishParser/publishTypesAndUtils"; -import { parseError, withUserRLS } from "../dboBuilderUtils"; +import { getErrorAsObject, parseError, withUserRLS } from "../dboBuilderUtils"; export async function size(this: ViewHandler, _filter?: Filter, selectParams?: SelectParams, param3_unused?: undefined, table_rules?: TableRule, localParams?: LocalParams): Promise { const filter = _filter || {}; + const start = Date.now(); try { - await this._log({ command: "size", localParams, data: { filter, selectParams } }); - return await this.find(filter, { ...selectParams, limit: 2 }, undefined, table_rules, localParams) + const result = await this.find(filter, { ...selectParams, limit: 2 }, undefined, table_rules, localParams) .then(async _allowed => { const q: string = await this.find( @@ -28,7 +28,10 @@ export async function size(this: ViewHandler, _filter?: Filter, selectParams?: S return (this.tx?.t || this.db).one(query).then(({ size }) => size || '0'); }); + await this._log({ command: "size", localParams, data: { filter, selectParams }, duration: Date.now() - start }); + return result; } catch (e) { + await this._log({ command: "size", localParams, data: { filter, selectParams }, duration: Date.now() - start, error: getErrorAsObject(e) }); if (localParams && localParams.testRule) throw e; throw parseError(e, `dbo.${this.name}.size()`); } diff --git a/lib/DboBuilder/ViewHandler/subscribe.ts b/lib/DboBuilder/ViewHandler/subscribe.ts index 271db311..daa8e7e7 100644 --- a/lib/DboBuilder/ViewHandler/subscribe.ts +++ b/lib/DboBuilder/ViewHandler/subscribe.ts @@ -1,6 +1,6 @@ import { AnyObject, SubscribeParams, SubscriptionChannels } from "prostgles-types"; import { TableRule } from "../../PublishParser/PublishParser"; -import { Filter, LocalParams, parseError } from "../DboBuilder"; +import { Filter, LocalParams, getErrorAsObject, parseError } from "../DboBuilder"; import { NewQuery } from "../QueryBuilder/QueryBuilder"; import { ViewHandler } from "./ViewHandler"; import { getSubscribeRelatedTables } from "../getSubscribeRelatedTables"; @@ -27,9 +27,8 @@ async function subscribe(this: ViewHandler, filter: Filter, params: SubscribePar async function subscribe(this: ViewHandler, filter: Filter, params: SubscribeParams, localFuncs: undefined, table_rules: TableRule | undefined, localParams: LocalParams): Promise async function subscribe(this: ViewHandler, filter: Filter, params: SubscribeParams, localFuncs?: LocalFuncs, table_rules?: TableRule, localParams?: LocalParams): Promise<{ unsubscribe: () => any } | SubscriptionChannels> { - + const start = Date.now(); try { - await this._log({ command: "subscribe", localParams, data: { filter, params } }); if (this.tx) { throw "subscribe not allowed within transactions"; @@ -82,13 +81,15 @@ async function subscribe(this: ViewHandler, filter: Filter, params: SubscribePar if (!localFuncs) { const { socket } = localParams ?? {}; - return pubSubManager.addSub({ + const result = await pubSubManager.addSub({ ...commonSubOpts, socket, localFuncs: undefined, socket_id: socket?.id, }); + await this._log({ command: "subscribe", localParams, data: { filter, params }, duration: Date.now() - start }); + return result; } else { const { channelName } = await pubSubManager.addSub({ @@ -102,10 +103,12 @@ async function subscribe(this: ViewHandler, filter: Filter, params: SubscribePar const pubSubManager = await this.dboBuilder.getPubSubManager(); pubSubManager.removeLocalSub(channelName, localFuncs) }; + await this._log({ command: "subscribe", localParams, data: { filter, params }, duration: Date.now() - start }); const res: { unsubscribe: () => any } = Object.freeze({ unsubscribe }) return res; } } catch (e) { + await this._log({ command: "subscribe", localParams, data: { filter, params }, duration: Date.now() - start, error: getErrorAsObject(e) }); if (localParams && localParams.testRule) throw e; throw parseError(e, `dbo.${this.name}.subscribe()`); } diff --git a/lib/DboBuilder/getColumns.ts b/lib/DboBuilder/getColumns.ts index f46f36a1..6191263b 100644 --- a/lib/DboBuilder/getColumns.ts +++ b/lib/DboBuilder/getColumns.ts @@ -1,9 +1,9 @@ -import { - AnyObject, PG_COLUMN_UDT_DATA_TYPE, - ValidatedColumnInfo, _PG_geometric, isObject +import { + AnyObject, PG_COLUMN_UDT_DATA_TYPE, + ValidatedColumnInfo, _PG_geometric, isObject } from "prostgles-types"; -import { isPlainObject, LocalParams, parseError, postgresToTsType } from "./DboBuilder"; import { TableRule } from "../PublishParser/PublishParser"; +import { LocalParams, getErrorAsObject, parseError, postgresToTsType } from "./DboBuilder"; import { TableHandler } from "./TableHandler/TableHandler"; import { ViewHandler } from "./ViewHandler/ViewHandler"; @@ -17,9 +17,8 @@ export async function getColumns( tableRules?: TableRule, localParams?: LocalParams ): Promise { - + const start = Date.now(); try { - await this._log({ command: "getColumns", localParams, data: { lang, params } }); const p = this.getValidatedRules(tableRules, localParams); @@ -94,9 +93,11 @@ export async function getColumns( return result; }).filter(c => c.select || c.update || c.delete || c.insert) + await this._log({ command: "getColumns", localParams, data: { lang, params }, duration: Date.now() - start }); return columns; } catch (e) { + await this._log({ command: "getColumns", localParams, data: { lang, params }, duration: Date.now() - start, error: getErrorAsObject(e) }); throw parseError(e, `db.${this.name}.getColumns()`); } } diff --git a/lib/Logging.ts b/lib/Logging.ts index 16514eb5..2c6b495a 100644 --- a/lib/Logging.ts +++ b/lib/Logging.ts @@ -19,6 +19,8 @@ export namespace EventTypes { command: keyof TableHandler; data: AnyObject; localParams: LocalParams | undefined; + duration: number; + error?: any; }; export type Sync = { diff --git a/lib/Prostgles.ts b/lib/Prostgles.ts index 5aaebef1..f1670d32 100644 --- a/lib/Prostgles.ts +++ b/lib/Prostgles.ts @@ -705,7 +705,7 @@ export class Prostgles { socket.emit(CHANNELS.SCHEMA, clientSchema); } catch (err) { - socket.emit(CHANNELS.SCHEMA, { err }); + socket.emit(CHANNELS.SCHEMA, { err: getErrorAsObject(err) }); } } } diff --git a/package-lock.json b/package-lock.json index ab7da891..1835baf3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prostgles-server", - "version": "4.2.58", + "version": "4.2.59", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "prostgles-server", - "version": "4.2.58", + "version": "4.2.59", "license": "MIT", "dependencies": { "bluebird": "^3.7.2", diff --git a/package.json b/package.json index dd607a69..4fb5e0b8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prostgles-server", - "version": "4.2.58", + "version": "4.2.59", "description": "", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/tests/server/package-lock.json b/tests/server/package-lock.json index 8cb6f1fe..5b1fa9c7 100644 --- a/tests/server/package-lock.json +++ b/tests/server/package-lock.json @@ -21,7 +21,7 @@ }, "../..": { "name": "prostgles-server", - "version": "4.2.58", + "version": "4.2.59", "license": "MIT", "dependencies": { "bluebird": "^3.7.2",