You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `makeServicePlugin` method creates a vuex plugin which connects a Feathers service to the Vuex store. Once you create a plugin, you must register it in the Vuex store's `plugins` section.
9
11
10
-
See the [setup documentation](/api-overview.html#service-plugins) to learn the basics of setting up a Service Plugin.
12
+
## Setup
13
+
14
+
See the [setup documentation](./getting-started.html#service-plugins) to learn the basics of setting up a Service Plugin.
11
15
12
16
## Configuration
13
17
14
-
The following options are supported on `makeServicePlugin`.
18
+
The following options are supported on `makeServicePlugin`:
15
19
16
20
```js
17
-
{
18
-
idField:'_id', // The field in each record that will contain the id
19
-
nameStyle:'path', // Use the full service path as the Vuex module name, instead of just the last section
20
-
namespace:'custom-namespace', // Customize the Vuex module name. Overrides nameStyle.
21
-
debug:true, // Enable some logging for debugging
22
-
servicePath:'', // Not all Feathers service plugins expose the service path, so it can be manually specified when missing.
23
-
instanceDefaults: () => ({}), // Override this method to provide default data for new instances. If using Model classes, specify this as a static class property.
24
-
setupInstance:instance=> instance, // Override this method to setup data types or related data on an instance. If using Model classes, specify this as a static class property.
25
-
autoRemove:true, // Automatically remove records missing from responses (only use with feathers-rest)
26
-
enableEvents:false, // Turn off socket event listeners. It's true by default
21
+
importModelfrom"../users.model";
22
+
23
+
constservicePath='users'
24
+
constservicePlugin=makeServicePlugin({
25
+
// necesarry
26
+
Model,
27
+
service:feathersClient.service(servicePath),
28
+
29
+
// optional
30
+
servicePath,
31
+
idField:'id',
32
+
tempIdField:'__id',
33
+
servicePath:'',
34
+
nameStyle:'short',
35
+
debug:false,
36
+
autoRemove:false,
37
+
preferUpdate:false,
38
+
enableEvents:true,
39
+
addOnUpsert:false,
40
+
replaceItems:false,
41
+
skipRequestIfExists:false,
42
+
43
+
namespace:null,
44
+
modelName:'User',
45
+
46
+
instanceDefaults: () => ({}),
47
+
setupInstance:instance=> instance,
27
48
handleEvents: {
28
-
created: (item, { model, models }) =>options.enableEvents,// handle `created` events, return true to add to the store
29
-
patched: (item, { model, models }) =>options.enableEvents,// handle `created` events, return true to update in the store
30
-
updated: (item, { model, models }) =>options.enableEvents,// handle `created` events, return true to update in the store
31
-
removed: (item, { model, models }) =>options.enableEvents// handle `removed` events, return true to remove from the store
addOnUpsert:true, // Add new records pushed by 'updated/patched' socketio events into store, instead of discarding them. It's false by default
34
-
replaceItems:true, // If true, updates & patches replace the record in the store. Default is false, which merges in changes
35
-
skipRequestIfExists:true, // For get action, if the record already exists in store, skip the remote request. It's false by default
36
-
modelName:'OldTask'// Default modelName would have been 'Task'
37
-
}
54
+
55
+
state: {},
56
+
getters: {},
57
+
mutations: {},
58
+
actions: {},
59
+
60
+
//...
61
+
});
38
62
```
63
+
The following options can also be configured in [Global Configuration](getting-started.html#global-configuration) for every service initialized using `feathers-client.js`:
64
+
65
+
-`idField {String}` - **Default:**`'id'` - The field in each record that will contain the id
66
+
-`tempIdField {Boolean}` - **Default:**`'__id'` - The field in each temporary record that contains the id
67
+
-`debug {Boolean}` - **Default:**`false` - Enable some logging for debugging
68
+
-`autoRemove {Boolean}` - **Default:**`false` - If `true` automatically remove records missing from responses (only use with feathers-rest)
69
+
-`addOnUpsert {Boolean}` - **Default:**`false` - If `true` add new records pushed by 'updated/patched' socketio events into store, instead of discarding them.
70
+
-`replaceItems {Boolean}` - **Default:**`false` - If `true`, updates & patches replace the record in the store. Default is false, which merges in changes.
71
+
-`skipRequestIfExists {Boolean}` - **Default:**`false` - For get action, if `true` the record already exists in store, skip the remote request.
72
+
-`preferUpdate {Boolean}` - **Default:**`false` - If `true`, calling `model.save()` will do an `update` instead of a `patch`.
73
+
-`enableEvents {Boolean}` - **Default:**`true` - If `false` socket event listeners will be turned off
74
+
-`nameStyle {'short'|'path'}` - **Default:**`'short'` - Use the full service path as the Vuex module name, instead of just the last section.
75
+
76
+
The following options can only configured individually per service plugin
77
+
78
+
-`servicePath {String}`- Not all Feathers service plugins expose the service path, so it can be manually specified when missing.
-`instanceDefaults {Function}` - **Default:**`() => ({})` - Override this method to provide default data for new instances. If using Model classes, specify this as a static class property.
82
+
-`setupInstance {Function}`**Default:**`instance => instance` - Override this method to setup data types or related data on an instance. If using Model classes, specify this as a static class property.
83
+
-`handleEvents {Object}`
84
+
-`created {Function}` - **Default:**`(item, { model, models }) => options.enableEvents` - handle `created` events, return true to add to the store
85
+
-`patched {Function}` - **Default:**`(item, { model, models }) => options.enableEvents` - handle `created` events, return true to update in the store
86
+
-`updated {Function}` - **Default:**`(item, { model, models }) => options.enableEvents` - handle `created` events, return true to update in the store
87
+
-`removed {Function}` - **Default:**`(item, { model, models }) => options.enableEvents` - handle `removed` events, return true to remove from the store
88
+
89
+
-`state {Object}` - **Default:**: `null` - Pass custom `states` to the service plugin or modify existing ones
90
+
-`getters {Object}` - **Default:**`null` - Pass custom `getters` to the service plugin or modify existing ones
91
+
-`mutations {Object}` - **Default:**`null` - Pass custom `mutations` to the service plugin or modify existing ones
92
+
-`actions {Object}` - **Default:**`null` - Pass custom `actions` to the service plugin or modify existing ones
39
93
40
94
## Realtime by Default
41
95
42
96
Service plugins automatically listen to all socket messages received by the Feathers Client. This can be disabled by setting `enableEvents: false` in the options, as shown above.
43
97
44
-
## New in Feathers-Vuex 2.0
45
-
46
-
Feathers-Vuex 2.0 includes a few breaking changes to the service plugin. Some of these changes are being made to prepare for future compatibility beyond FeathersJS
47
-
48
-
- The `service` method is now called `makeServicePlugin`
49
-
- The Feathers Client service is no longer created, internally, so a Feathers service object must be provided instead of just the path string.
50
-
- A Model class is now required. The `instanceDefaults` API has been moved into the Model class. You can find a basic example of a minimal Model class in the [Data Modeling](/model-classes.html) docs.
51
-
52
98
## The FeathersClient Service
53
99
54
-
Once the service plugin has been registered with Vuex, the FeathersClient Service will have a new `service.FeathersVuexModel` property. This provides access to the service's [Model class](./model-classes.md).
100
+
Once the service plugin has been registered with Vuex, the FeathersClient Service will have a new `service.FeathersVuexModel` property. This provides access to the service's [Model class](./model-classes.html).
55
101
56
102
```js
57
103
import { models } from'feathers-vuex'
@@ -66,15 +112,30 @@ Each service comes loaded with the following default state:
66
112
```js
67
113
{
68
114
ids: [],
69
-
keyedById: {}, // A hash map, keyed by id of each item
70
115
idField:'id',
71
-
servicePath:'v1/todos'// The full service path
72
-
autoRemove:false, // Indicates that this service will not automatically remove results missing from subsequent requests.
73
-
replaceItems:false, // When set to true, updates and patches will replace the record in the store instead of merging changes
74
-
paginate:false, // Indicates if pagination is enabled on the Feathers service.
75
-
76
-
paramsForServer: [], // Custom query operators that are ignored in the find getter, but will pass through to the server.
77
-
whitelist: [], // Custom query operators that will be allowed in the find getter.
116
+
keyedById: {},
117
+
tempsById: {},
118
+
tempsByNewId: {},
119
+
pagination: {
120
+
defaultLimit:null,
121
+
defaultSkip:null
122
+
},
123
+
servicePath:'v1/todos'
124
+
modelName:'Todo',
125
+
autoRemove:false,
126
+
replaceItems:false,
127
+
128
+
pagination: {
129
+
ids: []
130
+
limit:0
131
+
skip:0
132
+
ip:0
133
+
total:0,
134
+
mostRecent: any
135
+
},
136
+
137
+
paramsForServer: [],
138
+
whitelist: [],
78
139
79
140
isFindPending:false,
80
141
isGetPending:false,
@@ -92,25 +153,29 @@ Each service comes loaded with the following default state:
92
153
}
93
154
```
94
155
95
-
The following attributes are available in each service module's state:
96
-
97
156
-`ids {Array}` - an array of plain ids representing the ids that belong to each object in the `keyedById` map.
157
+
-`idField {String}` - the name of the field that holds each item's id. *Default: `'id'`*
98
158
-`keyedById {Object}` - a hash map keyed by the id of each item.
159
+
-`tempsById {Object}` - a hash map of temporary records, [keyed by tempIdField](./getting-started.html#global-configuration) of each item
160
+
-`tempsByNewId {Object}` - temporary storage for temps while getting transferred from tempsById to keyedById
99
161
-`servicePath {String}` - the full service path, even if you alias the namespace to something else.
100
162
-`modelName {String}` - the key in the $FeathersVuex plugin where the model will be found.
101
-
-`autoRemove {Boolean` - indicates that this service will not automatically remove results missing from subsequent requests. Only use with feathers-rest. Default is false.
163
+
-`autoRemove {Boolean}` - indicates that this service will not automatically remove results missing from subsequent requests. Only use with feathers-rest. Default is false.
102
164
-`replaceItems {Boolean}` - When set to true, updates and patches will replace the record in the store instead of merging changes. Default is false
103
-
-`idField {String}` - the name of the field that holds each item's id. *Default: `'id'`*
104
-
-`paginate {Boolean}` - Indicates if the service has pagination turned on.
165
+
166
+
-`pagination {Object}` - provides informaiton about the last made queries
167
+
168
+
-`paramsForServer {Array}` - Custom query operators that are ignored in the find getter, but will pass through to the server.
169
+
-`whitelist {Array}` - Custom query operators that will be allowed in the find getter.
105
170
106
171
The following state attributes allow you to bind to the pending state of requests:
107
172
108
-
-`isFindPending {Boolean}` - `true` if there's a pending `find` request. `false` if not.
109
-
-`isGetPending {Boolean}` - `true` if there's a pending `get` request. `false` if not.
110
-
-`isCreatePending {Boolean}` - `true` if there's a pending `create` request. `false` if not.
111
-
-`isUpdatePending {Boolean}` - `true` if there's a pending `update` request. `false` if not.
112
-
-`isPatchPending {Boolean}` - `true` if there's a pending `patch` request. `false` if not.
113
-
-`isRemovePending {Boolean}` - `true` if there's a pending `remove` request. `false` if not.
173
+
-`isFindPending {Boolean}` - `true` if there's a pending `find` request. `false` if not.
174
+
-`isGetPending {Boolean}` - `true` if there's a pending `get` request. `false` if not.
175
+
-`isCreatePending {Boolean}` - `true` if there's a pending `create` request. `false` if not.
176
+
-`isUpdatePending {Boolean}` - `true` if there's a pending `update` request. `false` if not.
177
+
-`isPatchPending {Boolean}` - `true` if there's a pending `patch` request. `false` if not.
178
+
-`isRemovePending {Boolean}` - `true` if there's a pending `remove` request. `false` if not.
114
179
115
180
The following state attribute will be populated with any request error, serialized as a plain object:
116
181
@@ -173,12 +238,6 @@ Removes the passed in items or ids from the store.
173
238
174
239
-`items {Array}` - An array of ids or of objects with ids that will be removed from the data store.
175
240
176
-
### `clearList(state)`
177
-
178
-
> Removed in 2.0
179
-
180
-
Clears the `list`, excepting the `current` item.
181
-
182
241
### `clearAll(state)`
183
242
184
243
Clears all data from `ids`, `keyedById`, and `currentId`
@@ -187,47 +246,13 @@ Clears all data from `ids`, `keyedById`, and `currentId`
187
246
188
247
The following mutations are called automatically by the service actions, and will rarely, if ever, need to be used manually.
189
248
190
-
Before Feathers-Vuex 2.0, these were the available mutations:
191
-
192
-
-`setFindPending(state)` - sets `isFindPending` to `true`
193
-
-`unsetFindPending(state)` - sets `isFindPending` to `false`
194
-
-`setGetPending(state)` - sets `isGetPending` to `true`
195
-
-`unsetGetPending(state)` - sets `isGetPending` to `false`
196
-
-`setCreatePending(state)` - sets `isCreatePending` to `true`
197
-
-`unsetCreatePending(state)` - sets `isCreatePending` to `false`
198
-
-`setUpdatePending(state)` - sets `isUpdatePending` to `true`
199
-
-`unsetUpdatePending(state)` - sets `isUpdatePending` to `false`
200
-
-`setPatchPending(state)` - sets `isPatchPending` to `true`
201
-
-`unsetPatchPending(state)` - sets `isPatchPending` to `false`
202
-
-`setRemovePending(state)` - sets `isRemovePending` to `true`
203
-
-`unsetRemovePending(state)` - sets `isRemovePending` to `false`
204
-
205
-
In Feathers-Vuex 2.0, these have changed to only two mutations:
206
-
207
249
-`setPending(state, method)` - sets the `is${method}Pending` attribute to true
208
250
-`unsetPending(state, method)` - sets the `is${method}Pending` attribute to false
209
251
210
252
### Mutations for Managing Errors
211
253
212
254
The following mutations are called automatically by the service actions, and will rarely need to be used manually.
213
255
214
-
Before Feathers-Vuex 2.0, these were the available mutations:
215
-
216
-
-`setFindError(state, error)`
217
-
-`clearFindError(state)`
218
-
-`setGetError(state, error)`
219
-
-`clearGetError(state)`
220
-
-`setCreateError(state, error)`
221
-
-`clearCreateError(state)`
222
-
-`setUpdateError(state, error)`
223
-
-`clearUpdateError(state)`
224
-
-`setPatchError(state, error)`
225
-
-`clearPatchError(state)`
226
-
-`setRemoveError(state, error)`
227
-
-`clearRemoveError(state)`
228
-
229
-
In Feathers-Vuex 2.0, these have changed to only two mutations:
230
-
231
256
-`setError(state, { method, error })` - sets the `errorOn${method}` attribute to the error
232
257
-`clearError(state, method)` - sets the `errorOn${method}` attribute to `null`
233
258
@@ -254,7 +279,7 @@ See the section about pagination, below, for more information that is applicable
254
279
255
280
### `afterFind(response)`
256
281
257
-
The `afterFind` action is called by the `find` action after a successful response is added to the store. It is called with the current response. By default, it is a no-op (it literally does nothing), and is just a placeholder for you to use when necessary. See the sections on [customizing the default store](#Customizing-a-Service’s-Default-Store) and [Handling custom server responses](./common-patterns.md#Handling-custom-server-responses) for example usage.
282
+
The `afterFind` action is called by the `find` action after a successful response is added to the store. It is called with the current response. By default, it is a no-op (it literally does nothing), and is just a placeholder for you to use when necessary. See the sections on [customizing the default store](#Customizing-a-Service’s-Default-Store) and [Handling custom server responses](./common-patterns.html#Handling-custom-server-responses) for example usage.
258
283
259
284
### `get(id)` or `get([id, params])`
260
285
@@ -583,7 +608,7 @@ In summary, any time a query param other than `$limit` and `$skip` changes, we g
583
608
584
609
Now that we've reviewed how pagination tracking works under the hood, you might be asking "Why?" There are a few reasons:
585
610
586
-
1. Improve performance with cacheing. It's now possible to skip making a query if we already have valid data for the current query. The [`makeFindMixin`](./mixins.md) mixin makes this very easy with its built-in `queryWhen` feature.
611
+
1. Improve performance with cacheing. It's now possible to skip making a query if we already have valid data for the current query. The [`makeFindMixin`](./mixins.html) mixin makes this very easy with its built-in `queryWhen` feature.
587
612
2. Allow fall-through cacheing of paginated data. A common challenge occurs when you provide the same query params to the `find` action and the `find` getter. As you'll learn in the next section, the `find` getter allows you to make queries against the Vuex store as though it were a Feathers database adapter. But what happens when you pass `{ $limit: 10, $skip: 10 }` to the action and getter?<br/>
588
613
First, lets review what happens with the `find` action. The database is aware of all 155 records, so it skips the first 10 and returns the next 10 records. Those records get populated in the store, so the store now has 10 records. Now we pass the query to the `find` getter and tell it to `$skip: 10`. It skips the only 10 records that are in the store and returns an empty array! That's definitely not what we wanted.<br/>
589
614
Since we're now storing this pagination structure, we can build a utility around the `find` getter which will allow us to return the same data with the same query. The data is still reactive and will automatically update when a record changes.
@@ -608,7 +633,7 @@ The `find` getter queries data from the local store using the same Feathers quer
608
633
609
634
## Customizing a Service's Default Store
610
635
611
-
As shown in the first example, the service module allows you to customize its store:
636
+
As shown in the example below, the service module allows you to customize its store:
612
637
613
638
```js
614
639
conststore=newVuex.Store({
@@ -624,30 +649,19 @@ const store = new Vuex.Store({
624
649
}
625
650
},
626
651
mutations: {
627
-
setTestToFalse (state) {
628
-
state.test=false
652
+
setTest (state, val) {
653
+
state.test=val;
629
654
},
630
-
setTestToTrue (state) {
631
-
state.test=true
632
-
}
633
655
},
634
656
actions: {
635
657
// Overwriting the built-in `afterFind` action.
636
658
afterFind ({ commit, dispatch, getters, state }, response) {
637
659
// Do something with the response.
638
660
// Keep in mind that the data is already in the store.
0 commit comments