@@ -144,26 +144,28 @@ DataStore.prototype.find = function ({ query, collection, options = {}, schema,
144144 return Promise . reject ( new Error ( 'DB_DISCONNECTED' ) )
145145 }
146146
147- query = this . prepareQuery ( query , schema )
148-
149- debug ( 'find in %s %o %o' , collection , query , options )
150-
151147 return new Promise ( ( resolve , reject ) => {
152148 // have we been passed an aggregation pipeline query?
153149 if ( Array . isArray ( query ) ) {
150+ debug ( 'aggregate in %s %o %o' , collection , JSON . stringify ( query ) , options )
151+
154152 this . database . collection ( collection ) . aggregate ( query , options , ( err , result ) => {
155153 if ( err ) return reject ( err )
156154 return resolve ( result )
157155 } )
158156 } else {
157+ query = this . prepareQuery ( query , schema )
158+
159+ debug ( 'find in %s %o %o' , collection , query , options )
160+
159161 this . database . collection ( collection ) . find ( query , options , ( err , cursor ) => {
160162 if ( err ) return reject ( err )
161163
162164 cursor . count ( ) . then ( count => {
163165 cursor . toArray ( ( err , result ) => {
164166 if ( err ) return reject ( err )
165167
166- const returnData = {
168+ let returnData = {
167169 results : result . map ( document => {
168170 if ( document . _id ) {
169171 document . _id = document . _id . toString ( )
@@ -193,6 +195,54 @@ DataStore.prototype.handshake = function () {
193195 }
194196}
195197
198+ /** Search for documents in the database
199+ *
200+ * @param {Object|Array } words -
201+ * @param {string } collection - the name of the collection to search
202+ * @param {object } options - options to modify the query
203+ * @param {Object } schema - the JSON schema for the collection
204+ * @param {Object } settings - the JSON settings configuration for the collection
205+ * @returns {Promise.<Array, Error> }
206+ */
207+ DataStore . prototype . search = function ( { words, collection, options = { } , schema, settings } ) {
208+ if ( this . readyState !== STATE_CONNECTED ) {
209+ return Promise . reject ( new Error ( 'DB_DISCONNECTED' ) )
210+ }
211+
212+ debug ( 'search in %s for %o' , collection , words )
213+
214+ let query = [
215+ {
216+ $match : {
217+ word : {
218+ $in : words
219+ }
220+ }
221+ } ,
222+ {
223+ $group : {
224+ _id : { document : '$document' } ,
225+ count : { $sum : 1 } ,
226+ weight : { $sum : '$weight' }
227+ }
228+ } ,
229+ {
230+ $sort : {
231+ weight : - 1
232+ }
233+ } ,
234+ { $limit : options . limit || 100 }
235+ ]
236+
237+ return this . find ( {
238+ query,
239+ collection,
240+ options,
241+ schema,
242+ settings
243+ } )
244+ }
245+
196246/**
197247 * Insert documents into the database
198248 *
@@ -286,6 +336,8 @@ DataStore.prototype.delete = function ({query, collection, schema}) {
286336
287337 query = this . prepareQuery ( query , schema )
288338
339+ debug ( 'delete %s %o %o %o' , collection , query )
340+
289341 return new Promise ( ( resolve , reject ) => {
290342 this . database . collection ( collection ) . deleteMany ( query , ( err , result ) => {
291343 if ( err ) return reject ( err )
@@ -417,13 +469,13 @@ DataStore.prototype.prepareQuery = function (query, schema) {
417469 * @param {Object } schema - a collection schema
418470 */
419471DataStore . prototype . createObjectIdFromString = function ( query , schema ) {
420- Object . keys ( query ) . forEach ( ( key ) => {
472+ Object . keys ( query ) . forEach ( key => {
421473 if ( / a p i V e r s i o n / . test ( key ) ) {
422474 return
423475 }
424476
425- const fieldSettings = getSchemaOrParent ( key , schema )
426- const type = fieldSettings ? fieldSettings . type : undefined
477+ let fieldSettings = getSchemaOrParent ( key , schema )
478+ let type = fieldSettings ? fieldSettings . type : undefined
427479
428480 if ( key === '$in' ) {
429481 if ( typeof query [ key ] === 'object' && Array . isArray ( query [ key ] ) ) {
@@ -491,21 +543,6 @@ function getSchemaOrParent (key, schema) {
491543 }
492544}
493545
494- /**
495- * Takes object and casts fields to BSON types as per this model's schema
496- *
497- * @param {object } obj
498- * @return undefined
499- * @api private
500- */
501- DataStore . prototype . castToBSON = function ( obj ) {
502- // TODO: Do we need to handle casting for all fields, or will `_id` be the only BSON specific type?
503- // this is starting to enter ODM land...
504- if ( typeof obj . _id === 'string' && ObjectID . isValid ( obj . _id ) && obj . _id . match ( / ^ [ a - f A - F 0 - 9 ] { 24 } $ / ) ) {
505- obj . _id = ObjectID . createFromHexString ( obj . _id )
506- }
507- }
508-
509546/**
510547 *
511548 */
0 commit comments