Skip to content

Commit

Permalink
fix: cacheId not used when loading relations with take (#9469)
Browse files Browse the repository at this point in the history
* fix: cacheId not used loading relations

The cacheId is set to undefined for the main request used for pagination when we load related entities

* fix: tests
  • Loading branch information
4l3ss committed Dec 3, 2022
1 parent 81fc9a9 commit 93e6b3d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3309,9 +3309,9 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
.limit(this.expressionMap.take)
.orderBy(orderBys)
.cache(
this.expressionMap.cache
? this.expressionMap.cache
: this.expressionMap.cacheId,
this.expressionMap.cache && this.expressionMap.cacheId
? `${this.expressionMap.cacheId}-pagination`
: this.expressionMap.cache,
this.expressionMap.cacheDuration,
)
.setParameters(this.getParameters())
Expand Down
55 changes: 55 additions & 0 deletions test/functional/cache/custom-cache-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { DataSource } from "../../../src/data-source/DataSource"
import { User } from "./entity/User"
import { MockQueryResultCache } from "./provider/MockQueryResultCache"
import { Address } from "./entity/Address"

describe("custom cache provider", () => {
let connections: DataSource[]
Expand Down Expand Up @@ -277,6 +278,60 @@ describe("custom cache provider", () => {
}),
))

it("should cache results with pagination enabled properly and custom id and loaded relations", () =>
Promise.all(
connections.map(async (connection) => {
if (connection.driver.options.type === "spanner") {
return
}

const noUser = await connection.manager
.getRepository(User)
.findOne({
where: { isAdmin: false },
relations: { addresses: true },
cache: { id: "user-address", milliseconds: 2000 },
})

expect(noUser).to.be.null

// first prepare data - insert users
const user1 = new User()
user1.firstName = "Timber"
user1.lastName = "Saw"
user1.isAdmin = false
await connection.manager.save(user1)

const user1Address = new Address()
user1Address.address = "1 random street"
user1Address.user = user1
await connection.manager.save(user1Address)

const user1Cached = await connection.manager
.getRepository(User)
.findOne({
relations: { addresses: true },
where: { isAdmin: false },
cache: { id: "user-address", milliseconds: 2000 },
})
expect(user1Cached).to.be.null

const user1WithAddressWithOtherCacheId =
await connection.manager.getRepository(User).findOne({
relations: { addresses: true },
where: { isAdmin: false },
cache: {
id: "user-1-different-cache-id",
milliseconds: 2000,
},
})

expect(
user1WithAddressWithOtherCacheId?.addresses,
).to.have.length(1)
}),
))

it("should cache results with custom id and duration supplied", () =>
Promise.all(
connections.map(async (connection) => {
Expand Down
19 changes: 19 additions & 0 deletions test/functional/cache/entity/Address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
Column,
Entity,
ManyToOne,
PrimaryGeneratedColumn,
} from "../../../../src"
import { User } from "./User"

@Entity()
export class Address {
@PrimaryGeneratedColumn()
id: number

@Column()
address: string

@ManyToOne(() => User)
user: User
}
5 changes: 5 additions & 0 deletions test/functional/cache/entity/User.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Entity } from "../../../../src/decorator/entity/Entity"
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn"
import { Column } from "../../../../src/decorator/columns/Column"
import { OneToMany } from "../../../../src"
import { Address } from "./Address"

@Entity()
export class User {
Expand All @@ -15,4 +17,7 @@ export class User {

@Column()
isAdmin: boolean

@OneToMany(() => Address, (a) => a.user)
addresses: Address[]
}

0 comments on commit 93e6b3d

Please sign in to comment.