@wmfs/pg-model
Advanced tools
Comparing version 1.26.3 to 1.27.0
@@ -0,1 +1,8 @@ | ||
# [1.27.0](https://github.com/wmfs/pg-model/compare/v1.26.3...v1.27.0) (2021-11-24) | ||
### ✨ Features | ||
* model and view functions to find counts ([8f61864](https://github.com/wmfs/pg-model/commit/8f6186440ee6797b1ce2f32b388e06f0bde6902e)) | ||
## [1.26.3](https://github.com/wmfs/pg-model/compare/v1.26.2...v1.26.3) (2021-11-24) | ||
@@ -2,0 +9,0 @@ |
@@ -25,2 +25,11 @@ 'use strict' | ||
async findCount (options = {}) { | ||
const sqlSelect = `SELECT COUNT(*) FROM ${this.model.fullTableName}` | ||
const parsedOptions = optionParser(sqlSelect, this.propertyIdToColumn, options) | ||
const result = await this.client.query(parsedOptions.sql, parsedOptions.values) | ||
return result && result.rows && result.rows.length | ||
? +result.rows[0].count | ||
: 0 | ||
} // findCount | ||
async find (targetRoot, options) { | ||
@@ -27,0 +36,0 @@ const sqlSelect = this.createSqlSelect(options) |
@@ -117,2 +117,10 @@ 'use strict' | ||
findCount (options, callback = NotSet) { | ||
if (callback !== NotSet) { | ||
return callbackify(this.findCount(options), callback) | ||
} // if ... | ||
return this.finder.findCount(options) | ||
} | ||
findOne (options, callback = NotSet) { | ||
@@ -119,0 +127,0 @@ if (callback !== NotSet) { |
@@ -54,2 +54,16 @@ const _ = require('lodash') | ||
findCount (options, callback = NotSet) { | ||
if (callback === NotSet) return this.promised(this.findCount, options) | ||
const sql = `SELECT COUNT(*) FROM ${this.fullViewName}` | ||
const parsedOptions = optionParser(sql, this.propertyIdToColumn, options) | ||
this.client.query( | ||
parsedOptions.sql, | ||
parsedOptions.values | ||
) | ||
.then(result => callback(null, result && result.rows && result.rows.length ? +result.rows[0].count : 0)) | ||
.catch(err => callback(err)) | ||
} // findCount | ||
findOne (options, callback = NotSet) { | ||
@@ -56,0 +70,0 @@ options.limit = 1 |
{ | ||
"name": "@wmfs/pg-model", | ||
"version": "1.26.3", | ||
"version": "1.27.0", | ||
"description": "Takes a relational database structure and returns model objects for noSQL-like abilities.", | ||
@@ -28,2 +28,6 @@ "author": "West Midlands Fire Service", | ||
"devDependencies": { | ||
"@semantic-release/changelog": "5.0.1", | ||
"@semantic-release/git": "9.0.0", | ||
"@wmfs/hl-pg-client": "1.28.0", | ||
"@wmfs/pg-diff-sync": "1.25.0", | ||
"chai": "4.3.4", | ||
@@ -37,7 +41,3 @@ "chai-subset": "1.6.0", | ||
"semantic-release": "17.4.4", | ||
"standard": "16.0.3", | ||
"@semantic-release/changelog": "5.0.1", | ||
"@semantic-release/git": "9.0.0", | ||
"@wmfs/hl-pg-client": "1.28.0", | ||
"@wmfs/pg-diff-sync": "1.25.0" | ||
"standard": "16.0.3" | ||
}, | ||
@@ -44,0 +44,0 @@ "scripts": { |
@@ -652,2 +652,56 @@ /* eslint-env mocha */ | ||
describe('find counts', () => { | ||
it('count all documents', async () => { | ||
const doc = await models.pgmodelTest.person.findCount() | ||
expect(doc).to.eql(5) | ||
}) | ||
it('count one Homer by first name and last name', async () => { | ||
const doc = await models.pgmodelTest.person.findCount({ | ||
where: { | ||
firstName: { equals: 'Homer' }, | ||
lastName: { equals: 'Simpson' } | ||
} | ||
}) | ||
expect(doc).to.eql(1) | ||
}) | ||
it('count one Homer by name', async () => { | ||
const doc = await models.pgmodelTest.peeps.findCount({ | ||
where: { | ||
name: { equals: 'Homer Simpson' } | ||
} | ||
}) | ||
expect(doc).to.eql(1) | ||
}) | ||
it('shouldn\'t count one missing person', async () => { | ||
const doc = await models.pgmodelTest.person.findCount({ | ||
where: { | ||
firstName: { equals: 'Ned' }, | ||
lastName: { equals: 'Flanders' } | ||
} | ||
}) | ||
expect(doc).to.eql(0) | ||
}) | ||
it('shouldn\'t count one missing peep', async () => { | ||
const doc = await models.pgmodelTest.peeps.findCount({ | ||
where: { | ||
name: { equals: 'Ned Flanders' } | ||
} | ||
}) | ||
expect(doc).to.eql(0) | ||
}) | ||
it('count Bart or Lisa by first name', async () => { | ||
const doc = await models.pgmodelTest.person.findCount({ | ||
where: { | ||
firstName: { equals: ['Bart', 'Lisa'] } | ||
} | ||
}) | ||
expect(doc).to.eql(2) | ||
}) | ||
}) | ||
describe('destroy', () => { | ||
@@ -654,0 +708,0 @@ it('delete Maggie/Margaret by via her id', () => { |
@@ -506,2 +506,86 @@ /* eslint-env mocha */ | ||
describe('find counts', () => { | ||
it('count all documents', (done) => { | ||
models.pgmodelTest.person.findCount( | ||
{}, | ||
(err, doc) => { | ||
expect(doc).to.eql(5) | ||
done(err) | ||
} | ||
) | ||
}) | ||
it('count one Homer by first name and last name', (done) => { | ||
models.pgmodelTest.person.findCount( | ||
{ | ||
where: { | ||
firstName: { equals: 'Homer' }, | ||
lastName: { equals: 'Simpson' } | ||
} | ||
}, | ||
(err, doc) => { | ||
expect(doc).to.eql(1) | ||
done(err) | ||
} | ||
) | ||
}) | ||
it('count one Homer by name', (done) => { | ||
models.pgmodelTest.peeps.findCount( | ||
{ | ||
where: { | ||
name: { equals: 'Homer Simpson' } | ||
} | ||
}, | ||
(err, doc) => { | ||
expect(doc).to.eql(1) | ||
done(err) | ||
} | ||
) | ||
}) | ||
it('shouldn\'t count one missing person', (done) => { | ||
models.pgmodelTest.person.findCount( | ||
{ | ||
where: { | ||
firstName: { equals: 'Ned' }, | ||
lastName: { equals: 'Flanders' } | ||
} | ||
}, | ||
(err, doc) => { | ||
expect(doc).to.eql(0) | ||
done(err) | ||
} | ||
) | ||
}) | ||
it('shouldn\'t count one missing peep', (done) => { | ||
models.pgmodelTest.peeps.findCount( | ||
{ | ||
where: { | ||
name: { equals: 'Ned Flanders' } | ||
} | ||
}, | ||
(err, doc) => { | ||
expect(doc).to.eql(0) | ||
done(err) | ||
} | ||
) | ||
}) | ||
it('count Bart or Lisa by first name', (done) => { | ||
models.pgmodelTest.person.findCount( | ||
{ | ||
where: { | ||
firstName: { equals: ['Bart', 'Lisa'] } | ||
} | ||
}, | ||
(err, doc) => { | ||
expect(doc).to.eql(2) | ||
done(err) | ||
} | ||
) | ||
}) | ||
}) | ||
describe('update', () => { | ||
@@ -508,0 +592,0 @@ it('update Maggie\'s age to 1', function (done) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
170618
3739