-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle Undefined values in driver URL options (#6925)
the `buildDriverOptions` parses the URL & will have undefined values which it ends up overwriting non-undefined values with - which isn't great
- Loading branch information
1 parent
a09fb7f
commit 6fa2df5
Showing
4 changed files
with
122 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Entity, ObjectID, ObjectIdColumn, Column } from "../../../../src"; | ||
|
||
@Entity("warnings") | ||
export class Warn { | ||
@ObjectIdColumn() | ||
id!: ObjectID | ||
|
||
@Column() | ||
guild!: string; | ||
|
||
@Column() | ||
user!: string; | ||
|
||
@Column() | ||
moderator!: string; | ||
|
||
@Column() | ||
reason!: string; | ||
|
||
@Column() | ||
createdAt!: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { expect } from "chai"; | ||
import { | ||
closeTestingConnections, reloadTestingDatabases, | ||
setupTestingConnections | ||
} from "../../utils/test-utils"; | ||
import {MongoDriver} from "../../../src/driver/mongodb/MongoDriver"; | ||
import {Connection, ConnectionOptions, createConnection, MongoClient} from "../../../src"; | ||
import {Warn} from "./entity/Warn"; | ||
|
||
describe("github issues > #6900 MongoDB ConnectionManager doesn't select given database, creates new database \"test\" instead", () => { | ||
let connections: Connection[] = []; | ||
afterEach(async () => { | ||
await closeTestingConnections(connections); | ||
connections.length = 0; | ||
}); | ||
|
||
it("should connect to the expected database", async () => { | ||
const options = setupTestingConnections({ enabledDrivers: ["mongodb"] }); | ||
|
||
if (options.length === 0) { | ||
// Skip if we can't grab the mongodb | ||
return; | ||
} | ||
|
||
const connection = await createConnection({ | ||
...options[0], | ||
url: 'mongodb://localhost', | ||
database: 'foo' | ||
} as ConnectionOptions); | ||
connections.push(connection); | ||
|
||
await reloadTestingDatabases(connections); | ||
|
||
const mongoDriver = connection.driver as MongoDriver ; | ||
const client = (mongoDriver.queryRunner!.databaseConnection as any) as MongoClient; | ||
|
||
expect(client.db().databaseName).to.be.equal('foo'); | ||
expect(mongoDriver.database).to.be.equal('foo'); | ||
}); | ||
|
||
it("should write data to the correct database", async () => { | ||
const options = setupTestingConnections({ enabledDrivers: ["mongodb"] }); | ||
|
||
if (options.length === 0) { | ||
// Skip if we can't grab the mongodb | ||
return; | ||
} | ||
|
||
const connection = await createConnection({ | ||
...options[0], | ||
entities: [ Warn ], | ||
url: 'mongodb://localhost', | ||
database: 'foo' | ||
} as ConnectionOptions); | ||
connections.push(connection); | ||
|
||
await reloadTestingDatabases(connections); | ||
|
||
const repo = connection.getRepository(Warn) | ||
|
||
await repo.insert({ | ||
id: Math.floor(Math.random() * 1000000), | ||
guild: "Hello", | ||
user: "WORLD", | ||
moderator: "Good Moderator", | ||
reason: "For Mongo not writing correctly to the databsae!", | ||
createdAt: new Date() | ||
}); | ||
|
||
const mongoDriver = connection.driver as MongoDriver ; | ||
const client = (mongoDriver.queryRunner!.databaseConnection as any) as MongoClient; | ||
|
||
expect(await client.db('foo').collection('warnings').count({})).to.be.greaterThan(0); | ||
}) | ||
}); |