Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add helper method dropDatabase #363

Merged
merged 2 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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<T = Document>(name: string): Collection<T> {
return new Collection<T>(this.#cluster.protocol, this.name, name);
}
Expand Down
6 changes: 2 additions & 4 deletions src/utils/srv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
);
}

Expand Down
31 changes: 31 additions & 0 deletions tests/cases/10_command_helpers.ts
Original file line number Diff line number Diff line change
@@ -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();
},
});