Installations
npm install objection-filter
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
>=14
Node Version
16.20.1
NPM Version
8.19.4
Score
61.8
Supply Chain
97.7
Quality
78.5
Maintenance
100
Vulnerability
99.3
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (72.38%)
TypeScript (27.62%)
Developer
tandg-digital
Download Statistics
Total Downloads
306,728
Last Day
59
Last Week
613
Last Month
5,877
Last Year
120,046
GitHub Statistics
113 Stars
260 Commits
24 Forks
5 Watching
14 Branches
9 Contributors
Package Meta Information
Latest Version
4.4.0
Package Id
objection-filter@4.4.0
Unpacked Size
172.55 kB
Size
33.02 kB
File Count
35
NPM Version
8.19.4
Node Version
16.20.1
Publised On
14 Feb 2024
Total Downloads
Cumulative downloads
Total Downloads
306,728
Last day
-80.5%
59
Compared to previous day
Last week
-47.3%
613
Compared to previous week
Last month
-21%
5,877
Compared to previous month
Last year
16.2%
120,046
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
1
Dev Dependencies
25
What is objection-filter?
objection-filter is a plugin for the objection.js ORM. It's designed to allow powerful filters and aggregations on your API.
Some examples of what you can do include:
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.
3. Aggregation and reporting
Creating quick counts and sums on a model can speed up development significantly. An example could be the numberOfOrders for a Customer model.
Shortcuts
Installation
npm i objection-filter --save
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. In later major versions of objection-filter, the top level "where" and "require" filters will be deprecated.
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
For any operators not available (eg ILIKE, refer to the custom operators section below).
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}
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 LIKE
operator, which may vary in implementation depending on the SQL dialect.
Example:
1const options = { 2 operators: { 3 $ilike: (property, operand, builder) => 4 builder.whereRaw('?? ILIKE ?', [property, operand]) 5 } 6}; 7 8buildFilter(Person, null, options) 9 .build({ 10 eager: { 11 $where: { 12 firstName: { $ilike: 'John' } 13 } 14 } 15 })
The $ilike
operator can now be used as a new operator and will use the custom operator callback specified.
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.
Aggregations
Aggregations such as count, sum, min, max, avg can be applied to the queried model.
Additionally for any aggregations, you can use them in other expressions above including:
- Filtering using
$where
- Ordering using
order
For more detailed descriptions of each feature, refer to the aggregations section.
Transform a basic aggregation like this on a GET /Customers
endpoint:
1{ 2 "eager": { 3 "$aggregations": [ 4 { 5 "type": "count", 6 "alias": "numberOfOrders", 7 "relation": "orders" 8 } 9 ] 10 } 11}
...into a result set like this:
1[ 2 { 3 "firstName": "John", 4 "lastName": "Smith", 5 "numberOfOrders": 10 6 },{ 7 "firstName": "Jane", 8 "lastName": "Bright", 9 "numberOfOrders": 5 10 },{ 11 "firstName": "Greg", 12 "lastName": "Parker", 13 "numberOfOrders": 7 14 } 15]
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 3/19 approved changesets -- score normalized to 1
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
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 14 are checked with a SAST tool
Reason
43 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- 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-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- 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-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-4jv9-3563-23j3
- 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-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-mqr2-w7wj-jjgr
- Warn: Project is vulnerable to: GHSA-49j4-86m8-q2jw
- Warn: Project is vulnerable to: GHSA-fpw7-j2hg-69v5
- Warn: Project is vulnerable to: GHSA-4rch-2fh8-94vw
- Warn: Project is vulnerable to: GHSA-pmh2-wpjm-fj45
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-g6ww-v8xp-vmwg
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-9qrh-qjmc-5w2p
- Warn: Project is vulnerable to: GHSA-jqv5-7xpx-qj74
- 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-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
1.8
/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