|
| 1 | +--- |
| 2 | +title: API Overview |
| 3 | +--- |
| 4 | + |
| 5 | +<!--- Usage ------------------------------------------------------------------------------------ --> |
| 6 | +[](https://travis-ci.org/feathers-plus/feathers-vuex) |
| 7 | +[](https://david-dm.org/feathers-plus/feathers-vuex) |
| 8 | +[](https://www.npmjs.com/package/feathers-vuex) |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +> Integrate the Feathers Client into Vuex |
| 13 | +
|
| 14 | +`feathers-vuex` is a first class integration of the Feathers Client and Vuex. It implements many Redux best practices under the hood, eliminates *a lot* of boilerplate code, and still allows you to easily customize the Vuex store. |
| 15 | + |
| 16 | +## Features |
| 17 | + |
| 18 | +- Fully powered by Vuex & Feathers |
| 19 | +- Realtime By Default |
| 20 | +- Actions With Reactive Data * |
| 21 | +- Local Queries |
| 22 | +- Fall-Through Caching * |
| 23 | +- Feathers Query Syntax |
| 24 | +- `$FeathersVuex` [Vue Plugin](./vue-plugin.md) * |
| 25 | +- Live Queries |
| 26 | +- [Per-Service Data Modeling](./common-patterns.md#Basic-Data-Modeling-with-instanceDefaults) * |
| 27 | +- Clone & Commit * |
| 28 | +- Simplified Auth |
| 29 | +- Vuex Strict Mode * |
| 30 | +- Per-Record Defaults * |
| 31 | +- Data Level Computes * |
| 32 | +- Relation Support * |
| 33 | + |
| 34 | +`* New in v1.2.0` |
| 35 | + |
| 36 | +## Installation |
| 37 | + |
| 38 | +```console |
| 39 | +npm install feathers-vuex --save |
| 40 | +``` |
| 41 | + |
| 42 | +## Use |
| 43 | +To setup `feathers-vuex`, we first need to setup a Feathers Client. Here's an example using the latest `@feathersjs` npm packages. |
| 44 | + |
| 45 | +**feathers-client.js:** |
| 46 | +```js |
| 47 | +import feathers from '@feathersjs/feathers' |
| 48 | +import socketio from '@feathersjs/socketio-client' |
| 49 | +import auth from '@feathersjs/authentication-client' |
| 50 | +import io from 'socket.io-client' |
| 51 | + |
| 52 | +const socket = io('http://localhost:3030', {transports: ['websocket']}) |
| 53 | + |
| 54 | +const feathersClient = feathers() |
| 55 | + .configure(socketio(socket)) |
| 56 | + .configure(auth({ storage: window.localStorage })) |
| 57 | + |
| 58 | +export default feathersClient |
| 59 | +``` |
| 60 | + |
| 61 | +And here's how you would integrate the Feathers Client into the Vuex store: |
| 62 | + |
| 63 | +**store/index.js:** |
| 64 | +```js |
| 65 | +import Vue from 'vue' |
| 66 | +import Vuex from 'vuex' |
| 67 | +import feathersVuex from 'feathers-vuex' |
| 68 | +import feathersClient from '../feathers-client' |
| 69 | + |
| 70 | +const { service, auth, FeathersVuex } = feathersVuex(feathersClient, { idField: '_id' }) |
| 71 | + |
| 72 | +Vue.use(Vuex) |
| 73 | +Vue.use(FeathersVuex) |
| 74 | + |
| 75 | +export default new Vuex.Store({ |
| 76 | + plugins: [ |
| 77 | + service('todos'), |
| 78 | + |
| 79 | + // Specify custom options per service |
| 80 | + service('/v1/tasks', { |
| 81 | + idField: '_id', // The field in each record that will contain the id |
| 82 | + nameStyle: 'path', // Use the full service path as the Vuex module name, instead of just the last section |
| 83 | + namespace: 'custom-namespace', // Customize the Vuex module name. Overrides nameStyle. |
| 84 | + debug: true, // Enable some logging for debugging |
| 85 | + autoRemove: true, // Automatically remove records missing from responses (only use with feathers-rest) |
| 86 | + enableEvents: false, // Turn off socket event listeners. It's true by default |
| 87 | + addOnUpsert: true, // Add new records pushed by 'updated/patched' socketio events into store, instead of discarding them. It's false by default |
| 88 | + replaceItems: true, // If true, updates & patches replace the record in the store. Default is false, which merges in changes |
| 89 | + skipRequestIfExists: true, // For get action, if the record already exists in store, skip the remote request. It's false by default |
| 90 | + modelName: 'OldTask' // Default modelName would have been 'Task' |
| 91 | + }) |
| 92 | + |
| 93 | + // Add custom state, getters, mutations, or actions, if needed. See example in another section, below. |
| 94 | + service('things', { |
| 95 | + state: {}, |
| 96 | + getters: {}, |
| 97 | + mutations: {}, |
| 98 | + actions: {} |
| 99 | + }) |
| 100 | + |
| 101 | + // Setup a service with defaults for Model instances |
| 102 | + service('manufacturers', { |
| 103 | + instanceDefaults: { |
| 104 | + name: '' |
| 105 | + } |
| 106 | + }) |
| 107 | + // Setup a service with light-weight relational data |
| 108 | + service('models', { |
| 109 | + instanceDefaults: { |
| 110 | + name: '', |
| 111 | + manufacturerId: '', |
| 112 | + manufacturer: 'Manufacturer' // Refers to data (populated on the server) that gets put in the `manufacturers` vuex store. |
| 113 | + } |
| 114 | + }) |
| 115 | + |
| 116 | + // Setup the auth plugin. |
| 117 | + auth({ userService: 'users' }) |
| 118 | + ] |
| 119 | +}) |
| 120 | +``` |
| 121 | + |
| 122 | +The new `feathers-vuex` API is more Vuex-like. All of the functionality remains the same, but it is no longer configured like a FeathersJS plugin. While the previous functionality was nice for prototyping, it didn't work well in SSR scenarios, like with Nuxt. |
| 123 | + |
| 124 | +To see `feathers-vuex` in a working vue-cli application, check out [`feathers-chat-vuex`](https://github.com/feathers-plus/feathers-chat-vuex). |
| 125 | + |
| 126 | + |
| 127 | +## Note about feathers-reactive |
| 128 | +Previous versions of this plugin required both RxJS and `feathers-reactive` to receive realtime updates. `[email protected]` has socket messaging support built in and takes advantage of Vuex reactivity, so RxJS and `feathers-reactive` are no longer required or supported. |
| 129 | + |
| 130 | +## Global Configuration |
| 131 | + |
| 132 | +The following default options are available for configuration: |
| 133 | + |
| 134 | +```js |
| 135 | +const defaultOptions = { |
| 136 | + idField: 'id', // The field in each record that will contain the id |
| 137 | + autoRemove: false, // automatically remove records missing from responses (only use with feathers-rest) |
| 138 | + nameStyle: 'short', // Determines the source of the module name. 'short' or 'path' |
| 139 | + enableEvents: true, // Set to false to explicitly disable socket event handlers. |
| 140 | + preferUpdate: false, // When true, calling modelInstance.save() will do an update instead of a patch. |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +Each service module can also be individually configured. |
| 145 | + |
| 146 | +## The Vuex modules |
| 147 | + |
| 148 | +There are two modules included: |
| 149 | +1. The [Service module](./service-module.md) adds a Vuex store for new services. |
| 150 | +2. The [Auth module](./auth-module.md) sets up the Vuex store for authentication / logout. |
| 151 | + |
| 152 | + |
| 153 | +## License |
| 154 | + |
| 155 | +Copyright (c) Forever and Ever, or at least the current year. |
| 156 | + |
| 157 | +Licensed under the [MIT license](LICENSE). |
0 commit comments