Skip to content

Commit

Permalink
feat support close clinet
Browse files Browse the repository at this point in the history
  • Loading branch information
manyuanrong committed Aug 11, 2020
1 parent 3f5b890 commit fd09868
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 21 deletions.
13 changes: 13 additions & 0 deletions src/command/close.rs
@@ -0,0 +1,13 @@
use crate::*;

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CloseResult {
success: bool,
}

pub fn close(command: Command) -> util::JsonResult<CloseResult> {
let client_id: &usize = &command.args.client_id.unwrap();
CLIENTS.lock().map_err(|e| e.to_string())?.remove(client_id);
Ok(CloseResult { success: true })
}
2 changes: 2 additions & 0 deletions src/command/mod.rs
@@ -1,4 +1,5 @@
mod aggregation;
mod close;
mod connect;
mod count;
mod delete;
Expand All @@ -11,6 +12,7 @@ mod list_database_names;
mod update;

pub use aggregation::aggregate;
pub use close::close;
pub use connect::{connect_with_options, connect_with_uri};
pub use count::count;
pub use delete::delete;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -35,6 +35,7 @@ lazy_static! {
pub enum CommandType {
ConnectWithOptions,
ConnectWithUri,
Close,
ListDatabases,
Find,
ListCollectionNames,
Expand Down Expand Up @@ -114,6 +115,7 @@ fn op_command(_interface: &mut dyn Interface, zero_copy: &mut [ZeroCopyBuf]) ->
match args2.command_type {
CommandType::ConnectWithOptions => util::sync_op(command::connect_with_options, command),
CommandType::ConnectWithUri => util::sync_op(command::connect_with_uri, command),
CommandType::Close => util::sync_op(command::close, command),
CommandType::ListDatabases => util::async_op(command::list_database_names, command),
CommandType::ListCollectionNames => util::async_op(command::list_collection_names, command),
CommandType::Find => util::async_op(command::find, command),
Expand Down
53 changes: 35 additions & 18 deletions test.ts
@@ -1,13 +1,13 @@
import { cargoBuild } from "./build.ts";
import { init } from "./ts/util.ts";
import { MongoClient } from "./ts/client.ts";
import {
assert,
assertEquals,
exists,
assertThrowsAsync,
exists,
} from "./test.deps.ts";
import { MongoClient } from "./ts/client.ts";
import { ObjectId } from "./ts/types.ts";
import { init } from "./ts/util.ts";
interface IUser {
username: string;
password: string;
Expand All @@ -17,9 +17,13 @@ interface IUser {
const { test } = Deno;
const dateNow = Date.now();

let testClient: MongoClient | undefined;

function getClient(): MongoClient {
if (testClient) return testClient;
const client = new MongoClient();
client.connectWithUri("mongodb://localhost:27017");
testClient = client;
return client;
}

Expand Down Expand Up @@ -73,13 +77,17 @@ test("testInsertOne", async () => {
test("testUpsertOne", async () => {
const db = getClient().database("test");
const users = db.collection<IUser>("mongo_test_users");
const { upsertedId } = await users.updateOne({
_id: ObjectId("aaaaaaaaaaaaaaaaaaaaaaaa"),
}, {
username: "user1",
password: "pass1",
date: new Date(dateNow),
}, { upsert: true });
const { upsertedId } = await users.updateOne(
{
_id: ObjectId("aaaaaaaaaaaaaaaaaaaaaaaa"),
},
{
username: "user1",
password: "pass1",
date: new Date(dateNow),
},
{ upsert: true }
);

assert(upsertedId);
assertEquals(Object.keys(upsertedId), ["$oid"]);
Expand Down Expand Up @@ -111,7 +119,7 @@ test("testInsertOneTwice", async () => {
username: "user1",
}) as any,
undefined,
"E11000",
"E11000"
);
});

Expand Down Expand Up @@ -162,11 +170,14 @@ test("testFindOr", async () => {
const db = getClient().database("test");
const users = db.collection<IUser>("mongo_test_users");
const user1 = await users.find({
$or: [{
password: "pass1",
}, {
password: "pass2",
}],
$or: [
{
password: "pass1",
},
{
password: "pass2",
},
],
});

assert(user1 instanceof Array);
Expand All @@ -178,7 +189,7 @@ test("testFind", async () => {
const users = db.collection("mongo_test_users");
const findUsers = await users.find(
{ username: "many" },
{ skip: 1, limit: 1 },
{ skip: 1, limit: 1 }
);
assert(findUsers instanceof Array);
assertEquals(findUsers.length, 1);
Expand Down Expand Up @@ -209,7 +220,7 @@ test("testUpdateMany", async () => {
const users = db.collection("mongo_test_users");
const result = await users.updateMany(
{ username: "many" },
{ $set: { username: "MANY" } },
{ $set: { username: "MANY" } }
);
assertEquals(result, { matchedCount: 2, modifiedCount: 2, upsertedId: null });
});
Expand Down Expand Up @@ -238,6 +249,12 @@ test("testDistinct", async () => {
// console.log(result);
// });

test("testClose", async () => {
const result = getClient().close();
assertEquals(result, { success: true });
testClient = undefined;
});

if (await exists(".deno_plugins")) {
await Deno.remove(".deno_plugins", { recursive: true });
}
Expand Down
15 changes: 12 additions & 3 deletions ts/client.ts
@@ -1,6 +1,6 @@
import { Database } from "./database.ts";
import { CommandType } from "./types.ts";
import { decode, dispatch, dispatchAsync, encode } from "./util.ts";
import { dispatch, dispatchAsync, encode } from "./util.ts";

export interface ClientOptions {
/**
Expand Down Expand Up @@ -100,15 +100,15 @@ export class MongoClient {
connectWithUri(uri: string) {
const data = dispatch(
{ command_type: CommandType.ConnectWithUri },
encode(uri),
encode(uri)
) as ConnectResult;
this._clientId = data.clientId;
}

connectWithOptions(options: ClientOptions) {
const data = dispatch(
{ command_type: CommandType.ConnectWithOptions },
encode(JSON.stringify(options)),
encode(JSON.stringify(options))
) as ConnectResult;
this._clientId = data.clientId;
}
Expand All @@ -120,6 +120,15 @@ export class MongoClient {
})) as string[];
}

close() {
return dispatch({
command_type: CommandType.Close,
client_id: this._clientId,
}) as {
success: boolean;
};
}

database(name: string): Database {
return new Database(this, name);
}
Expand Down
1 change: 1 addition & 0 deletions ts/types.ts
@@ -1,6 +1,7 @@
export enum CommandType {
ConnectWithUri = "ConnectWithUri",
ConnectWithOptions = "ConnectWithOptions",
Close = "Close",
ListDatabases = "ListDatabases",
ListCollectionNames = "ListCollectionNames",
Find = "Find",
Expand Down

0 comments on commit fd09868

Please sign in to comment.