Skip to content

Commit 2c41313

Browse files
author
WebFreak001
committed
Fix #1
1 parent 9ee8c55 commit 2c41313

File tree

1 file changed

+147
-3
lines changed

1 file changed

+147
-3
lines changed

source/mongoschema/package.d

+147-3
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,135 @@ T fromSchemaBson(T)(Bson bson)
375375
return obj;
376376
}
377377

378+
struct DocumentRange(Schema)
379+
{
380+
private MongoCursor!(Bson, Bson, typeof(null)) _cursor;
381+
382+
package this(MongoCursor!(Bson, Bson, typeof(null)) cursor)
383+
{
384+
_cursor = cursor;
385+
}
386+
387+
/**
388+
Returns true if there are no more documents for this cursor.
389+
390+
Throws: An exception if there is a query or communication error.
391+
*/
392+
@property bool empty()
393+
{
394+
return _cursor.empty;
395+
}
396+
397+
/**
398+
Returns the current document of the response.
399+
400+
Use empty and popFront to iterate over the list of documents using an
401+
input range interface. Note that calling this function is only allowed
402+
if empty returns false.
403+
*/
404+
@property Schema front()
405+
{
406+
return fromSchemaBson!Schema(_cursor.front);
407+
}
408+
409+
/**
410+
Controls the order in which the query returns matching documents.
411+
412+
This method must be called before starting to iterate, or an exeption
413+
will be thrown. If multiple calls to $(D sort()) are issued, only
414+
the last one will have an effect.
415+
416+
Params:
417+
order = A BSON object convertible value that defines the sort order
418+
of the result. This BSON object must be structured according to
419+
the MongoDB documentation (see below).
420+
421+
Returns: Reference to the modified original curser instance.
422+
423+
Throws:
424+
An exception if there is a query or communication error.
425+
Also throws if the method was called after beginning of iteration.
426+
427+
See_Also: $(LINK http://docs.mongodb.org/manual/reference/method/cursor.sort)
428+
*/
429+
auto sort(T)(T order)
430+
{
431+
_cursor.sort(serializeToBson(order));
432+
return this;
433+
}
434+
435+
/**
436+
Limits the number of documents that the cursor returns.
437+
438+
This method must be called before beginnig iteration in order to have
439+
effect. If multiple calls to limit() are made, the one with the lowest
440+
limit will be chosen.
441+
442+
Params:
443+
count = The maximum number number of documents to return. A value
444+
of zero means unlimited.
445+
446+
Returns: the same cursor
447+
448+
See_Also: $(LINK http://docs.mongodb.org/manual/reference/method/cursor.limit)
449+
*/
450+
auto limit(size_t count)
451+
{
452+
_cursor.limit(count);
453+
return this;
454+
}
455+
456+
/**
457+
Skips a given number of elements at the beginning of the cursor.
458+
459+
This method must be called before beginnig iteration in order to have
460+
effect. If multiple calls to skip() are made, the one with the maximum
461+
number will be chosen.
462+
463+
Params:
464+
count = The number of documents to skip.
465+
466+
Returns: the same cursor
467+
468+
See_Also: $(LINK http://docs.mongodb.org/manual/reference/method/cursor.skip)
469+
*/
470+
auto skip(int count)
471+
{
472+
_cursor.skip(count);
473+
return this;
474+
}
475+
476+
/**
477+
Advances the cursor to the next document of the response.
478+
479+
Note that calling this function is only allowed if empty returns false.
480+
*/
481+
void popFront()
482+
{
483+
_cursor.popFront();
484+
}
485+
486+
/**
487+
Iterates over all remaining documents.
488+
489+
Note that iteration is one-way - elements that have already been visited
490+
will not be visited again if another iteration is done.
491+
492+
Throws: An exception if there is a query or communication error.
493+
*/
494+
int opApply(int delegate(Schema doc) del)
495+
{
496+
while (!_cursor.empty)
497+
{
498+
auto doc = _cursor.front;
499+
_cursor.popFront();
500+
if (auto ret = del(fromSchemaBson!Schema(doc)))
501+
return ret;
502+
}
503+
return 0;
504+
}
505+
}
506+
378507
class DocumentNotFoundException : Exception
379508
{
380509
this(string msg, string file = __FILE__, size_t line = __LINE__) pure nothrow @nogc @safe
@@ -481,7 +610,7 @@ mixin template MongoSchema()
481610
}
482611

483612
/// Queries all elements from the collection.
484-
static typeof(this)[] find()
613+
deprecated("use findAll instead") static typeof(this)[] find()
485614
{
486615
typeof(this)[] values;
487616
foreach (entry; _schema_collection_.find())
@@ -491,6 +620,20 @@ mixin template MongoSchema()
491620
return values;
492621
}
493622

623+
/// Finds one or more elements using a query as range.
624+
static DocumentRange!(typeof(this)) findRange(T)(T query,
625+
QueryFlags flags = QueryFlags.None, int num_skip = 0, int num_docs_per_chunk = 0)
626+
{
627+
return DocumentRange!(typeof(this))(_schema_collection_.find(query,
628+
null, flags, num_skip, num_docs_per_chunk));
629+
}
630+
631+
/// Queries all elements from the collection as range.
632+
static DocumentRange!(typeof(this)) findAll()
633+
{
634+
return DocumentRange!(typeof(this))(_schema_collection_.find());
635+
}
636+
494637
/// Updates a document.
495638
static void update(T, U)(T query, U update, UpdateFlags options = UpdateFlags.none)
496639
{
@@ -706,6 +849,7 @@ unittest
706849
import vibe.db.mongo.mongo;
707850
import std.digest.sha;
708851
import std.exception;
852+
import std.array;
709853

710854
auto client = connectMongoDB("localhost");
711855
auto database = client.getDatabase("test");
@@ -726,7 +870,7 @@ unittest
726870

727871
users.register!User;
728872

729-
assert(User.find().length == 0);
873+
assert(User.findAll().array.length == 0);
730874

731875
User user;
732876
user.username = "Example";
@@ -761,7 +905,7 @@ unittest
761905

762906
assert(actualFakeID != faker.bsonID);
763907

764-
foreach (usr; User.find)
908+
foreach (usr; User.findAll)
765909
{
766910
usr.profilePicture = "default.png"; // Reset all profile pictures
767911
usr.save();

0 commit comments

Comments
 (0)