Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
abstracted mango query
Browse files Browse the repository at this point in the history
  • Loading branch information
larryboymi committed Oct 22, 2016
1 parent 4446548 commit 5b805d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
27 changes: 11 additions & 16 deletions services/clients/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
const express = require('express')
const agent = require('../../src/agent')
const { couchUrl } = require('../config')
const _ = require('underscore')

const mango = require('../mango')
const router = express.Router()

const getClients = (q) =>
agent.post(`${couchUrl}/clients/_find`)
.send({
selector: {
_id: {
$gt: null
},
last_name: {
$regex: `(?i)^(${q})`
}
const clientMango = (q) => ({
selector: {
_id: {
$gt: null
},
last_name: {
$regex: `(?i)^(${q})`
}
})
.then(({ body }) => body.docs)
}
})

router.get('/', (req, res) =>
getClients(req.query.q)
mango('clients', clientMango(req.query.q))
.then((clients) => res.json(_(clients).sortBy('first_name')))
)

Expand Down
9 changes: 9 additions & 0 deletions services/mango.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const agent = require('../src/agent')
const { couchUrl } = require('./config')

const execMango = (db, mango) =>
agent.post(`${couchUrl}/${db}/_find`)
.send(mango)
.then(({ body }) => body.docs)

module.exports = execMango

0 comments on commit 5b805d5

Please sign in to comment.