Skip to content

Commit dcbcc24

Browse files
committed
Updating countInStore to fix issues with passing params incorrectly. Also added the ability to pass query parameters to both count() and countInStore()
1 parent cdfe39b commit dcbcc24

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

docs/model-classes.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ created () {
8585
}
8686
```
8787

88-
### count()
88+
### count(params)
8989

90-
Model classes have a `count` method, which is a proxy to the [`find` action] with the param `$limit: 0` (./service-plugin.html#find-params). <Badge text="3.10.5+" />
90+
Model classes have a `count` method, which is a proxy to the [`find` action] with the param `$limit: 0` automaticalluy appended. On the Feathers server, `$limit: 0` results in a fast count query. (./service-plugin.html#find-params). <Badge text="3.10.5+" />
9191

9292
```js
9393
// In your Vue component
9494
async created () {
9595
const { Todo } = this.$FeathersVuex.api
96-
const todosCount = await Todo.count()
96+
const todosCount = await Todo.count({ query: { priority: 'critical' }})
9797
// or
9898
Todo.count().then((total) => { this.todoCount = total })
9999
}
@@ -107,7 +107,7 @@ Model classes have a `countInStore` method, which is a proxy to the [`count` get
107107
// In your Vue component
108108
created () {
109109
const { Todo } = this.$FeathersVuex.api
110-
const todosCount = Todo.countInStore()
110+
const todosCount = Todo.countInStore({ query: { priority: 'critical' }})
111111
}
112112
```
113113

src/service-module/make-base-model.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,18 @@ export default function makeBaseModel(options: FeathersVuexOptions) {
176176
return this._getters('find', params)
177177
}
178178

179-
public static count() {
180-
const params = {
181-
$limit: 0 // <- limit 0 in feathers is a fast count query
179+
public static count(params) {
180+
params = params || {
181+
query: {}
182182
}
183+
params.query.$limit = 0; // <- limit 0 in feathers is a fast count query
183184
return this._dispatch('find', params).then((res) => {
184185
return res.total
185186
})
186187
}
187188

188-
public static countInStore() {
189-
return this._getters('count')
189+
public static countInStore(params) {
190+
return this._getters('count', params)
190191
}
191192

192193
public static get(id, params) {

src/service-module/service-module.getters.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,34 @@ export default function makeServiceGetters() {
2121
list(state) {
2222
return state.ids.map(id => state.keyedById[id])
2323
},
24-
count: state => () => {
25-
return state.ids.length;
24+
count: state => params => {
25+
if (isRef(params)) {
26+
params = params.value
27+
}
28+
params = { ...params } || {}
29+
30+
// Set params.temps to true to include the tempsById records
31+
params.temps = params.hasOwnProperty('temps') ? params.temps : false
32+
33+
const { paramsForServer, whitelist, keyedById } = state
34+
const q = _omit(params.query || {}, paramsForServer)
35+
const customOperators = Object.keys(q).filter(
36+
k => k[0] === '$' && !defaultOps.includes(k)
37+
)
38+
const cleanQuery = _omit(q, customOperators)
39+
40+
const { query, filters } = filterQuery(cleanQuery, {
41+
operators: additionalOperators.concat(whitelist)
42+
})
43+
let values = _.values(keyedById)
44+
45+
if (params.temps) {
46+
values = values.concat(_.values(state.tempsById))
47+
}
48+
49+
values = values.filter(sift(query))
50+
51+
return values.length
2652
},
2753
find: state => params => {
2854
if (isRef(params)) {

0 commit comments

Comments
 (0)