From 9605a2cd88bd271f0bd86bc9dfccdb5a37d272f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Fri, 22 Nov 2019 11:18:46 +0100 Subject: [PATCH] Make possible to use a custom uid and simplify the usage --- README.md | 2 +- meilidb-http/src/routes/index.rs | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 23213d378..81cf68216 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ MeiliDB can serve multiple indexes, with different kinds of documents, therefore, it is required to create the index before sending documents to it. ```bash -curl -i -X POST 'http://127.0.0.1:8080/indexes/movies' +curl -i -X POST 'http://127.0.0.1:8080/indexes' --data '{ "name": "Movies", "uid": "movies" }' ``` Now that the server knows about our brand new index, we can send it data. diff --git a/meilidb-http/src/routes/index.rs b/meilidb-http/src/routes/index.rs index 799e56d53..4e116af82 100644 --- a/meilidb-http/src/routes/index.rs +++ b/meilidb-http/src/routes/index.rs @@ -123,6 +123,7 @@ pub async fn get_index(ctx: Context) -> SResult { #[serde(rename_all = "camelCase", deny_unknown_fields)] struct IndexCreateRequest { name: String, + uid: Option, schema: Option, } @@ -148,14 +149,17 @@ pub async fn create_index(mut ctx: Context) -> SResult { let db = &ctx.state().db; - let generated_uid = loop { - let uid = generate_uid(); - if db.open_index(&uid).is_none() { - break uid; - } + let uid = match body.uid { + Some(uid) => uid, + None => loop { + let uid = generate_uid(); + if db.open_index(&uid).is_none() { + break uid; + } + }, }; - let created_index = match db.create_index(&generated_uid) { + let created_index = match db.create_index(&uid) { Ok(index) => index, Err(e) => return Err(ResponseError::create_index(e)), }; @@ -189,7 +193,7 @@ pub async fn create_index(mut ctx: Context) -> SResult { let response_body = IndexCreateResponse { name: body.name, - uid: generated_uid, + uid, schema: body.schema, update_id: response_update_id, created_at: Utc::now(),