diff --git a/lib/Prostgles.ts b/lib/Prostgles.ts index 0688b524..089d00d7 100644 --- a/lib/Prostgles.ts +++ b/lib/Prostgles.ts @@ -124,7 +124,7 @@ export class Prostgles { onReady: 1, dbConnection: 1, dbOptions: 1, publishMethods: 1, io: 1, publish: 1, schema: 1, publishRawSQL: 1, wsChannelNamePrefix: 1, onSocketConnect: 1, onSocketDisconnect: 1, sqlFilePath: 1, auth: 1, - DEBUG_MODE: 1, watchSchema: 1, watchSchemaType: 1, fileTable: 1, + DEBUG_MODE: 1, watchSchema: 1, watchSchemaType: 1, fileTable: 1, onQuery: 1, tableConfig: 1, tableConfigMigrations: 1, keywords: 1, onNotice: 1, onLog: 1, restApi: 1, testRulesOnConnect: 1 }; const unknownParams = Object.keys(params).filter((key: string) => !Object.keys(config).includes(key)) diff --git a/lib/ProstglesTypes.ts b/lib/ProstglesTypes.ts index 78de429f..54dd3050 100644 --- a/lib/ProstglesTypes.ts +++ b/lib/ProstglesTypes.ts @@ -118,7 +118,8 @@ export type ProstglesInitOptions & { socket: PRGLIOSocket }) => void | Promise; onSocketDisconnect?: (args: AuthRequestParams & { socket: PRGLIOSocket }) => void | Promise; auth?: Auth; - DEBUG_MODE?: boolean | ((error: any, ctx: pgPromise.IEventContext) => void); + DEBUG_MODE?: boolean; + onQuery?: (error: any, ctx: pgPromise.IEventContext) => void; watchSchemaType?: /** diff --git a/lib/PubSubManager/getPubSubManagerInitQuery.ts b/lib/PubSubManager/getPubSubManagerInitQuery.ts index 097140fe..3a0196fd 100644 --- a/lib/PubSubManager/getPubSubManagerInitQuery.ts +++ b/lib/PubSubManager/getPubSubManagerInitQuery.ts @@ -629,7 +629,7 @@ COMMIT; */ export const getPubSubManagerInitQuery = async function(this: DboBuilder): Promise { - const initQuery = getInitQuery(this.prostgles.opts.DEBUG_MODE === true); + const initQuery = getInitQuery(this.prostgles.opts.DEBUG_MODE); const { schema_md5 = "none" } = await this.db.oneOrNone("SELECT md5($1) as schema_md5", [initQuery.trim()]); const query = pgp.as.format(initQuery, { schema_md5, version }); const existingSchema = await this.db.any(PROSTGLES_SCHEMA_EXISTS_QUERY); diff --git a/lib/SyncReplication.ts b/lib/SyncReplication.ts index 2b6cd255..f8073132 100644 --- a/lib/SyncReplication.ts +++ b/lib/SyncReplication.ts @@ -452,7 +452,7 @@ export async function syncData (this: PubSubManager, sync: SyncParams, clientDat /* Used to throttle and merge incomming updates */ sync.wal = new WAL({ id_fields, synced_field, throttle, batch_size, - DEBUG_MODE: this.dboBuilder.prostgles.opts.DEBUG_MODE === true, + DEBUG_MODE: this.dboBuilder.prostgles.opts.DEBUG_MODE, onSendStart: () => { sync.is_syncing = true; }, diff --git a/lib/initProstgles.ts b/lib/initProstgles.ts index 2b4bf3f8..d5db996c 100644 --- a/lib/initProstgles.ts +++ b/lib/initProstgles.ts @@ -220,18 +220,29 @@ export const initProstgles = async function(this: Prostgles, onReady: OnReadyCal } } -type GetDbConnectionArgs = Pick; -const getDbConnection = function({ dbConnection, DEBUG_MODE, dbOptions, onNotice }: GetDbConnectionArgs): { db: DB, pgp: PGP } { +type GetDbConnectionArgs = Pick; +const getDbConnection = function({ dbConnection, onQuery, DEBUG_MODE, dbOptions, onNotice }: GetDbConnectionArgs): { db: DB, pgp: PGP } { + + const onQueryOrError: undefined | ((error: any, ctx: pgPromise.IEventContext) => void) = !onQuery && !DEBUG_MODE? undefined : (error, ctx) => { + if (onQuery) { + onQuery(error, ctx); + } else if (DEBUG_MODE) { + if(error){ + console.error(error, ctx); + } else { + console.log(ctx) + } + } + }; - const onQueryOrError = typeof DEBUG_MODE === "function" ? DEBUG_MODE : DEBUG_MODE? console.log : undefined; const pgp: PGP = pgPromise({ promiseLib: promise, ...(onQueryOrError ? { - query: onQueryOrError, + query: ctx => onQueryOrError(undefined, ctx), error: onQueryOrError } : {}), - ...((onNotice || DEBUG_MODE === true) ? { + ...((onNotice || DEBUG_MODE) ? { connect: function ({ client, useCount }) { const isFresh = !useCount; if (isFresh && !client.listeners('notice').length) { diff --git a/package-lock.json b/package-lock.json index b2f638b4..20e0269d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prostgles-server", - "version": "4.2.116", + "version": "4.2.117", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "prostgles-server", - "version": "4.2.116", + "version": "4.2.117", "license": "MIT", "dependencies": { "bluebird": "^3.7.2", diff --git a/package.json b/package.json index 8dd10a31..4b640578 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prostgles-server", - "version": "4.2.116", + "version": "4.2.117", "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 14d8e5b1..5a9635da 100644 --- a/tests/server/package-lock.json +++ b/tests/server/package-lock.json @@ -21,7 +21,7 @@ }, "../..": { "name": "prostgles-server", - "version": "4.2.116", + "version": "4.2.117", "license": "MIT", "dependencies": { "bluebird": "^3.7.2",