Installations
npm install @tranminhhoang2/objection-filter
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=7.6.0
Node Version
10.18.0
NPM Version
6.13.4
Score
73.2
Supply Chain
99
Quality
75.1
Maintenance
100
Vulnerability
99.6
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
Download Statistics
Total Downloads
803
Last Day
1
Last Week
2
Last Month
3
Last Year
81
GitHub Statistics
1 Commits
1 Watching
8 Branches
1 Contributors
Package Meta Information
Latest Version
1.0.1
Package Id
@tranminhhoang2/objection-filter@1.0.1
Unpacked Size
137.47 kB
Size
22.27 kB
File Count
28
NPM Version
6.13.4
Node Version
10.18.0
Total Downloads
Cumulative downloads
Total Downloads
803
Last day
0%
1
Compared to previous day
Last week
0%
2
Compared to previous week
Last month
-57.1%
3
Compared to previous month
Last year
-46%
81
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
objection-filter
objection-filter is a filtering module for the objection.js ORM. It was originally based on objection-find, but has since moved in a different direction. It aims to fulfil some requirements that occur often during API development:
1. Filtering on nested relations
For example, if you have the models Customer belongsTo City belongsTo Country, we can query all Customers where the Country starts with A
.
2. Eagerly loading data
Eagerly load a bunch of related data in a single query. This is useful for getting a list models e.g. Customers then including all their Orders in the same query.
Shortcuts
objection-filter >= 1.0.0 is fully backwards compatible with older queries, but now supports nested and/or filtering as well as the new objection.js object notation. The 1.0.0 denotation was used due to these changes and the range of query combinations possible.
Usage
The filtering library can be applied onto every findAll REST endpoint e.g. GET /api/{Model}?filter={"limit": 1}
A typical express route handler with a filter applied:
1const { buildFilter } = require('objection-filter'); 2const { Customer } = require('./models'); 3 4app.get('/Customers', function(req, res, next) { 5 buildFilter(Customer) 6 .build(JSON.parse(req.query.filter)) 7 .then(customers => res.send(customers)) 8 .catch(next); 9});
Available filter properties include:
1// GET /api/Customers 2{ 3 // Filtering and eager loading 4 "eager": { 5 // Top level $where filters on the root model 6 "$where": { 7 "firstName": "John" 8 "profile.isActivated": true, 9 "city.country": { "$like": "A" } 10 }, 11 // Nested $where filters on each related model 12 "orders": { 13 "$where": { 14 "state.isComplete": true 15 }, 16 "products": { 17 "$where": { 18 "category.name": { "$like": "A" } 19 } 20 } 21 } 22 }, 23 // An objection.js order by expression 24 "order": "firstName desc", 25 "limit": 10, 26 "offset": 10, 27 // An array of dot notation fields to select on the root model and eagerly loaded models 28 "fields": ["firstName", "lastName", "orders.code", "products.name"] 29}
The
where
operator from < v1.0.0 is still available and can be combined with theeager
string type notation. The same is applicable to therequire
operator. For filtering going forward, it's recommended to use the objection object-notation for eager loading along with$where
definitions at each level.
Filter Operators
There are a number of built-in operations that can be applied to columns (custom ones can also be created). These include:
- $like - The SQL LIKE operator, can be used with expressions such as ab% to search for strings that start with ab
- $gt/$lt/$gte/$lte - Greater than and Less than operators for numerical fields
- =/$equals - Explicitly specify equality
- $in - Whether the target value is in an array of values
- $exists - Whether a property is not null
- $or - A top level OR conditional operator
Example
An example of operator usage
1{ 2 "eager": { 3 "$where": { 4 "property0": "Exactly Equals", 5 "property1": { 6 "$equals": 5 7 }, 8 "property2": { 9 "$gt": 5 10 }, 11 "property3": { 12 "$lt": 10, 13 "$gt": 5 14 }, 15 "property4": { 16 "$in": [ 1, 2, 3 ] 17 }, 18 "property5": { 19 "$exists": false 20 }, 21 "property6": { 22 "$or": [ 23 { "$in": [ 1, 2, 3 ] }, 24 { "$equals": 100 } 25 ] 26 } 27 } 28 } 29}
Logical Expressions
Logical expressions can be applied to both the eager
and require
helpers. The where
top level operator will eventually be deprecated and replaced by the new eager
object notation in objection.js.
Examples using $where
The $where
expression is used to "filter models". Given this, related fields between models can be mixed anywhere in the logical expression.
1{ 2 "eager": { 3 "$where": { 4 "$or": [ 5 { "city.country.name": "Australia" }, 6 { "city.code": "09" } 7 ] 8 } 9 } 10}
Logical expressions can also be nested
1{ 2 "eager": { 3 "$where": { 4 "$and": { 5 "name": "John", 6 "$or": [ 7 { "city.country.name": "Australia" }, 8 { "city.code": { "$like": "01" }} 9 ] 10 } 11 } 12 } 13}
Note that in these examples, all logical expressions come before the property name. However, logical expressions can also come after the property name.
1{ 2 "eager": { 3 "$where": { 4 "$or": [ 5 { "city.country.name": "Australia" }, 6 { 7 "city.code": { 8 "$or": [ 9 { "$equals": "12" }, 10 { "$like": "13" } 11 ] 12 } 13 } 14 ] 15 } 16 } 17}
The $where
will apply to the relation that immediately precedes it in the tree, in the above case "city". The $where
will apply to relations of the eager model using dot notation. For example, you can query Customers
, eager load their orders
and filter those orders by the product.name
. Note that product.name
is a related field of the order model, not the customers model.
Custom Operators
If the built in filter operators aren't quite enough, custom operators can be added. A common use case for this may be to add a lower case string comparison
operator, which may vary in implementation depending on the SQL dialect.
Example:
1const options = { 2 operators: { 3 $equalsLower: (property, operand, builder) => 4 builder.whereRaw('LOWER(??) = LOWER(?)', [property, operand]) 5 } 6}; 7 8buildFilter(Person, null, options) 9 .build({ 10 eager: { 11 $where: { 12 firstName: { $equalsLower: 'John' } 13 } 14 } 15 })
The $equalsLower
operator can now be used as a new operator and will use the custom operator callback specified.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Warn: project license file does not contain an FSF or OSI license.
Reason
Found 0/1 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: containerImage not pinned by hash: node_modules/knex/scripts/oracle-tests-Dockerfile:1
- Warn: downloadThenRun not pinned by hash: node_modules/knex/scripts/oracle-tests-Dockerfile:22
- Info: 0 out of 1 containerImage dependencies pinned
- Info: 0 out of 1 downloadThenRun dependencies pinned
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
126 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-9vvw-cc9w-f27h
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-rrc9-gqf8-8rwg
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-h6ch-v84p-w6p9
- Warn: Project is vulnerable to: GHSA-c6rp-wrp9-qr4q
- Warn: Project is vulnerable to: GHSA-r32x-jhw5-g48p
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-6h5x-7c5m-7cr7
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-qrmc-fj45-qfc2
- Warn: Project is vulnerable to: GHSA-mpcf-4gmh-23w8
- Warn: Project is vulnerable to: GHSA-9qj9-36jm-prpv
- Warn: Project is vulnerable to: GHSA-957j-59c2-j692
- Warn: Project is vulnerable to: GHSA-qh2h-chj9-jffq
- Warn: Project is vulnerable to: GHSA-m5pj-vjjf-4m3h
- Warn: Project is vulnerable to: GHSA-j383-35pm-c5h4
- Warn: Project is vulnerable to: GHSA-rm36-94g8-835r
- Warn: Project is vulnerable to: GHSA-q42p-pg8m-cqh6
- Warn: Project is vulnerable to: GHSA-w457-6q6x-cgp9
- Warn: Project is vulnerable to: GHSA-62gr-4qp9-h98f
- Warn: Project is vulnerable to: GHSA-f52g-6jhx-586p
- Warn: Project is vulnerable to: GHSA-2cf5-4w76-r9qv
- Warn: Project is vulnerable to: GHSA-3cqr-58rm-57f8
- Warn: Project is vulnerable to: GHSA-g9r4-xpmj-mj65
- Warn: Project is vulnerable to: GHSA-q2c6-c6pm-g3gh
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-44pw-h2cw-w3vq
- Warn: Project is vulnerable to: GHSA-jp4x-w63m-7wgm
- Warn: Project is vulnerable to: GHSA-c429-5p7v-vgjp
- Warn: Project is vulnerable to: GHSA-6x33-pw7p-hmpq
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-8g7p-74h8-hg48
- Warn: Project is vulnerable to: GHSA-pc5p-h8pf-mvwp
- Warn: Project is vulnerable to: GHSA-4hpf-3wq7-5rpr
- Warn: Project is vulnerable to: GHSA-f522-ffg8-j8r6
- Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546
- Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-282f-qqgm-c34q
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-2m96-9w4j-wgv7
- Warn: Project is vulnerable to: GHSA-h726-x36v-rx45
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-wrvr-8mpx-r7pp
- Warn: Project is vulnerable to: GHSA-hxm2-r34f-qmc5
- Warn: Project is vulnerable to: GHSA-gwg9-rgvj-4h5j
- Warn: Project is vulnerable to: GHSA-28xh-wpgr-7fm8
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-f9cm-p3w6-xvr3
- Warn: Project is vulnerable to: GHSA-jjv7-qpx3-h62q
- Warn: Project is vulnerable to: GHSA-gqgv-6jq5-jjj9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-hxcm-v35h-mg2x
- Warn: Project is vulnerable to: GHSA-6g33-f262-xjp4
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-hjp8-2cm3-cc45
- Warn: Project is vulnerable to: GHSA-x3m3-4wpv-5vgc
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-c9g6-9335-x697
- Warn: Project is vulnerable to: GHSA-2m39-62fm-q8r3
- Warn: Project is vulnerable to: GHSA-mf6x-7mm4-x2g7
- Warn: Project is vulnerable to: GHSA-g7q5-pjjr-gqvp
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-cf4h-3jhx-xvhq
- Warn: Project is vulnerable to: GHSA-v2p6-4mp7-3r9v
- Warn: Project is vulnerable to: GHSA-pv4c-p2j5-38j4
- Warn: Project is vulnerable to: GHSA-46c4-8wrp-j99v
- Warn: Project is vulnerable to: GHSA-9m6j-fcg5-2442
- Warn: Project is vulnerable to: GHSA-hh27-ffr2-f2jc
- Warn: Project is vulnerable to: GHSA-rqff-837h-mm52
- Warn: Project is vulnerable to: GHSA-8v38-pw62-9cw2
- Warn: Project is vulnerable to: GHSA-hgjh-723h-mx2j
- Warn: Project is vulnerable to: GHSA-jf5r-8hm2-f872
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-cf66-xwfp-gvc4
- Warn: Project is vulnerable to: GHSA-g78m-2chm-r7qv
- Warn: Project is vulnerable to: GHSA-776f-qx25-q3cc
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-hr2v-3952-633q
- Warn: Project is vulnerable to: GHSA-xf7w-r453-m56c
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-58v4-qwx5-7f59
- Warn: Project is vulnerable to: GHSA-4jv9-3563-23j3
- Warn: Project is vulnerable to: GHSA-4xcv-9jjx-gfj3
- Warn: Project is vulnerable to: GHSA-fhjf-83wg-r2j9
- Warn: Project is vulnerable to: GHSA-r659-8xfp-j327
- Warn: Project is vulnerable to: GHSA-g6ww-v8xp-vmwg
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-j44m-qm6p-hp7m
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-xc7v-wxcw-j472
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
1.6
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More