Skip to content

Remove Document trait asking for UIDType #255

@bidoubiwa

Description

@bidoubiwa

Currently to communicate a primary key on document addition the document structure must implement the following trait:

// src/document.rs
pub trait Document: DeserializeOwned + std::fmt::Debug + Serialize {
    /// The type of the primary key
    type UIDType: Display;

    /// The method returning the primary key of the Document.
    ///
    /// **WARNING**! This method **MUST** only return an object that displays himself only using alphanumeric characters, '/' and '-'.
    /// Otherwise, the Meilisearch server will reject your document.
    fn get_uid(&self) -> &Self::UIDType;
}

Here UIDType is the primary key. This trait is not very intuitive and makes the code harder to use than it should be.
The naming is not very clear and it requires an additional step of implementation on all document addition even when the user does not need to communicate the primary key.

for example:

  // Define the type of our documents
  #[derive(Serialize, Deserialize, Debug)]
  struct Movie {
    id: String,
    title: String
  }
  impl Document for Movie {
    type UIDType = String;
    fn get_uid(&self) -> &Self::UIDType { &self.id }
  }

  // Add a document to our index
  let task: Task = client.index("movies").add_documents(&[
    Movie {
      id: "123sq178".to_string(),
      title: "Amélie Poulain".to_string(),
    }
  ], None).await.unwrap();

and when the user wants to communicate the primary key he still needs to give it as the second parameter of add_documents

add_documents(documents, Some("id"))

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions