New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@wmfs/pg-model

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wmfs/pg-model - npm Package Compare versions

Comparing version

to
1.28.0

15

CHANGELOG.md

@@ -0,1 +1,16 @@

# [1.28.0](https://github.com/wmfs/pg-model/compare/v1.27.0...v1.28.0) (2021-11-25)
### ✨ Features
* ability to search model ([6b865ee](https://github.com/wmfs/pg-model/commit/6b865ee5155c1ef1f55b5d86b6e73390ead4133c))
### 🛠 Builds
* **deps-dev:** update dependency mocha to v9.1.3 ([d50db58](https://github.com/wmfs/pg-model/commit/d50db58341ebb2e2eefea7388f256676b1633652))
* **deps-dev:** update dependency standard to v16.0.4 ([aa9609d](https://github.com/wmfs/pg-model/commit/aa9609dc6c71d7c275968c255e2e7cbec1fd95dd))
* **deps-dev:** update semantic-release monorepo ([b0d613c](https://github.com/wmfs/pg-model/commit/b0d613c318564b1a799991de5ac438cb6ce46337))
* **deps-dev:** update semantic-release monorepo ([1496c46](https://github.com/wmfs/pg-model/commit/1496c465f8851643b81af343ad12021e9c9cdc96))
# [1.27.0](https://github.com/wmfs/pg-model/compare/v1.26.3...v1.27.0) (2021-11-24)

@@ -2,0 +17,0 @@

@@ -25,2 +25,33 @@ 'use strict'

async search (options = {}) {
const page = options.page || 1
const limit = options.limit || 10
const offset = Number.isInteger(options.offset)
? options.offset
: (page - 1) * limit
const where = options.where || {}
const filters = {
where,
offset,
limit,
fields: options.fields || [],
orderBy: options.orderBy || [],
nullsLast: options.nullsLast || false
}
const totalHits = await this.findCount({ where })
const results = await this.findAll(filters)
const totalPages = Math.ceil(totalHits / limit)
return {
page,
totalPages,
results,
totalHits
}
} // search
async findCount (options = {}) {

@@ -27,0 +58,0 @@ const sqlSelect = `SELECT COUNT(*) FROM ${this.model.fullTableName}`

10

lib/Model/index.js

@@ -9,3 +9,3 @@ 'use strict'

const NotSet = 'NetSet'
const NotSet = 'NotSet'

@@ -100,2 +100,10 @@ function callbackify (promise, callback) {

search (options, callback = NotSet) {
if (callback !== NotSet) {
return callbackify(this.search(options), callback)
}
return this.finder.search(options)
}
findById (id, callback = NotSet) {

@@ -102,0 +110,0 @@ if (callback !== NotSet) {

2

lib/utils/option-parser.js

@@ -44,3 +44,3 @@ const expressionTypeFormatters = {

// --------
if (Object.prototype.hasOwnProperty.call(options, 'orderBy')) {
if (Object.prototype.hasOwnProperty.call(options, 'orderBy') && options.orderBy.length) {
const parts = []

@@ -47,0 +47,0 @@ sql += ' ORDER BY '

{
"name": "@wmfs/pg-model",
"version": "1.27.0",
"version": "1.28.0",
"description": "Takes a relational database structure and returns model objects for noSQL-like abilities.",

@@ -28,4 +28,4 @@ "author": "West Midlands Fire Service",

"devDependencies": {
"@semantic-release/changelog": "5.0.1",
"@semantic-release/git": "9.0.0",
"@semantic-release/changelog": "6.0.1",
"@semantic-release/git": "10.0.1",
"@wmfs/hl-pg-client": "1.28.0",

@@ -38,6 +38,6 @@ "@wmfs/pg-diff-sync": "1.25.0",

"cz-conventional-changelog": "3.3.0",
"mocha": "9.0.3",
"mocha": "9.1.3",
"nyc": "15.1.0",
"semantic-release": "17.4.4",
"standard": "16.0.3"
"semantic-release": "18.0.1",
"standard": "16.0.4"
},

@@ -44,0 +44,0 @@ "scripts": {

@@ -1102,2 +1102,63 @@ /* eslint-env mocha */

describe('searching', () => {
it('search all documents without options', async () => {
const doc = await models.pgmodelTest.person.search({})
expect(doc.page).to.eql(1)
expect(doc.totalPages).to.eql(1)
expect(doc.totalHits).to.eql(5)
expect(doc.results.length).to.eql(5)
})
it('search all documents with offset as 2', async () => {
const doc = await models.pgmodelTest.person.search({ offset: 2 })
expect(doc.page).to.eql(1)
expect(doc.totalPages).to.eql(1)
expect(doc.totalHits).to.eql(5)
expect(doc.results.length).to.eql(3)
})
it('search all documents with limit as 2', async () => {
const doc = await models.pgmodelTest.person.search({ limit: 2 })
expect(doc.page).to.eql(1)
expect(doc.totalPages).to.eql(3)
expect(doc.totalHits).to.eql(5)
expect(doc.results.length).to.eql(2)
})
it('search all documents with limit as 2 and page as 2', async () => {
const doc = await models.pgmodelTest.person.search({ limit: 2, page: 2 })
expect(doc.page).to.eql(2)
expect(doc.totalPages).to.eql(3)
expect(doc.totalHits).to.eql(5)
expect(doc.results.length).to.eql(2)
})
it('search all documents with limit as 2 and offset as 4', async () => {
const doc = await models.pgmodelTest.person.search({ limit: 2, offset: 4 })
expect(doc.totalPages).to.eql(3)
expect(doc.totalHits).to.eql(5)
expect(doc.results.length).to.eql(1)
})
it('search all documents with filter on and order by age', async () => {
const doc = await models.pgmodelTest.person.search({
orderBy: ['-age'],
fields: ['firstName', 'lastName'],
where: {
age: { moreThan: 30 }
}
})
expect(doc.page).to.eql(1)
expect(doc.totalPages).to.eql(1)
expect(doc.totalHits).to.eql(2)
expect(doc.results.length).to.eql(2)
expect(doc.results[0].firstName).to.eql('Homer')
expect(
Object.keys(doc.results[0]).sort()
).to.eql(
['firstName', 'lastName']
)
})
})
describe('cleanup', () => {

@@ -1104,0 +1165,0 @@ it('finally drop-cascade the pg_model_test schema', async () => {

@@ -1161,2 +1161,94 @@ /* eslint-env mocha */

describe('searching', () => {
it('search all documents without options', done => {
models.pgmodelTest.person.search(
{},
(err, doc) => {
expect(doc.page).to.eql(1)
expect(doc.totalPages).to.eql(1)
expect(doc.totalHits).to.eql(5)
expect(doc.results.length).to.eql(5)
done(err)
}
)
})
it('search all documents with offset as 2', done => {
models.pgmodelTest.person.search(
{ offset: 2 },
(err, doc) => {
expect(doc.page).to.eql(1)
expect(doc.totalPages).to.eql(1)
expect(doc.totalHits).to.eql(5)
expect(doc.results.length).to.eql(3)
done(err)
}
)
})
it('search all documents with limit as 2', done => {
models.pgmodelTest.person.search(
{ limit: 2 },
(err, doc) => {
expect(doc.page).to.eql(1)
expect(doc.totalPages).to.eql(3)
expect(doc.totalHits).to.eql(5)
expect(doc.results.length).to.eql(2)
done(err)
}
)
})
it('search all documents with limit as 2 and page as 2', done => {
models.pgmodelTest.person.search(
{ limit: 2, page: 2 },
(err, doc) => {
expect(doc.page).to.eql(2)
expect(doc.totalPages).to.eql(3)
expect(doc.totalHits).to.eql(5)
expect(doc.results.length).to.eql(2)
done(err)
}
)
})
it('search all documents with limit as 2 and offset as 4', done => {
models.pgmodelTest.person.search(
{ limit: 2, offset: 4 },
(err, doc) => {
expect(doc.page).to.eql(1)
expect(doc.totalPages).to.eql(3)
expect(doc.totalHits).to.eql(5)
expect(doc.results.length).to.eql(1)
done(err)
}
)
})
it('search all documents with filter on and order by age', done => {
models.pgmodelTest.person.search(
{
orderBy: ['-age'],
fields: ['firstName', 'lastName'],
where: {
age: { moreThan: 30 }
}
},
(err, doc) => {
expect(doc.page).to.eql(1)
expect(doc.totalPages).to.eql(1)
expect(doc.totalHits).to.eql(2)
expect(doc.results.length).to.eql(2)
expect(doc.results[0].firstName).to.eql('Homer')
expect(
Object.keys(doc.results[0]).sort()
).to.eql(
['firstName', 'lastName']
)
done(err)
}
)
})
})
describe('cleanup', () => {

@@ -1163,0 +1255,0 @@ it('finally drop-cascade the pg_model_test schema', async () => {