Skip to content

Commit

Permalink
log query ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Sep 29, 2024
1 parent 2c84442 commit 961f1eb
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 56 deletions.
3 changes: 2 additions & 1 deletion lib/DboBuilder/DboBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ export class DboBuilder {
const transaction = await this.db.tx(t => {
const dbTX: DbTxTableHandlers & Pick<DBHandlerServer, "sql"> = {};
this.tablesOrViews?.map(tov => {
dbTX[tov.name] = new (tov.is_view ? ViewHandler : TableHandler)(this.db, tov, this, { t, dbTX }, this.shortestJoinPaths);
const handlerClass = tov.is_view ? ViewHandler : TableHandler;
dbTX[tov.name] = new handlerClass(this.db, tov, this, { t, dbTX }, this.shortestJoinPaths);
});
dbTX.sql = (q, args, opts, localP) => this.runSQL(q, args, opts, { tx: { dbTX, t }, ...(localP ?? {}) })

Expand Down
4 changes: 3 additions & 1 deletion lib/ProstglesTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
import type { Server } from "socket.io";
import { Publish, PublishMethods, PublishParams } from "./PublishParser/PublishParser";
import { DB } from "./Prostgles";
import pgPromise from "pg-promise";
import pg from "pg-promise/typescript/pg-subset";

/**
* Allows uploading and downloading files.
Expand Down Expand Up @@ -116,7 +118,7 @@ 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;
DEBUG_MODE?: boolean | ((error: any, ctx: pgPromise.IEventContext<pg.IClient>) => void);
watchSchemaType?:

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/PubSubManager/getCreatePubSubManagerError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DboBuilder } from "../DboBuilder/DboBuilder";

export const getCreatePubSubManagerError = async (dboBuilder: DboBuilder): Promise<string | undefined> => {
const db = dboBuilder.db;

const canExecute = await getCanExecute(db)
if (!canExecute) return "Cannot run EXECUTE statements on this connection";

Expand All @@ -19,7 +20,6 @@ export const getCreatePubSubManagerError = async (dboBuilder: DboBuilder): Promi
const allGood = await db.tx(async t => {
await t.none(`
DROP SCHEMA IF EXISTS prostgles;
ROLLBACK;
CREATE SCHEMA IF NOT EXISTS prostgles;
ROLLBACK;
`);
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);
const initQuery = getInitQuery(this.prostgles.opts.DEBUG_MODE === true);
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,
DEBUG_MODE: this.dboBuilder.prostgles.opts.DEBUG_MODE === true,
onSendStart: () => {
sync.is_syncing = true;
},
Expand Down
39 changes: 18 additions & 21 deletions lib/initProstgles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { DbTableInfo, PublishParser } from "./PublishParser/PublishParser";
import { sleep } from "./utils";
import { SchemaWatch } from "./SchemaWatch/SchemaWatch";
import { ProstglesInitOptions } from "./ProstglesTypes";
import { type } from "os";

export type DbConnection = string | pg.IConnectionParameters<pg.IClient>;
export type DbConnectionOpts = pg.IDefaults;
Expand Down Expand Up @@ -83,14 +84,16 @@ export const initProstgles = async function(this: Prostgles, onReady: OnReadyCal
const application_name = `prostgles ${this.appId} ${existingAppName}`;

/* 1. Connect to db */
const { db, pgp } = getDbConnection({ ...conObj, application_name }, this.opts.dbOptions, this.opts.DEBUG_MODE,
notice => {
const { db, pgp } = getDbConnection({
...this.opts,
dbConnection: { ...conObj, application_name },
onNotice: notice => {
if (this.opts.onNotice) this.opts.onNotice(notice);
if (this.dbEventsManager) {
this.dbEventsManager.onNotice(notice)
}
}
);
});
this.db = db;
this.pgp = pgp;
this.isSuperUser = await getIsSuperUser(db);
Expand Down Expand Up @@ -218,25 +221,19 @@ export const initProstgles = async function(this: Prostgles, onReady: OnReadyCal
}
}

function getDbConnection(dbConnection: DbConnection, options: DbConnectionOpts | undefined, debugQueries = false, onNotice: ProstglesInitOptions["onNotice"]): { db: DB, pgp: PGP } {
type GetDbConnectionArgs = Pick<ProstglesInitOptions, "DEBUG_MODE" | "dbConnection" | "dbOptions" | "onNotice">;
const getDbConnection = function({ dbConnection, DEBUG_MODE, dbOptions, onNotice }: GetDbConnectionArgs): { db: DB, pgp: PGP } {

const onQueryOrError = typeof DEBUG_MODE === "function" ? DEBUG_MODE : DEBUG_MODE? console.log : undefined;
const pgp: PGP = pgPromise({

promiseLib: promise,
...(debugQueries ? {
query: function (ctx) {
console.log({
...pickKeys(ctx, ["params", "query"]),
});
},
error: (error, ctx) => {
console.log({
...pickKeys(ctx, ["params", "query"]),
error
});
}
...(onQueryOrError ? {
query: onQueryOrError,
error: onQueryOrError
} : {}),
...((onNotice || debugQueries) ? {
connect: function ({ client, dc, useCount }) {
...((onNotice || onQueryOrError) ? {
connect: function ({ client, useCount }) {
const isFresh = !useCount;
if (isFresh && !client.listeners('notice').length) {
client.on('notice', function (msg) {
Expand All @@ -259,7 +256,7 @@ function getDbConnection(dbConnection: DbConnection, options: DbConnectionOpts |
},
} : {})
});
pgp.pg.defaults.max = 70;
// pgp.pg.defaults.max = 70;

// /* Casts count/sum/max to bigint. Needs rework to remove casting "+count" and other issues; */
// pgp.pg.types.setTypeParser(20, BigInt);
Expand All @@ -279,8 +276,8 @@ function getDbConnection(dbConnection: DbConnection, options: DbConnectionOpts |
pgp.pg.types.setTypeParser(pgp.pg.types.builtins.DATE, v => v); // date


if (options) {
Object.assign(pgp.pg.defaults, options);
if (dbOptions) {
Object.assign(pgp.pg.defaults, dbOptions);
}

return {
Expand Down
53 changes: 28 additions & 25 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.2.114",
"version": "4.2.115",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -41,7 +41,7 @@
"file-type": "^18.5.0",
"pg": "^8.11.5",
"pg-cursor": "^2.11.0",
"pg-promise": "^11.8.0",
"pg-promise": "^11.9.1",
"prostgles-types": "^4.0.89"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions 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 961f1eb

Please sign in to comment.