Gathering detailed insights and metrics for @yadah/objection-scope
Gathering detailed insights and metrics for @yadah/objection-scope
Gathering detailed insights and metrics for @yadah/objection-scope
Gathering detailed insights and metrics for @yadah/objection-scope
npm install @yadah/objection-scope
Typescript
Module System
Min. Node Version
Node Version
NPM Version
70.4
Supply Chain
89.9
Quality
74.9
Maintenance
100
Vulnerability
100
License
JavaScript (99.92%)
Shell (0.08%)
Total Downloads
422
Last Day
1
Last Week
3
Last Month
8
Last Year
80
1 Stars
42 Commits
2 Watching
1 Branches
1 Contributors
Latest Version
0.2.0
Package Id
@yadah/objection-scope@0.2.0
Unpacked Size
11.85 kB
Size
3.40 kB
File Count
7
NPM Version
9.5.0
Node Version
18.14.2
Publised On
08 Apr 2023
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
0%
3
Compared to previous week
Last month
300%
8
Compared to previous month
Last year
-64%
80
Compared to previous year
A mixin for Objection.js which
extends the QueryBuilder to have a scope()
method. It allows a model to define
common query building functions based on a criteria object with a well-defined
shape.
The Model.scopes
object maps criteria keys to functions that work in a
similar way to Model.modifiers
. Scope modifiers accept the query object and the criteria object that was passed to the QueryBuilder.scope()
function (this object will have a property with key that matches the Model.scopes
key).
1import ScopeMixin from "@yadah/objection-scope" 2import { mixin, Model } from "objection" 3 4class MyModel extends mixin(Model, [ScopeMixin]) { 5 static get scopes() { 6 return { 7 ...super.scopes, 8 keyword: (query, { keyword }) { 9 return query.where("title", "like", `%${keyword}`) 10 } 11 } 12 } 13} 14 15MyModel.query().scope({ keyword: "abc" }) 16// select * from "tableName" where "tableName"."title" like ? 17// bindings: ["%abc"]
The ScopeBuilder class can be used to help write common modifier patterns.
1class MyModel extends mixin(Model, [ScopeMixin]) { 2 static get scopes() { 3 const matcher = new this.ScopeBuilder(); 4 return { 5 ...super.scopes, 6 field1: matcher, 7 field2: matcher, 8 }; 9 } 10} 11MyModel.query().scope({ field1: "foo", field2: "bar" }); 12// select * from "tableName" where "tableName"."field1" = ? and "tableName"."field2" = ? 13// bindings: ["foo", "bar"]
The matcher
object can be chained to produce various common types of query modifier.
1const matcher = new this.ScopeBuilder(); 2const scopes = { 3 field1: matcher, 4 minField2: matcher.min, 5 maxField3: matcher.max, 6 field4: matcher.related("related"), 7 field5: matcher.notNull, 8};
Expression | Result | Notes |
---|---|---|
matcher | .where(fieldName, value) | if value is not an array |
matcher | .whereIn(fieldName, value) | if value is an array |
matcher.min | .where(fieldName, ">=", value) | |
matcher.max | .where(fieldname, "<=", value) | |
matcher.related(relationName) | .joinRelated(relationName).where(`${relationName}.${fieldName}`, value) | if value is not an array |
matcher.related(relationName) | .joinRelated(relationName).whereIn(`${relationName}.${fieldName}`, value) | if value is an array |
matcher.exists | .whereExists(Model.relatedQuery(fieldName).scope(value)) | |
matcher.notNull | .whereNotNull(fieldname) | if value is true |
matcher.notNull | .whereNull(fieldName) | if value is false |
matcher.name(name) | changes fieldName to name |
No vulnerabilities found.
No security vulnerabilities found.