Giter Site home page Giter Site logo

Batch document creation? about wither HOT 3 OPEN

wbrickner avatar wbrickner commented on June 14, 2024
Batch document creation?

from wither.

Comments (3)

thedodd avatar thedodd commented on June 14, 2024 1

👋 @wbrickner

As of now, the answer is no. However, there is a short-term workaround, as well as a long-term solution.

  • short-term: you could do Model::collection(&db).insert_many(...). Docs for that method. You could actually pass it a vec or other iterator of model instances, as it will serialize them for you.
  • long-term: we should just introduce an associated function which mirrors the same functionality, except specific to the model type.

Would you be interested in opening a PR for that long-term solution? Should be quite minimal, all things considered.

from wither.

wbrickner avatar wbrickner commented on June 14, 2024

Working on a PR.

I'm a little stuck right now.

I want to add a method like

/// Inserts all model instances as documents, provided in some (rust) collection of 
/// instances which can be iterated over (e.g. `Vec`, `HashSet`, etc).
///
/// Wraps the driver's `Collection.insert_many` method.
async fn insert_many<O, D, M>(db: &Database, documents: D, options: O) -> Result<InsertManyResult>
where
    D: IntoIterator<Item = Document> + Send,
    O: Into<Option<options::InsertManyOptions>> + Send,
{
    Ok(Self::collection(db).insert_many(documents, options).await?)
}

So that any collection the user happens to end up with is suitable for batch insertion directly, so long as the collection is iterable and the iterator items can be converted into documents.

I go on to provide an path for implicit conversion:

impl<M: Model> From<M> for Document {
  fn from(model: M) -> Document {
    model.document_from_instance()
  }
}

The type system is not happy with me:

type parameter `M` must be used as the type parameter for some local type (e.g., `MyStruct<M>`)
implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
only traits defined in the current crate can be implemented for a type parameter

Is this the right way to handle an unknown iterator which yields some item /which can be converted to a document/?
I think it would be really elegant to be able to not worry if you're batch inserting a hashmap or a btree or some deserialization container from some crate, whatever it is.

Hoping you can give me some advice, I've never found myself in this particular situation in Rust before.

from wither.

thedodd avatar thedodd commented on June 14, 2024

Yea, so the issue is that M is not used anywhere within the new method you've defined, and Rust doesn't accept that. If you just remove the M generic param, you should be good. Rust's way of communicating this is with the line:

type parameter M must be used as the type parameter for some local type (e.g., MyStruct<M>)

We shouldn't need that impl<M: Model> From<M> for Document { either. I don't think we have anywhere in the code where such would be needed, unless there is some other part of the PR which uses it.

That should help.

from wither.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.