Skip to content

Commit 62d56fb

Browse files
committed
Adding new model functions count() and countInStore()
1 parent 0af97a1 commit 62d56fb

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

docs/model-classes.md

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

88+
### count()
89+
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+" />
91+
92+
```js
93+
// In your Vue component
94+
async created () {
95+
const { Todo } = this.$FeathersVuex.api
96+
const todosCount = await Todo.count()
97+
// or
98+
Todo.count().then((total) => { this.todoCount = total })
99+
}
100+
```
101+
102+
### countInStore(params)
103+
104+
Model classes have a `countInStore` method, which is a proxy to the [`count` getter](./service-plugin.html#Service-Getters). <Badge text="3.10.5+" />
105+
106+
```js
107+
// In your Vue component
108+
created () {
109+
const { Todo } = this.$FeathersVuex.api
110+
const todosCount = Todo.countInStore()
111+
}
112+
```
113+
88114
### get(id, params)
89115

90116
Model classes have a `get` method, which is a proxy to the [`get` action](./service-plugin.html#get-id-or-get-id-params). <Badge text="1.7.0+" /> Notice that the signature is more Feathers-like, and doesn't require using an array to passing both id and params.

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ 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
182+
}
183+
return this._dispatch('find', params).then((res) => {
184+
return res.total
185+
})
186+
}
187+
188+
public static countInStore() {
189+
return this._getters('count')
190+
}
191+
179192
public static get(id, params) {
180193
if (params) {
181194
return this._dispatch('get', [id, params])

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ 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;
26+
},
2427
find: state => params => {
2528
if (isRef(params)) {
2629
params = params.value

test/service-module/model-base.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ describe('makeModel / BaseModel', function () {
6060
'getId',
6161
'find',
6262
'findInStore',
63+
'count',
64+
'countInStore',
6365
'get',
6466
'getFromStore'
6567
]

test/service-module/model-methods.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,27 @@ describe('Models - Methods', function() {
155155
assert(typeof Task.findInStore === 'function')
156156
})
157157

158+
it('Model.count is a function', function() {
159+
const { Task } = makeContext()
160+
161+
assert(typeof Task.count === 'function')
162+
})
163+
164+
it('Model.count returns a Promise', function() {
165+
const { Task } = makeContext()
166+
const result = Task.count()
167+
assert(typeof result.then !== 'undefined')
168+
result.catch(err => {
169+
/* noop -- prevents UnhandledPromiseRejectionWarning */
170+
})
171+
})
172+
173+
it('Model.countInStore', function() {
174+
const { Task } = makeContext()
175+
176+
assert(typeof Task.countInStore === 'function')
177+
})
178+
158179
it('Model.get', function() {
159180
const { Task } = makeContext()
160181

0 commit comments

Comments
 (0)