Comments (3)
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.
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.
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)
- thread 'main' panicked at 'there is no timer running, must be called from the context of a Tokio 0.2.x runtime' HOT 4
- Mongo Rust driver upgrade coming HOT 6
- wither::Migration extensions HOT 6
- How to run aggregations? HOT 3
- Document references / cross-collection population HOT 8
- How to Specify Enum Literals? HOT 4
- Nice way to collect a cursor? HOT 2
- Docs cleanup & updates
- Add `update_many` method HOT 3
- [Docs] How does projection work in Rust? HOT 1
- Using basic example HOT 11
- [Enhancement] add created at and updated at timestamps HOT 1
- Upgrade to mongo driver 2.0 HOT 1
- Re-introduce IndexModel from the official driver
- Partial updates and typed documents
- Non-ObjectId _id fields HOT 1
- Call to action HOT 6
- Wither not working with mongodb operating in async-std HOT 3
- Mongodb _id Serialized to Id
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wither.