diff --git a/src/database.ts b/src/database.ts index 97cff9c..218792b 100644 --- a/src/database.ts +++ b/src/database.ts @@ -3,6 +3,7 @@ import { CommandCursor } from "./protocol/mod.ts"; import { CreateCollectionOptions, CreateUserOptions } from "./types.ts"; import { Cluster } from "./cluster.ts"; import { Document } from "../deps.ts"; +import { WriteConcern } from "./types/read_write_concern.ts"; interface ListCollectionsReponse { cursor: { @@ -30,6 +31,13 @@ export class Database { this.#cluster = cluster; } + async dropDatabase(writeConcern?: WriteConcern) { + return await this.#cluster.protocol.commandSingle(this.name, { + dropDatabase: 1, + writeConcern, + }); + } + collection(name: string): Collection { return new Collection(this.#cluster.protocol, this.name, name); } diff --git a/src/utils/srv.ts b/src/utils/srv.ts index 478f186..56bac28 100644 --- a/src/utils/srv.ts +++ b/src/utils/srv.ts @@ -71,15 +71,13 @@ export class Srv { ); if (!(srvRecord?.length > 0)) { throw new SRVError( - `Expected at least one SRV record, received ${srvRecord - ?.length} for url ${url}`, + `Expected at least one SRV record, received ${srvRecord?.length} for url ${url}`, ); } const txtRecords = await this.resolver.resolveDns(url, "TXT"); if (txtRecords?.length !== 1) { throw new SRVError( - `Expected exactly one TXT record, received ${txtRecords - ?.length} for url ${url}`, + `Expected exactly one TXT record, received ${txtRecords?.length} for url ${url}`, ); } diff --git a/tests/cases/10_command_helpers.ts b/tests/cases/10_command_helpers.ts new file mode 100644 index 0000000..b8675d5 --- /dev/null +++ b/tests/cases/10_command_helpers.ts @@ -0,0 +1,31 @@ +import { MongoClient } from "../../mod.ts"; +import { assert, assertEquals } from "../test.deps.ts"; + +Deno.test({ + name: "db.dropDatabase", + fn: async (t) => { + const client = new MongoClient(); + const databaseName = `TEST_DATABASE_MUST_NOT_MATCH_${+new Date()}`; + const db = await client.connect( + `mongodb://127.0.0.1:27017/${databaseName}`, + ); + const collectioName = `${databaseName}_collection`; + + // To create database physically + await db.createCollection<{ foo: string }>(`${collectioName}`); + + await t.step("test if the database is dropped", async () => { + // A sanity check to test existence of the collection inside the test db + assertEquals((await db.listCollectionNames()).length, 1); + const result = await db.dropDatabase(); + + assert(result); + assertEquals(result.ok, 1); + + // The collection inside the test db must not exist + assertEquals((await db.listCollectionNames()).length, 0); + }); + + client.close(); + }, +});