diff --git a/src/command/indexes.rs b/src/command/indexes.rs new file mode 100644 index 00000000..e26862ee --- /dev/null +++ b/src/command/indexes.rs @@ -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, +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +struct IndexModelArgs { + keys: Value, + options: Option, +} + +impl IndexModelArgs { + fn part(self) -> (Value, Option) { + (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 = 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()) +} diff --git a/src/command/mod.rs b/src/command/mod.rs index 80281da9..dc679a19 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -2,6 +2,7 @@ mod aggregation; mod connect; mod delete; mod find; +mod indexes; mod insert; mod list_collection_names; mod list_database_names; @@ -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; diff --git a/src/lib.rs b/src/lib.rs index dbf2a294..ec227e4a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,7 @@ pub enum CommandType { Delete, Aggregate, Update, + CreateIndexes, } #[derive(Deserialize)] @@ -101,6 +102,7 @@ fn op_command(data: &[u8], zero_copy: Option) -> CoreOp { CommandType::Delete => command::delete, CommandType::Update => command::update, CommandType::Aggregate => command::aggregate, + CommandType::CreateIndexes => command::create_indexes, }; executor(Command::new(args, zero_copy)) diff --git a/test.ts b/test.ts index 5bf27181..1b9244e7 100644 --- a/test.ts +++ b/test.ts @@ -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 }); } diff --git a/ts/collection.ts b/ts/collection.ts index c001a695..6aeccdec 100644 --- a/ts/collection.ts +++ b/ts/collection.ts @@ -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 { + 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[]; + } } diff --git a/ts/types.ts b/ts/types.ts index 6babfc1f..9b2b629b 100644 --- a/ts/types.ts +++ b/ts/types.ts @@ -10,7 +10,8 @@ export enum CommandType { InsertMany = "InsertMany", Delete = "Delete", Update = "Update", - Aggregate = "Aggregate" + Aggregate = "Aggregate", + CreateIndexes = "CreateIndexes" } export interface ObjectId {