From d2d664f5795d3426f49f604a4a0c21477d834d96 Mon Sep 17 00:00:00 2001 From: Maksim Sitnikov Date: Sun, 21 Sep 2025 23:21:05 +0300 Subject: [PATCH 1/2] feat: add cancelOnTimeout and useLimitInFirst params to getModel --- lib/core.ts | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/core.ts b/lib/core.ts index 5a34bae..ccc2a27 100644 --- a/lib/core.ts +++ b/lib/core.ts @@ -1,6 +1,6 @@ import type {Knex} from 'knex'; import _ from 'lodash'; -import {Model} from 'objection'; +import {type Constructor, Model} from 'objection'; import {defaultDispatcherOptions, defaultExLogger, defaultKnexOptions} from './constants'; import {PGDispatcher} from './dispatcher'; @@ -20,9 +20,14 @@ export interface CoreDBConstructorArgs { logger?: ExLogger; } -export function getModel(): typeof BaseModel { +export type GetModelParams = {cancelOnTimeout?: boolean; useLimitInFirst?: boolean}; + +export function getModel(params: GetModelParams = {}): typeof BaseModel { let _db: PGDispatcher; + const cancelOnTimeout = Boolean(params.cancelOnTimeout); + const useLimitInFirst = Boolean(params.useLimitInFirst); + class CoreBaseModel extends Model { static set db(value: PGDispatcher) { if (!_db) { @@ -45,6 +50,28 @@ export function getModel(): typeof BaseModel { get replica() { return _db.replica; } + + static query( + this: Constructor, + ...args: Parameters> + ): ReturnType> { + const query = super.query(...args); + + if (cancelOnTimeout) { + const originalTimeout = query.timeout; + + query.timeout = (ms, options) => { + const optionsWithCancel = {cancel: true, ...(options ?? {})}; + return originalTimeout.apply(query, [ms, optionsWithCancel]); + }; + } + + return query; + } + + static get useLimitInFirst() { + return useLimitInFirst; + } } return CoreBaseModel; From a897020ce973dccb98a1a0f11e43142678e309ef Mon Sep 17 00:00:00 2001 From: Maksim Sitnikov Date: Mon, 22 Sep 2025 15:48:36 +0300 Subject: [PATCH 2/2] fix: after review --- lib/core.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/core.ts b/lib/core.ts index ccc2a27..e076748 100644 --- a/lib/core.ts +++ b/lib/core.ts @@ -13,15 +13,16 @@ export interface CoreDBDispatcherOptions { beforeTerminate?: () => Promise; } +export type GetModelParams = {cancelOnTimeout?: boolean; useLimitInFirst?: boolean}; + export interface CoreDBConstructorArgs { connectionString: string; dispatcherOptions?: CoreDBDispatcherOptions; knexOptions?: Knex.Config; logger?: ExLogger; + modelParams?: GetModelParams; } -export type GetModelParams = {cancelOnTimeout?: boolean; useLimitInFirst?: boolean}; - export function getModel(params: GetModelParams = {}): typeof BaseModel { let _db: PGDispatcher; @@ -82,6 +83,7 @@ export function initDB({ dispatcherOptions, knexOptions = {}, logger = defaultExLogger, + modelParams, }: CoreDBConstructorArgs) { if (!connectionString) { throw new Error('Empty connection string'); @@ -110,7 +112,7 @@ export function initDB({ process.on('SIGINT', terminate); - const CoreBaseModel = getModel(); + const CoreBaseModel = getModel(modelParams); CoreBaseModel.db = db; const helpers = {