Skip to content

Commit

Permalink
Merge pull request #49 from dadi/feature/search-method
Browse files Browse the repository at this point in the history
Search method
  • Loading branch information
jimlambie authored Jul 19, 2018
2 parents 3cd2005 + 4feea3b commit 8afc98f
Show file tree
Hide file tree
Showing 12 changed files with 3,345 additions and 1,480 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
node_modules
.DS_Store
coverage/*
scripts/*.svg
config/mongodb.test.json
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ cache:
notifications:
email: false
node_js:
- '10'
- '8'
- '6'
before_script:
- npm prune
Expand All @@ -15,4 +17,4 @@ branches:
- /^v\d+\.\d+\.\d+$/
services:
- mongodb
before_install: if [[ `npm -v` != 3* ]]; then npm i -g npm@latest; fi
before_install: if [[ `npm -v` != 5* ]]; then npm i -g npm@latest; fi
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# API MongoDB Adapter

[![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)
[![coverage](https://img.shields.io/badge/coverage-85%25-yellow.svg?style=flat-square)](https://github.com/dadi/api-mongodb)
[![npm version](https://badge.fury.io/js/%40dadi%2Fapi-mongodb.svg)](https://badge.fury.io/js/%40dadi%2Fapi-mongodb)
[![Coverage Status](https://coveralls.io/repos/github/dadi/api-mongodb/badge.svg?branch=master)](https://coveralls.io/github/dadi/api-mongodb?branch=master)
[![Build Status](https://travis-ci.org/dadi/api-mongodb.svg?branch=master)](https://travis-ci.org/dadi/api-mongodb)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)
[![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)
Expand All @@ -19,7 +19,7 @@ To use this adapter with your DADI API installation, you'll need to add it to yo

```bash
$ cd my-api
$ npm install --save @dadi/api-mongodb
$ npm install @dadi/api-mongodb
```

## Tests
Expand Down
83 changes: 60 additions & 23 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,28 @@ DataStore.prototype.find = function ({ query, collection, options = {}, schema,
return Promise.reject(new Error('DB_DISCONNECTED'))
}

query = this.prepareQuery(query, schema)

debug('find in %s %o %o', collection, query, options)

return new Promise((resolve, reject) => {
// have we been passed an aggregation pipeline query?
if (Array.isArray(query)) {
debug('aggregate in %s %o %o', collection, JSON.stringify(query), options)

this.database.collection(collection).aggregate(query, options, (err, result) => {
if (err) return reject(err)
return resolve(result)
})
} else {
query = this.prepareQuery(query, schema)

debug('find in %s %o %o', collection, query, options)

this.database.collection(collection).find(query, options, (err, cursor) => {
if (err) return reject(err)

cursor.count().then(count => {
cursor.toArray((err, result) => {
if (err) return reject(err)

const returnData = {
let returnData = {
results: result.map(document => {
if (document._id) {
document._id = document._id.toString()
Expand Down Expand Up @@ -193,6 +195,54 @@ DataStore.prototype.handshake = function () {
}
}

/** Search for documents in the database
*
* @param {Object|Array} words -
* @param {string} collection - the name of the collection to search
* @param {object} options - options to modify the query
* @param {Object} schema - the JSON schema for the collection
* @param {Object} settings - the JSON settings configuration for the collection
* @returns {Promise.<Array, Error>}
*/
DataStore.prototype.search = function ({ words, collection, options = {}, schema, settings }) {
if (this.readyState !== STATE_CONNECTED) {
return Promise.reject(new Error('DB_DISCONNECTED'))
}

debug('search in %s for %o', collection, words)

let query = [
{
$match: {
word: {
$in: words
}
}
},
{
$group: {
_id: { document: '$document' },
count: { $sum: 1 },
weight: { $sum: '$weight' }
}
},
{
$sort: {
weight: -1
}
},
{ $limit: options.limit || 100 }
]

return this.find({
query,
collection,
options,
schema,
settings
})
}

/**
* Insert documents into the database
*
Expand Down Expand Up @@ -286,6 +336,8 @@ DataStore.prototype.delete = function ({query, collection, schema}) {

query = this.prepareQuery(query, schema)

debug('delete %s %o %o %o', collection, query)

return new Promise((resolve, reject) => {
this.database.collection(collection).deleteMany(query, (err, result) => {
if (err) return reject(err)
Expand Down Expand Up @@ -417,13 +469,13 @@ DataStore.prototype.prepareQuery = function (query, schema) {
* @param {Object} schema - a collection schema
*/
DataStore.prototype.createObjectIdFromString = function (query, schema) {
Object.keys(query).forEach((key) => {
Object.keys(query).forEach(key => {
if (/apiVersion/.test(key)) {
return
}

const fieldSettings = getSchemaOrParent(key, schema)
const type = fieldSettings ? fieldSettings.type : undefined
let fieldSettings = getSchemaOrParent(key, schema)
let type = fieldSettings ? fieldSettings.type : undefined

if (key === '$in') {
if (typeof query[key] === 'object' && Array.isArray(query[key])) {
Expand Down Expand Up @@ -491,21 +543,6 @@ function getSchemaOrParent (key, schema) {
}
}

/**
* Takes object and casts fields to BSON types as per this model's schema
*
* @param {object} obj
* @return undefined
* @api private
*/
DataStore.prototype.castToBSON = function (obj) {
// TODO: Do we need to handle casting for all fields, or will `_id` be the only BSON specific type?
// this is starting to enter ODM land...
if (typeof obj._id === 'string' && ObjectID.isValid(obj._id) && obj._id.match(/^[a-fA-F0-9]{24}$/)) {
obj._id = ObjectID.createFromHexString(obj._id)
}
}

/**
*
*/
Expand Down
Loading

0 comments on commit 8afc98f

Please sign in to comment.