Skip to content

Commit ce3568c

Browse files
committed
move model-count to service-action
1 parent c422b58 commit ce3568c

File tree

3 files changed

+24
-40
lines changed

3 files changed

+24
-40
lines changed

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,13 @@ export default function makeBaseModel(options: FeathersVuexOptions) {
177177
}
178178

179179
public static count(params) {
180-
params = params || {
181-
query: {}
182-
}
183-
params.query.$limit = 0; // <- limit 0 in feathers is a fast count query
184-
return this._dispatch('find', params).then((res) => {
185-
return res.total
186-
})
180+
return this._dispatch('count', params)
187181
}
188182

189183
public static countInStore(params) {
190184
return this._getters('count', params)
191185
}
192-
186+
193187
public static get(id, params) {
194188
if (params) {
195189
return this._dispatch('get', [id, params])

src/service-module/service-module.actions.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default function makeServiceActions(service: Service<any>) {
5050
commit('setPending', 'get')
5151
return service
5252
.get(id, params)
53-
.then(async function(item) {
53+
.then(async function (item) {
5454
dispatch('addOrUpdate', item)
5555
commit('unsetPending', 'get')
5656
return state.keyedById[id]
@@ -134,7 +134,7 @@ export default function makeServiceActions(service: Service<any>) {
134134

135135
return service
136136
.update(id, data, params)
137-
.then(async function(item) {
137+
.then(async function (item) {
138138
dispatch('addOrUpdate', item)
139139
commit('unsetPending', 'update')
140140
return state.keyedById[id]
@@ -164,7 +164,7 @@ export default function makeServiceActions(service: Service<any>) {
164164

165165
return service
166166
.patch(id, data, params)
167-
.then(async function(item) {
167+
.then(async function (item) {
168168
dispatch('addOrUpdate', item)
169169
commit('unsetPending', 'patch')
170170
return state.keyedById[id]
@@ -208,6 +208,22 @@ export default function makeServiceActions(service: Service<any>) {
208208
}
209209

210210
const actions = {
211+
count({ dispatch }, params) {
212+
params = params || {}
213+
params = fastCopy(params)
214+
215+
if (!params.query) {
216+
throw 'params must contain a query-object'
217+
}
218+
219+
params.query.$limit = 0 // <- limit 0 in feathers is a fast count query
220+
221+
return dispatch('find', params)
222+
.then(response => {
223+
return response.total || response.length
224+
})
225+
.catch(error => dispatch('handleFindError', { params, error }))
226+
},
211227
/**
212228
* Handle the response from the find action.
213229
*

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

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,6 @@ export default function makeServiceGetters() {
2121
list(state) {
2222
return state.ids.map(id => state.keyedById[id])
2323
},
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
52-
},
5324
find: state => params => {
5425
if (isRef(params)) {
5526
params = params.value
@@ -102,6 +73,9 @@ export default function makeServiceGetters() {
10273
data: values
10374
}
10475
},
76+
count: (state, getters) => params => {
77+
return getters.find(state)(params).total
78+
},
10579
get: ({ keyedById, tempsById, idField, tempIdField }) => (
10680
id,
10781
params = {}

0 commit comments

Comments
 (0)