@@ -717,10 +717,16 @@ private:
717
717
mixin template MongoSchema()
718
718
{
719
719
import std.typecons : Nullable;
720
+ import std.range : isInputRange, ElementType;
720
721
721
722
static MongoCollection _schema_collection_;
722
723
private BsonObjectID _schema_object_id_;
723
724
725
+ @property static MongoCollection collection() @safe
726
+ {
727
+ return _schema_collection_;
728
+ }
729
+
724
730
// / Returns: the _id value (if set by save or find)
725
731
@property ref BsonObjectID bsonID() @safe
726
732
{
@@ -732,14 +738,30 @@ mixin template MongoSchema()
732
738
{
733
739
if (_schema_object_id_.valid)
734
740
{
735
- _schema_collection_ .update(Bson([" _id" : Bson(_schema_object_id_)]),
741
+ collection .update(Bson([" _id" : Bson(_schema_object_id_)]),
736
742
this .toSchemaBson(), UpdateFlags.upsert);
737
743
}
738
744
else
739
745
{
740
746
_schema_object_id_ = BsonObjectID.generate;
741
747
auto bson = this .toSchemaBson();
742
- _schema_collection_.insert(bson);
748
+ collection.insert(bson);
749
+ }
750
+ }
751
+
752
+ // / Inserts or merges into an existing value.
753
+ void merge ()
754
+ {
755
+ if (_schema_object_id_.valid)
756
+ {
757
+ collection.update(Bson([" _id" : Bson(_schema_object_id_)]),
758
+ Bson([" $set" : this .toSchemaBson()]), UpdateFlags.upsert);
759
+ }
760
+ else
761
+ {
762
+ _schema_object_id_ = BsonObjectID.generate;
763
+ auto bson = this .toSchemaBson();
764
+ collection.insert(bson);
743
765
}
744
766
}
745
767
@@ -748,7 +770,7 @@ mixin template MongoSchema()
748
770
{
749
771
if (! _schema_object_id_.valid)
750
772
return false ;
751
- _schema_collection_ .remove(Bson([" _id" : Bson(_schema_object_id_)]),
773
+ collection .remove(Bson([" _id" : Bson(_schema_object_id_)]),
752
774
DeleteFlags.SingleRemove);
753
775
return true ;
754
776
}
@@ -757,7 +779,7 @@ mixin template MongoSchema()
757
779
// / Throws: DocumentNotFoundException if not found
758
780
static auto findOneOrThrow (T)(T query)
759
781
{
760
- Bson found = _schema_collection_ .findOne(query);
782
+ Bson found = collection .findOne(query);
761
783
if (found.isNull)
762
784
throw new DocumentNotFoundException(" Could not find one " ~ typeof (this ).stringof);
763
785
return found;
@@ -787,7 +809,7 @@ mixin template MongoSchema()
787
809
// / Tries to find a document by the _id field and returns a Nullable which `isNull` if it could not be found. Otherwise it will be the document wrapped in the nullable.
788
810
static Nullable! (typeof (this )) tryFindById (BsonObjectID id)
789
811
{
790
- Bson found = _schema_collection_ .findOne(Bson([" _id" : Bson(id)]));
812
+ Bson found = collection .findOne(Bson([" _id" : Bson(id)]));
791
813
if (found.isNull)
792
814
return Nullable! (typeof (this )).init;
793
815
return Nullable! (typeof (this ))(fromSchemaBson! (typeof (this ))(found));
@@ -802,7 +824,7 @@ mixin template MongoSchema()
802
824
// / Tries to find a document in this collection. It will return a Nullable which `isNull` if the document could not be found. Otherwise it will be the document wrapped in the nullable.
803
825
static Nullable! (typeof (this )) tryFindOne (T)(T query)
804
826
{
805
- Bson found = _schema_collection_ .findOne(query);
827
+ Bson found = collection .findOne(query);
806
828
if (found.isNull)
807
829
return Nullable! (typeof (this )).init;
808
830
return Nullable! (typeof (this ))(fromSchemaBson! (typeof (this ))(found));
@@ -813,7 +835,7 @@ mixin template MongoSchema()
813
835
int num_skip = 0 , int num_docs_per_chunk = 0 )
814
836
{
815
837
typeof (this )[] values ;
816
- foreach (entry; _schema_collection_ .find(query, null , flags, num_skip, num_docs_per_chunk))
838
+ foreach (entry; collection .find(query, null , flags, num_skip, num_docs_per_chunk))
817
839
{
818
840
values ~= fromSchemaBson! (typeof (this ))(entry);
819
841
}
@@ -824,7 +846,7 @@ mixin template MongoSchema()
824
846
deprecated (" use findAll instead" ) static typeof (this )[] find()
825
847
{
826
848
typeof (this )[] values ;
827
- foreach (entry; _schema_collection_ .find())
849
+ foreach (entry; collection .find())
828
850
{
829
851
values ~= fromSchemaBson! (typeof (this ))(entry);
830
852
}
@@ -835,58 +857,73 @@ mixin template MongoSchema()
835
857
static DocumentRange! (typeof (this )) findRange (T)(T query,
836
858
QueryFlags flags = QueryFlags.None, int num_skip = 0 , int num_docs_per_chunk = 0 )
837
859
{
838
- return DocumentRange! (typeof (this ))(_schema_collection_ .find(serializeToBson(query),
860
+ return DocumentRange! (typeof (this ))(collection .find(serializeToBson(query),
839
861
null , flags, num_skip, num_docs_per_chunk));
840
862
}
841
863
842
864
// / Queries all elements from the collection as range.
843
865
static DocumentRange! (typeof (this )) findAll ()
844
866
{
845
- return DocumentRange! (typeof (this ))(_schema_collection_.find());
867
+ return DocumentRange! (typeof (this ))(collection.find());
868
+ }
869
+
870
+ // / Inserts many documents at once. The resulting IDs of the symbols will be generated by the server and not known to the caller.
871
+ static void insertMany (T)(T documents, InsertFlags options = InsertFlags.none)
872
+ if (isInputRange! T && is (ElementType! T : typeof (this )))
873
+ {
874
+ import std.array : array;
875
+ import std.algorithm : map;
876
+
877
+ if (documents.empty)
878
+ return ;
879
+ collection.insert(documents.map! ((a) {
880
+ a.bsonID = BsonObjectID.init;
881
+ return a.toSchemaBson;
882
+ }).array, options); // .array needed because of vibe-d issue #2185
846
883
}
847
884
848
885
// / Updates a document.
849
886
static void update (T, U)(T query, U update, UpdateFlags options = UpdateFlags.none)
850
887
{
851
- _schema_collection_ .update(query, update, options);
888
+ collection .update(query, update, options);
852
889
}
853
890
854
891
// / Updates a document or inserts it when not existent. Shorthand for `update(..., UpdateFlags.upsert)`
855
892
static void upsert (T, U)(T query, U update, UpdateFlags options = UpdateFlags.upsert)
856
893
{
857
- _schema_collection_ .update(query, update, options);
894
+ collection .update(query, update, options);
858
895
}
859
896
860
897
// / Deletes one or any amount of documents matching the selector based on the flags.
861
898
static void remove (T)(T selector, DeleteFlags flags = DeleteFlags.none)
862
899
{
863
- _schema_collection_ .remove(selector, flags);
900
+ collection .remove(selector, flags);
864
901
}
865
902
866
903
// / Removes all documents from this collection.
867
904
static void removeAll ()
868
905
{
869
- _schema_collection_ .remove();
906
+ collection .remove();
870
907
}
871
908
872
909
// / Drops the entire collection and all indices in the database.
873
910
static void dropTable ()
874
911
{
875
- _schema_collection_ .drop();
912
+ collection .drop();
876
913
}
877
914
878
915
// / Returns the count of documents in this collection matching this query.
879
916
static auto count (T)(T query)
880
917
{
881
- return _schema_collection_ .count(query);
918
+ return collection .count(query);
882
919
}
883
920
884
921
// / Returns the count of documents in this collection.
885
922
static auto countAll ()
886
923
{
887
924
import vibe.data.bson : Bson;
888
925
889
- return _schema_collection_ .count(Bson.emptyObject);
926
+ return collection .count(Bson.emptyObject);
890
927
}
891
928
892
929
// / Start of an aggregation call. Returns a pipeline with typesafe functions for modifying the pipeline and running it at the end.
@@ -906,7 +943,7 @@ mixin template MongoSchema()
906
943
// / --------------------
907
944
static SchemaPipeline aggregate ()
908
945
{
909
- return SchemaPipeline (_schema_collection_ );
946
+ return SchemaPipeline (collection );
910
947
}
911
948
}
912
949
@@ -917,8 +954,10 @@ void register(T)(MongoCollection collection) @safe
917
954
918
955
static if (hasMember! (T, " _schema_collection_" ))
919
956
{
920
- assert (T._schema_collection_.name.length == 0 , " Can't register a Schema to 2 collections!" );
921
- T._schema_collection_ = collection;
957
+ (() @trusted {
958
+ assert (T._schema_collection_.name.length == 0 , " Can't register a Schema to 2 collections!" );
959
+ T._schema_collection_ = collection;
960
+ })();
922
961
}
923
962
924
963
foreach (memberName; __traits (allMembers , T))
0 commit comments