@@ -599,43 +599,81 @@ export class MongoProvider<StoredValue = unknown> extends JoshProvider<StoredVal
599
599
600
600
// Due to the use of $sample, the output will never have duplicates
601
601
public async [ Method . Random ] ( payload : Payload . Random < StoredValue > ) : Promise < Payload . Random < StoredValue > > {
602
- const docCount = await this . collection . countDocuments ( { } ) ;
602
+ let { count, unique } = payload ;
603
+ const size = await this . collection . countDocuments ( { } ) ;
603
604
604
605
// TODO: @dan -online fix this yourself idk how this work
605
606
// Basically just this:
606
607
// if(unique && size < count) throw InvalidCount
607
608
// if (size === 0) throw MissingData
608
609
// Also try no to get an infinite loop with unique off and count > size
609
610
610
- if ( docCount === 0 ) return { ...payload , data : [ ] } ;
611
- if ( docCount < payload . count ) {
612
- payload . errors . push ( this . error ( { identifier : CommonIdentifiers . InvalidCount , method : Method . Random } ) ) ;
611
+ if ( unique && size < count ) {
612
+ payload . errors . push ( this . error ( { identifier : CommonIdentifiers . InvalidCount , method : Method . Random } , { size } ) ) ;
613
613
614
614
return payload ;
615
615
}
616
616
617
- const aggr : Document [ ] = [ { $sample : { size : payload . count } } ] ;
618
- const docs = ( await this . collection . aggregate ( aggr ) . toArray ( ) ) || [ ] ;
617
+ if ( size === 0 ) {
618
+ payload . errors . push ( this . error ( { identifier : CommonIdentifiers . MissingData , method : Method . Random } , { unique , count } ) ) ;
619
619
620
- if ( docs . length > 0 ) payload . data = docs . map ( ( doc ) => this . deserialize ( doc . value ) ) ;
620
+ return payload ;
621
+ }
622
+
623
+ payload . data = [ ] ;
624
+
625
+ if ( unique ) {
626
+ const aggr : Document [ ] = [ { $sample : { size : payload . count } } ] ;
627
+ const docs = ( await this . collection . aggregate ( aggr ) . toArray ( ) ) || [ ] ;
628
+
629
+ payload . data = docs . map ( ( doc ) => this . deserialize ( doc . value ) ) ;
630
+ } else {
631
+ while ( count > 0 ) {
632
+ const aggr : Document [ ] = [ { $sample : { size : 1 } } ] ;
633
+ const docs = ( await this . collection . aggregate ( aggr ) . toArray ( ) ) || [ ] ;
634
+
635
+ payload . data . push ( this . deserialize ( docs [ 0 ] . value ) ) ;
636
+
637
+ count -- ;
638
+ }
639
+ }
621
640
622
641
return payload ;
623
642
}
624
643
625
644
public async [ Method . RandomKey ] ( payload : Payload . RandomKey ) : Promise < Payload . RandomKey > {
626
- const docCount = await this . collection . countDocuments ( { } ) ;
645
+ const size = await this . collection . countDocuments ( { } ) ;
646
+ let { count, unique } = payload ;
627
647
628
- if ( docCount === 0 ) return { ...payload , data : [ ] } ;
629
- if ( docCount < payload . count ) {
630
- payload . errors . push ( this . error ( { identifier : CommonIdentifiers . InvalidCount , method : Method . RandomKey } ) ) ;
648
+ if ( unique && size < count ) {
649
+ payload . errors . push ( this . error ( { identifier : CommonIdentifiers . InvalidCount , method : Method . Random } , { size } ) ) ;
631
650
632
651
return payload ;
633
652
}
634
653
635
- const aggr : Document [ ] = [ { $sample : { size : payload . count } } ] ;
636
- const docs = ( await this . collection . aggregate ( aggr ) . toArray ( ) ) || [ ] ;
654
+ if ( size === 0 ) {
655
+ payload . errors . push ( this . error ( { identifier : CommonIdentifiers . MissingData , method : Method . Random } , { unique , count } ) ) ;
637
656
638
- if ( docs . length > 0 ) payload . data = docs . map ( ( doc ) => doc . key ) ;
657
+ return payload ;
658
+ }
659
+
660
+ payload . data = [ ] ;
661
+
662
+ if ( unique ) {
663
+ const aggr : Document [ ] = [ { $sample : { size : payload . count } } ] ;
664
+ const docs = ( await this . collection . aggregate ( aggr ) . toArray ( ) ) || [ ] ;
665
+
666
+ payload . data = docs . map ( ( doc ) => doc . key ) ;
667
+ } else {
668
+ while ( count > 0 ) {
669
+ const aggr : Document [ ] = [ { $sample : { size : 1 } } ] ;
670
+ const docs = ( await this . collection . aggregate ( aggr ) . toArray ( ) ) || [ ] ;
671
+
672
+ payload . data . push ( docs [ 0 ] . key ) ;
673
+
674
+ count -- ;
675
+ }
676
+ }
639
677
640
678
return payload ;
641
679
}
0 commit comments