@wmfs/pg-model
Advanced tools
Comparing version 1.14.0 to 1.15.0
@@ -0,1 +1,13 @@ | ||
# [1.15.0](https://github.com/wmfs/pg-model/compare/v1.14.0...v1.15.0) (2019-04-09) | ||
### ✨ Features | ||
* Model.find options can now take fields array ([1a338c4](https://github.com/wmfs/pg-model/commit/1a338c4)) | ||
### 💎 Styles | ||
* remove an empty line ([da4f907](https://github.com/wmfs/pg-model/commit/da4f907)) | ||
# [1.14.0](https://github.com/wmfs/pg-model/compare/v1.13.0...v1.14.0) (2019-04-05) | ||
@@ -2,0 +14,0 @@ |
@@ -10,2 +10,3 @@ 'use strict' | ||
constructor (model) { | ||
this.model = model | ||
this.modelId = model.modelId | ||
@@ -16,3 +17,2 @@ this.propertyIdToColumn = model.propertyIdToColumn | ||
this.client = model.client | ||
this.sql = `SELECT ${model.columnNamesWithPropertyAliases} FROM ${model.fullTableName}` | ||
} | ||
@@ -36,4 +36,6 @@ | ||
const _this = this | ||
const parsedOptions = optionParser(this.sql, this.propertyIdToColumn, options) | ||
const sqlSelect = this.createSqlSelect(options) | ||
const parsedOptions = optionParser(sqlSelect, this.propertyIdToColumn, options) | ||
this.client.query( | ||
@@ -87,4 +89,17 @@ parsedOptions.sql, | ||
} | ||
createSqlSelect (options) { | ||
const filter = Array.isArray(options.fields) | ||
? ([col, prop]) => options.fields.includes(prop) | ||
: () => true | ||
const columnNamesWithPropertyAliases = | ||
Object.entries(this.model.columnToPropertyId) | ||
.filter(filter) | ||
.map(([col, prop]) => `${col} AS "${prop}"`) | ||
return `SELECT ${columnNamesWithPropertyAliases} FROM ${this.model.fullTableName}` | ||
} | ||
} | ||
module.exports = Finder |
@@ -49,3 +49,2 @@ 'use strict' | ||
this.columnNamesWithPropertyAliases = Object.entries(this.columnToPropertyId).map(([col, prop]) => `${col} AS "${prop}"`) | ||
this.propertyIds = Object.entries(this.columnToPropertyId).filter(([col]) => col[0] !== '_').map(([col, prop]) => prop) | ||
@@ -52,0 +51,0 @@ this.pkColumnNames = table.pkColumnNames |
{ | ||
"name": "@wmfs/pg-model", | ||
"version": "1.14.0", | ||
"version": "1.15.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,2 +323,21 @@ /* eslint-env mocha */ | ||
}) | ||
it('find Bart\'s name fields', async () => { | ||
const doc = await models.pgmodelTest.person.find( | ||
{ | ||
fields: ['firstName', 'lastName'], | ||
where: { | ||
firstName: { equals: 'Bart' }, | ||
lastName: { equals: 'Simpson' } | ||
} | ||
} | ||
) | ||
expect(doc).to.have.length(1) | ||
expect(doc[0]).to.deep.equal({ | ||
'firstName': 'Bart', | ||
'lastName': 'Simpson' | ||
}) | ||
}) | ||
it('find Bart or Lisa by first name', async () => { | ||
@@ -372,22 +391,2 @@ const doc = await models.pgmodelTest.person.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 () => { | ||
@@ -394,0 +393,0 @@ const doc = await models.pgmodelTest.person.find( |
Sorry, the diff of this file is not supported yet
148570
3551