Skip to content

Commit

Permalink
tidy log query ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Sep 29, 2024
1 parent 39ce4da commit 74bebb8
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/Prostgles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion lib/ProstglesTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ export type ProstglesInitOptions<S = void, SUser extends SessionUser = SessionUs
onSocketConnect?: (args: AuthRequestParams<S, SUser> & { socket: PRGLIOSocket }) => void | Promise<void>;
onSocketDisconnect?: (args: AuthRequestParams<S, SUser> & { socket: PRGLIOSocket }) => void | Promise<void>;
auth?: Auth<S, SUser>;
DEBUG_MODE?: boolean | ((error: any, ctx: pgPromise.IEventContext<pg.IClient>) => void);
DEBUG_MODE?: boolean;
onQuery?: (error: any, ctx: pgPromise.IEventContext<pg.IClient>) => void;
watchSchemaType?:

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/PubSubManager/getPubSubManagerInitQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ COMMIT;
*/
export const getPubSubManagerInitQuery = async function(this: DboBuilder): Promise<string | undefined> {

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);
Expand Down
2 changes: 1 addition & 1 deletion lib/SyncReplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down
21 changes: 16 additions & 5 deletions lib/initProstgles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,29 @@ export const initProstgles = async function(this: Prostgles, onReady: OnReadyCal
}
}

type GetDbConnectionArgs = Pick<ProstglesInitOptions, "DEBUG_MODE" | "dbConnection" | "dbOptions" | "onNotice">;
const getDbConnection = function({ dbConnection, DEBUG_MODE, dbOptions, onNotice }: GetDbConnectionArgs): { db: DB, pgp: PGP } {
type GetDbConnectionArgs = Pick<ProstglesInitOptions, "DEBUG_MODE" | "onQuery" | "dbConnection" | "dbOptions" | "onNotice">;
const getDbConnection = function({ dbConnection, onQuery, DEBUG_MODE, dbOptions, onNotice }: GetDbConnectionArgs): { db: DB, pgp: PGP } {

const onQueryOrError: undefined | ((error: any, ctx: pgPromise.IEventContext<pg.IClient>) => 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) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 74bebb8

Please sign in to comment.