Skip to content

Commit 8afc98f

Browse files
authored
Merge pull request #49 from dadi/feature/search-method
Search method
2 parents 3cd2005 + 4feea3b commit 8afc98f

File tree

12 files changed

+3345
-1480
lines changed

12 files changed

+3345
-1480
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
node_modules
22
.DS_Store
33
coverage/*
4-
scripts/*.svg
54
config/mongodb.test.json

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ cache:
55
notifications:
66
email: false
77
node_js:
8+
- '10'
9+
- '8'
810
- '6'
911
before_script:
1012
- npm prune
@@ -15,4 +17,4 @@ branches:
1517
- /^v\d+\.\d+\.\d+$/
1618
services:
1719
- mongodb
18-
before_install: if [[ `npm -v` != 3* ]]; then npm i -g npm@latest; fi
20+
before_install: if [[ `npm -v` != 5* ]]; then npm i -g npm@latest; fi

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# API MongoDB Adapter
22

3-
[![npm (scoped)](https://img.shields.io/npm/v/@dadi/api-mongodb.svg?maxAge=10800&style=flat-square)](https://www.npmjs.com/package/@dadi/api-mongodb)
4-
[![coverage](https://img.shields.io/badge/coverage-85%25-yellow.svg?style=flat-square)](https://github.com/dadi/api-mongodb)
3+
[![npm version](https://badge.fury.io/js/%40dadi%2Fapi-mongodb.svg)](https://badge.fury.io/js/%40dadi%2Fapi-mongodb)
4+
[![Coverage Status](https://coveralls.io/repos/github/dadi/api-mongodb/badge.svg?branch=master)](https://coveralls.io/github/dadi/api-mongodb?branch=master)
55
[![Build Status](https://travis-ci.org/dadi/api-mongodb.svg?branch=master)](https://travis-ci.org/dadi/api-mongodb)
66
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)
77
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)
@@ -19,7 +19,7 @@ To use this adapter with your DADI API installation, you'll need to add it to yo
1919

2020
```bash
2121
$ cd my-api
22-
$ npm install --save @dadi/api-mongodb
22+
$ npm install @dadi/api-mongodb
2323
```
2424

2525
## Tests

lib/index.js

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
419471
DataStore.prototype.createObjectIdFromString = function (query, schema) {
420-
Object.keys(query).forEach((key) => {
472+
Object.keys(query).forEach(key => {
421473
if (/apiVersion/.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-fA-F0-9]{24}$/)) {
505-
obj._id = ObjectID.createFromHexString(obj._id)
506-
}
507-
}
508-
509546
/**
510547
*
511548
*/

0 commit comments

Comments
 (0)