Skip to content

Commit

Permalink
add createIndexes prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
manyuanrong committed Mar 15, 2020
1 parent bc524af commit 92ab132
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
54 changes: 54 additions & 0 deletions src/command/indexes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use crate::*;
use mongodb::options::IndexModel;
use serde_json::Value;

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct CreateIndexesArgs {
db_name: String,
collection_name: String,
models: Vec<IndexModelArgs>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct IndexModelArgs {
keys: Value,
options: Option<Value>,
}

impl IndexModelArgs {
fn part(self) -> (Value, Option<Value>) {
(self.keys, self.options)
}
}

pub fn create_indexes(command: Command) -> CoreOp {
let fut = async move {
let client = command.get_client();
let data = command.data;
let args: CreateIndexesArgs = serde_json::from_slice(data.unwrap().as_ref()).unwrap();
let db_name = args.db_name;
let collection_name = args.collection_name;
let models = args.models;
let models: Vec<IndexModel> = models
.into_iter()
.map(|model| {
let (keys, options) = model.part();
IndexModel {
keys: util::json_to_document(keys).unwrap(),
options: util::maybe_json_to_document(options),
}
})
.collect();

println!("{:?}", models);

let database = client.database(&db_name);
let collection = database.collection(&collection_name);

let result = collection.create_indexes(models).unwrap();
Ok(util::async_result(&command.args, result))
};
CoreOp::Async(fut.boxed())
}
2 changes: 2 additions & 0 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod aggregation;
mod connect;
mod delete;
mod find;
mod indexes;
mod insert;
mod list_collection_names;
mod list_database_names;
Expand All @@ -11,6 +12,7 @@ pub use aggregation::aggregate;
pub use connect::{connect_with_options, connect_with_uri};
pub use delete::delete;
pub use find::find;
pub use indexes::create_indexes;
pub use insert::{insert_many, insert_one};
pub use list_collection_names::list_collection_names;
pub use list_database_names::list_database_names;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub enum CommandType {
Delete,
Aggregate,
Update,
CreateIndexes,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -101,6 +102,7 @@ fn op_command(data: &[u8], zero_copy: Option<ZeroCopyBuf>) -> CoreOp {
CommandType::Delete => command::delete,
CommandType::Update => command::update,
CommandType::Aggregate => command::aggregate,
CommandType::CreateIndexes => command::create_indexes,
};

executor(Command::new(args, zero_copy))
Expand Down
10 changes: 10 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ test(async function testDeleteMany() {
assertEquals(deleteCount, 2);
});

// TODO mongdb_rust official library has not implemented this feature
// test(async function testCreateIndexes() {
// const db = getClient().database("test");
// const collection = db.collection("mongo_indexes");
// const result = await collection.createIndexes([
// { keys: { created_at: 1 }, options: { expireAfterSeconds: 10000 } }
// ]);
// console.log(result);
// });

if (await exists(".deno_plugins")) {
await Deno.remove(".deno_plugins", { recursive: true });
}
Expand Down
30 changes: 30 additions & 0 deletions ts/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,34 @@ export class Collection {
);
return docs as T[];
}

public async createIndexes(
models: {
keys: Object;
options?: {
background?: boolean;
unique?: boolean;
name?: string;
partialFilterExpression?: Object;
sparse?: boolean;
expireAfterSeconds?: number;
storageEngine?: Object;
};
}[]
): Promise<string[]> {
const docs = await dispatchAsync(
{
command_type: CommandType.CreateIndexes,
client_id: this.client.clientId
},
encode(
JSON.stringify({
dbName: this.dbName,
collectionName: this.collectionName,
models
})
)
);
return docs as string[];
}
}
3 changes: 2 additions & 1 deletion ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export enum CommandType {
InsertMany = "InsertMany",
Delete = "Delete",
Update = "Update",
Aggregate = "Aggregate"
Aggregate = "Aggregate",
CreateIndexes = "CreateIndexes"
}

export interface ObjectId {
Expand Down

0 comments on commit 92ab132

Please sign in to comment.