@wmfs/pg-model
Advanced tools
Comparing version 1.13.0 to 1.14.0
@@ -0,1 +1,14 @@ | ||
# [1.14.0](https://github.com/wmfs/pg-model/compare/v1.13.0...v1.14.0) (2019-04-05) | ||
### ✨ Features | ||
* find filter - passing an array of values generates an OR subclause ([cb43876](https://github.com/wmfs/pg-model/commit/cb43876)) | ||
### 📦 Code Refactoring | ||
* Pull out buildWhereClause in option-parser ([a5469ee](https://github.com/wmfs/pg-model/commit/a5469ee)) | ||
* Swapped out _.forOwn in favour of for...of ([deef9d7](https://github.com/wmfs/pg-model/commit/deef9d7)) | ||
# [1.13.0](https://github.com/wmfs/pg-model/compare/v1.12.0...v1.13.0) (2019-02-08) | ||
@@ -2,0 +15,0 @@ |
@@ -1,3 +0,1 @@ | ||
const _ = require('lodash') | ||
const expressionTypeFormatters = { | ||
@@ -34,19 +32,6 @@ equals: function (columnName, value, values) { | ||
if (options) { | ||
let parts | ||
// WHERE | ||
// ----- | ||
if (options.hasOwnProperty('where')) { | ||
parts = [] | ||
sql += ' WHERE ' | ||
_.forOwn( | ||
options.where, | ||
function (expression, propertyId) { | ||
const columnName = propertyIdToColumn[propertyId] | ||
const expressionType = _.keys(expression)[0] | ||
const param = _.values(expression)[0] | ||
parts.push(expressionTypeFormatters[expressionType](columnName, param, values)) | ||
} | ||
) | ||
sql += parts.join(' AND ') | ||
sql += buildWhereClause(options.where, propertyIdToColumn, values) | ||
} | ||
@@ -57,3 +42,3 @@ | ||
if (options.hasOwnProperty('orderBy')) { | ||
parts = [] | ||
const parts = [] | ||
sql += ' ORDER BY ' | ||
@@ -90,2 +75,3 @@ options.orderBy.forEach( | ||
} | ||
return { | ||
@@ -95,2 +81,24 @@ sql: sql, | ||
} | ||
} | ||
} // refineSql | ||
function buildWhereClause (where, propertyIdToColumn, values) { | ||
const parts = [] | ||
for (const [propertyId, expression] of Object.entries(where)) { | ||
const columnName = propertyIdToColumn[propertyId] | ||
const expressionType = Object.keys(expression)[0] | ||
const formatter = expressionTypeFormatters[expressionType] | ||
const param = Object.values(expression)[0] | ||
if (!Array.isArray(param)) { | ||
parts.push(formatter(columnName, param, values)) | ||
} else { | ||
const subclauseParts = param | ||
.map(p => formatter(columnName, p, values)) | ||
const subclause = `(${subclauseParts.join(' OR ')})` | ||
parts.push(subclause) | ||
} | ||
} // for ... | ||
return ` WHERE ${parts.join(' AND ')}` | ||
} // buildWhereClause |
{ | ||
"name": "@wmfs/pg-model", | ||
"version": "1.13.0", | ||
"version": "1.14.0", | ||
"description": "Takes a relational database structure and returns model objects for noSQL-like abilities.", | ||
@@ -5,0 +5,0 @@ "author": "West Midlands Fire Service", |
@@ -323,3 +323,30 @@ /* eslint-env mocha */ | ||
}) | ||
it('find Bart or Lisa by first name', async () => { | ||
const doc = await models.pgmodelTest.person.find( | ||
{ | ||
where: { | ||
firstName: { equals: ['Bart', 'Lisa'] } | ||
} | ||
} | ||
) | ||
expect(doc).to.have.length(2) | ||
expect(doc).to.containSubset( | ||
[ | ||
{ | ||
'age': 10, | ||
'employeeNo': '5', | ||
'firstName': 'Bart', | ||
'lastName': 'Simpson' | ||
}, | ||
{ | ||
'age': 8, | ||
'employeeNo': '3', | ||
'firstName': 'Lisa', | ||
'lastName': 'Simpson' | ||
} | ||
] | ||
) | ||
}) | ||
it('find Bart by name', async () => { | ||
@@ -345,2 +372,22 @@ const doc = await models.pgmodelTest.peeps.find( | ||
it('find Bart by name', async () => { | ||
const doc = await models.pgmodelTest.peeps.find( | ||
{ | ||
where: { | ||
name: { equals: 'Bart Simpson' } | ||
} | ||
} | ||
) | ||
expect(doc).to.have.length(1) | ||
expect(doc).to.containSubset( | ||
[ | ||
{ | ||
'employeeNo': '5', | ||
'name': 'Bart Simpson' | ||
} | ||
] | ||
) | ||
}) | ||
it('find Bart and Lisa (eldest with limit 2/offset 2)', async () => { | ||
@@ -347,0 +394,0 @@ const doc = await models.pgmodelTest.person.find( |
Sorry, the diff of this file is not supported yet
147727
3543