@@ -15,13 +15,18 @@ let Author = mongoose.model('Author', AuthorSchema);
15
15
let PostSchema = new mongoose . Schema ( {
16
16
title : String ,
17
17
date : Date ,
18
- body : String ,
18
+ body : {
19
+ type : String ,
20
+ text : true ,
21
+ } ,
19
22
author : {
20
23
type : mongoose . Schema . ObjectId ,
21
24
ref : 'Author'
22
25
}
23
26
} ) ;
24
27
28
+ PostSchema . index ( { body : 'text' } ) ;
29
+
25
30
PostSchema . plugin ( mongooseCursorPaginate ) ;
26
31
27
32
let Post = mongoose . model ( 'Post' , PostSchema ) ;
@@ -67,4 +72,37 @@ describe('mongoose plugin', (it) => {
67
72
t . is ( data . hasOwnProperty ( 'next' ) , true ) ;
68
73
t . is ( data . hasOwnProperty ( 'hasNext' ) , true ) ;
69
74
} ) ;
75
+
76
+ it ( 'return find results' , async function ( t ) {
77
+ let data = await Post . paginate ( {
78
+ query : { title : 'Post #99' } ,
79
+ } ) ;
80
+ t . is ( data . results . length , 1 ) ;
81
+ } ) ;
82
+
83
+ it ( 'return aggregate results' , async function ( t ) {
84
+ let data = await Author . paginateFN ( {
85
+ aggregation : [
86
+ {
87
+ $lookup : {
88
+ from : 'posts' ,
89
+ localField : '_id' ,
90
+ foreignField : 'author' ,
91
+ as : 'posts' ,
92
+ }
93
+ } ,
94
+ ] ,
95
+ } ) ;
96
+ t . is ( data . results . length , 1 ) ;
97
+ t . is ( Array . isArray ( data . results [ 0 ] . posts ) , true ) ;
98
+ t . is ( data . results [ 0 ] . posts . length , 100 ) ;
99
+ } ) ;
100
+
101
+ it ( 'return search results' , async function ( t ) {
102
+ let data = await Post . paginate ( {
103
+ search : '99' ,
104
+ } ) ;
105
+ t . is ( data . results . length , 1 ) ;
106
+ } ) ;
107
+
70
108
} ) ;
0 commit comments