Skip to content

Commit f691eb1

Browse files
authored
Merge pull request #12 from WebFreak001/support-mongodb-5.1
2 parents 57c7a5d + 50e1d44 commit f691eb1

File tree

4 files changed

+114
-99
lines changed

4 files changed

+114
-99
lines changed

.github/workflows/test.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ jobs:
88

99
strategy:
1010
matrix:
11-
dcompiler: [dmd-latest, dmd-beta, dmd-master, dmd-2.088.0, dmd-2.087.1, dmd-2.086.1, ldc-latest, ldc-beta, ldc-master]
12-
os: [ubuntu-latest, windows-latest, macOS-latest]
11+
dcompiler: [dmd-latest, dmd-beta, dmd-master, dmd-2.094.2, ldc-latest, ldc-beta, ldc-master]
12+
os: [ubuntu-latest, windows-latest]
1313

1414
steps:
1515
- uses: actions/checkout@v2

dub.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "mongoschemad",
33
"description": "MongoDB Schema support",
4-
"copyright": "Copyright © 2017, webfreak",
4+
"copyright": "Copyright © 2017-2024, webfreak",
55
"authors": ["webfreak"],
66
"license": "MIT",
77
"dependencies":
88
{
9-
"vibe-d:mongodb": "*",
9+
"vibe-d:mongodb": ">=0.9.6",
1010
"vibe-d:data": "*"
1111
}
1212
}

source/mongoschema/db.d

+78-92
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ private:
400400
/// Mixin for functions for interacting with Mongo collections.
401401
mixin template MongoSchema()
402402
{
403+
import vibe.db.mongo.collection;
404+
403405
import std.typecons : Nullable;
404406
import std.range : isInputRange, ElementType;
405407

@@ -428,14 +430,16 @@ mixin template MongoSchema()
428430
{
429431
if (_schema_object_id_.valid)
430432
{
431-
collection.update(Bson(["_id": Bson(_schema_object_id_)]),
432-
this.toSchemaBson(), UpdateFlags.upsert);
433+
UpdateOptions options;
434+
options.upsert = true;
435+
collection.replaceOne(Bson(["_id": Bson(_schema_object_id_)]),
436+
this.toSchemaBson(), options);
433437
}
434438
else
435439
{
436440
_schema_object_id_ = BsonObjectID.generate;
437441
auto bson = this.toSchemaBson();
438-
collection.insert(bson);
442+
collection.insertOne(bson);
439443
}
440444
}
441445

@@ -444,14 +448,16 @@ mixin template MongoSchema()
444448
{
445449
if (_schema_object_id_.valid)
446450
{
447-
collection.update(Bson(["_id": Bson(_schema_object_id_)]),
448-
Bson(["$set": this.toSchemaBson()]), UpdateFlags.upsert);
451+
UpdateOptions options;
452+
options.upsert = true;
453+
collection.updateOne(Bson(["_id": Bson(_schema_object_id_)]),
454+
Bson(["$set": this.toSchemaBson()]), options);
449455
}
450456
else
451457
{
452458
_schema_object_id_ = BsonObjectID.generate;
453459
auto bson = this.toSchemaBson();
454-
collection.insert(bson);
460+
collection.insertOne(bson);
455461
}
456462
}
457463

@@ -460,7 +466,7 @@ mixin template MongoSchema()
460466
{
461467
if (!_schema_object_id_.valid)
462468
return false;
463-
collection.remove(Bson(["_id": Bson(_schema_object_id_)]), DeleteFlags.SingleRemove);
469+
collection.deleteOne(Bson(["_id": Bson(_schema_object_id_)]));
464470
return true;
465471
}
466472

@@ -568,37 +574,32 @@ mixin template MongoSchema()
568574
}
569575

570576
/// Finds one or more elements using a query.
571-
static typeof(this)[] find(Query!(typeof(this)) query,
572-
QueryFlags flags = QueryFlags.None, int num_skip = 0, int num_docs_per_chunk = 0)
577+
static typeof(this)[] find(Query!(typeof(this)) query, FindOptions options = FindOptions.init)
573578
{
574-
return find(query._query, flags, num_skip, num_docs_per_chunk);
579+
return find(query._query, options);
575580
}
576581

577582
/// ditto
578-
static typeof(this)[] find(T)(T query, QueryFlags flags = QueryFlags.None,
579-
int num_skip = 0, int num_docs_per_chunk = 0)
583+
static typeof(this)[] find(T)(T query, FindOptions options = FindOptions.init)
580584
{
581585
typeof(this)[] values;
582-
foreach (entry; collection.find(query, null, flags, num_skip, num_docs_per_chunk))
586+
foreach (entry; collection.find(query, options))
583587
{
584588
values ~= fromSchemaBson!(typeof(this))(entry);
585589
}
586590
return values;
587591
}
588592

589593
/// Finds one or more elements using a query as range.
590-
static DocumentRange!(typeof(this)) findRange(Query!(typeof(this)) query,
591-
QueryFlags flags = QueryFlags.None, int num_skip = 0, int num_docs_per_chunk = 0)
594+
static DocumentRange!(typeof(this)) findRange(Query!(typeof(this)) query, FindOptions options = FindOptions.init)
592595
{
593-
return findRange(query._query, flags, num_skip, num_docs_per_chunk);
596+
return findRange(query._query, options);
594597
}
595598

596599
/// ditto
597-
static DocumentRange!(typeof(this)) findRange(T)(T query,
598-
QueryFlags flags = QueryFlags.None, int num_skip = 0, int num_docs_per_chunk = 0)
600+
static DocumentRange!(typeof(this)) findRange(T)(T query, FindOptions options = FindOptions.init)
599601
{
600-
return DocumentRange!(typeof(this))(collection.find(serializeToBson(query),
601-
null, flags, num_skip, num_docs_per_chunk));
602+
return DocumentRange!(typeof(this))(collection.find(serializeToBson(query), options));
602603
}
603604

604605
/// Queries all elements from the collection as range.
@@ -616,54 +617,66 @@ mixin template MongoSchema()
616617

617618
if (documents.empty)
618619
return;
619-
collection.insert(documents.map!((a) {
620+
collection.insertMany(documents.map!((a) {
620621
a.bsonID = BsonObjectID.init;
621622
return a.toSchemaBson;
622623
}).array, options); // .array needed because of vibe-d issue #2185
623624
}
624625

625-
/// Updates a document.
626-
static void update(U)(Query!(typeof(this)) query, U update, UpdateFlags options = UpdateFlags
627-
.none)
626+
/// Updates a document with `$dollarOperations`.
627+
static void updateOne(U)(Query!(typeof(this)) query, U update, UpdateOptions options = UpdateOptions.init)
628628
{
629-
update(query._query, update, options);
629+
updateOne(query._query, update, options);
630630
}
631631

632632
/// ditto
633-
static void update(T, U)(T query, U update, UpdateFlags options = UpdateFlags.none)
633+
static void updateOne(T, U)(T query, U update, UpdateOptions options = UpdateOptions.init)
634634
{
635-
collection.update(query, update, options);
635+
collection.updateOne(query, update, options);
636636
}
637637

638-
/// Updates a document or inserts it when not existent. Shorthand for `update(..., UpdateFlags.upsert)`
639-
static void upsert(U)(Query!(typeof(this)) query, U upsert,
640-
UpdateFlags options = UpdateFlags.upsert)
638+
/// Updates any amount of documents.
639+
static void updateMany(U)(Query!(typeof(this)) query, U update, UpdateOptions options = UpdateOptions.init)
640+
{
641+
updateMany(query._query, update, options);
642+
}
643+
644+
/// ditto
645+
static void updateMany(T, U)(T query, U update, UpdateOptions options = UpdateOptions.init)
641646
{
647+
collection.updateMany(query, update, options);
648+
}
649+
650+
/// Updates a document or inserts it when not existent. Calls `replaceOne` with `options.upsert` set to true.
651+
static void upsert(U)(Query!(typeof(this)) query, U upsert, UpdateOptions options = UpdateOptions.init)
652+
{
653+
options.upsert = true;
642654
upsert(query._query, upsert, options);
643655
}
644656

645657
/// ditto
646-
static void upsert(T, U)(T query, U update, UpdateFlags options = UpdateFlags.upsert)
658+
static void upsert(T, U)(T query, U update, UpdateOptions options = UpdateOptions.init)
647659
{
648-
collection.update(query, update, options);
660+
options.upsert = true;
661+
collection.replaceOne(query, update, options);
649662
}
650663

651-
/// Deletes one or any amount of documents matching the selector based on the flags.
652-
static void remove(Query!(typeof(this)) query, DeleteFlags flags = DeleteFlags.none)
664+
/// Deletes one or any amount of documents matching the selector based on the options.
665+
static void remove(Query!(typeof(this)) query, DeleteOptions options = DeleteOptions.init)
653666
{
654-
remove(query._query, flags);
667+
remove(query._query, options);
655668
}
656669

657670
/// ditto
658-
static void remove(T)(T selector, DeleteFlags flags = DeleteFlags.none)
671+
static void remove(T)(T selector, DeleteOptions options = DeleteOptions.init)
659672
{
660-
collection.remove(selector, flags);
673+
collection.deleteMany(selector, options);
661674
}
662675

663676
/// Removes all documents from this collection.
664-
static void removeAll()
677+
static void removeAll(DeleteOptions options = DeleteOptions.init)
665678
{
666-
collection.remove();
679+
collection.deleteAll(options);
667680
}
668681

669682
/// Drops the entire collection and all indices in the database.
@@ -681,15 +694,15 @@ mixin template MongoSchema()
681694
/// ditto
682695
static auto count(T)(T query)
683696
{
684-
return collection.count(query);
697+
return collection.countDocuments(query);
685698
}
686699

687700
/// Returns the count of documents in this collection.
688701
static auto countAll()
689702
{
690703
import vibe.data.bson : Bson;
691704

692-
return collection.count(Bson.emptyObject);
705+
return collection.countDocuments(Bson.emptyObject);
693706
}
694707

695708
/// Start of an aggregation call. Returns a pipeline with typesafe functions for modifying the pipeline and running it at the end.
@@ -754,57 +767,30 @@ void register(T)(MongoCollection collection) @safe
754767
}
755768

756769

757-
static if (is(IndexOptions))
770+
IndexOptions indexOptions;
771+
static if (hasUDA!(member, mongoBackground))
772+
{
773+
indexOptions.background = true;
774+
}
775+
static if (hasUDA!(member, mongoDropDuplicates))
776+
{
777+
indexOptions.dropDups = true;
778+
}
779+
static if (hasUDA!(member, mongoSparse))
758780
{
759-
IndexOptions indexOptions;
760-
static if (hasUDA!(member, mongoBackground))
761-
{
762-
indexOptions.background = true;
763-
}
764-
static if (hasUDA!(member, mongoDropDuplicates))
765-
{
766-
indexOptions.dropDups = true;
767-
}
768-
static if (hasUDA!(member, mongoSparse))
769-
{
770-
indexOptions.sparse = true;
771-
}
772-
static if (hasUDA!(member, mongoUnique))
773-
{
774-
indexOptions.unique = true;
775-
}
776-
static if (hasUDA!(member, mongoExpire))
777-
{
778-
indexOptions.expireAfterSeconds = cast(int)expires;
779-
}
781+
indexOptions.sparse = true;
780782
}
781-
else
783+
static if (hasUDA!(member, mongoUnique))
782784
{
783-
IndexFlags flags = IndexFlags.None;
784-
static if (hasUDA!(member, mongoBackground))
785-
{
786-
flags |= IndexFlags.Background;
787-
}
788-
static if (hasUDA!(member, mongoDropDuplicates))
789-
{
790-
flags |= IndexFlags.DropDuplicates;
791-
}
792-
static if (hasUDA!(member, mongoSparse))
793-
{
794-
flags |= IndexFlags.Sparse;
795-
}
796-
static if (hasUDA!(member, mongoUnique))
797-
{
798-
flags |= IndexFlags.Unique;
799-
}
800-
static if (hasUDA!(member, mongoExpire))
801-
{
802-
flags |= IndexFlags.ExpireAfterSeconds;
803-
}
804-
805-
if (flags != IndexFlags.None || force)
806-
collection.ensureIndex([tuple(name, 1)], flags, dur!"seconds"(expires));
785+
indexOptions.unique = true;
807786
}
787+
static if (hasUDA!(member, mongoExpire))
788+
{
789+
indexOptions.expireAfterSeconds = cast(int)expires;
790+
}
791+
792+
if (indexOptions != indexOptions.init)
793+
collection.createIndex([name: 1], indexOptions);
808794
}
809795
}
810796
}
@@ -1021,7 +1007,7 @@ unittest
10211007
auto client = connectMongoDB("127.0.0.1");
10221008
auto database = client.getDatabase("test");
10231009
MongoCollection users = database["users"];
1024-
users.remove(); // Clears collection
1010+
users.deleteAll(); // Clears collection
10251011

10261012
struct User
10271013
{
@@ -1056,10 +1042,10 @@ unittest
10561042
User faker;
10571043
faker.username = "Example";
10581044
faker.hash = sha512Of("PASSWORD").dup;
1059-
faker.profilePicture = "example-avatar.png";
1045+
faker.profilePicture = "faker-avatar.png";
10601046

1061-
assertThrown(faker.save());
10621047
// Unique username
1048+
assertThrown(faker.save());
10631049

10641050
faker.username = "Example_";
10651051
assertNotThrown(faker.save());
@@ -1129,7 +1115,7 @@ unittest
11291115
}
11301116

11311117
auto coll = client.getCollection("test.users2");
1132-
coll.remove();
1118+
coll.deleteAll();
11331119
coll.register!User;
11341120

11351121
User register(string name, string password)

0 commit comments

Comments
 (0)