-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
service-module.getters.ts
156 lines (134 loc) · 4.4 KB
/
service-module.getters.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
eslint
@typescript-eslint/explicit-function-return-type: 0,
@typescript-eslint/no-explicit-any: 0
*/
import sift from 'sift'
import { _ } from '@feathersjs/commons'
import { filterQuery, sorter, select } from '@feathersjs/adapter-commons'
import { globalModels as models } from './global-models'
import _get from 'lodash/get'
import _omit from 'lodash/omit'
import { isRef } from '@vue/composition-api'
import { ServiceState } from '..'
import { Id } from '@feathersjs/feathers'
const FILTERS = ['$sort', '$limit', '$skip', '$select']
const additionalOperators = ['$elemMatch']
const getCopiesById = ({
keepCopiesInStore,
servicePath,
serverAlias,
copiesById
}) => {
if (keepCopiesInStore) {
return copiesById
} else {
const Model = _get(models, [serverAlias, 'byServicePath', servicePath])
return Model.copiesById
}
}
export default function makeServiceGetters() {
return {
list(state) {
return state.ids.map(id => state.keyedById[id])
},
find: state => params => {
if (isRef(params)) {
params = params.value
}
params = { ...params } || {}
// Set params.temps to true to include the tempsById records
params.temps = params.hasOwnProperty('temps') ? params.temps : false
// Set params.copies to true to include the copiesById records
params.copies = params.hasOwnProperty('copies') ? params.copies : false
const { paramsForServer, whitelist, keyedById } = state
const q = _omit(params.query || {}, paramsForServer)
const { query, filters } = filterQuery(q, {
operators: additionalOperators.concat(whitelist)
})
let values = _.values(keyedById)
if (params.temps) {
values.push(..._.values(state.tempsById))
}
values = values.filter(sift(query))
if (params.copies) {
const { idField } = state
const copiesById = getCopiesById(state)
values.forEach((val, i, arr) => {
const copy = copiesById[val[idField]]
if (copy) {
// replace keyedById value with existing clone value
arr[i] = copy
}
})
}
const total = values.length
if (filters.$sort) {
values.sort(sorter(filters.$sort))
}
if (filters.$skip) {
values = values.slice(filters.$skip)
}
if (typeof filters.$limit !== 'undefined') {
values = values.slice(0, filters.$limit)
}
if (filters.$select) {
values = values.map(value => _.pick(value, ...filters.$select.slice()))
}
return {
total,
limit: filters.$limit || 0,
skip: filters.$skip || 0,
data: values
}
},
count: (state, getters) => params => {
if (isRef(params)) {
params = params.value
}
if (!params.query) {
throw 'params must contain a query-object'
}
const cleanQuery = _omit(params.query, FILTERS)
params.query = cleanQuery
return getters.find(params).total
},
get: ({ keyedById, tempsById, idField, tempIdField }) => (
id,
params = {}
) => {
if (isRef(id)) {
id = id.value
}
if (isRef(params)) {
params = params.value
}
const record = keyedById[id] && select(params, idField)(keyedById[id])
if (record) {
return record
}
const tempRecord =
tempsById[id] && select(params, tempIdField)(tempsById[id])
return tempRecord || null
},
getCopyById: state => id => {
const copiesById = getCopiesById(state)
return copiesById[id]
},
isCreatePendingById: ({ isIdCreatePending }: ServiceState) => (id: Id) =>
isIdCreatePending.includes(id),
isUpdatePendingById: ({ isIdUpdatePending }: ServiceState) => (id: Id) =>
isIdUpdatePending.includes(id),
isPatchPendingById: ({ isIdPatchPending }: ServiceState) => (id: Id) =>
isIdPatchPending.includes(id),
isRemovePendingById: ({ isIdRemovePending }: ServiceState) => (id: Id) =>
isIdRemovePending.includes(id),
isSavePendingById: (state: ServiceState, getters) => (id: Id) =>
getters.isCreatePendingById(id) ||
getters.isUpdatePendingById(id) ||
getters.isPatchPendingById(id),
isPendingById: (state: ServiceState, getters) => (id: Id) =>
getters.isSavePendingById(id) || getters.isRemovePendingById(id)
}
}
export type GetterName = keyof ReturnType<typeof makeServiceGetters>