Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 85 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ const bufferEncoding = 'base64'
const idKey = '_id'


module.exports = { idKey, inputRecord, outputRecord, mapValues, castValue }
module.exports = {
idKey,
inputRecord,
outputRecord,
mapValues,
castValue,
generateQuery
}


// Cast and assign values per field.
Expand Down Expand Up @@ -139,3 +146,80 @@ function castValue (value) {
function generateId () {
return crypto.randomBytes(15).toString(bufferEncoding)
}

function generateRangeQuery (fields, options) {
const range = {}
Object.keys(options).forEach(key => {
if (!(key in fields)) return

const value = options[key]

if (fields[key].isArray) {
if (value[0] != null)
range[`${key}.${value[0] - 1}`] = { $exists: true }
if (value[1] != null)
range[`${key}.${value[1]}`] = { $exists: false }
return
}

range[key] = { $ne: null }
if (value[0] != null) range[key].$gte = castValue(value[0])
if (value[1] != null) range[key].$lte = castValue(value[1])
})

return range
}

// Wrap operator queries with the $not operator
function applyNotOperator (clause) {
return mapValues(clause, value => {
return { $not: value }
})
}

// Generate a mongoDB query from a fortune query object
function generateQuery (fields, options, not) {
let $and = []
for (const key in options)
switch (key) {
case 'and':
case 'or':
const query = {}
query[`$${key}`] = mapValues(options[key], value => {
return generateQuery(fields, value)
})
return query
case 'not':
return generateQuery(fields, options[key], true)
case 'range':
$and.push(generateRangeQuery(fields, options.range))
break
case 'match':
$and.push(mapValues(options.match, value => Array.isArray(value) ?
{ $in: value.map(castValue) } : castValue(value)))
break
case 'exists':
$and.push(mapValues(options.exists, (value, key) => {
if (!(key in fields)) return void 0

if (fields[key].isArray)
return value ? { $ne: [] } : []

return value ? { $ne: null } : null
}))
break
default:
}

if (not)
$and = $and.map(applyNotOperator)

switch ($and.length) {
case 0:
return {}
case 1:
return $and[0]
default:
return { $and }
}
}
47 changes: 3 additions & 44 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const Store = require('nedb')
const helpers = require('./helpers')
const inputRecord = helpers.inputRecord
const outputRecord = helpers.outputRecord
const generateQuery = helpers.generateQuery
const mapValues = helpers.mapValues
const castValue = helpers.castValue
const idKey = helpers.idKey


Expand Down Expand Up @@ -74,49 +74,8 @@ module.exports = Adapter => class NedbAdapter extends Adapter {
if (!options) options = {}

const Promise = this.Promise
const recordTypes = this.recordTypes
const isArrayKey = this.keys.isArray
let query = { $and: [] }

if ('match' in options)
query.$and.push(mapValues(options.match, value =>
Array.isArray(value) ? { $in: value.map(castValue) } :
castValue(value)))

if ('exists' in options)
query.$and.push(mapValues(options.exists, (value, key) => {
if (!(key in recordTypes[type])) return void 0

if (recordTypes[type][key][isArrayKey])
return value ? { $ne: [] } : []

return value ? { $ne: null } : null
}))

if ('range' in options) {
const range = {}

query.$and.push(range)
Object.keys(options.range).forEach(key => {
if (!(key in recordTypes[type])) return

const value = options.range[key]

if (recordTypes[type][key][isArrayKey]) {
if (value[0] != null)
range[`${key}.${value[0] - 1}`] = { $exists: true }
if (value[1] != null)
range[`${key}.${value[1]}`] = { $exists: false }
return
}

range[key] = { $ne: null }
if (value[0] != null) range[key].$gte = castValue(value[0])
if (value[1] != null) range[key].$lte = castValue(value[1])
})
}

if (!query.$and.length) delete query.$and
const fields = this.recordTypes[type]
let query = generateQuery(fields, options)

if ('query' in options) {
const result = options.query(query)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"@tap-format/dot": "^0.2.0",
"eslint": "^3.8.1",
"eslint-config-boss": "^1.0.4",
"fortune": "^4.2.10",
"fortune": "fortunejs/fortune#find-logical-operators",
"mkdirp": "^0.5.1",
"rimraf": "^2.5.4",
"tapdance": "^4.1.2"
"tapdance": "^5.0.2"
},
"files": [
"lib/",
Expand Down