fix: prevent error when replication is undefined (#11423)

* fix: prevent error when replication is undefined

* fix format

* add test

* update test name

* fix test

* fix test

* skip test

* add unit test

* fix unit test

* fix unit test
This commit is contained in:
Caíque de Castro Soares da Silva 2025-04-25 09:30:42 -03:00 committed by GitHub
parent b9ddd14298
commit 61a6f971af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 110 additions and 1 deletions

View File

@ -750,7 +750,10 @@ export class DataSource {
* Get the replication mode SELECT queries should use for this datasource by default
*/
defaultReplicationModeForReads(): ReplicationMode {
if ("replication" in this.driver.options) {
if (
"replication" in this.driver.options &&
this.driver.options.replication
) {
const value = (
this.driver.options.replication as {
defaultMode?: ReplicationMode

View File

@ -195,4 +195,54 @@ describe("Connection replication", () => {
expect(result[0].current_setting).to.equal("master")
})
})
describe("with undefined replication", function () {
let connection: DataSource
beforeEach(async () => {
connection = (
await createTestingConnections({
entities: [Post, Category],
enabledDrivers: ["postgres"],
schemaCreate: true,
dropSchema: true,
driverSpecific: {
replication: undefined,
},
})
)[0]
const post = new Post()
post.title = "TypeORM Intro"
await connection
.createQueryBuilder()
.insert()
.into(Post)
.values(post)
.execute()
})
afterEach(() => closeTestingConnections([connection]))
it("query runners should go to the available instance", async () => {
const queryRunner = connection.createQueryRunner()
expect(queryRunner.getReplicationMode()).to.equal("master")
await expectCurrentApplicationName(queryRunner, "")
await queryRunner.release()
})
it("read queries should go to the available instance", async () => {
const result = await connection.manager
.createQueryBuilder(Post, "post")
.select("id")
.addSelect(
"current_setting('application_name')",
"current_setting",
)
.execute()
expect(result[0].current_setting).to.equal("")
})
})
})

View File

@ -0,0 +1,12 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"
@Entity({
name: "post",
})
export class Post {
@PrimaryGeneratedColumn()
id: number
@Column()
title: string
}

View File

@ -0,0 +1,44 @@
import { expect } from "chai"
import { DataSource, Repository } from "../../../src"
import { PostgresConnectionOptions } from "../../../src/driver/postgres/PostgresConnectionOptions"
import {
closeTestingConnections,
reloadTestingDatabases,
setupSingleTestingConnection,
} from "../../utils/test-utils"
import { Post } from "./entity/Post"
describe("github issues > #11423", () => {
let dataSource: DataSource
let repository: Repository<Post>
before(async () => {
const options = setupSingleTestingConnection("postgres", {
entities: [Post],
}) as PostgresConnectionOptions
if (!options) return
dataSource = new DataSource({
...options,
replication: undefined,
})
await dataSource.initialize()
})
beforeEach(async () => {
if (!dataSource) return
await reloadTestingDatabases([dataSource])
})
after(() => closeTestingConnections([dataSource]))
it("allow replication to be undefined", async () => {
if (!dataSource) return
repository = dataSource.getRepository(Post)
const posts = await repository.find({
order: {
title: "DESC",
},
})
expect(posts).to.be.an("array")
})
})