Skip to content

Commit

Permalink
feat: add comment param to FindOptions (#8545)
Browse files Browse the repository at this point in the history
* add comment option to FindOptions.

Allows users to pass in comments for queries issued through the entity manager
like they are for the query builder.

* use promisify for node 12

* fix comment
  • Loading branch information
nathanjordan committed Jan 31, 2022
1 parent cbb61eb commit ece0da0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/find-options/FindOneOptions.ts
Expand Up @@ -8,6 +8,13 @@ import {FindConditions} from "./FindConditions";
*/
export interface FindOneOptions<Entity = any> {

/**
* Adds a comment with the supplied string in the generated query. This is
* helpful for debugging purposes, such as finding a specific query in the
* database server's logs, or for categorization using an APM product.
*/
comment?: string;

/**
* Specifies what columns should be retrieved.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/find-options/FindOptionsUtils.ts
Expand Up @@ -32,6 +32,7 @@ export class FindOptionsUtils {
possibleOptions.cache instanceof Object ||
typeof possibleOptions.cache === "boolean" ||
typeof possibleOptions.cache === "number" ||
typeof possibleOptions.comment === "string" ||
possibleOptions.lock instanceof Object ||
possibleOptions.loadRelationIds instanceof Object ||
typeof possibleOptions.loadRelationIds === "boolean" ||
Expand Down Expand Up @@ -97,6 +98,10 @@ export class FindOptionsUtils {
const metadata = qb.expressionMap.mainAlias!.metadata;

// apply all options from FindOptions
if (options.comment) {
qb.comment(options.comment);
}

if (options.withDeleted) {
qb.withDeleted();
}
Expand Down
35 changes: 35 additions & 0 deletions test/functional/repository/find-options/repository-find-options.ts
Expand Up @@ -7,6 +7,9 @@ import {Category} from "./entity/Category";
import {Post} from "./entity/Post";
import {Photo} from "./entity/Photo";
import sinon from "sinon";
import {FileLogger} from "../../../../src";
import {promisify} from "util";
import {readFile, unlink} from "fs";

describe("repository > find options", () => {

Expand Down Expand Up @@ -192,6 +195,38 @@ describe("repository > find options", () => {

});

describe("repository > find options > comment", () => {
let connections: Connection[];
const logPath = "find_comment_test.log";

before(async () => {
// TODO: would be nice to be able to do this in memory with some kind of
// test logger that buffers messages.
const logger = new FileLogger(["query"], { logPath });
connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
createLogger: () => logger,
});
});
beforeEach(() => reloadTestingDatabases(connections));
after(async () => {
closeTestingConnections(connections);
await promisify(unlink)(logPath);
});

it("repository should insert comment", () => Promise.all(connections.map(async connection => {
await connection.getRepository(User)
.find({comment: "This is a query comment."});

const logs = await promisify(readFile)(logPath);
const lines = logs.toString().split("\n");
const lastLine = lines[lines.length - 2]; // last line is blank after newline
// remove timestamp and prefix
const sql = lastLine.replace(/^.*\[QUERY\]\: /, "");
expect(sql).to.match(/^\/\* This is a query comment. \*\//);
})));
});


describe("repository > find options > cache", () => {
let connections: Connection[];
Expand Down

0 comments on commit ece0da0

Please sign in to comment.