Gathering detailed insights and metrics for objection-filter
Gathering detailed insights and metrics for objection-filter
Gathering detailed insights and metrics for objection-filter
Gathering detailed insights and metrics for objection-filter
npm install objection-filter
Typescript
Module System
Min. Node Version
Node Version
NPM Version
61.8
Supply Chain
97.7
Quality
78.5
Maintenance
100
Vulnerability
99.3
License
JavaScript (72.38%)
TypeScript (27.62%)
Total Downloads
306,728
Last Day
59
Last Week
613
Last Month
5,877
Last Year
120,046
113 Stars
260 Commits
24 Forks
5 Watching
14 Branches
9 Contributors
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
Cumulative downloads
Total Downloads
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
1
25
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:
For example, if you have the models Customer belongsTo City belongsTo Country, we can query all Customers where the Country starts with A
.
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.
Creating quick counts and sums on a model can speed up development significantly. An example could be the numberOfOrders for a Customer model.
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.
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.
There are a number of built-in operations that can be applied to columns (custom ones can also be created). These include:
For any operators not available (eg ILIKE, refer to the custom operators section below).
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}
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 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.
$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 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:
$where
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
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
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
43 existing vulnerabilities detected
Details
Score
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