1- 'use strict' ;
2-
3- const cheerio = require ( 'cheerio' ) ;
4- const config = require ( '../.config' ) ;
5- const mongoose = require ( 'mongoose' ) ;
6-
7- let conn = null ;
8- const contentSchema = new mongoose . Schema ( {
9- title : { type : String , required : true } ,
10- body : { type : String , required : true } ,
11- url : { type : String , required : true } ,
12- version : { type : String } ,
13- versionNumber : { type : Number }
14- } ) ;
15-
16- module . exports = async function search ( context , req ) {
17- let Content ;
18- if ( conn == null ) {
19- conn = mongoose . createConnection ( config . uri ) ;
20- await conn . asPromise ( ) ;
21- }
22-
23- Content = conn . model ( 'Content' , contentSchema , 'Content' ) ;
24-
25- const query = req . query . search . toString ( ) ;
26- const version = req . query . version ? + req . query . version . replace ( / \. x $ / , '' ) : null ;
27- let results = await Content . aggregate ( [
28- {
29- $search : {
30- index : 'mongoose-content' ,
31- compound : {
32- must : [
33- ...( version ? [ {
34- equals : {
35- path : 'versionNumber' ,
36- value : version
37- }
38- } ] : [ ] ) ,
39- { text : { query, path : { wildcard : '*' } , fuzzy : { } } }
40- ]
41- }
42- }
43- } ,
44- { $limit : 10 }
45- ] ) ;
46-
47- results = results . map ( doc => {
48- const $ = cheerio . load ( doc . body ) ;
49-
50- doc . body = $ ( 'p' ) . get ( 0 ) ? $ ( 'p' ) . first ( ) . html ( ) : doc . body ;
51-
52- return doc ;
53- } ) ;
54-
55- context . res = {
56- // status: 200, /* Defaults to 200 */
57- body : JSON . stringify ( { results } )
58- } ;
1+ 'use strict' ;
2+
3+ const Archetype = require ( 'archetype' ) ;
4+ const cheerio = require ( 'cheerio' ) ;
5+ const extrovert = require ( 'extrovert' ) ;
6+ const mongoose = require ( 'mongoose' ) ;
7+
8+ let conn = null ;
9+ const contentSchema = new mongoose . Schema ( {
10+ title : { type : String , required : true } ,
11+ body : { type : String , required : true } ,
12+ url : { type : String , required : true } ,
13+ version : { type : String } ,
14+ versionNumber : { type : Number }
15+ } ) ;
16+
17+ const SearchParams = new Archetype ( {
18+ version : {
19+ $type : 'string'
20+ } ,
21+ search : {
22+ $type : 'string' ,
23+ $required : true
24+ }
25+ } )
26+
27+ module . exports = extrovert . toNetlifyFunction ( async function search ( params ) {
28+ params = new SearchParams ( params ) ;
29+ let Content ;
30+ if ( conn == null ) {
31+ conn = mongoose . createConnection ( config . uri ) ;
32+ await conn . asPromise ( ) ;
33+ }
34+
35+ Content = conn . model ( 'Content' , contentSchema , 'Content' ) ;
36+
37+ const query = params . search ;
38+ const version = params . version ? + params . version . replace ( / \. x $ / , '' ) : null ;
39+ let results = await Content . aggregate ( [
40+ {
41+ $search : {
42+ index : 'mongoose-content' ,
43+ compound : {
44+ must : [
45+ ...( version ? [ {
46+ equals : {
47+ path : 'versionNumber' ,
48+ value : version
49+ }
50+ } ] : [ ] ) ,
51+ { text : { query, path : { wildcard : '*' } , fuzzy : { } } }
52+ ]
53+ }
54+ }
55+ } ,
56+ { $limit : 10 }
57+ ] ) ;
58+
59+ results = results . map ( doc => {
60+ const $ = cheerio . load ( doc . body ) ;
61+
62+ doc . body = $ ( 'p' ) . get ( 0 ) ? $ ( 'p' ) . first ( ) . html ( ) : doc . body ;
63+
64+ return doc ;
65+ } ) ;
66+
67+ return { results } ;
5968}
0 commit comments