Skip to content

Commit

Permalink
fix(core): respect logging options in em.count
Browse files Browse the repository at this point in the history
Closes #5085
  • Loading branch information
B4nan committed Jan 8, 2024
1 parent 2bd612e commit 481d02e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/core/src/drivers/IDatabaseDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export interface LockOptions extends DriverMethodOptions {
lockMode?: LockMode;
lockVersion?: number | Date;
lockTableAliases?: string[];
logging?: LoggingOptions;
}

export interface DriverMethodOptions {
Expand Down
8 changes: 4 additions & 4 deletions packages/knex/src/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection
}

protected async wrapVirtualExpressionInSubquery<T extends object>(meta: EntityMetadata<T>, expression: string, where: FilterQuery<T>, options: FindOptions<T, any>, type: QueryType): Promise<T[] | number> {
const qb = this.createQueryBuilder(meta.className, options?.ctx, options.connectionType, options.convertCustomTypes)
const qb = this.createQueryBuilder(meta.className, options?.ctx, options.connectionType, options.convertCustomTypes, options.logging)
.indexHint(options.indexHint!)
.comment(options.comments!)
.hintComment(options.hintComments!);
Expand Down Expand Up @@ -410,7 +410,7 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection
return this.countVirtual<T>(entityName, where, options);
}

const qb = this.createQueryBuilder<T>(entityName, options.ctx, options.connectionType, false)
const qb = this.createQueryBuilder<T>(entityName, options.ctx, options.connectionType, false, options.logging)
.indexHint(options.indexHint!)
.comment(options.comments!)
.hintComment(options.hintComments!)
Expand Down Expand Up @@ -858,7 +858,7 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection
const pivotProp2 = pivotMeta.relations[prop.owner ? 0 : 1];
const ownerMeta = this.metadata.find(pivotProp2.type)!;
options = { ...options };
const qb = this.createQueryBuilder<T>(prop.pivotEntity, ctx, options.connectionType)
const qb = this.createQueryBuilder<T>(prop.pivotEntity, ctx, options.connectionType, undefined, options?.logging)
.withSchema(this.getSchemaName(pivotMeta, options))
.indexHint(options.indexHint!)
.comment(options.comments!)
Expand Down Expand Up @@ -1243,7 +1243,7 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection

override async lockPessimistic<T extends object>(entity: T, options: LockOptions): Promise<void> {
const meta = helper(entity).__meta;
const qb = this.createQueryBuilder((entity as object).constructor.name, options.ctx).withSchema(options.schema ?? meta.schema);
const qb = this.createQueryBuilder((entity as object).constructor.name, options.ctx, undefined, undefined, options.logging).withSchema(options.schema ?? meta.schema);
const cond = Utils.getPrimaryKeyCond(entity, meta.primaryKeys);
qb.select(raw('1')).where(cond!).setLockMode(options.lockMode, options.lockTableAliases);
await this.rethrow(qb.execute());
Expand Down
4 changes: 2 additions & 2 deletions packages/mongodb/src/MongoDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class MongoDriver extends DatabaseDriver<MongoConnection> {
const { orderBy: newOrderBy, where: newWhere } = this.processCursorOptions(meta, options, options.orderBy!);
const newWhereConverted = this.renameFields(entityName, newWhere as FilterQuery<T>, true);
const orderBy = Utils.asArray(newOrderBy).map(order => this.renameFields(entityName, order));
const res = await this.rethrow(this.getConnection('read').find(entityName, andWhere(where, newWhereConverted), orderBy, options.limit, options.offset, fields, options.ctx));
const res = await this.rethrow(this.getConnection('read').find(entityName, andWhere(where, newWhereConverted), orderBy, options.limit, options.offset, fields, options.ctx, options.logging));

if (isCursorPagination && !first && !!last) {
res.reverse();
Expand Down Expand Up @@ -105,7 +105,7 @@ export class MongoDriver extends DatabaseDriver<MongoConnection> {
const orderBy = Utils.asArray(options.orderBy).map(orderBy =>
this.renameFields(entityName, orderBy, false),
);
const res = await this.rethrow(this.getConnection('read').find(entityName, where, orderBy, 1, undefined, fields, options.ctx));
const res = await this.rethrow(this.getConnection('read').find(entityName, where, orderBy, 1, undefined, fields, options.ctx, options.logging));

return this.mapResult<T>(res[0], this.metadata.find(entityName)!);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/features/logging/logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ describe('logging', () => {
loggerContext: { bar: 456, new: true },
});

await em.count(Example, { id: 1 }, {
logging: { enabled: false },
});

await em.findOne(Example, { id: 1 }, { refresh: true });

expect(mock.mock.calls).toHaveLength(2);
Expand Down

0 comments on commit 481d02e

Please sign in to comment.